[tests] disconnect_ban: use wait_until instead of sleep

pull/10143/head
John Newbery 8 years ago
parent 2077fdacd3
commit 12de2f252c

@ -3,9 +3,9 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test node disconnect and ban behavior""" """Test node disconnect and ban behavior"""
import time
import urllib.parse import urllib.parse
from test_framework.mininode import wait_until
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (assert_equal, from test_framework.util import (assert_equal,
assert_raises_jsonrpc, assert_raises_jsonrpc,
@ -32,7 +32,7 @@ class DisconnectBanTest(BitcoinTestFramework):
self.log.info("setban: successfully ban single IP address") self.log.info("setban: successfully ban single IP address")
assert_equal(len(self.nodes[1].getpeerinfo()), 2) # node1 should have 2 connections to node0 at this point assert_equal(len(self.nodes[1].getpeerinfo()), 2) # node1 should have 2 connections to node0 at this point
self.nodes[1].setban("127.0.0.1", "add") self.nodes[1].setban("127.0.0.1", "add")
time.sleep(3) # wait till the nodes are disconected wait_until(lambda: len(self.nodes[1].getpeerinfo()) == 0)
assert_equal(len(self.nodes[1].getpeerinfo()), 0) # all nodes must be disconnected at this point assert_equal(len(self.nodes[1].getpeerinfo()), 0) # all nodes must be disconnected at this point
assert_equal(len(self.nodes[1].listbanned()), 1) assert_equal(len(self.nodes[1].listbanned()), 1)
@ -65,10 +65,9 @@ class DisconnectBanTest(BitcoinTestFramework):
self.nodes[1].setban("192.168.0.1", "add", 1) # ban for 1 seconds self.nodes[1].setban("192.168.0.1", "add", 1) # ban for 1 seconds
self.nodes[1].setban("2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/19", "add", 1000) # ban for 1000 seconds self.nodes[1].setban("2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/19", "add", 1000) # ban for 1000 seconds
listBeforeShutdown = self.nodes[1].listbanned() listBeforeShutdown = self.nodes[1].listbanned()
assert_equal("192.168.0.1/32", listBeforeShutdown[2]['address']) # must be here assert_equal("192.168.0.1/32", listBeforeShutdown[2]['address'])
time.sleep(2) # make 100% sure we expired 192.168.0.1 node time wait_until(lambda: len(self.nodes[1].listbanned()) == 3)
# stop node
stop_node(self.nodes[1], 1) stop_node(self.nodes[1], 1)
self.nodes[1] = start_node(1, self.options.tmpdir) self.nodes[1] = start_node(1, self.options.tmpdir)
@ -86,7 +85,7 @@ class DisconnectBanTest(BitcoinTestFramework):
self.log.info("disconnectnode: successfully disconnect node") self.log.info("disconnectnode: successfully disconnect node")
url = urllib.parse.urlparse(self.nodes[1].url) url = urllib.parse.urlparse(self.nodes[1].url)
self.nodes[0].disconnectnode(url.hostname + ":" + str(p2p_port(1))) self.nodes[0].disconnectnode(url.hostname + ":" + str(p2p_port(1)))
time.sleep(2) # disconnecting a node needs a little bit of time wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1)
for node in self.nodes[0].getpeerinfo(): for node in self.nodes[0].getpeerinfo():
assert(node['addr'] != url.hostname + ":" + str(p2p_port(1))) assert(node['addr'] != url.hostname + ":" + str(p2p_port(1)))

Loading…
Cancel
Save