test: Fix race condition in p2p_txexpiringsoon

The recent changes to mempool inv logic mean that nodes are much less
likely to immediately return an `inv` message in response to a `mempool`
message. The `p2p_txexpiringsoon` RPC test was relying on the prior
behaviour.

`TestNode.sync_with_ping` now takes an optional `waiting_for` closure
that allows the caller to require that a specific message kind is
received prior to the timeout.
This commit is contained in:
Jack Grigg 2021-08-09 17:43:48 +01:00
parent 6be1a2a9da
commit 75b9fc4f27
2 changed files with 4 additions and 3 deletions

View File

@ -51,7 +51,7 @@ class TxExpiringSoonTest(BitcoinTestFramework):
testnode.send_message(msg_mempool())
# Sync up with node after p2p messages delivered
testnode.sync_with_ping()
testnode.sync_with_ping(waiting_for=lambda x: x.last_inv)
with mininode_lock:
msg = testnode.last_inv

View File

@ -66,12 +66,13 @@ class TestNode(NodeConnCB):
# The following function is mostly copied from p2p-acceptblock.py
# Sync up with the node after delivery of a message
def sync_with_ping(self, timeout=30):
def sync_with_ping(self, timeout=30, waiting_for=None):
self.connection.send_message(msg_ping(nonce=self.ping_counter))
sleep_time = 0.05
while timeout > 0:
with mininode_lock:
if self.last_pong.nonce == self.ping_counter:
ready = True if waiting_for is None else waiting_for(self) is not None
if ready and self.last_pong.nonce == self.ping_counter:
self.ping_counter += 1
return
time.sleep(sleep_time)