Skip to content

Commit

Permalink
qa: Move wait_until to util
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoFalke committed Aug 17, 2017
1 parent 22e301a commit 08ce33f
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 89 deletions.
4 changes: 2 additions & 2 deletions test/functional/bip65-cltv-p2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def run_test(self):
node0.send_and_ping(msg_block(block))
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)

assert wait_until(lambda: "reject" in node0.last_message.keys())
wait_until(lambda: "reject" in node0.last_message.keys(), lock=mininode_lock)
with mininode_lock:
assert_equal(node0.last_message["reject"].code, REJECT_OBSOLETE)
assert_equal(node0.last_message["reject"].reason, b'bad-version(0x00000003)')
Expand Down Expand Up @@ -138,7 +138,7 @@ def run_test(self):
node0.send_and_ping(msg_block(block))
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)

assert wait_until (lambda: "reject" in node0.last_message.keys())
wait_until(lambda: "reject" in node0.last_message.keys(), lock=mininode_lock)
with mininode_lock:
assert node0.last_message["reject"].code in [REJECT_INVALID, REJECT_NONSTANDARD]
assert_equal(node0.last_message["reject"].data, block.sha256)
Expand Down
4 changes: 2 additions & 2 deletions test/functional/bipdersig-p2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def run_test(self):
node0.send_and_ping(msg_block(block))
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)

assert wait_until(lambda: "reject" in node0.last_message.keys())
wait_until(lambda: "reject" in node0.last_message.keys(), lock=mininode_lock)
with mininode_lock:
assert_equal(node0.last_message["reject"].code, REJECT_OBSOLETE)
assert_equal(node0.last_message["reject"].reason, b'bad-version(0x00000002)')
Expand Down Expand Up @@ -128,7 +128,7 @@ def run_test(self):
node0.send_and_ping(msg_block(block))
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)

assert wait_until (lambda: "reject" in node0.last_message.keys())
wait_until(lambda: "reject" in node0.last_message.keys(), lock=mininode_lock)
with mininode_lock:
# We can receive different reject messages depending on whether
# bitcoind is running with multiple script check threads. If script
Expand Down
16 changes: 9 additions & 7 deletions test/functional/disconnect_ban.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"""Test node disconnect and ban behavior"""
import time

from test_framework.mininode import wait_until
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (assert_equal,
assert_raises_jsonrpc,
connect_nodes_bi)
from test_framework.util import (
assert_equal,
assert_raises_jsonrpc,
connect_nodes_bi,
wait_until,
)

class DisconnectBanTest(BitcoinTestFramework):

Expand All @@ -24,7 +26,7 @@ def run_test(self):
self.log.info("setban: successfully ban single IP address")
assert_equal(len(self.nodes[1].getpeerinfo()), 2) # node1 should have 2 connections to node0 at this point
self.nodes[1].setban("127.0.0.1", "add")
assert wait_until(lambda: len(self.nodes[1].getpeerinfo()) == 0, timeout=10)
wait_until(lambda: len(self.nodes[1].getpeerinfo()) == 0, timeout=10)
assert_equal(len(self.nodes[1].getpeerinfo()), 0) # all nodes must be disconnected at this point
assert_equal(len(self.nodes[1].listbanned()), 1)

Expand Down Expand Up @@ -90,7 +92,7 @@ def run_test(self):
self.log.info("disconnectnode: successfully disconnect node by address")
address1 = self.nodes[0].getpeerinfo()[0]['addr']
self.nodes[0].disconnectnode(address=address1)
assert wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1, timeout=10)
wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1, timeout=10)
assert not [node for node in self.nodes[0].getpeerinfo() if node['addr'] == address1]

self.log.info("disconnectnode: successfully reconnect node")
Expand All @@ -101,7 +103,7 @@ def run_test(self):
self.log.info("disconnectnode: successfully disconnect node by node id")
id1 = self.nodes[0].getpeerinfo()[0]['id']
self.nodes[0].disconnectnode(nodeid=id1)
assert wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1, timeout=10)
wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1, timeout=10)
assert not [node for node in self.nodes[0].getpeerinfo() if node['id'] == id1]

if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions test/functional/example_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
mininode_lock,
msg_block,
msg_getdata,
wait_until,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
connect_nodes,
p2p_port,
wait_until,
)

# NodeConnCB is a class containing callbacks to be executed when a P2P
Expand Down Expand Up @@ -209,7 +209,7 @@ def run_test(self):

# wait_until() will loop until a predicate condition is met. Use it to test properties of the
# NodeConnCB objects.
assert wait_until(lambda: sorted(blocks) == sorted(list(node2.block_receive_map.keys())), timeout=5)
wait_until(lambda: sorted(blocks) == sorted(list(node2.block_receive_map.keys())), timeout=5, lock=mininode_lock)

self.log.info("Check that each block was received only once")
# The network thread uses a global lock on data access to the NodeConn objects when sending and receiving
Expand Down
5 changes: 2 additions & 3 deletions test/functional/mempool_persist.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"""
import time

from test_framework.mininode import wait_until
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *

Expand Down Expand Up @@ -69,7 +68,7 @@ def run_test(self):
self.nodes.append(self.start_node(1, self.options.tmpdir))
# Give bitcoind a second to reload the mempool
time.sleep(1)
assert wait_until(lambda: len(self.nodes[0].getrawmempool()) == 5)
wait_until(lambda: len(self.nodes[0].getrawmempool()) == 5)
assert_equal(len(self.nodes[1].getrawmempool()), 0)

self.log.debug("Stop-start node0 with -persistmempool=0. Verify that it doesn't load its mempool.dat file.")
Expand All @@ -84,7 +83,7 @@ def run_test(self):
self.stop_nodes()
self.nodes = []
self.nodes.append(self.start_node(0, self.options.tmpdir))
assert wait_until(lambda: len(self.nodes[0].getrawmempool()) == 5)
wait_until(lambda: len(self.nodes[0].getrawmempool()) == 5)

if __name__ == '__main__':
MempoolPersistTest().main()
47 changes: 16 additions & 31 deletions test/functional/p2p-compactblocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,23 @@ def send_header_for_blocks(self, new_blocks):
def request_headers_and_sync(self, locator, hashstop=0):
self.clear_block_announcement()
self.get_headers(locator, hashstop)
assert wait_until(self.received_block_announcement, timeout=30)
wait_until(self.received_block_announcement, timeout=30, lock=mininode_lock)
self.clear_block_announcement()

# Block until a block announcement for a particular block hash is
# received.
def wait_for_block_announcement(self, block_hash, timeout=30):
def received_hash():
return (block_hash in self.announced_blockhashes)
return wait_until(received_hash, timeout=timeout)
wait_until(received_hash, timeout=timeout, lock=mininode_lock)

def send_await_disconnect(self, message, timeout=30):
"""Sends a message to the node and wait for disconnect.
This is used when we want to send a message into the node that we expect
will get us disconnected, eg an invalid block."""
self.send_message(message)
success = wait_until(lambda: not self.connected, timeout=timeout)
if not success:
logger.error("send_await_disconnect failed!")
raise AssertionError("send_await_disconnect failed!")
return success
wait_until(lambda: not self.connected, timeout=timeout, lock=mininode_lock)

class CompactBlocksTest(BitcoinTestFramework):
def __init__(self):
Expand Down Expand Up @@ -150,9 +146,7 @@ def test_sendcmpct(self, node, test_node, preferred_version, old_node=None):
# Make sure we get a SENDCMPCT message from our peer
def received_sendcmpct():
return (len(test_node.last_sendcmpct) > 0)
got_message = wait_until(received_sendcmpct, timeout=30)
assert(received_sendcmpct())
assert(got_message)
wait_until(received_sendcmpct, timeout=30, lock=mininode_lock)
with mininode_lock:
# Check that the first version received is the preferred one
assert_equal(test_node.last_sendcmpct[0].version, preferred_version)
Expand All @@ -167,7 +161,6 @@ def check_announcement_of_new_block(node, peer, predicate):
block_hash = int(node.generate(1)[0], 16)
peer.wait_for_block_announcement(block_hash, timeout=30)
assert(peer.block_announced)
assert(got_message)

with mininode_lock:
assert predicate(peer), (
Expand Down Expand Up @@ -282,7 +275,7 @@ def test_compactblock_construction(self, node, test_node, version, use_witness_a

# Wait until we've seen the block announcement for the resulting tip
tip = int(node.getbestblockhash(), 16)
assert(test_node.wait_for_block_announcement(tip))
test_node.wait_for_block_announcement(tip)

# Make sure we will receive a fast-announce compact block
self.request_cb_announcements(test_node, node, version)
Expand All @@ -297,8 +290,7 @@ def test_compactblock_construction(self, node, test_node, version, use_witness_a
block.rehash()

# Wait until the block was announced (via compact blocks)
wait_until(test_node.received_block_announcement, timeout=30)
assert(test_node.received_block_announcement())
wait_until(test_node.received_block_announcement, timeout=30, lock=mininode_lock)

# Now fetch and check the compact block
header_and_shortids = None
Expand All @@ -314,8 +306,7 @@ def test_compactblock_construction(self, node, test_node, version, use_witness_a
inv = CInv(4, block_hash) # 4 == "CompactBlock"
test_node.send_message(msg_getdata([inv]))

wait_until(test_node.received_block_announcement, timeout=30)
assert(test_node.received_block_announcement())
wait_until(test_node.received_block_announcement, timeout=30, lock=mininode_lock)

# Now fetch and check the compact block
header_and_shortids = None
Expand Down Expand Up @@ -386,13 +377,11 @@ def test_compactblock_requests(self, node, test_node, version, segwit):

if announce == "inv":
test_node.send_message(msg_inv([CInv(2, block.sha256)]))
success = wait_until(lambda: "getheaders" in test_node.last_message, timeout=30)
assert(success)
wait_until(lambda: "getheaders" in test_node.last_message, timeout=30, lock=mininode_lock)
test_node.send_header_for_blocks([block])
else:
test_node.send_header_for_blocks([block])
success = wait_until(lambda: "getdata" in test_node.last_message, timeout=30)
assert(success)
wait_until(lambda: "getdata" in test_node.last_message, timeout=30, lock=mininode_lock)
assert_equal(len(test_node.last_message["getdata"].inv), 1)
assert_equal(test_node.last_message["getdata"].inv[0].type, 4)
assert_equal(test_node.last_message["getdata"].inv[0].hash, block.sha256)
Expand Down Expand Up @@ -571,8 +560,7 @@ def test_incorrect_blocktxn_response(self, node, test_node, version):
assert_equal(int(node.getbestblockhash(), 16), block.hashPrevBlock)

# We should receive a getdata request
success = wait_until(lambda: "getdata" in test_node.last_message, timeout=10)
assert(success)
wait_until(lambda: "getdata" in test_node.last_message, timeout=10, lock=mininode_lock)
assert_equal(len(test_node.last_message["getdata"].inv), 1)
assert(test_node.last_message["getdata"].inv[0].type == 2 or test_node.last_message["getdata"].inv[0].type == 2|MSG_WITNESS_FLAG)
assert_equal(test_node.last_message["getdata"].inv[0].hash, block.sha256)
Expand All @@ -599,8 +587,7 @@ def test_getblocktxn_handler(self, node, test_node, version):
num_to_request = random.randint(1, len(block.vtx))
msg.block_txn_request.from_absolute(sorted(random.sample(range(len(block.vtx)), num_to_request)))
test_node.send_message(msg)
success = wait_until(lambda: "blocktxn" in test_node.last_message, timeout=10)
assert(success)
wait_until(lambda: "blocktxn" in test_node.last_message, timeout=10, lock=mininode_lock)

[tx.calc_sha256() for tx in block.vtx]
with mininode_lock:
Expand Down Expand Up @@ -639,22 +626,20 @@ def test_compactblocks_not_at_tip(self, node, test_node):
for i in range(MAX_CMPCTBLOCK_DEPTH + 1):
test_node.clear_block_announcement()
new_blocks.append(node.generate(1)[0])
wait_until(test_node.received_block_announcement, timeout=30)
wait_until(test_node.received_block_announcement, timeout=30, lock=mininode_lock)

test_node.clear_block_announcement()
test_node.send_message(msg_getdata([CInv(4, int(new_blocks[0], 16))]))
success = wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30)
assert(success)
wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30, lock=mininode_lock)

test_node.clear_block_announcement()
node.generate(1)
wait_until(test_node.received_block_announcement, timeout=30)
wait_until(test_node.received_block_announcement, timeout=30, lock=mininode_lock)
test_node.clear_block_announcement()
with mininode_lock:
test_node.last_message.pop("block", None)
test_node.send_message(msg_getdata([CInv(4, int(new_blocks[0], 16))]))
success = wait_until(lambda: "block" in test_node.last_message, timeout=30)
assert(success)
wait_until(lambda: "block" in test_node.last_message, timeout=30, lock=mininode_lock)
with mininode_lock:
test_node.last_message["block"].block.calc_sha256()
assert_equal(test_node.last_message["block"].block.sha256, int(new_blocks[0], 16))
Expand Down Expand Up @@ -705,7 +690,7 @@ def test_end_to_end_block_relay(self, node, listeners):
node.submitblock(ToHex(block))

for l in listeners:
wait_until(lambda: l.received_block_announcement(), timeout=30)
wait_until(lambda: l.received_block_announcement(), timeout=30, lock=mininode_lock)
with mininode_lock:
for l in listeners:
assert "cmpctblock" in l.last_message
Expand Down
14 changes: 7 additions & 7 deletions test/functional/p2p-leaktests.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ def run_test(self):

NetworkThread().start() # Start up network handling in another thread

assert wait_until(lambda: no_version_bannode.ever_connected, timeout=10)
assert wait_until(lambda: no_version_idlenode.ever_connected, timeout=10)
assert wait_until(lambda: no_verack_idlenode.version_received, timeout=10)
assert wait_until(lambda: unsupported_service_bit5_node.ever_connected, timeout=10)
assert wait_until(lambda: unsupported_service_bit7_node.ever_connected, timeout=10)
wait_until(lambda: no_version_bannode.ever_connected, timeout=10, lock=mininode_lock)
wait_until(lambda: no_version_idlenode.ever_connected, timeout=10, lock=mininode_lock)
wait_until(lambda: no_verack_idlenode.version_received, timeout=10, lock=mininode_lock)
wait_until(lambda: unsupported_service_bit5_node.ever_connected, timeout=10, lock=mininode_lock)
wait_until(lambda: unsupported_service_bit7_node.ever_connected, timeout=10, lock=mininode_lock)

# Mine a block and make sure that it's not sent to the connected nodes
self.nodes[0].generate(1)
Expand Down Expand Up @@ -158,8 +158,8 @@ def run_test(self):
allowed_service_bit5_node.add_connection(connections[5])
allowed_service_bit7_node.add_connection(connections[6])

assert wait_until(lambda: allowed_service_bit5_node.message_count["verack"], timeout=10)
assert wait_until(lambda: allowed_service_bit7_node.message_count["verack"], timeout=10)
wait_until(lambda: allowed_service_bit5_node.message_count["verack"], timeout=10, lock=mininode_lock)
wait_until(lambda: allowed_service_bit7_node.message_count["verack"], timeout=10, lock=mininode_lock)

if __name__ == '__main__':
P2PLeakTest().main()
6 changes: 3 additions & 3 deletions test/functional/sendheaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def check_last_announcement(self, headers=None, inv=None):
expect_headers = headers if headers != None else []
expect_inv = inv if inv != None else []
test_function = lambda: self.block_announced
assert(wait_until(test_function, timeout=60))
wait_until(test_function, timeout=60, lock=mininode_lock)
with mininode_lock:
self.block_announced = False

Expand All @@ -155,12 +155,12 @@ def wait_for_getdata(self, hash_list, timeout=60):
return

test_function = lambda: "getdata" in self.last_message and [x.hash for x in self.last_message["getdata"].inv] == hash_list
assert(wait_until(test_function, timeout=timeout))
wait_until(test_function, timeout=timeout, lock=mininode_lock)
return

def wait_for_block_announcement(self, block_hash, timeout=60):
test_function = lambda: self.last_blockhash_announced == block_hash
assert(wait_until(test_function, timeout=timeout))
wait_until(test_function, timeout=timeout, lock=mininode_lock)
return

def send_header_for_blocks(self, new_blocks):
Expand Down
12 changes: 5 additions & 7 deletions test/functional/test_framework/comptool.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from .mininode import *
from .blockstore import BlockStore, TxStore
from .util import p2p_port
from .util import p2p_port, wait_until

import logging

Expand Down Expand Up @@ -189,15 +189,15 @@ def clear_all_connections(self):
def wait_for_disconnections(self):
def disconnected():
return all(node.closed for node in self.test_nodes)
return wait_until(disconnected, timeout=10)
wait_until(disconnected, timeout=10, lock=mininode_lock)

def wait_for_verack(self):
return all(node.wait_for_verack() for node in self.test_nodes)

def wait_for_pings(self, counter):
def received_pongs():
return all(node.received_ping_response(counter) for node in self.test_nodes)
return wait_until(received_pongs)
wait_until(received_pongs, lock=mininode_lock)

# sync_blocks: Wait for all connections to request the blockhash given
# then send get_headers to find out the tip of each node, and synchronize
Expand All @@ -210,8 +210,7 @@ def blocks_requested():
)

# --> error if not requested
if not wait_until(blocks_requested, attempts=20*num_blocks):
raise AssertionError("Not all nodes requested block")
wait_until(blocks_requested, attempts=20*num_blocks, lock=mininode_lock)

# Send getheaders message
[ c.cb.send_getheaders() for c in self.connections ]
Expand All @@ -231,8 +230,7 @@ def transaction_requested():
)

# --> error if not requested
if not wait_until(transaction_requested, attempts=20*num_events):
raise AssertionError("Not all nodes requested transaction")
wait_until(transaction_requested, attempts=20*num_events, lock=mininode_lock)

# Get the mempool
[ c.cb.send_mempool() for c in self.connections ]
Expand Down
Loading

0 comments on commit 08ce33f

Please sign in to comment.