Skip to content

Commit

Permalink
test: Add delay parameter to wait_until
Browse files Browse the repository at this point in the history
This also replaces two sleep calls in functional tests with wait_until
  • Loading branch information
fjahr committed Sep 21, 2024
1 parent facb801 commit 44bbb33
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 15 deletions.
4 changes: 2 additions & 2 deletions test/functional/test_framework/p2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/functional/test_framework/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/functional/test_framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) + "'''"
Expand Down
4 changes: 2 additions & 2 deletions test/functional/wallet_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from test_framework.util import (
assert_raises_rpc_error,
assert_equal,
try_rpc,
)
from test_framework.wallet_util import WalletUnlock

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions test/functional/wallet_inactive_hdchains.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions test/functional/wallet_keypool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 44bbb33

Please sign in to comment.