18
18
import socket
19
19
import struct
20
20
import sys
21
- from threading import RLock , Thread
21
+ import threading
22
22
23
23
from test_framework .messages import *
24
24
from test_framework .util import wait_until
@@ -397,9 +397,12 @@ def sync_with_ping(self, timeout=60):
397
397
# and whenever adding anything to the send buffer (in send_message()). This
398
398
# lock should be acquired in the thread running the test logic to synchronize
399
399
# access to any data shared with the P2PInterface or P2PConnection.
400
- mininode_lock = RLock ()
400
+ mininode_lock = threading .RLock ()
401
+
402
+ class NetworkThread (threading .Thread ):
403
+ def __init__ (self ):
404
+ super ().__init__ (name = "NetworkThread" )
401
405
402
- class NetworkThread (Thread ):
403
406
def run (self ):
404
407
while mininode_socket_map :
405
408
# We check for whether to disconnect outside of the asyncore
@@ -412,3 +415,21 @@ def run(self):
412
415
[obj .handle_close () for obj in disconnected ]
413
416
asyncore .loop (0.1 , use_poll = True , map = mininode_socket_map , count = 1 )
414
417
logger .debug ("Network thread closing" )
418
+
419
+ def network_thread_start ():
420
+ """Start the network thread."""
421
+ NetworkThread ().start ()
422
+
423
+ def network_thread_running ():
424
+ """Return whether the network thread is running."""
425
+ return any ([thread .name == "NetworkThread" for thread in threading .enumerate ()])
426
+
427
+ def network_thread_join (timeout = 10 ):
428
+ """Wait timeout seconds for the network thread to terminate.
429
+
430
+ Throw if the network thread doesn't terminate in timeout seconds."""
431
+ network_threads = [thread for thread in threading .enumerate () if thread .name == "NetworkThread" ]
432
+ assert len (network_threads ) <= 1
433
+ for thread in network_threads :
434
+ thread .join (timeout )
435
+ assert not thread .is_alive ()
0 commit comments