Skip to content

Commit 5fc6e71

Browse files
committed
[tests] Add network_thread_ utility functions.
Add network thread_start(), network_thread_running() and network_thread_join() utility functions in mininode.py and use network_thread_running() in network thread assertions.
1 parent f60b4ad commit 5fc6e71

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

test/functional/test_framework/mininode.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import socket
1919
import struct
2020
import sys
21-
from threading import RLock, Thread
21+
import threading
2222

2323
from test_framework.messages import *
2424
from test_framework.util import wait_until
@@ -397,9 +397,12 @@ def sync_with_ping(self, timeout=60):
397397
# and whenever adding anything to the send buffer (in send_message()). This
398398
# lock should be acquired in the thread running the test logic to synchronize
399399
# 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")
401405

402-
class NetworkThread(Thread):
403406
def run(self):
404407
while mininode_socket_map:
405408
# We check for whether to disconnect outside of the asyncore
@@ -412,3 +415,21 @@ def run(self):
412415
[obj.handle_close() for obj in disconnected]
413416
asyncore.loop(0.1, use_poll=True, map=mininode_socket_map, count=1)
414417
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

Comments
 (0)