Skip to content

Commit b199b32

Browse files
committed
test: Fix intermittent issue in p2p_handshake.py
If we reuse the same port when disconnecting and establishing connections again, we might hit this scenario: - disconnection is done on python side for P2PConnection - disconnection is not complete on c++ side for TestNode - we're trying to establish a new connection on same port again Prevent this scenario from happening by using a different p2p_idx/port everytime.
1 parent c8e3978 commit b199b32

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

test/functional/p2p_handshake.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class P2PHandshakeTest(BitcoinTestFramework):
3131
def set_test_params(self):
3232
self.num_nodes = 1
3333

34-
def add_outbound_connection(self, node, connection_type, services, wait_for_disconnect):
34+
def add_outbound_connection(self, node, peer_id, connection_type, services, wait_for_disconnect):
3535
peer = node.add_outbound_p2p_connection(
36-
P2PInterface(), p2p_idx=0, wait_for_disconnect=wait_for_disconnect,
36+
P2PInterface(), p2p_idx=peer_id, wait_for_disconnect=wait_for_disconnect,
3737
connection_type=connection_type, services=services,
3838
supports_v2_p2p=self.options.v2transport, advertise_v2_p2p=self.options.v2transport)
3939
if not wait_for_disconnect:
@@ -47,6 +47,7 @@ def test_desirable_service_flags(self, node, service_flag_tests, desirable_servi
4747
service flags in the VERSION message. The test is exercised for all relevant
4848
outbound connection types where the desirable service flags check is done."""
4949
CONNECTION_TYPES = ["outbound-full-relay", "block-relay-only", "addr-fetch"]
50+
peer_id = 0
5051
for conn_type, services in itertools.product(CONNECTION_TYPES, service_flag_tests):
5152
if self.options.v2transport:
5253
services |= NODE_P2P_V2
@@ -57,10 +58,11 @@ def test_desirable_service_flags(self, node, service_flag_tests, desirable_servi
5758
expected_debug_log = f'does not offer the expected services ' \
5859
f'({services:08x} offered, {desirable_service_flags:08x} expected)'
5960
with node.assert_debug_log([expected_debug_log]):
60-
self.add_outbound_connection(node, conn_type, services, wait_for_disconnect=True)
61+
self.add_outbound_connection(node, peer_id, conn_type, services, wait_for_disconnect=True)
6162
else:
6263
assert (services & desirable_service_flags) == desirable_service_flags
63-
self.add_outbound_connection(node, conn_type, services, wait_for_disconnect=False)
64+
self.add_outbound_connection(node, peer_id, conn_type, services, wait_for_disconnect=False)
65+
peer_id += 1
6466

6567
def generate_at_mocktime(self, time):
6668
self.nodes[0].setmocktime(time)
@@ -85,7 +87,7 @@ def run_test(self):
8587

8688
self.log.info("Check that feeler connections get disconnected immediately")
8789
with node.assert_debug_log([f"feeler connection completed"]):
88-
self.add_outbound_connection(node, "feeler", NODE_NONE, wait_for_disconnect=True)
90+
self.add_outbound_connection(node, 0, "feeler", NODE_NONE, wait_for_disconnect=True)
8991

9092

9193
if __name__ == '__main__':

0 commit comments

Comments
 (0)