Skip to content

Commit 81b0822

Browse files
committed
test: Use wait_until in tests where time was used for polling
1 parent 7be9a9a commit 81b0822

File tree

5 files changed

+13
-34
lines changed

5 files changed

+13
-34
lines changed

test/functional/feature_pruning.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from test_framework.test_framework import BitcoinTestFramework
1313
from test_framework.util import *
14-
import time
1514
import os
1615

1716
MIN_BLOCKS_TO_KEEP = 288
@@ -79,11 +78,8 @@ def test_height_min(self):
7978
for i in range(25):
8079
mine_large_block(self.nodes[0], self.utxo_cache_0)
8180

82-
waitstart = time.time()
83-
while os.path.isfile(self.prunedir+"blk00000.dat"):
84-
time.sleep(0.1)
85-
if time.time() - waitstart > 30:
86-
raise AssertionError("blk00000.dat not pruned when it should be")
81+
# Wait for blk00000.dat to be pruned
82+
wait_until(lambda: not os.path.isfile(self.prunedir+"blk00000.dat"), timeout=30)
8783

8884
self.log.info("Success")
8985
usage = calc_usage(self.prunedir)
@@ -218,11 +214,8 @@ def reorg_back(self):
218214
goalbestheight = first_reorg_height + 1
219215

220216
self.log.info("Verify node 2 reorged back to the main chain, some blocks of which it had to redownload")
221-
waitstart = time.time()
222-
while self.nodes[2].getblockcount() < goalbestheight:
223-
time.sleep(0.1)
224-
if time.time() - waitstart > 900:
225-
raise AssertionError("Node 2 didn't reorg to proper height")
217+
# Wait for Node 2 to reorg to proper height
218+
wait_until(lambda: self.nodes[2].getblockcount() >= goalbestheight, timeout=900)
226219
assert(self.nodes[2].getbestblockhash() == goalbesthash)
227220
# Verify we can now have the data for a block previously pruned
228221
assert(self.nodes[2].getblock(self.forkhash)["height"] == self.forkheight)

test/functional/feature_reindex.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"""
1111

1212
from test_framework.test_framework import BitcoinTestFramework
13-
from test_framework.util import assert_equal
14-
import time
13+
from test_framework.util import wait_until
1514

1615
class ReindexTest(BitcoinTestFramework):
1716

@@ -25,9 +24,7 @@ def reindex(self, justchainstate=False):
2524
self.stop_nodes()
2625
extra_args = [["-reindex-chainstate" if justchainstate else "-reindex", "-checkblockindex=1"]]
2726
self.start_nodes(extra_args)
28-
while self.nodes[0].getblockcount() < blockcount:
29-
time.sleep(0.1)
30-
assert_equal(self.nodes[0].getblockcount(), blockcount)
27+
wait_until(lambda: self.nodes[0].getblockcount() == blockcount)
3128
self.log.info("Success")
3229

3330
def run_test(self):

test/functional/rpc_net.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
Tests correspond to code in rpc/net.cpp.
88
"""
99

10-
import time
11-
1210
from test_framework.test_framework import BitcoinTestFramework
1311
from test_framework.util import (
1412
assert_equal,
@@ -62,12 +60,8 @@ def _test_getnetworkinginfo(self):
6260

6361
self.nodes[0].setnetworkactive(False)
6462
assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
65-
timeout = 3
66-
while self.nodes[0].getnetworkinfo()['connections'] != 0:
67-
# Wait a bit for all sockets to close
68-
assert timeout > 0, 'not all connections closed in time'
69-
timeout -= 0.1
70-
time.sleep(0.1)
63+
# Wait a bit for all sockets to close
64+
wait_until(lambda: self.nodes[0].getnetworkinfo()['connections'] == 0, timeout=3)
7165

7266
self.nodes[0].setnetworkactive(True)
7367
connect_nodes_bi(self.nodes, 0, 1)

test/functional/test_framework/util.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -340,20 +340,15 @@ def disconnect_nodes(from_connection, node_num):
340340
for peer_id in [peer['id'] for peer in from_connection.getpeerinfo() if "testnode%d" % node_num in peer['subver']]:
341341
from_connection.disconnectnode(nodeid=peer_id)
342342

343-
for _ in range(50):
344-
if [peer['id'] for peer in from_connection.getpeerinfo() if "testnode%d" % node_num in peer['subver']] == []:
345-
break
346-
time.sleep(0.1)
347-
else:
348-
raise AssertionError("timed out waiting for disconnect")
343+
# wait to disconnect
344+
wait_until(lambda: [peer['id'] for peer in from_connection.getpeerinfo() if "testnode%d" % node_num in peer['subver']] == [], timeout=5)
349345

350346
def connect_nodes(from_connection, node_num):
351347
ip_port = "127.0.0.1:" + str(p2p_port(node_num))
352348
from_connection.addnode(ip_port, "onetry")
353349
# poll until version handshake complete to avoid race conditions
354350
# with transaction relaying
355-
while any(peer['version'] == 0 for peer in from_connection.getpeerinfo()):
356-
time.sleep(0.1)
351+
wait_until(lambda: all(peer['version'] != 0 for peer in from_connection.getpeerinfo()))
357352

358353
def connect_nodes_bi(nodes, a, b):
359354
connect_nodes(nodes[a], b)

test/functional/wallet_basic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,9 @@ def run_test(self):
379379
self.start_node(0, [m, "-limitancestorcount="+str(chainlimit)])
380380
self.start_node(1, [m, "-limitancestorcount="+str(chainlimit)])
381381
self.start_node(2, [m, "-limitancestorcount="+str(chainlimit)])
382-
while m == '-reindex' and [block_count] * 3 != [self.nodes[i].getblockcount() for i in range(3)]:
382+
if m == '-reindex':
383383
# reindex will leave rpc warm up "early"; Wait for it to finish
384-
time.sleep(0.1)
384+
wait_until(lambda: [block_count] * 3 == [self.nodes[i].getblockcount() for i in range(3)])
385385
assert_equal(balance_nodes, [self.nodes[i].getbalance() for i in range(3)])
386386

387387
# Exercise listsinceblock with the last two blocks

0 commit comments

Comments
 (0)