Skip to content

Commit 5468a23

Browse files
committed
test: Add check_interval parameter to wait_until
This also replaces two sleep calls in functional tests with wait_until
1 parent 16c87d9 commit 5468a23

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

test/functional/test_framework/p2p.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,13 @@ def on_version(self, message):
579579

580580
# Connection helper methods
581581

582-
def wait_until(self, test_function_in, *, timeout=60, check_connected=True):
582+
def wait_until(self, test_function_in, *, timeout=60, check_connected=True, check_interval=0.05):
583583
def test_function():
584584
if check_connected:
585585
assert self.is_connected
586586
return test_function_in()
587587

588-
wait_until_helper_internal(test_function, timeout=timeout, lock=p2p_lock, timeout_factor=self.timeout_factor)
588+
wait_until_helper_internal(test_function, timeout=timeout, lock=p2p_lock, timeout_factor=self.timeout_factor, check_interval=check_interval)
589589

590590
def wait_for_connect(self, *, timeout=60):
591591
test_function = lambda: self.is_connected

test/functional/test_framework/test_framework.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,8 @@ def sync_all(self, nodes=None):
787787
self.sync_blocks(nodes)
788788
self.sync_mempools(nodes)
789789

790-
def wait_until(self, test_function, timeout=60):
791-
return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.options.timeout_factor)
790+
def wait_until(self, test_function, timeout=60, check_interval=0.05):
791+
return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.options.timeout_factor, check_interval=check_interval)
792792

793793
# Private helper methods. These should not be accessed by the subclass test scripts.
794794

test/functional/test_framework/test_node.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,8 @@ def bumpmocktime(self, seconds):
838838
self.mocktime += seconds
839839
self.setmocktime(self.mocktime)
840840

841-
def wait_until(self, test_function, timeout=60):
842-
return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.timeout_factor)
841+
def wait_until(self, test_function, timeout=60, check_interval=0.05):
842+
return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.timeout_factor, check_interval=check_interval)
843843

844844

845845
class TestNodeCLIAttr:

test/functional/test_framework/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def ensure_for(*, duration, f, check_interval=0.2):
289289
time.sleep(check_interval)
290290

291291

292-
def wait_until_helper_internal(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=None, timeout_factor=1.0):
292+
def wait_until_helper_internal(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=None, timeout_factor=1.0, check_interval=0.05):
293293
"""Sleep until the predicate resolves to be True.
294294
295295
Warning: Note that this method is not recommended to be used in tests as it is
@@ -313,7 +313,7 @@ def wait_until_helper_internal(predicate, *, attempts=float('inf'), timeout=floa
313313
if predicate():
314314
return
315315
attempt += 1
316-
time.sleep(0.05)
316+
time.sleep(check_interval)
317317

318318
# Print the cause of the timeout
319319
predicate_source = "''''\n" + inspect.getsource(predicate) + "'''"

test/functional/wallet_inactive_hdchains.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
Test Inactive HD Chains.
77
"""
88
import shutil
9-
import time
109

1110
from test_framework.authproxy import JSONRPCException
1211
from test_framework.test_framework import BitcoinTestFramework
@@ -75,12 +74,13 @@ def do_inactive_test(self, base_wallet, test_wallet, encrypt=False):
7574
self.generate(self.nodes[0], 1)
7675

7776
# Wait for the test wallet to see the transaction
78-
while True:
77+
def is_tx_available(txid):
7978
try:
8079
test_wallet.gettransaction(txid)
81-
break
80+
return True
8281
except JSONRPCException:
83-
time.sleep(0.1)
82+
return False
83+
self.nodes[0].wait_until(lambda: is_tx_available(txid), timeout=10, check_interval=0.1)
8484

8585
if encrypt:
8686
# The test wallet will not be able to generate the topped up keypool

0 commit comments

Comments
 (0)