Skip to content

Commit 5a67c05

Browse files
committed
[tests] Fix intermittent rpc_net.py failure.
rpc_net.py would intermittently fail on Travis, probably due to assuming that two consecutive RPC calls were atomic. Fix this by only testing that amounts are bounded above and below rather than equal.
1 parent ac898b6 commit 5a67c05

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

test/functional/rpc_net.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from test_framework.test_framework import BitcoinTestFramework
1111
from test_framework.util import (
1212
assert_equal,
13+
assert_greater_than_or_equal,
1314
assert_raises_rpc_error,
1415
connect_nodes_bi,
1516
p2p_port,
@@ -33,26 +34,34 @@ def _test_connection_count(self):
3334
assert_equal(self.nodes[0].getconnectioncount(), 2)
3435

3536
def _test_getnettotals(self):
36-
# check that getnettotals totalbytesrecv and totalbytessent
37-
# are consistent with getpeerinfo
37+
# getnettotals totalbytesrecv and totalbytessent should be
38+
# consistent with getpeerinfo. Since the RPC calls are not atomic,
39+
# and messages might have been recvd or sent between RPC calls, call
40+
# getnettotals before and after and verify that the returned values
41+
# from getpeerinfo are bounded by those values.
42+
net_totals_before = self.nodes[0].getnettotals()
3843
peer_info = self.nodes[0].getpeerinfo()
44+
net_totals_after = self.nodes[0].getnettotals()
3945
assert_equal(len(peer_info), 2)
40-
net_totals = self.nodes[0].getnettotals()
41-
assert_equal(sum([peer['bytesrecv'] for peer in peer_info]),
42-
net_totals['totalbytesrecv'])
43-
assert_equal(sum([peer['bytessent'] for peer in peer_info]),
44-
net_totals['totalbytessent'])
46+
peers_recv = sum([peer['bytesrecv'] for peer in peer_info])
47+
peers_sent = sum([peer['bytessent'] for peer in peer_info])
48+
49+
assert_greater_than_or_equal(peers_recv, net_totals_before['totalbytesrecv'])
50+
assert_greater_than_or_equal(net_totals_after['totalbytesrecv'], peers_recv)
51+
assert_greater_than_or_equal(peers_sent, net_totals_before['totalbytessent'])
52+
assert_greater_than_or_equal(net_totals_after['totalbytessent'], peers_sent)
53+
4554
# test getnettotals and getpeerinfo by doing a ping
4655
# the bytes sent/received should change
4756
# note ping and pong are 32 bytes each
4857
self.nodes[0].ping()
49-
wait_until(lambda: (net_totals['totalbytessent'] + 32*2) == self.nodes[0].getnettotals()['totalbytessent'], timeout=1)
50-
wait_until(lambda: (net_totals['totalbytesrecv'] + 32*2) == self.nodes[0].getnettotals()['totalbytesrecv'], timeout=1)
58+
wait_until(lambda: (self.nodes[0].getnettotals()['totalbytessent'] >= net_totals_after['totalbytessent'] + 32 * 2), timeout=1)
59+
wait_until(lambda: (self.nodes[0].getnettotals()['totalbytesrecv'] >= net_totals_after['totalbytesrecv'] + 32 * 2), timeout=1)
5160

5261
peer_info_after_ping = self.nodes[0].getpeerinfo()
5362
for before, after in zip(peer_info, peer_info_after_ping):
54-
assert_equal(before['bytesrecv_per_msg']['pong'] + 32, after['bytesrecv_per_msg']['pong'])
55-
assert_equal(before['bytessent_per_msg']['ping'] + 32, after['bytessent_per_msg']['ping'])
63+
assert_greater_than_or_equal(after['bytesrecv_per_msg']['pong'], before['bytesrecv_per_msg']['pong'] + 32)
64+
assert_greater_than_or_equal(after['bytessent_per_msg']['ping'], before['bytessent_per_msg']['ping'] + 32)
5665

5766
def _test_getnetworkinginfo(self):
5867
assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
@@ -78,8 +87,7 @@ def _test_getaddednodeinfo(self):
7887
assert_equal(len(added_nodes), 1)
7988
assert_equal(added_nodes[0]['addednode'], ip_port)
8089
# check that a non-existent node returns an error
81-
assert_raises_rpc_error(-24, "Node has not been added",
82-
self.nodes[0].getaddednodeinfo, '1.1.1.1')
90+
assert_raises_rpc_error(-24, "Node has not been added", self.nodes[0].getaddednodeinfo, '1.1.1.1')
8391

8492
def _test_getpeerinfo(self):
8593
peer_info = [x.getpeerinfo() for x in self.nodes]

0 commit comments

Comments
 (0)