refactor: Use read_json_api in more tests.

This commit is contained in:
tecnovert
2022-07-04 00:47:30 +02:00
parent 0c620ea388
commit 02bd90053a
12 changed files with 118 additions and 115 deletions

View File

@@ -196,12 +196,19 @@ def wait_for_in_progress(delay_event, swap_client, bid_id, sent=False):
raise ValueError('wait_for_in_progress timed out.')
def read_json_api(port, path=None):
url = f'http://127.0.0.1:{port}/json'
if path is not None:
url += '/' + path
return json.loads(urlopen(url).read())
def wait_for_none_active(delay_event, port, wait_for=30):
for i in range(wait_for):
if delay_event.is_set():
raise ValueError('Test stopped.')
delay_event.wait(1)
js = json.loads(urlopen('http://127.0.0.1:{}/json'.format(port)).read())
js = read_json_api(port)
if js['num_swapping'] == 0 and js['num_watched_outputs'] == 0:
return
raise ValueError('wait_for_none_active timed out.')
@@ -213,7 +220,7 @@ def waitForServer(delay_event, port, wait_for=20):
raise ValueError('Test stopped.')
try:
delay_event.wait(1)
summary = json.loads(urlopen('http://127.0.0.1:{}/json'.format(port)).read())
summary = read_json_api(port)
return
except Exception as e:
print('waitForServer, error:', str(e))
@@ -224,7 +231,7 @@ def waitForNumOffers(delay_event, port, offers, wait_for=20):
for i in range(wait_for):
if delay_event.is_set():
raise ValueError('Test stopped.')
summary = json.loads(urlopen('http://127.0.0.1:{}/json'.format(port)).read())
summary = read_json_api(port)
if summary['num_network_offers'] >= offers:
return
delay_event.wait(1)
@@ -235,7 +242,7 @@ def waitForNumBids(delay_event, port, bids, wait_for=20):
for i in range(wait_for):
if delay_event.is_set():
raise ValueError('Test stopped.')
summary = json.loads(urlopen('http://127.0.0.1:{}/json'.format(port)).read())
summary = read_json_api(port)
if summary['num_recv_bids'] >= bids:
return
delay_event.wait(1)
@@ -246,7 +253,7 @@ def waitForNumSwapping(delay_event, port, bids, wait_for=60):
for i in range(wait_for):
if delay_event.is_set():
raise ValueError('Test stopped.')
summary = json.loads(urlopen('http://127.0.0.1:{}/json'.format(port)).read())
summary = read_json_api(port)
if summary['num_swapping'] >= bids:
return
delay_event.wait(1)
@@ -368,10 +375,3 @@ def compare_bid_states(states, expect_states):
raise ValueError(f'Expected state {expect_states[i]}, found {s[1]}')
assert(s[1] == expect_states[i])
return True
def read_json_api(port, path=None):
url = f'http://127.0.0.1:{port}/json'
if path is not None:
url += '/' + path
return json.loads(urlopen(url).read())