From 88bc42c7f27110e7d410531414183d28d51522c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 10 Oct 2019 15:05:04 +0200 Subject: [PATCH] Fix race conditions in networking test suite --- tfw/internals/networking/test_networking.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tfw/internals/networking/test_networking.py b/tfw/internals/networking/test_networking.py index deb86c5..4e67ff4 100644 --- a/tfw/internals/networking/test_networking.py +++ b/tfw/internals/networking/test_networking.py @@ -39,9 +39,12 @@ def zmq_connector(_listener_and_connector): def run_ioloop_once(): - # hack: we have to wait for the messages to get through + # Hack: we have to wait for the messages to get through # the network stack of the OS while the IOLoop is waiting - # for them via select/epoll/kqueue + # for them via select/epoll/kqueue. + # This is an inherent race condition, but solving this + # problem properly would make the test code difficult + # to understand, so we use this half measure. IOLoop.current().call_later(0.1, IOLoop.current().stop) IOLoop.current().start() @@ -67,7 +70,7 @@ def test_messages(): def wait_until_subscriber_connects(listener, connector): # Warning: you are better off without comprehending how this works # Reference: ZMQ PUB-SUB slow joiner problem - + connector.subscribe('-', '_') # Wait until something can go through the connection dummy = {'key': '-'} while True: @@ -82,6 +85,7 @@ def wait_until_subscriber_connects(listener, connector): with suppress(IOError): if connector.recv_message(block=False) == sentinel: break + connector.unsubscribe('-', '_') def test_server_downlink(zmq_listener, zmq_connector, test_messages): @@ -158,8 +162,10 @@ def test_connector_preserves_intent(zmq_listener, zmq_connector): def test_server_uplink(zmq_listener, zmq_connector, test_messages): - messages = [] zmq_connector.subscribe('') + wait_until_subscriber_connects(zmq_listener, zmq_connector) + + messages = [] zmq_connector.register_callback(messages.append) for message in test_messages: @@ -175,8 +181,10 @@ def test_connector_downlink_subscribe(zmq_listener, zmq_connector): key2_messages = [{'key': '2', 'data': i} for i in range(randint(128, 256))] all_messages = key1_messages + key2_messages - messages = [] zmq_connector.subscribe('1') + wait_until_subscriber_connects(zmq_listener, zmq_connector) + + messages = [] zmq_connector.register_callback(messages.append) for message in all_messages: @@ -197,6 +205,7 @@ def test_listener_sync_recv(zmq_listener, zmq_connector, test_messages): def test_connector_sync_recv(zmq_listener, zmq_connector, test_messages): zmq_connector.subscribe('') wait_until_subscriber_connects(zmq_listener, zmq_connector) + for message in test_messages: zmq_listener.send_message(message) assert zmq_connector.recv_message() == message