Fix bug where message scope would be replaced with default scope

This commit is contained in:
Kristóf Tóth 2019-08-28 14:56:59 +02:00
parent 13e247b009
commit 0295baad47
2 changed files with 67 additions and 4 deletions

View File

@ -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('')

View File

@ -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))