From 44bbb331c581710551a21b85e0d9a6675593b918 Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Sat, 21 Sep 2024 21:23:14 +0200 Subject: [PATCH] test: Add delay parameter to wait_until This also replaces two sleep calls in functional tests with wait_until --- test/functional/test_framework/p2p.py | 4 ++-- test/functional/test_framework/test_node.py | 4 ++-- test/functional/test_framework/util.py | 4 ++-- test/functional/wallet_encryption.py | 4 ++-- test/functional/wallet_inactive_hdchains.py | 8 ++++---- test/functional/wallet_keypool.py | 4 +--- 6 files changed, 13 insertions(+), 15 deletions(-) diff --git a/test/functional/test_framework/p2p.py b/test/functional/test_framework/p2p.py index eb60cd93f9bdf3..2dbc6f347d3f81 100755 --- a/test/functional/test_framework/p2p.py +++ b/test/functional/test_framework/p2p.py @@ -580,13 +580,13 @@ def on_version(self, message): # 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, delay=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, delay=delay) def ensure_for(self, *, duration, f, delay=0.2): return ensure_for_helper_internal(duration=duration, predicate=f, delay=delay) diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py index 45b6d766fa03ff..dcae06b2902f85 100755 --- a/test/functional/test_framework/test_node.py +++ b/test/functional/test_framework/test_node.py @@ -839,8 +839,8 @@ def bumpmocktime(self, seconds): 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, delay=0.05): + return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.timeout_factor, delay=delay) def ensure_for(self, *, duration, f, delay=0.2): return ensure_for_helper_internal(duration=duration, predicate=f, delay=delay) diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py index 445af83ffe3365..cfe3dd5fcc4f57 100644 --- a/test/functional/test_framework/util.py +++ b/test/functional/test_framework/util.py @@ -275,7 +275,7 @@ def ensure_for_helper_internal(*, duration, predicate, delay=0.2): raise AssertionError("Predicate {} became false within {} seconds".format(predicate_source, duration)) time.sleep(delay) -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, delay=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 @@ -299,7 +299,7 @@ def wait_until_helper_internal(predicate, *, attempts=float('inf'), timeout=floa if predicate(): return attempt += 1 - time.sleep(0.05) + time.sleep(delay) # Print the cause of the timeout predicate_source = "''''\n" + inspect.getsource(predicate) + "'''" diff --git a/test/functional/wallet_encryption.py b/test/functional/wallet_encryption.py index 5a5fb3e5be377a..2a293a6b7d4ecb 100755 --- a/test/functional/wallet_encryption.py +++ b/test/functional/wallet_encryption.py @@ -10,6 +10,7 @@ from test_framework.util import ( assert_raises_rpc_error, assert_equal, + try_rpc, ) from test_framework.wallet_util import WalletUnlock @@ -53,8 +54,7 @@ def run_test(self): assert self.nodes[0].verifymessage(address, sig, msg) # Check that the timeout is right - time.sleep(3) - assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].signmessage, address, msg) + self.nodes[0].wait_until(lambda: try_rpc(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].signmessage, address, msg), timeout=3, delay=0.5) # Test wrong passphrase assert_raises_rpc_error(-14, "wallet passphrase entered was incorrect", self.nodes[0].walletpassphrase, passphrase + "wrong", 10) diff --git a/test/functional/wallet_inactive_hdchains.py b/test/functional/wallet_inactive_hdchains.py index 3b0c09c02bed01..0fe2372d09af5e 100755 --- a/test/functional/wallet_inactive_hdchains.py +++ b/test/functional/wallet_inactive_hdchains.py @@ -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 @@ def do_inactive_test(self, base_wallet, test_wallet, encrypt=False): 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, delay=0.1) if encrypt: # The test wallet will not be able to generate the topped up keypool diff --git a/test/functional/wallet_keypool.py b/test/functional/wallet_keypool.py index d3b6ca1ed11126..62c5d3c0e74304 100755 --- a/test/functional/wallet_keypool.py +++ b/test/functional/wallet_keypool.py @@ -4,7 +4,6 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test the wallet keypool and interaction with wallet encryption/locking.""" -import time from decimal import Decimal from test_framework.test_framework import BitcoinTestFramework @@ -137,8 +136,7 @@ def run_test(self): nodes[0].keypoolrefill(3) # test walletpassphrase timeout - time.sleep(1.1) - assert_equal(nodes[0].getwalletinfo()["unlocked_until"], 0) + self.nodes[0].wait_until(lambda: nodes[0].getwalletinfo()["unlocked_until"] == 0, timeout=1.1, delay=0.5) # drain the keypool for _ in range(3):