mirror of
https://github.com/dashpay/dash.git
synced 2024-12-27 21:12:48 +01:00
Merge #12553: Prefer wait_until over polling with time.sleep
9d7f839a2
test: Use os.path.join consistently in feature_pruning tests (Ben Woosley)81b082277
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
This commit is contained in:
commit
93634f296e
@ -11,7 +11,6 @@ This test takes 30 mins or more (up to 2 hours)
|
|||||||
|
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.util import *
|
from test_framework.util import *
|
||||||
import time
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
MIN_BLOCKS_TO_KEEP = 288
|
MIN_BLOCKS_TO_KEEP = 288
|
||||||
@ -23,7 +22,7 @@ TIMESTAMP_WINDOW = 2 * 60 * 60
|
|||||||
|
|
||||||
|
|
||||||
def calc_usage(blockdir):
|
def calc_usage(blockdir):
|
||||||
return sum(os.path.getsize(blockdir+f) for f in os.listdir(blockdir) if os.path.isfile(blockdir+f)) / (1024. * 1024.)
|
return sum(os.path.getsize(blockdir+f) for f in os.listdir(blockdir) if os.path.isfile(os.path.join(blockdir, f))) / (1024. * 1024.)
|
||||||
|
|
||||||
class PruneTest(BitcoinTestFramework):
|
class PruneTest(BitcoinTestFramework):
|
||||||
def set_test_params(self):
|
def set_test_params(self):
|
||||||
@ -70,7 +69,7 @@ class PruneTest(BitcoinTestFramework):
|
|||||||
sync_blocks(self.nodes[0:5])
|
sync_blocks(self.nodes[0:5])
|
||||||
|
|
||||||
def test_height_min(self):
|
def test_height_min(self):
|
||||||
if not os.path.isfile(self.prunedir+"blk00000.dat"):
|
if not os.path.isfile(os.path.join(self.prunedir, "blk00000.dat")):
|
||||||
raise AssertionError("blk00000.dat is missing, pruning too early")
|
raise AssertionError("blk00000.dat is missing, pruning too early")
|
||||||
self.log.info("Success")
|
self.log.info("Success")
|
||||||
self.log.info("Though we're already using more than 550MiB, current usage: %d" % calc_usage(self.prunedir))
|
self.log.info("Though we're already using more than 550MiB, current usage: %d" % calc_usage(self.prunedir))
|
||||||
@ -79,11 +78,8 @@ class PruneTest(BitcoinTestFramework):
|
|||||||
for i in range(25):
|
for i in range(25):
|
||||||
mine_large_block(self.nodes[0], self.utxo_cache_0)
|
mine_large_block(self.nodes[0], self.utxo_cache_0)
|
||||||
|
|
||||||
waitstart = time.time()
|
# Wait for blk00000.dat to be pruned
|
||||||
while os.path.isfile(self.prunedir+"blk00000.dat"):
|
wait_until(lambda: not os.path.isfile(os.path.join(self.prunedir, "blk00000.dat")), timeout=30)
|
||||||
time.sleep(0.1)
|
|
||||||
if time.time() - waitstart > 30:
|
|
||||||
raise AssertionError("blk00000.dat not pruned when it should be")
|
|
||||||
|
|
||||||
self.log.info("Success")
|
self.log.info("Success")
|
||||||
usage = calc_usage(self.prunedir)
|
usage = calc_usage(self.prunedir)
|
||||||
@ -218,11 +214,8 @@ class PruneTest(BitcoinTestFramework):
|
|||||||
goalbestheight = first_reorg_height + 1
|
goalbestheight = first_reorg_height + 1
|
||||||
|
|
||||||
self.log.info("Verify node 2 reorged back to the main chain, some blocks of which it had to redownload")
|
self.log.info("Verify node 2 reorged back to the main chain, some blocks of which it had to redownload")
|
||||||
waitstart = time.time()
|
# Wait for Node 2 to reorg to proper height
|
||||||
while self.nodes[2].getblockcount() < goalbestheight:
|
wait_until(lambda: self.nodes[2].getblockcount() >= goalbestheight, timeout=900)
|
||||||
time.sleep(0.1)
|
|
||||||
if time.time() - waitstart > 900:
|
|
||||||
raise AssertionError("Node 2 didn't reorg to proper height")
|
|
||||||
assert(self.nodes[2].getbestblockhash() == goalbesthash)
|
assert(self.nodes[2].getbestblockhash() == goalbesthash)
|
||||||
# Verify we can now have the data for a block previously pruned
|
# Verify we can now have the data for a block previously pruned
|
||||||
assert(self.nodes[2].getblock(self.forkhash)["height"] == self.forkheight)
|
assert(self.nodes[2].getblock(self.forkhash)["height"] == self.forkheight)
|
||||||
@ -262,7 +255,7 @@ class PruneTest(BitcoinTestFramework):
|
|||||||
assert_equal(ret, expected_ret)
|
assert_equal(ret, expected_ret)
|
||||||
|
|
||||||
def has_block(index):
|
def has_block(index):
|
||||||
return os.path.isfile(self.options.tmpdir + "/node{}/regtest/blocks/blk{:05}.dat".format(node_number, index))
|
return os.path.isfile(os.path.join(self.nodes[node_number].datadir, "regtest", "blocks", "blk{:05}.dat".format(index)))
|
||||||
|
|
||||||
# should not prune because chain tip of node 3 (995) < PruneAfterHeight (1000)
|
# should not prune because chain tip of node 3 (995) < PruneAfterHeight (1000)
|
||||||
assert_raises_rpc_error(-1, "Blockchain is too short for pruning", node.pruneblockchain, height(500))
|
assert_raises_rpc_error(-1, "Blockchain is too short for pruning", node.pruneblockchain, height(500))
|
||||||
|
@ -10,8 +10,7 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.util import assert_equal
|
from test_framework.util import wait_until
|
||||||
import time
|
|
||||||
|
|
||||||
class ReindexTest(BitcoinTestFramework):
|
class ReindexTest(BitcoinTestFramework):
|
||||||
|
|
||||||
@ -25,9 +24,7 @@ class ReindexTest(BitcoinTestFramework):
|
|||||||
self.stop_nodes()
|
self.stop_nodes()
|
||||||
extra_args = [["-reindex-chainstate" if justchainstate else "-reindex", "-checkblockindex=1"]]
|
extra_args = [["-reindex-chainstate" if justchainstate else "-reindex", "-checkblockindex=1"]]
|
||||||
self.start_nodes(extra_args)
|
self.start_nodes(extra_args)
|
||||||
while self.nodes[0].getblockcount() < blockcount:
|
wait_until(lambda: self.nodes[0].getblockcount() == blockcount)
|
||||||
time.sleep(0.1)
|
|
||||||
assert_equal(self.nodes[0].getblockcount(), blockcount)
|
|
||||||
self.log.info("Success")
|
self.log.info("Success")
|
||||||
|
|
||||||
def run_test(self):
|
def run_test(self):
|
||||||
|
@ -7,8 +7,6 @@
|
|||||||
Tests correspond to code in rpc/net.cpp.
|
Tests correspond to code in rpc/net.cpp.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.util import (
|
from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
@ -62,12 +60,8 @@ class NetTest(BitcoinTestFramework):
|
|||||||
|
|
||||||
self.nodes[0].setnetworkactive(False)
|
self.nodes[0].setnetworkactive(False)
|
||||||
assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
|
assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
|
||||||
timeout = 3
|
|
||||||
while self.nodes[0].getnetworkinfo()['connections'] != 0:
|
|
||||||
# Wait a bit for all sockets to close
|
# Wait a bit for all sockets to close
|
||||||
assert timeout > 0, 'not all connections closed in time'
|
wait_until(lambda: self.nodes[0].getnetworkinfo()['connections'] == 0, timeout=3)
|
||||||
timeout -= 0.1
|
|
||||||
time.sleep(0.1)
|
|
||||||
|
|
||||||
self.nodes[0].setnetworkactive(True)
|
self.nodes[0].setnetworkactive(True)
|
||||||
connect_nodes_bi(self.nodes, 0, 1)
|
connect_nodes_bi(self.nodes, 0, 1)
|
||||||
|
@ -340,20 +340,15 @@ def disconnect_nodes(from_connection, node_num):
|
|||||||
for peer_id in [peer['id'] for peer in from_connection.getpeerinfo() if "testnode%d" % node_num in peer['subver']]:
|
for peer_id in [peer['id'] for peer in from_connection.getpeerinfo() if "testnode%d" % node_num in peer['subver']]:
|
||||||
from_connection.disconnectnode(nodeid=peer_id)
|
from_connection.disconnectnode(nodeid=peer_id)
|
||||||
|
|
||||||
for _ in range(50):
|
# wait to disconnect
|
||||||
if [peer['id'] for peer in from_connection.getpeerinfo() if "testnode%d" % node_num in peer['subver']] == []:
|
wait_until(lambda: [peer['id'] for peer in from_connection.getpeerinfo() if "testnode%d" % node_num in peer['subver']] == [], timeout=5)
|
||||||
break
|
|
||||||
time.sleep(0.1)
|
|
||||||
else:
|
|
||||||
raise AssertionError("timed out waiting for disconnect")
|
|
||||||
|
|
||||||
def connect_nodes(from_connection, node_num):
|
def connect_nodes(from_connection, node_num):
|
||||||
ip_port = "127.0.0.1:" + str(p2p_port(node_num))
|
ip_port = "127.0.0.1:" + str(p2p_port(node_num))
|
||||||
from_connection.addnode(ip_port, "onetry")
|
from_connection.addnode(ip_port, "onetry")
|
||||||
# poll until version handshake complete to avoid race conditions
|
# poll until version handshake complete to avoid race conditions
|
||||||
# with transaction relaying
|
# with transaction relaying
|
||||||
while any(peer['version'] == 0 for peer in from_connection.getpeerinfo()):
|
wait_until(lambda: all(peer['version'] != 0 for peer in from_connection.getpeerinfo()))
|
||||||
time.sleep(0.1)
|
|
||||||
|
|
||||||
def connect_nodes_bi(nodes, a, b):
|
def connect_nodes_bi(nodes, a, b):
|
||||||
connect_nodes(nodes[a], b)
|
connect_nodes(nodes[a], b)
|
||||||
|
@ -379,9 +379,9 @@ class WalletTest(BitcoinTestFramework):
|
|||||||
self.start_node(0, [m, "-limitancestorcount="+str(chainlimit)])
|
self.start_node(0, [m, "-limitancestorcount="+str(chainlimit)])
|
||||||
self.start_node(1, [m, "-limitancestorcount="+str(chainlimit)])
|
self.start_node(1, [m, "-limitancestorcount="+str(chainlimit)])
|
||||||
self.start_node(2, [m, "-limitancestorcount="+str(chainlimit)])
|
self.start_node(2, [m, "-limitancestorcount="+str(chainlimit)])
|
||||||
while m == '-reindex' and [block_count] * 3 != [self.nodes[i].getblockcount() for i in range(3)]:
|
if m == '-reindex':
|
||||||
# reindex will leave rpc warm up "early"; Wait for it to finish
|
# reindex will leave rpc warm up "early"; Wait for it to finish
|
||||||
time.sleep(0.1)
|
wait_until(lambda: [block_count] * 3 == [self.nodes[i].getblockcount() for i in range(3)])
|
||||||
assert_equal(balance_nodes, [self.nodes[i].getbalance() for i in range(3)])
|
assert_equal(balance_nodes, [self.nodes[i].getbalance() for i in range(3)])
|
||||||
|
|
||||||
# Exercise listsinceblock with the last two blocks
|
# Exercise listsinceblock with the last two blocks
|
||||||
|
Loading…
Reference in New Issue
Block a user