mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-02 00:38:15 +01:00
test: fix v2 transport intermittent test failure (#29002)
Only relying on the number of peers for detecting a new connection suffers from race conditions, as unrelated previous peers could disconnect at anytime in-between. Use the more robust approach of watching for an increased highest peer id instead (again using the `getpeerinfo` RPC call), with a newly introduced context manager method `TestNode.wait_for_new_peer()`. Fixes #29009.
This commit is contained in:
@@ -519,6 +519,24 @@ class TestNode():
|
||||
'Expected messages "{}" does not partially match log:\n\n{}\n\n'.format(
|
||||
str(expected_msgs), print_log))
|
||||
|
||||
@contextlib.contextmanager
|
||||
def wait_for_new_peer(self, timeout=5):
|
||||
"""
|
||||
Wait until the node is connected to at least one new peer. We detect this
|
||||
by watching for an increased highest peer id, using the `getpeerinfo` RPC call.
|
||||
Note that the simpler approach of only accounting for the number of peers
|
||||
suffers from race conditions, as disconnects from unrelated previous peers
|
||||
could happen anytime in-between.
|
||||
"""
|
||||
def get_highest_peer_id():
|
||||
peer_info = self.getpeerinfo()
|
||||
return peer_info[-1]["id"] if peer_info else -1
|
||||
|
||||
initial_peer_id = get_highest_peer_id()
|
||||
yield
|
||||
wait_until_helper_internal(lambda: get_highest_peer_id() > initial_peer_id,
|
||||
timeout=timeout, timeout_factor=self.timeout_factor)
|
||||
|
||||
@contextlib.contextmanager
|
||||
def profile_with_perf(self, profile_name: str):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user