Changeset View
Changeset View
Standalone View
Standalone View
test/functional/example_test.py
Show All 17 Lines | |||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
CInv, | CInv, | ||||
MSG_BLOCK, | MSG_BLOCK, | ||||
msg_block, | msg_block, | ||||
msg_getdata | msg_getdata | ||||
) | ) | ||||
from test_framework.mininode import ( | from test_framework.mininode import ( | ||||
P2PInterface, | P2PInterface, | ||||
mininode_lock, | p2p_lock, | ||||
) | ) | ||||
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, | ||||
connect_nodes, | connect_nodes, | ||||
wait_until, | wait_until, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 178 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
getdata_request = msg_getdata() | getdata_request = msg_getdata() | ||||
for block in blocks: | for block in blocks: | ||||
getdata_request.inv.append(CInv(MSG_BLOCK, block)) | getdata_request.inv.append(CInv(MSG_BLOCK, block)) | ||||
self.nodes[2].p2p.send_message(getdata_request) | self.nodes[2].p2p.send_message(getdata_request) | ||||
# wait_until() will loop until a predicate condition is met. Use it to test properties of the | # wait_until() will loop until a predicate condition is met. Use it to test properties of the | ||||
# P2PInterface objects. | # P2PInterface objects. | ||||
wait_until(lambda: sorted(blocks) == sorted( | wait_until(lambda: sorted(blocks) == sorted( | ||||
list(self.nodes[2].p2p.block_receive_map.keys())), timeout=5, lock=mininode_lock) | list(self.nodes[2].p2p.block_receive_map.keys())), timeout=5, lock=p2p_lock) | ||||
self.log.info("Check that each block was received only once") | self.log.info("Check that each block was received only once") | ||||
# The network thread uses a global lock on data access to the P2PConnection objects when sending and receiving | # The network thread uses a global lock on data access to the P2PConnection objects when sending and receiving | ||||
# messages. The test thread should acquire the global lock before accessing any P2PConnection data to avoid locking | # messages. The test thread should acquire the global lock before accessing any P2PConnection data to avoid locking | ||||
# and synchronization issues. Note wait_until() acquires this global | # and synchronization issues. Note wait_until() acquires this global | ||||
# lock when testing the predicate. | # lock when testing the predicate. | ||||
with mininode_lock: | with p2p_lock: | ||||
for block in self.nodes[2].p2p.block_receive_map.values(): | for block in self.nodes[2].p2p.block_receive_map.values(): | ||||
assert_equal(block, 1) | assert_equal(block, 1) | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
ExampleTest().main() | ExampleTest().main() |