Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/mininode.py
Show First 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | class P2PConnection(asyncore.dispatcher): | ||||
- reading bytes from and writing bytes to the socket | - reading bytes from and writing bytes to the socket | ||||
- deserializing and serializing the P2P message header | - deserializing and serializing the P2P message header | ||||
- logging messages as they are sent and received | - logging messages as they are sent and received | ||||
This class contains no logic for handing the P2P message payloads. It must be | This class contains no logic for handing the P2P message payloads. It must be | ||||
sub-classed and the on_message() callback overridden.""" | sub-classed and the on_message() callback overridden.""" | ||||
def __init__(self): | def __init__(self): | ||||
# All P2PConnections must be created before starting the NetworkThread. | |||||
# assert that the network thread is not running. | |||||
assert not network_thread_running() | |||||
super().__init__(map=mininode_socket_map) | super().__init__(map=mininode_socket_map) | ||||
def peer_connect(self, dstaddr, dstport, net="regtest"): | def peer_connect(self, dstaddr, dstport, net="regtest"): | ||||
self.dstaddr = dstaddr | self.dstaddr = dstaddr | ||||
self.dstport = dstport | self.dstport = dstport | ||||
self.create_socket(socket.AF_INET, socket.SOCK_STREAM) | self.create_socket(socket.AF_INET, socket.SOCK_STREAM) | ||||
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) | self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) | ||||
self.sendbuf = b"" | self.sendbuf = b"" | ||||
▲ Show 20 Lines • Show All 368 Lines • ▼ Show 20 Lines | def run(self): | ||||
disconnected = [] | disconnected = [] | ||||
for fd, obj in mininode_socket_map.items(): | for fd, obj in mininode_socket_map.items(): | ||||
if obj.disconnect: | if obj.disconnect: | ||||
disconnected.append(obj) | disconnected.append(obj) | ||||
[obj.handle_close() for obj in disconnected] | [obj.handle_close() for obj in disconnected] | ||||
asyncore.loop(0.1, use_poll=True, map=mininode_socket_map, count=1) | asyncore.loop(0.1, use_poll=True, map=mininode_socket_map, count=1) | ||||
logger.debug("Network thread closing") | logger.debug("Network thread closing") | ||||
def network_thread_start(): | def network_thread_start(): | ||||
"""Start the network thread.""" | """Start the network thread.""" | ||||
# Only one network thread may run at a time | |||||
assert not network_thread_running() | |||||
NetworkThread().start() | NetworkThread().start() | ||||
def network_thread_running(): | def network_thread_running(): | ||||
"""Return whether the network thread is running.""" | """Return whether the network thread is running.""" | ||||
return any([thread.name == "NetworkThread" for thread in threading.enumerate()]) | return any([thread.name == "NetworkThread" for thread in threading.enumerate()]) | ||||
def network_thread_join(timeout=10): | def network_thread_join(timeout=10): | ||||
"""Wait timeout seconds for the network thread to terminate. | """Wait timeout seconds for the network thread to terminate. | ||||
Throw if the network thread doesn't terminate in timeout seconds.""" | Throw if the network thread doesn't terminate in timeout seconds.""" | ||||
network_threads = [thread for thread in threading.enumerate() if thread.name == "NetworkThread"] | network_threads = [ | ||||
thread for thread in threading.enumerate() if thread.name == "NetworkThread"] | |||||
jasonbcox: Pretty sure this change is already in the previous diff. That shouldn't affect your merging… | |||||
assert len(network_threads) <= 1 | assert len(network_threads) <= 1 | ||||
for thread in network_threads: | for thread in network_threads: | ||||
thread.join(timeout) | thread.join(timeout) | ||||
assert not thread.is_alive() | assert not thread.is_alive() |
Pretty sure this change is already in the previous diff. That shouldn't affect your merging, so this should be ok.