Skip to content

Commit 8bf0a5c

Browse files
laanwjPastaPastaPasta
authored andcommitted
Merge bitcoin#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 bitcoin#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 bitcoin#12545 will go in first, but I'm happy to squash them if that's preferred. Tree-SHA512: 7a861244001c87fd6b59b6bc248ee741ac8178f7255d6f1fda39bc693c5ff3b7de5f53d13afe9829aef6ea69153481edb0a9d5bc07c36c4f66b4315edd180bb4
1 parent 62bbabb commit 8bf0a5c

File tree

5 files changed

+16
-37
lines changed

5 files changed

+16
-37
lines changed

test/functional/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,
@@ -71,12 +69,8 @@ def _test_getnetworkinginfo(self):
7169

7270
self.nodes[0].setnetworkactive(False)
7371
assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
74-
timeout = 3
75-
while self.nodes[0].getnetworkinfo()['connections'] != 0:
76-
# Wait a bit for all sockets to close
77-
assert timeout > 0, 'not all connections closed in time'
78-
timeout -= 0.1
79-
time.sleep(0.1)
72+
# Wait a bit for all sockets to close
73+
wait_until(lambda: self.nodes[0].getnetworkinfo()['connections'] == 0, timeout=3)
8074

8175
self.nodes[0].setnetworkactive(True)
8276
connect_nodes_bi(self.nodes, 0, 1)

test/functional/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
import sys
1716

@@ -24,7 +23,7 @@
2423

2524

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

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

7372
def test_height_min(self):
74-
if not os.path.isfile(self.prunedir+"blk00000.dat"):
73+
if not os.path.isfile(os.path.join(self.prunedir, "blk00000.dat")):
7574
raise AssertionError("blk00000.dat is missing, pruning too early")
7675
self.log.info("Success")
7776
self.log.info("Though we're already using more than 550MiB, current usage: %d" % calc_usage(self.prunedir))
@@ -80,11 +79,8 @@ def test_height_min(self):
8079
for i in range(25):
8180
mine_large_block(self.nodes[0], self.utxo_cache_0)
8281

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

8985
self.log.info("Success")
9086
usage = calc_usage(self.prunedir)
@@ -219,11 +215,8 @@ def reorg_back(self):
219215
goalbestheight = first_reorg_height + 1
220216

221217
self.log.info("Verify node 2 reorged back to the main chain, some blocks of which it had to redownload")
222-
waitstart = time.time()
223-
while self.nodes[2].getblockcount() < goalbestheight:
224-
time.sleep(0.1)
225-
if time.time() - waitstart > 900:
226-
raise AssertionError("Node 2 didn't reorg to proper height")
218+
# Wait for Node 2 to reorg to proper height
219+
wait_until(lambda: self.nodes[2].getblockcount() >= goalbestheight, timeout=900)
227220
assert(self.nodes[2].getbestblockhash() == goalbesthash)
228221
# Verify we can now have the data for a block previously pruned
229222
assert(self.nodes[2].getblock(self.forkhash)["height"] == self.forkheight)
@@ -263,7 +256,7 @@ def prune(index, expected_ret=None):
263256
assert_equal(ret, expected_ret)
264257

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

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

test/functional/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/test_framework/util.py

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

364-
for _ in range(50):
365-
if [peer['id'] for peer in from_connection.getpeerinfo() if "testnode%d" % node_num in peer['subver']] == []:
366-
break
367-
time.sleep(0.1)
368-
else:
369-
raise AssertionError("timed out waiting for disconnect")
364+
# wait to disconnect
365+
wait_until(lambda: [peer['id'] for peer in from_connection.getpeerinfo() if "testnode%d" % node_num in peer['subver']] == [], timeout=5)
370366

371367
def connect_nodes(from_connection, node_num):
372368
ip_port = "127.0.0.1:" + str(p2p_port(node_num))
373369
from_connection.addnode(ip_port, "onetry")
374370
# poll until version handshake complete to avoid race conditions
375371
# with transaction relaying
376-
while any(peer['version'] == 0 for peer in from_connection.getpeerinfo()):
377-
time.sleep(0.1)
372+
wait_until(lambda: all(peer['version'] != 0 for peer in from_connection.getpeerinfo()))
378373

379374
def connect_nodes_bi(nodes, a, b):
380375
connect_nodes(nodes[a], b)

test/functional/wallet.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)