Skip to content

Commit b241716

Browse files
committed
tests: add function to wait while condition holds
This adds a new utils function that is a slight generalisation of `wait_for` with an additional condition that must always be met while waiting. `wait_for` now calls this new function with the condition being one that is always true.
1 parent 3fcbb0b commit b241716

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

tests/test_framework/utils.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,19 @@ def wait_for(success, timeout=TIMEOUT, debug_fn=None):
3535
debug_fn is logged at each call to success, it can be useful for debugging
3636
when tests fail.
3737
"""
38+
wait_for_while_condition_holds(success, lambda: True, timeout, debug_fn)
39+
40+
41+
def wait_for_while_condition_holds(success, condition, timeout=TIMEOUT, debug_fn=None):
42+
"""
43+
Run success() either until it returns True, or until the timeout is reached,
44+
as long as condition() holds.
45+
debug_fn is logged at each call to success, it can be useful for debugging
46+
when tests fail.
47+
"""
3848
start_time = time.time()
3949
interval = 0.25
40-
while not success() and time.time() < start_time + timeout:
50+
while not success() and condition() and time.time() < start_time + timeout:
4151
if debug_fn is not None:
4252
logging.info(debug_fn())
4353
time.sleep(interval)
@@ -46,6 +56,10 @@ def wait_for(success, timeout=TIMEOUT, debug_fn=None):
4656
interval = 5
4757
if time.time() > start_time + timeout:
4858
raise ValueError("Error waiting for {}", success)
59+
if not condition():
60+
raise ValueError(
61+
"Condition {} not met while waiting for {}", condition, success
62+
)
4963

5064

5165
def get_txid(hex_tx):

0 commit comments

Comments
 (0)