mirror of
https://github.com/basicswap/basicswap.git
synced 2026-04-08 18:37:23 +02:00
tests: add wait_for_bid_state
This commit is contained in:
@@ -103,6 +103,32 @@ def modifyConfig(test_path, i):
|
|||||||
json.dump(settings, fp, indent=4)
|
json.dump(settings, fp, indent=4)
|
||||||
|
|
||||||
|
|
||||||
|
def wait_for_bid_state(
|
||||||
|
delay_event, node_port: int, bid_id: str, state=None, wait_for: int = 40
|
||||||
|
) -> None:
|
||||||
|
logger.info(f"TEST: wait_for_bid {bid_id}, state {state}")
|
||||||
|
|
||||||
|
pass_state_strs = []
|
||||||
|
if isinstance(state, (list, tuple)):
|
||||||
|
for s in state:
|
||||||
|
pass_state_strs.append(strBidState(s))
|
||||||
|
elif state is not None:
|
||||||
|
pass_state_strs.append(strBidState(state))
|
||||||
|
|
||||||
|
for i in range(wait_for):
|
||||||
|
if delay_event.is_set():
|
||||||
|
raise ValueError("Test stopped.")
|
||||||
|
delay_event.wait(1)
|
||||||
|
try:
|
||||||
|
rv = read_json_api(node_port, f"bids/{bid_id}")
|
||||||
|
if rv["bid_state"] in pass_state_strs or state is None:
|
||||||
|
return
|
||||||
|
except Exception as e: # noqa: F841
|
||||||
|
pass
|
||||||
|
# logger.debug(f"TEST: wait_for_bid {bid_id}, error {e}")
|
||||||
|
raise ValueError(f"wait_for_bid timed out {bid_id}.")
|
||||||
|
|
||||||
|
|
||||||
class TestFunctions(BaseTestWithPrepare):
|
class TestFunctions(BaseTestWithPrepare):
|
||||||
__test__ = False
|
__test__ = False
|
||||||
|
|
||||||
@@ -153,33 +179,22 @@ class TestFunctions(BaseTestWithPrepare):
|
|||||||
"amount_from": offer["amount_from"],
|
"amount_from": offer["amount_from"],
|
||||||
"validmins": 60,
|
"validmins": 60,
|
||||||
}
|
}
|
||||||
post_json_api(port_node_to, "bids/new", data)
|
rv = post_json_api(port_node_to, "bids/new", data)
|
||||||
waitForNumBids(self.delay_event, port_node_from, 1)
|
bid_id: str = rv["bid_id"]
|
||||||
|
wait_for_bid_state(
|
||||||
for i in range(20):
|
self.delay_event, port_node_from, bid_id, BidStates.BID_RECEIVED
|
||||||
bids = read_json_api(port_node_from, "bids")
|
|
||||||
bid = bids[0]
|
|
||||||
if bid["bid_state"] == "Received":
|
|
||||||
break
|
|
||||||
self.delay_event.wait(1)
|
|
||||||
assert bid["bid_state"] == "Received"
|
|
||||||
|
|
||||||
rv = post_json_api(
|
|
||||||
port_node_from, "bids/{}".format(bid["bid_id"]), {"accept": True}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
rv = post_json_api(port_node_from, "bids/{}".format(bid_id), {"accept": True})
|
||||||
assert rv["bid_state"] in ("Accepted", "Request accepted")
|
assert rv["bid_state"] in ("Accepted", "Request accepted")
|
||||||
|
|
||||||
logger.info("Completing swap")
|
logger.info("Completing swap")
|
||||||
for i in range(240):
|
wait_for_bid_state(
|
||||||
if self.delay_event.is_set():
|
self.delay_event, port_node_from, bid_id, BidStates.SWAP_COMPLETED, 240
|
||||||
raise ValueError("Test stopped.")
|
)
|
||||||
self.delay_event.wait(4)
|
wait_for_bid_state(
|
||||||
|
self.delay_event, port_node_to, bid_id, BidStates.SWAP_COMPLETED, 240
|
||||||
rv = read_json_api(port_node_from, "bids/{}".format(bid["bid_id"]))
|
)
|
||||||
if rv["bid_state"] == "Completed":
|
|
||||||
break
|
|
||||||
assert rv["bid_state"] == "Completed"
|
|
||||||
|
|
||||||
# Wait for bid to be removed from in-progress
|
# Wait for bid to be removed from in-progress
|
||||||
waitForNumBids(self.delay_event, port_node_from, 0)
|
waitForNumBids(self.delay_event, port_node_from, 0)
|
||||||
|
|
||||||
@@ -240,8 +255,8 @@ class TestFunctions(BaseTestWithPrepare):
|
|||||||
}
|
}
|
||||||
rv = post_json_api(port_node_to, "bids/new", data)
|
rv = post_json_api(port_node_to, "bids/new", data)
|
||||||
bid_id: str = rv["bid_id"]
|
bid_id: str = rv["bid_id"]
|
||||||
waitForNumBids(self.delay_event, port_node_from, 1)
|
|
||||||
|
|
||||||
|
wait_for_bid_state(self.delay_event, port_follower, bid_id)
|
||||||
rv = post_json_api(
|
rv = post_json_api(
|
||||||
port_follower,
|
port_follower,
|
||||||
f"bids/{bid_id}",
|
f"bids/{bid_id}",
|
||||||
@@ -249,24 +264,20 @@ class TestFunctions(BaseTestWithPrepare):
|
|||||||
)
|
)
|
||||||
assert "bid_state" in rv # Test that the return didn't fail
|
assert "bid_state" in rv # Test that the return didn't fail
|
||||||
|
|
||||||
for i in range(20):
|
wait_for_bid_state(
|
||||||
bid = read_json_api(port_node_from, f"bids/{bid_id}")
|
self.delay_event, port_node_from, bid_id, BidStates.BID_RECEIVED
|
||||||
if bid["bid_state"] == "Received":
|
)
|
||||||
break
|
|
||||||
self.delay_event.wait(1)
|
|
||||||
assert bid["bid_state"] == "Received"
|
|
||||||
|
|
||||||
rv = post_json_api(port_offerer, f"bids/{bid_id}", {"accept": True})
|
rv = post_json_api(port_offerer, f"bids/{bid_id}", {"accept": True})
|
||||||
assert rv["bid_state"] in ("Accepted", "Request accepted")
|
assert rv["bid_state"] in ("Accepted", "Request accepted")
|
||||||
|
|
||||||
for i in range(100):
|
wait_for_bid_state(
|
||||||
if self.delay_event.is_set():
|
self.delay_event,
|
||||||
raise ValueError("Test stopped.")
|
port_leader,
|
||||||
self.delay_event.wait(4)
|
bid_id,
|
||||||
rv = read_json_api(port_leader, f"bids/{bid_id}")
|
BidStates.XMR_SWAP_FAILED_REFUNDED,
|
||||||
if rv["bid_state"] == strBidState(BidStates.XMR_SWAP_FAILED_REFUNDED):
|
240,
|
||||||
break
|
)
|
||||||
assert rv["bid_state"] == strBidState(BidStates.XMR_SWAP_FAILED_REFUNDED)
|
|
||||||
|
|
||||||
|
|
||||||
class Test(TestFunctions):
|
class Test(TestFunctions):
|
||||||
@@ -456,7 +467,12 @@ class Test(TestFunctions):
|
|||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
self.do_test_02_leader_recover_a_lock_tx(
|
self.do_test_02_leader_recover_a_lock_tx(
|
||||||
self.test_coin_b, Coins.XMR, self.port_node_1, self.port_node_0
|
self.test_coin_b, self.test_coin_xmr, self.port_node_1, self.port_node_0
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_02_b_leader_recover_a_lock_tx_reverse(self):
|
||||||
|
self.do_test_02_leader_recover_a_lock_tx(
|
||||||
|
self.test_coin_xmr, self.test_coin_b, self.port_node_1, self.port_node_0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user