Tests: add timeout to sync_blocks() and sync_mempools()

Previously these functions would infinitely loop if sync failed;
now they have a default timeout of 60 seconds, after which an
AssertionError is raised.

sync_blocks() has also been improved and now compares the tip
hash of each node, rather than just using block count.

Zcash: Kept block count check for a couple of tests where we use it.
This commit is contained in:
Suhas Daftuar 2016-05-26 14:19:07 -04:00 committed by Jack Grigg
parent 17caf1ddd1
commit 2399cfc7ba
2 changed files with 28 additions and 14 deletions

View File

@ -6,7 +6,7 @@
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, \
start_nodes, start_node, connect_nodes_bi, bitcoind_processes, \
nuparams, OVERWINTER_BRANCH_ID, SAPLING_BRANCH_ID
nuparams, sync_blocks, OVERWINTER_BRANCH_ID, SAPLING_BRANCH_ID
import time
@ -48,7 +48,7 @@ class RewindBlockIndexTest (BitcoinTestFramework):
print("Mining diverging blocks")
block10s = self.nodes[1].generate(1)[0]
block10o = self.nodes[2].generate(1)[0]
self.sync_all()
sync_blocks(self.nodes, allow_different_tips=True)
assert_equal(self.nodes[0].getbestblockhash(), block10o)
assert_equal(self.nodes[1].getbestblockhash(), block10s)

View File

@ -105,31 +105,41 @@ def hex_str_to_bytes(hex_str):
def str_to_b64str(string):
return b64encode(string.encode('utf-8')).decode('ascii')
def sync_blocks(rpc_connections, wait=1):
def sync_blocks(rpc_connections, wait=1, timeout=60, allow_different_tips=False):
"""
Wait until everybody has the same block count, and has notified
all internal listeners of them
Wait until everybody has the same tip, and has notified
all internal listeners of them.
If allow_different_tips is True, waits until everyone has
the same block count.
"""
while True:
counts = [ x.getblockcount() for x in rpc_connections ]
if counts == [ counts[0] ]*len(counts):
while timeout > 0:
if allow_different_tips:
tips = [ x.getblockcount() for x in rpc_connections ]
else:
tips = [ x.getbestblockhash() for x in rpc_connections ]
if tips == [ tips[0] ]*len(tips):
break
time.sleep(wait)
timeout -= wait
# Now that the block counts are in sync, wait for the internal
# notifications to finish
while True:
while timeout > 0:
notified = [ x.getblockchaininfo()['fullyNotified'] for x in rpc_connections ]
if notified == [ True ] * len(notified):
break
return True
time.sleep(wait)
timeout -= wait
def sync_mempools(rpc_connections, wait=1):
raise AssertionError("Block sync failed")
def sync_mempools(rpc_connections, wait=1, timeout=60):
"""
Wait until everybody has the same transactions in their memory
pools, and has notified all internal listeners of them
"""
while True:
while timeout > 0:
pool = set(rpc_connections[0].getrawmempool())
num_match = 1
for i in range(1, len(rpc_connections)):
@ -138,14 +148,18 @@ def sync_mempools(rpc_connections, wait=1):
if num_match == len(rpc_connections):
break
time.sleep(wait)
timeout -= wait
# Now that the mempools are in sync, wait for the internal
# notifications to finish
while True:
while timeout > 0:
notified = [ x.getmempoolinfo()['fullyNotified'] for x in rpc_connections ]
if notified == [ True ] * len(notified):
break
return True
time.sleep(wait)
timeout -= wait
raise AssertionError("Mempool sync failed")
bitcoind_processes = {}