Skip to content

Commit e871f83

Browse files
committed
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.
1 parent 425278d commit e871f83

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

qa/rpc-tests/test_framework/util.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,30 +121,34 @@ def hex_str_to_bytes(hex_str):
121121
def str_to_b64str(string):
122122
return b64encode(string.encode('utf-8')).decode('ascii')
123123

124-
def sync_blocks(rpc_connections, wait=1):
124+
def sync_blocks(rpc_connections, wait=1, timeout=60):
125125
"""
126-
Wait until everybody has the same block count
126+
Wait until everybody has the same tip
127127
"""
128-
while True:
129-
counts = [ x.getblockcount() for x in rpc_connections ]
130-
if counts == [ counts[0] ]*len(counts):
131-
break
128+
while timeout > 0:
129+
tips = [ x.getbestblockhash() for x in rpc_connections ]
130+
if tips == [ tips[0] ]*len(tips):
131+
return True
132132
time.sleep(wait)
133+
timeout -= wait
134+
raise AssertionError("Block sync failed")
133135

134-
def sync_mempools(rpc_connections, wait=1):
136+
def sync_mempools(rpc_connections, wait=1, timeout=60):
135137
"""
136138
Wait until everybody has the same transactions in their memory
137139
pools
138140
"""
139-
while True:
141+
while timeout > 0:
140142
pool = set(rpc_connections[0].getrawmempool())
141143
num_match = 1
142144
for i in range(1, len(rpc_connections)):
143145
if set(rpc_connections[i].getrawmempool()) == pool:
144146
num_match = num_match+1
145147
if num_match == len(rpc_connections):
146-
break
148+
return True
147149
time.sleep(wait)
150+
timeout -= wait
151+
raise AssertionError("Mempool sync failed")
148152

149153
bitcoind_processes = {}
150154

0 commit comments

Comments
 (0)