test: Add check_interval parameter to wait_until

This also replaces two sleep calls in functional tests with wait_until
This commit is contained in:
Fabian Jahr 2024-09-21 21:23:14 +02:00
parent 16c87d91fd
commit 5468a23eb9
No known key found for this signature in database
GPG key ID: F13D1E9D890798CD
5 changed files with 12 additions and 12 deletions

View file

@ -579,13 +579,13 @@ class P2PInterface(P2PConnection):
# Connection helper methods # Connection helper methods
def wait_until(self, test_function_in, *, timeout=60, check_connected=True): def wait_until(self, test_function_in, *, timeout=60, check_connected=True, check_interval=0.05):
def test_function(): def test_function():
if check_connected: if check_connected:
assert self.is_connected assert self.is_connected
return test_function_in() return test_function_in()
wait_until_helper_internal(test_function, timeout=timeout, lock=p2p_lock, timeout_factor=self.timeout_factor) wait_until_helper_internal(test_function, timeout=timeout, lock=p2p_lock, timeout_factor=self.timeout_factor, check_interval=check_interval)
def wait_for_connect(self, *, timeout=60): def wait_for_connect(self, *, timeout=60):
test_function = lambda: self.is_connected test_function = lambda: self.is_connected

View file

@ -787,8 +787,8 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
self.sync_blocks(nodes) self.sync_blocks(nodes)
self.sync_mempools(nodes) self.sync_mempools(nodes)
def wait_until(self, test_function, timeout=60): def wait_until(self, test_function, timeout=60, check_interval=0.05):
return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.options.timeout_factor) return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.options.timeout_factor, check_interval=check_interval)
# Private helper methods. These should not be accessed by the subclass test scripts. # Private helper methods. These should not be accessed by the subclass test scripts.

View file

@ -838,8 +838,8 @@ class TestNode():
self.mocktime += seconds self.mocktime += seconds
self.setmocktime(self.mocktime) self.setmocktime(self.mocktime)
def wait_until(self, test_function, timeout=60): def wait_until(self, test_function, timeout=60, check_interval=0.05):
return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.timeout_factor) return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.timeout_factor, check_interval=check_interval)
class TestNodeCLIAttr: class TestNodeCLIAttr:

View file

@ -289,7 +289,7 @@ def ensure_for(*, duration, f, check_interval=0.2):
time.sleep(check_interval) time.sleep(check_interval)
def wait_until_helper_internal(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=None, timeout_factor=1.0): def wait_until_helper_internal(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=None, timeout_factor=1.0, check_interval=0.05):
"""Sleep until the predicate resolves to be True. """Sleep until the predicate resolves to be True.
Warning: Note that this method is not recommended to be used in tests as it is 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
if predicate(): if predicate():
return return
attempt += 1 attempt += 1
time.sleep(0.05) time.sleep(check_interval)
# Print the cause of the timeout # Print the cause of the timeout
predicate_source = "''''\n" + inspect.getsource(predicate) + "'''" predicate_source = "''''\n" + inspect.getsource(predicate) + "'''"

View file

@ -6,7 +6,6 @@
Test Inactive HD Chains. Test Inactive HD Chains.
""" """
import shutil import shutil
import time
from test_framework.authproxy import JSONRPCException from test_framework.authproxy import JSONRPCException
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
@ -75,12 +74,13 @@ class InactiveHDChainsTest(BitcoinTestFramework):
self.generate(self.nodes[0], 1) self.generate(self.nodes[0], 1)
# Wait for the test wallet to see the transaction # Wait for the test wallet to see the transaction
while True: def is_tx_available(txid):
try: try:
test_wallet.gettransaction(txid) test_wallet.gettransaction(txid)
break return True
except JSONRPCException: except JSONRPCException:
time.sleep(0.1) return False
self.nodes[0].wait_until(lambda: is_tx_available(txid), timeout=10, check_interval=0.1)
if encrypt: if encrypt:
# The test wallet will not be able to generate the topped up keypool # The test wallet will not be able to generate the topped up keypool