Skip to content

Commit 136fe4c

Browse files
committed
Merge #19816: test: Rename wait until helper to wait_until_helper
fa1cd9e test: Remove unused lock arg from BitcoinTestFramework.wait_until (MarcoFalke) fad2794 test: Rename wait until helper to wait_until_helper (MarcoFalke) facb41b test: Remove unused p2p_lock in VersionBitsWarningTest (MarcoFalke) Pull request description: This avoids confusion with the `wait_until` member functions, which should be preferred because they take the appropriate locks and scale the timeout appropriately on their own. ACKs for top commit: laanwj: Code review ACK fa1cd9e hebasto: ACK fa1cd9e, I have reviewed the code and it looks OK, I agree it can be merged. Tree-SHA512: 319d400085606a4c738e314824037f72998e6657d8622b363726842aba968744f23c56d27275dfe506b8cbbb6e97fc39ca1d325db05d4d67df0e8b35f2244d5c
2 parents 9876ab8 + fa1cd9e commit 136fe4c

File tree

7 files changed

+27
-26
lines changed

7 files changed

+27
-26
lines changed

test/functional/example_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,11 @@ def run_test(self):
207207
self.log.info("Check that each block was received only once")
208208
# The network thread uses a global lock on data access to the P2PConnection objects when sending and receiving
209209
# messages. The test thread should acquire the global lock before accessing any P2PConnection data to avoid locking
210-
# and synchronization issues. Note wait_until() acquires this global lock when testing the predicate.
210+
# and synchronization issues. Note p2p.wait_until() acquires this global lock internally when testing the predicate.
211211
with p2p_lock:
212212
for block in self.nodes[2].p2p.block_receive_map.values():
213213
assert_equal(block, 1)
214214

215+
215216
if __name__ == '__main__':
216217
ExampleTest().main()

test/functional/feature_versionbits_warning.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from test_framework.blocktools import create_block, create_coinbase
1414
from test_framework.messages import msg_block
15-
from test_framework.p2p import p2p_lock, P2PInterface
15+
from test_framework.p2p import P2PInterface
1616
from test_framework.test_framework import BitcoinTestFramework
1717

1818
VB_PERIOD = 144 # versionbits period length for regtest
@@ -90,7 +90,7 @@ def run_test(self):
9090

9191
# Generating one block guarantees that we'll get out of IBD
9292
node.generatetoaddress(1, node_deterministic_address)
93-
self.wait_until(lambda: not node.getblockchaininfo()['initialblockdownload'], timeout=10, lock=p2p_lock)
93+
self.wait_until(lambda: not node.getblockchaininfo()['initialblockdownload'])
9494
# Generating one more block will be enough to generate an error.
9595
node.generatetoaddress(1, node_deterministic_address)
9696
# Check that get*info() shows the versionbits unknown rules warning

test/functional/p2p_leak.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
msg_ping,
1818
msg_version,
1919
)
20-
from test_framework.p2p import p2p_lock, P2PInterface
20+
from test_framework.p2p import P2PInterface
2121
from test_framework.test_framework import BitcoinTestFramework
2222
from test_framework.util import (
2323
assert_equal,
@@ -113,9 +113,9 @@ def run_test(self):
113113
# verack, since we never sent one
114114
no_verack_idle_peer.wait_for_verack()
115115

116-
self.wait_until(lambda: no_version_disconnect_peer.ever_connected, timeout=10, lock=p2p_lock)
117-
self.wait_until(lambda: no_version_idle_peer.ever_connected, timeout=10, lock=p2p_lock)
118-
self.wait_until(lambda: no_verack_idle_peer.version_received, timeout=10, lock=p2p_lock)
116+
no_version_disconnect_peer.wait_until(lambda: no_version_disconnect_peer.ever_connected, check_connected=False)
117+
no_version_idle_peer.wait_until(lambda: no_version_idle_peer.ever_connected)
118+
no_verack_idle_peer.wait_until(lambda: no_verack_idle_peer.version_received)
119119

120120
# Mine a block and make sure that it's not sent to the connected peers
121121
self.nodes[0].generate(nblocks=1)

test/functional/test_framework/p2p.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
NODE_WITNESS,
7070
sha256,
7171
)
72-
from test_framework.util import wait_until
72+
from test_framework.util import wait_until_helper
7373

7474
logger = logging.getLogger("TestFramework.p2p")
7575

@@ -293,7 +293,7 @@ def __init__(self):
293293

294294
# Track the most recent message of each type.
295295
# To wait for a message to be received, pop that message from
296-
# this and use wait_until.
296+
# this and use self.wait_until.
297297
self.last_message = {}
298298

299299
# A count of the number of ping messages we've sent to the node
@@ -398,7 +398,7 @@ def test_function():
398398
assert self.is_connected
399399
return test_function_in()
400400

401-
wait_until(test_function, timeout=timeout, lock=p2p_lock, timeout_factor=self.timeout_factor)
401+
wait_until_helper(test_function, timeout=timeout, lock=p2p_lock, timeout_factor=self.timeout_factor)
402402

403403
def wait_for_disconnect(self, timeout=60):
404404
test_function = lambda: not self.is_connected
@@ -522,7 +522,7 @@ def run(self):
522522
def close(self, timeout=10):
523523
"""Close the connections and network event loop."""
524524
self.network_event_loop.call_soon_threadsafe(self.network_event_loop.stop)
525-
wait_until(lambda: not self.network_event_loop.is_running(), timeout=timeout)
525+
wait_until_helper(lambda: not self.network_event_loop.is_running(), timeout=timeout)
526526
self.network_event_loop.close()
527527
self.join(timeout)
528528
# Safe to remove event loop.

test/functional/test_framework/test_framework.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
disconnect_nodes,
3232
get_datadir_path,
3333
initialize_datadir,
34-
wait_until,
34+
wait_until_helper,
3535
)
3636

3737

@@ -603,8 +603,8 @@ def sync_all(self, nodes=None):
603603
self.sync_blocks(nodes)
604604
self.sync_mempools(nodes)
605605

606-
def wait_until(self, test_function, timeout=60, lock=None):
607-
return wait_until(test_function, timeout=timeout, lock=lock, timeout_factor=self.options.timeout_factor)
606+
def wait_until(self, test_function, timeout=60):
607+
return wait_until_helper(test_function, timeout=timeout, timeout_factor=self.options.timeout_factor)
608608

609609
# Private helper methods. These should not be accessed by the subclass test scripts.
610610

test/functional/test_framework/test_node.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
get_auth_cookie,
3232
get_rpc_proxy,
3333
rpc_url,
34-
wait_until,
34+
wait_until_helper,
3535
p2p_port,
3636
EncodeDecimal,
3737
)
@@ -231,7 +231,7 @@ def wait_for_rpc_connection(self):
231231
if self.version_is_at_least(190000):
232232
# getmempoolinfo.loaded is available since commit
233233
# bb8ae2c (version 0.19.0)
234-
wait_until(lambda: rpc.getmempoolinfo()['loaded'])
234+
wait_until_helper(lambda: rpc.getmempoolinfo()['loaded'], timeout_factor=self.timeout_factor)
235235
# Wait for the node to finish reindex, block import, and
236236
# loading the mempool. Usually importing happens fast or
237237
# even "immediate" when the node is started. However, there
@@ -359,7 +359,7 @@ def is_node_stopped(self):
359359
return True
360360

361361
def wait_until_stopped(self, timeout=BITCOIND_PROC_WAIT_TIMEOUT):
362-
wait_until(self.is_node_stopped, timeout=timeout, timeout_factor=self.timeout_factor)
362+
wait_until_helper(self.is_node_stopped, timeout=timeout, timeout_factor=self.timeout_factor)
363363

364364
@contextlib.contextmanager
365365
def assert_debug_log(self, expected_msgs, unexpected_msgs=None, timeout=2):
@@ -560,7 +560,7 @@ def disconnect_p2ps(self):
560560
for p in self.p2ps:
561561
p.peer_disconnect()
562562
del self.p2ps[:]
563-
wait_until(lambda: self.num_test_p2p_connections() == 0)
563+
wait_until_helper(lambda: self.num_test_p2p_connections() == 0, timeout_factor=self.timeout_factor)
564564

565565

566566
class TestNodeCLIAttr:

test/functional/test_framework/util.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,14 @@ def satoshi_round(amount):
226226
return Decimal(amount).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN)
227227

228228

229-
def wait_until(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=None, timeout_factor=1.0):
229+
def wait_until_helper(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=None, timeout_factor=1.0):
230230
"""Sleep until the predicate resolves to be True.
231231
232232
Warning: Note that this method is not recommended to be used in tests as it is
233-
not aware of the context of the test framework. Using `wait_until()` counterpart
234-
from `BitcoinTestFramework` or `P2PInterface` class ensures an understandable
235-
amount of timeout and a common shared timeout_factor. Furthermore, `wait_until()`
236-
from `P2PInterface` class in `mininode.py` has a preset lock.
233+
not aware of the context of the test framework. Using the `wait_until()` members
234+
from `BitcoinTestFramework` or `P2PInterface` class ensures the timeout is
235+
properly scaled. Furthermore, `wait_until()` from `P2PInterface` class in
236+
`p2p.py` has a preset lock.
237237
"""
238238
if attempts == float('inf') and timeout == float('inf'):
239239
timeout = 60
@@ -438,7 +438,7 @@ def get_peer_ids():
438438
raise
439439

440440
# wait to disconnect
441-
wait_until(lambda: not get_peer_ids(), timeout=5)
441+
wait_until_helper(lambda: not get_peer_ids(), timeout=5)
442442

443443

444444
def connect_nodes(from_connection, node_num):
@@ -449,8 +449,8 @@ def connect_nodes(from_connection, node_num):
449449
# See comments in net_processing:
450450
# * Must have a version message before anything else
451451
# * Must have a verack message before anything else
452-
wait_until(lambda: all(peer['version'] != 0 for peer in from_connection.getpeerinfo()))
453-
wait_until(lambda: all(peer['bytesrecv_per_msg'].pop('verack', 0) == 24 for peer in from_connection.getpeerinfo()))
452+
wait_until_helper(lambda: all(peer['version'] != 0 for peer in from_connection.getpeerinfo()))
453+
wait_until_helper(lambda: all(peer['bytesrecv_per_msg'].pop('verack', 0) == 24 for peer in from_connection.getpeerinfo()))
454454

455455

456456
# Transaction/Block functions

0 commit comments

Comments
 (0)