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
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():
if check_connected:
assert self.is_connected
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):
test_function = lambda: self.is_connected

View file

@ -787,8 +787,8 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
self.sync_blocks(nodes)
self.sync_mempools(nodes)
def wait_until(self, test_function, timeout=60):
return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.options.timeout_factor)
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, check_interval=check_interval)
# 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.setmocktime(self.mocktime)
def wait_until(self, test_function, timeout=60):
return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.timeout_factor)
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, check_interval=check_interval)
class TestNodeCLIAttr:

View file

@ -289,7 +289,7 @@ def ensure_for(*, duration, f, check_interval=0.2):
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.
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():
return
attempt += 1
time.sleep(0.05)
time.sleep(check_interval)
# Print the cause of the timeout
predicate_source = "''''\n" + inspect.getsource(predicate) + "'''"

View file

@ -6,7 +6,6 @@
Test Inactive HD Chains.
"""
import shutil
import time
from test_framework.authproxy import JSONRPCException
from test_framework.test_framework import BitcoinTestFramework
@ -75,12 +74,13 @@ class InactiveHDChainsTest(BitcoinTestFramework):
self.generate(self.nodes[0], 1)
# Wait for the test wallet to see the transaction
while True:
def is_tx_available(txid):
try:
test_wallet.gettransaction(txid)
break
return True
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:
# The test wallet will not be able to generate the topped up keypool