Skip to content

Commit aa2622a

Browse files
committed
qa: Refactor ZMQ test
1 parent 6bc1ff9 commit aa2622a

File tree

1 file changed

+31
-38
lines changed

1 file changed

+31
-38
lines changed

test/functional/interface_zmq.py

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE
99
from test_framework.test_framework import BitcoinTestFramework
1010
from test_framework.messages import CTransaction, hash256
11-
from test_framework.util import assert_equal
11+
from test_framework.util import assert_equal, connect_nodes
1212
from io import BytesIO
1313

14-
ADDRESS = "tcp://127.0.0.1:28332"
15-
1614
def hash256_reversed(byte_str):
1715
return hash256(byte_str)[::-1]
1816

@@ -43,66 +41,61 @@ def skip_test_if_missing_module(self):
4341
self.skip_if_no_py3_zmq()
4442
self.skip_if_no_bitcoind_zmq()
4543

46-
def setup_nodes(self):
44+
def run_test(self):
4745
import zmq
46+
self.ctx = zmq.Context()
47+
try:
48+
self.test_basic()
49+
finally:
50+
# Destroy the ZMQ context.
51+
self.log.debug("Destroying ZMQ context")
52+
self.ctx.destroy(linger=None)
4853

49-
# Initialize ZMQ context and socket.
54+
def test_basic(self):
5055
# All messages are received in the same socket which means
5156
# that this test fails if the publishing order changes.
5257
# Note that the publishing order is not defined in the documentation and
5358
# is subject to change.
54-
self.zmq_context = zmq.Context()
55-
socket = self.zmq_context.socket(zmq.SUB)
59+
import zmq
60+
address = 'tcp://127.0.0.1:28332'
61+
socket = self.ctx.socket(zmq.SUB)
5662
socket.set(zmq.RCVTIMEO, 60000)
57-
socket.connect(ADDRESS)
63+
socket.connect(address)
5864

5965
# Subscribe to all available topics.
60-
self.hashblock = ZMQSubscriber(socket, b"hashblock")
61-
self.hashtx = ZMQSubscriber(socket, b"hashtx")
62-
self.rawblock = ZMQSubscriber(socket, b"rawblock")
63-
self.rawtx = ZMQSubscriber(socket, b"rawtx")
64-
65-
self.extra_args = [
66-
["-zmqpub%s=%s" % (sub.topic.decode(), ADDRESS) for sub in [self.hashblock, self.hashtx, self.rawblock, self.rawtx]],
67-
[],
68-
]
69-
self.add_nodes(self.num_nodes, self.extra_args)
70-
self.start_nodes()
71-
self.import_deterministic_coinbase_privkeys()
66+
hashblock = ZMQSubscriber(socket, b"hashblock")
67+
hashtx = ZMQSubscriber(socket, b"hashtx")
68+
rawblock = ZMQSubscriber(socket, b"rawblock")
69+
rawtx = ZMQSubscriber(socket, b"rawtx")
7270

73-
def run_test(self):
74-
try:
75-
self._zmq_test()
76-
finally:
77-
# Destroy the ZMQ context.
78-
self.log.debug("Destroying ZMQ context")
79-
self.zmq_context.destroy(linger=None)
71+
self.restart_node(0, ["-zmqpub%s=%s" % (sub.topic.decode(), address) for sub in [hashblock, hashtx, rawblock, rawtx]])
72+
connect_nodes(self.nodes[0], 1)
8073

81-
def _zmq_test(self):
8274
num_blocks = 5
8375
self.log.info("Generate %(n)d blocks (and %(n)d coinbase txes)" % {"n": num_blocks})
8476
genhashes = self.nodes[0].generatetoaddress(num_blocks, ADDRESS_BCRT1_UNSPENDABLE)
77+
8578
self.sync_all()
8679

8780
for x in range(num_blocks):
8881
# Should receive the coinbase txid.
89-
txid = self.hashtx.receive()
82+
txid = hashtx.receive()
9083

9184
# Should receive the coinbase raw transaction.
92-
hex = self.rawtx.receive()
85+
hex = rawtx.receive()
9386
tx = CTransaction()
9487
tx.deserialize(BytesIO(hex))
9588
tx.calc_sha256()
9689
assert_equal(tx.hash, txid.hex())
9790

9891
# Should receive the generated block hash.
99-
hash = self.hashblock.receive().hex()
92+
hash = hashblock.receive().hex()
10093
assert_equal(genhashes[x], hash)
10194
# The block should only have the coinbase txid.
10295
assert_equal([txid.hex()], self.nodes[1].getblock(hash)["tx"])
10396

10497
# Should receive the generated raw block.
105-
block = self.rawblock.receive()
98+
block = rawblock.receive()
10699
assert_equal(genhashes[x], hash256_reversed(block[:80]).hex())
107100

108101
if self.is_wallet_compiled():
@@ -111,20 +104,20 @@ def _zmq_test(self):
111104
self.sync_all()
112105

113106
# Should receive the broadcasted txid.
114-
txid = self.hashtx.receive()
107+
txid = hashtx.receive()
115108
assert_equal(payment_txid, txid.hex())
116109

117110
# Should receive the broadcasted raw transaction.
118-
hex = self.rawtx.receive()
111+
hex = rawtx.receive()
119112
assert_equal(payment_txid, hash256_reversed(hex).hex())
120113

121114

122115
self.log.info("Test the getzmqnotifications RPC")
123116
assert_equal(self.nodes[0].getzmqnotifications(), [
124-
{"type": "pubhashblock", "address": ADDRESS, "hwm": 1000},
125-
{"type": "pubhashtx", "address": ADDRESS, "hwm": 1000},
126-
{"type": "pubrawblock", "address": ADDRESS, "hwm": 1000},
127-
{"type": "pubrawtx", "address": ADDRESS, "hwm": 1000},
117+
{"type": "pubhashblock", "address": address, "hwm": 1000},
118+
{"type": "pubhashtx", "address": address, "hwm": 1000},
119+
{"type": "pubrawblock", "address": address, "hwm": 1000},
120+
{"type": "pubrawtx", "address": address, "hwm": 1000},
128121
])
129122

130123
assert_equal(self.nodes[1].getzmqnotifications(), [])

0 commit comments

Comments
 (0)