Skip to content

Commit 8f3e384

Browse files
committed
Merge #10169: [tests] Remove func test code duplication
2a52ae6 Remove duplicate method definitions in NodeConnCB subclasses (John Newbery) 52e15aa Adds helper functions to NodeConnCB (John Newbery) Tree-SHA512: 2d7909eb85b3bde0fc3ebf133798eca21e561f4b2a2880937750820a42856cfb61fc94e30591c14ac13218bcfae0ebe7c5e8662a7b10f5b02470325c44a86cf1
2 parents 2580ff8 + 2a52ae6 commit 8f3e384

12 files changed

+235
-513
lines changed

test/functional/maxuploadtarget.py

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,24 @@
1010
if uploadtarget has been reached.
1111
* Verify that the upload counters are reset after 24 hours.
1212
"""
13+
from collections import defaultdict
14+
import time
1315

1416
from test_framework.mininode import *
1517
from test_framework.test_framework import BitcoinTestFramework
1618
from test_framework.util import *
17-
import time
1819

19-
# TestNode: bare-bones "peer". Used mostly as a conduit for a test to sending
20-
# p2p messages to a node, generating the messages in the main testing logic.
2120
class TestNode(NodeConnCB):
2221
def __init__(self):
2322
super().__init__()
24-
self.connection = None
25-
self.ping_counter = 1
26-
self.last_pong = msg_pong()
27-
self.block_receive_map = {}
28-
29-
def add_connection(self, conn):
30-
self.connection = conn
31-
self.peer_disconnected = False
23+
self.block_receive_map = defaultdict(int)
3224

3325
def on_inv(self, conn, message):
3426
pass
3527

36-
# Track the last getdata message we receive (used in the test)
37-
def on_getdata(self, conn, message):
38-
self.last_getdata = message
39-
4028
def on_block(self, conn, message):
4129
message.block.calc_sha256()
42-
try:
43-
self.block_receive_map[message.block.sha256] += 1
44-
except KeyError as e:
45-
self.block_receive_map[message.block.sha256] = 1
46-
47-
# Spin until verack message is received from the node.
48-
# We use this to signal that our test can begin. This
49-
# is called from the testing thread, so it needs to acquire
50-
# the global lock.
51-
def wait_for_verack(self):
52-
def veracked():
53-
return self.verack_received
54-
return wait_until(veracked, timeout=10)
55-
56-
def wait_for_disconnect(self):
57-
def disconnected():
58-
return self.peer_disconnected
59-
return wait_until(disconnected, timeout=10)
60-
61-
# Wrapper for the NodeConn's send_message function
62-
def send_message(self, message):
63-
self.connection.send_message(message)
64-
65-
def on_pong(self, conn, message):
66-
self.last_pong = message
67-
68-
def on_close(self, conn):
69-
self.peer_disconnected = True
30+
self.block_receive_map[message.block.sha256] += 1
7031

7132
class MaxUploadTest(BitcoinTestFramework):
7233

@@ -192,33 +153,26 @@ def run_test(self):
192153
stop_node(self.nodes[0], 0)
193154
self.nodes[0] = start_node(0, self.options.tmpdir, ["-whitelist=127.0.0.1", "-maxuploadtarget=1", "-blockmaxsize=999000"])
194155

195-
#recreate/reconnect 3 test nodes
196-
test_nodes = []
197-
connections = []
198-
199-
for i in range(3):
200-
test_nodes.append(TestNode())
201-
connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i]))
202-
test_nodes[i].add_connection(connections[i])
156+
#recreate/reconnect a test node
157+
test_nodes = [TestNode()]
158+
connections = [NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[0])]
159+
test_nodes[0].add_connection(connections[0])
203160

204161
NetworkThread().start() # Start up network handling in another thread
205-
[x.wait_for_verack() for x in test_nodes]
162+
test_nodes[0].wait_for_verack()
206163

207164
#retrieve 20 blocks which should be enough to break the 1MB limit
208165
getdata_request.inv = [CInv(2, big_new_block)]
209166
for i in range(20):
210-
test_nodes[1].send_message(getdata_request)
211-
test_nodes[1].sync_with_ping()
212-
assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1)
167+
test_nodes[0].send_message(getdata_request)
168+
test_nodes[0].sync_with_ping()
169+
assert_equal(test_nodes[0].block_receive_map[big_new_block], i+1)
213170

214171
getdata_request.inv = [CInv(2, big_old_block)]
215-
test_nodes[1].send_message(getdata_request)
216-
test_nodes[1].wait_for_disconnect()
217-
assert_equal(len(self.nodes[0].getpeerinfo()), 3) #node is still connected because of the whitelist
172+
test_nodes[0].send_and_ping(getdata_request)
173+
assert_equal(len(self.nodes[0].getpeerinfo()), 1) #node is still connected because of the whitelist
218174

219-
self.log.info("Peer 1 still connected after trying to download old block (whitelisted)")
220-
221-
[c.disconnect_node() for c in connections]
175+
self.log.info("Peer still connected after trying to download old block (whitelisted)")
222176

223177
if __name__ == '__main__':
224178
MaxUploadTest().main()

test/functional/p2p-acceptblock.py

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -54,40 +54,6 @@
5454
import time
5555
from test_framework.blocktools import create_block, create_coinbase
5656

57-
# TestNode: bare-bones "peer". Used mostly as a conduit for a test to sending
58-
# p2p messages to a node, generating the messages in the main testing logic.
59-
class TestNode(NodeConnCB):
60-
def __init__(self):
61-
super().__init__()
62-
self.connection = None
63-
self.ping_counter = 1
64-
self.last_pong = msg_pong()
65-
66-
def add_connection(self, conn):
67-
self.connection = conn
68-
69-
# Track the last getdata message we receive (used in the test)
70-
def on_getdata(self, conn, message):
71-
self.last_getdata = message
72-
73-
# Spin until verack message is received from the node.
74-
# We use this to signal that our test can begin. This
75-
# is called from the testing thread, so it needs to acquire
76-
# the global lock.
77-
def wait_for_verack(self):
78-
while True:
79-
with mininode_lock:
80-
if self.verack_received:
81-
return
82-
time.sleep(0.05)
83-
84-
# Wrapper for the NodeConn's send_message function
85-
def send_message(self, message):
86-
self.connection.send_message(message)
87-
88-
def on_pong(self, conn, message):
89-
self.last_pong = message
90-
9157
class AcceptBlockTest(BitcoinTestFramework):
9258
def add_options(self, parser):
9359
parser.add_option("--testbinary", dest="testbinary",
@@ -112,8 +78,8 @@ def setup_network(self):
11278

11379
def run_test(self):
11480
# Setup the p2p connections and start up the network thread.
115-
test_node = TestNode() # connects to node0 (not whitelisted)
116-
white_node = TestNode() # connects to node1 (whitelisted)
81+
test_node = NodeConnCB() # connects to node0 (not whitelisted)
82+
white_node = NodeConnCB() # connects to node1 (whitelisted)
11783

11884
connections = []
11985
connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node))
@@ -238,12 +204,12 @@ def run_test(self):
238204
# triggers a getdata on block 2 (it should if block 2 is missing).
239205
with mininode_lock:
240206
# Clear state so we can check the getdata request
241-
test_node.last_getdata = None
207+
test_node.last_message.pop("getdata", None)
242208
test_node.send_message(msg_inv([CInv(2, blocks_h3[0].sha256)]))
243209

244210
test_node.sync_with_ping()
245211
with mininode_lock:
246-
getdata = test_node.last_getdata
212+
getdata = test_node.last_message["getdata"]
247213

248214
# Check that the getdata includes the right block
249215
assert_equal(getdata.inv[0].hash, blocks_h2f[0].sha256)

0 commit comments

Comments
 (0)