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