Skip to content

Commit 93634f2

Browse files
committed
Merge #12553: Prefer wait_until over polling with time.sleep
9d7f839 test: Use os.path.join consistently in feature_pruning tests (Ben Woosley) 81b0822 test: Use wait_until in tests where time was used for polling (Ben Woosley) Pull request description: This is prompted by and builds on #12545, a nice cleanup / consolidation of patterns. In cases where the exception message was meaningful, I tried to represent it as well in a comment. I expect #12545 will go in first, but I'm happy to squash them if that's preferred. Tree-SHA512: 7a861244001c87fd6b59b6bc248ee741ac8178f7255d6f1fda39bc693c5ff3b7de5f53d13afe9829aef6ea69153481edb0a9d5bc07c36c4f66b4315edd180bb4
2 parents c39dd2e + 9d7f839 commit 93634f2

File tree

5 files changed

+16
-37
lines changed

5 files changed

+16
-37
lines changed

test/functional/feature_pruning.py

Lines changed: 7 additions & 14 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
@@ -23,7 +22,7 @@
2322

2423

2524
def calc_usage(blockdir):
26-
return sum(os.path.getsize(blockdir+f) for f in os.listdir(blockdir) if os.path.isfile(blockdir+f)) / (1024. * 1024.)
25+
return sum(os.path.getsize(blockdir+f) for f in os.listdir(blockdir) if os.path.isfile(os.path.join(blockdir, f))) / (1024. * 1024.)
2726

2827
class PruneTest(BitcoinTestFramework):
2928
def set_test_params(self):
@@ -70,7 +69,7 @@ def create_big_chain(self):
7069
sync_blocks(self.nodes[0:5])
7170

7271
def test_height_min(self):
73-
if not os.path.isfile(self.prunedir+"blk00000.dat"):
72+
if not os.path.isfile(os.path.join(self.prunedir, "blk00000.dat")):
7473
raise AssertionError("blk00000.dat is missing, pruning too early")
7574
self.log.info("Success")
7675
self.log.info("Though we're already using more than 550MiB, current usage: %d" % calc_usage(self.prunedir))
@@ -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(os.path.join(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)
@@ -262,7 +255,7 @@ def prune(index, expected_ret=None):
262255
assert_equal(ret, expected_ret)
263256

264257
def has_block(index):
265-
return os.path.isfile(self.options.tmpdir + "/node{}/regtest/blocks/blk{:05}.dat".format(node_number, index))
258+
return os.path.isfile(os.path.join(self.nodes[node_number].datadir, "regtest", "blocks", "blk{:05}.dat".format(index)))
266259

267260
# should not prune because chain tip of node 3 (995) < PruneAfterHeight (1000)
268261
assert_raises_rpc_error(-1, "Blockchain is too short for pruning", node.pruneblockchain, height(500))

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)