Skip to content

Commit 643fa0b

Browse files
author
MarcoFalke
committed
Merge #10555: [tests] various improvements to zmq_test.py
0a4912e [tests] timeout integration tests on travis after 20 minutes (John Newbery) 7c51e9f [tests] destroy zmq context in zmq_tests.py (John Newbery) b1bac1c [tests] in zmq test, timeout if message not received (John Newbery) 5ebd5f9 [tests] tidy up zmq_test.py (John Newbery) 4a0c08f [tests] update zmq test to use correct config.ini file (John Newbery) Tree-SHA512: 5e607af2f2dc5c73fba4b2d3890097580a7525f6a4996c7c78f01822e45e0054fd0a225ea53fce6308804e560cced6de6cf5d16137469dcf100b2e9643e08d03
2 parents e053e05 + 0a4912e commit 643fa0b

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

test/functional/test_runner.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import os
2121
import time
2222
import shutil
23+
import signal
2324
import sys
2425
import subprocess
2526
import tempfile
@@ -78,7 +79,7 @@
7879
'rawtransactions.py',
7980
'reindex.py',
8081
# vv Tests less than 30s vv
81-
"zmq_test.py",
82+
'zmq_test.py',
8283
'mempool_resurrect_test.py',
8384
'txn_doublespend.py --mineblock',
8485
'txn_clone.py',
@@ -390,6 +391,10 @@ def get_next(self):
390391
time.sleep(.5)
391392
for j in self.jobs:
392393
(name, time0, proc, log_out, log_err) = j
394+
if os.getenv('TRAVIS') == 'true' and int(time.time() - time0) > 20 * 60:
395+
# In travis, timeout individual tests after 20 minutes (to stop tests hanging and not
396+
# providing useful output.
397+
proc.send_signal(signal.SIGINT)
393398
if proc.poll() is not None:
394399
log_out.seek(0), log_err.seek(0)
395400
[stdout, stderr] = [l.read().decode('utf-8') for l in (log_out, log_err)]

test/functional/zmq_test.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
import struct
99

1010
from test_framework.test_framework import BitcoinTestFramework, SkipTest
11-
from test_framework.util import *
11+
from test_framework.util import (assert_equal,
12+
bytes_to_hex_str,
13+
)
1214

1315
class ZMQTest (BitcoinTestFramework):
1416

1517
def __init__(self):
1618
super().__init__()
17-
self.num_nodes = 4
18-
19-
port = 28332
19+
self.num_nodes = 2
2020

2121
def setup_nodes(self):
2222
# Try to import python3-zmq. Skip this test if the import fails.
@@ -28,82 +28,87 @@ def setup_nodes(self):
2828
# Check that bitcoin has been built with ZMQ enabled
2929
config = configparser.ConfigParser()
3030
if not self.options.configfile:
31-
self.options.configfile = os.path.dirname(__file__) + "/config.ini"
31+
self.options.configfile = os.path.dirname(__file__) + "/../config.ini"
3232
config.read_file(open(self.options.configfile))
3333

3434
if not config["components"].getboolean("ENABLE_ZMQ"):
3535
raise SkipTest("bitcoind has not been built with zmq enabled.")
3636

3737
self.zmqContext = zmq.Context()
3838
self.zmqSubSocket = self.zmqContext.socket(zmq.SUB)
39+
self.zmqSubSocket.set(zmq.RCVTIMEO, 60000)
3940
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
4041
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtx")
41-
self.zmqSubSocket.connect("tcp://127.0.0.1:%i" % self.port)
42-
self.nodes = self.start_nodes(self.num_nodes, self.options.tmpdir, extra_args=[
43-
['-zmqpubhashtx=tcp://127.0.0.1:'+str(self.port), '-zmqpubhashblock=tcp://127.0.0.1:'+str(self.port)],
44-
[],
45-
[],
46-
[]
47-
])
42+
ip_address = "tcp://127.0.0.1:28332"
43+
self.zmqSubSocket.connect(ip_address)
44+
extra_args = [['-zmqpubhashtx=%s' % ip_address, '-zmqpubhashblock=%s' % ip_address], []]
45+
self.nodes = self.start_nodes(self.num_nodes, self.options.tmpdir, extra_args)
4846

4947
def run_test(self):
50-
self.sync_all()
48+
try:
49+
self._zmq_test()
50+
finally:
51+
# Destroy the zmq context
52+
self.log.debug("Destroying zmq context")
53+
self.zmqContext.destroy(linger=None)
5154

55+
def _zmq_test(self):
5256
genhashes = self.nodes[0].generate(1)
5357
self.sync_all()
5458

55-
self.log.info("listen...")
59+
self.log.info("Wait for tx")
5660
msg = self.zmqSubSocket.recv_multipart()
5761
topic = msg[0]
5862
assert_equal(topic, b"hashtx")
5963
body = msg[1]
6064
msgSequence = struct.unpack('<I', msg[-1])[-1]
61-
assert_equal(msgSequence, 0) #must be sequence 0 on hashtx
65+
assert_equal(msgSequence, 0) # must be sequence 0 on hashtx
6266

67+
self.log.info("Wait for block")
6368
msg = self.zmqSubSocket.recv_multipart()
6469
topic = msg[0]
6570
body = msg[1]
6671
msgSequence = struct.unpack('<I', msg[-1])[-1]
67-
assert_equal(msgSequence, 0) #must be sequence 0 on hashblock
72+
assert_equal(msgSequence, 0) # must be sequence 0 on hashblock
6873
blkhash = bytes_to_hex_str(body)
6974

70-
assert_equal(genhashes[0], blkhash) #blockhash from generate must be equal to the hash received over zmq
75+
assert_equal(genhashes[0], blkhash) # blockhash from generate must be equal to the hash received over zmq
7176

77+
self.log.info("Generate 10 blocks (and 10 coinbase txes)")
7278
n = 10
7379
genhashes = self.nodes[1].generate(n)
7480
self.sync_all()
7581

7682
zmqHashes = []
7783
blockcount = 0
78-
for x in range(0,n*2):
84+
for x in range(n * 2):
7985
msg = self.zmqSubSocket.recv_multipart()
8086
topic = msg[0]
8187
body = msg[1]
8288
if topic == b"hashblock":
8389
zmqHashes.append(bytes_to_hex_str(body))
8490
msgSequence = struct.unpack('<I', msg[-1])[-1]
85-
assert_equal(msgSequence, blockcount+1)
91+
assert_equal(msgSequence, blockcount + 1)
8692
blockcount += 1
8793

88-
for x in range(0,n):
89-
assert_equal(genhashes[x], zmqHashes[x]) #blockhash from generate must be equal to the hash received over zmq
94+
for x in range(n):
95+
assert_equal(genhashes[x], zmqHashes[x]) # blockhash from generate must be equal to the hash received over zmq
9096

91-
#test tx from a second node
97+
self.log.info("Wait for tx from second node")
98+
# test tx from a second node
9299
hashRPC = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1.0)
93100
self.sync_all()
94101

95102
# now we should receive a zmq msg because the tx was broadcast
96103
msg = self.zmqSubSocket.recv_multipart()
97104
topic = msg[0]
98105
body = msg[1]
99-
hashZMQ = ""
100-
if topic == b"hashtx":
101-
hashZMQ = bytes_to_hex_str(body)
102-
msgSequence = struct.unpack('<I', msg[-1])[-1]
103-
assert_equal(msgSequence, blockcount+1)
104-
105-
assert_equal(hashRPC, hashZMQ) #blockhash from generate must be equal to the hash received over zmq
106+
assert_equal(topic, b"hashtx")
107+
hashZMQ = bytes_to_hex_str(body)
108+
msgSequence = struct.unpack('<I', msg[-1])[-1]
109+
assert_equal(msgSequence, blockcount + 1)
106110

111+
assert_equal(hashRPC, hashZMQ) # txid from sendtoaddress must be equal to the hash received over zmq
107112

108113
if __name__ == '__main__':
109-
ZMQTest ().main ()
114+
ZMQTest().main()

0 commit comments

Comments
 (0)