diff --git a/tfw/internals/networking/test_networking.py b/tfw/internals/networking/test_networking.py index d1d4261..deb86c5 100644 --- a/tfw/internals/networking/test_networking.py +++ b/tfw/internals/networking/test_networking.py @@ -8,7 +8,7 @@ from contextlib import suppress import pytest from tornado.ioloop import IOLoop -from tfw.internals.networking import ZMQListener, ZMQConnector +from tfw.internals.networking import ZMQListener, ZMQConnector, Scope, Intent @pytest.fixture @@ -96,6 +96,67 @@ def test_server_downlink(zmq_listener, zmq_connector, test_messages): assert messages == test_messages +def test_connector_default_scope_is_zmq(zmq_listener, zmq_connector): + messages = [] + zmq_listener.register_callback(messages.append) + + zmq_connector.send_message({'key': 'cica'}) + + run_ioloop_once() + + assert messages[0]['scope'] == Scope.ZMQ.value + + +def test_connector_preserves_scope(zmq_listener, zmq_connector): + messages = [] + zmq_listener.register_callback(messages.append) + + zmq_connector.send_message({'key': 'cica', 'scope': Scope.WEBSOCKET.value}) + + run_ioloop_once() + + assert messages[0]['scope'] == Scope.WEBSOCKET.value + + +def test_connector_scope_overrides_message_scope(zmq_listener, zmq_connector): + messages = [] + zmq_listener.register_callback(messages.append) + + zmq_connector.send_message( + {'key': 'cica', 'scope': Scope.WEBSOCKET.value}, + scope=Scope.ZMQ + ) + + run_ioloop_once() + + assert messages[0]['scope'] == Scope.ZMQ.value + + +def test_connector_adds_intent(zmq_listener, zmq_connector): + messages = [] + zmq_listener.register_callback(messages.append) + + zmq_connector.send_message( + {'key': 'cica'}, + intent=Intent.EVENT + ) + + run_ioloop_once() + + assert messages[0]['intent'] == Intent.EVENT.value + + +def test_connector_preserves_intent(zmq_listener, zmq_connector): + messages = [] + zmq_listener.register_callback(messages.append) + + zmq_connector.send_message({'key': 'cica', 'intent': Intent.EVENT.value}) + + run_ioloop_once() + + assert messages[0]['intent'] == Intent.EVENT.value + + def test_server_uplink(zmq_listener, zmq_connector, test_messages): messages = [] zmq_connector.subscribe('') diff --git a/tfw/internals/networking/zmq_connector.py b/tfw/internals/networking/zmq_connector.py index ff82758..cd1111e 100644 --- a/tfw/internals/networking/zmq_connector.py +++ b/tfw/internals/networking/zmq_connector.py @@ -4,7 +4,6 @@ import zmq from zmq.eventloop.zmqstream import ZMQStream from .scope import Scope -from .intent import Intent from .serialization import ( serialize_tfw_msg, deserialize_tfw_msg, @@ -52,8 +51,11 @@ class ZMQUplinkConnector: self._zmq_push_socket.setsockopt(zmq.SNDHWM, 0) self._zmq_push_socket.connect(connect_addr) - def send_message(self, message, scope=Scope.ZMQ, intent=None): - message['scope'] = scope.value + def send_message(self, message, scope=None, intent=None): + if 'scope' not in message: + message['scope'] = Scope.ZMQ.value + if scope is not None: + message['scope'] = scope.value if intent is not None: message['intent'] = intent.value self._zmq_push_socket.send_multipart(serialize_tfw_msg(message))