Beispiel #1
0
    def test_receive_two_messages_from_other_node(self):
        handler_mock = mock.MagicMock()

        connection = session.ClientConnection(handler_mock)
        self.run_greenlet(self.topic.add_subscriber, "7", connection)

        event_msg = EventMessage(subscription_id="1",
                                 publication_id="1",
                                 kwargs={"type": "test"})
        msg = BroadcastMessage("test", event_msg, 1)
        msg.publisher_node_id = uuid.uuid4().hex
        node2_topic = Topic(name="test",
                            redis={
                                "host": "127.0.0.1",
                                "port": 6379
                            })
        self.run_greenlet(node2_topic.publish, msg)

        event_msg.kwargs["type"] = "test2"
        self.run_greenlet(node2_topic.publish, msg)

        # wait for all futures to execute
        self.wait_for(self.topic._publisher_connection.call("GET", "a"))
        self.io_loop.clear_instance()

        event_msg.subscription_id = "7"
        expected_calls = [
            mock.call('[36, "7", "1", {}, [], {"type": "test"}]'),
            mock.call('[36, "7", "1", {}, [], {"type": "test2"}]')
        ]
        self.assertEqual(handler_mock.write_message.mock_calls, expected_calls)
Beispiel #2
0
    def test_receive_two_messages_from_other_node(self):
        handler_mock = mock.MagicMock()

        connection = session.ClientConnection(handler_mock)
        self.run_greenlet(self.topic.add_subscriber, "7", connection)

        event_msg = EventMessage(subscription_id="1", publication_id="1", kwargs={"type": "test"})
        msg = BroadcastMessage("test", event_msg, 1)
        msg.publisher_node_id = uuid.uuid4().hex
        node2_topic = Topic(name="test", redis={"host": "127.0.0.1", "port": 6379})
        self.run_greenlet(node2_topic.publish, msg)

        event_msg.kwargs["type"] = "test2"
        self.run_greenlet(node2_topic.publish, msg)

        # wait for all futures to execute
        self.wait_for(self.topic._publisher_connection.call("GET", "a"))
        self.io_loop.clear_instance()

        event_msg.subscription_id = "7"
        expected_calls = [
            mock.call('[36, "7", "1", {}, [], {"type": "test"}]'),
            mock.call('[36, "7", "1", {}, [], {"type": "test2"}]')
        ]
        self.assertEqual(handler_mock.write_message.mock_calls, expected_calls)
Beispiel #3
0
    def test_add_subscriber(self):
        handler_mock = mock.MagicMock()

        connection = session.ClientConnection(handler_mock)

        sub_id = self.run_greenlet(tornwamp_topic.topics.add_subscriber,
                                   "test", connection)
        self.assertIsNotNone(sub_id)

        # We use a dummy connection id as we are not testing local delivery
        msg = BroadcastMessage(
            "test",
            EventMessage(publication_id="1", kwargs={"type": "test"}),
            connection.id,
        )
        client = PubSubClient(autoconnect=True, ioloop=IOLoop.current())

        self.wait_for(client.pubsub_subscribe("test"))
        ret = self.run_greenlet(tornwamp_topic.topics["test"].publish, msg)
        self.assertTrue(ret)
        type_, topic, received_msg = self.wait_for(client.pubsub_pop_message())
        self.assertEqual(type_.decode("utf-8"), u"message")
        self.assertEqual(topic.decode("utf-8"), u"test")
        received_msg = BroadcastMessage.from_text(received_msg.decode("utf-8"))
        self.assertEqual(received_msg.json, msg.json)
Beispiel #4
0
    def test_receive_message_from_same_node(self):
        handler_mock = mock.MagicMock()

        connection = session.ClientConnection(handler_mock)
        self.run_greenlet(self.topic.add_subscriber, "7", connection)

        event_msg = EventMessage(subscription_id="1", publication_id="1", kwargs={"type": "test"})
        msg = BroadcastMessage("test", event_msg, 1)
        self.run_greenlet(self.topic.publish, msg)

        # wait for all futures to execute
        self.wait_for(self.topic._publisher_connection.call("GET", "a"))
        self.io_loop.clear_instance()

        event_msg.subscription_id = "7"
        handler_mock.write_message.assert_called_once_with(event_msg.json)
Beispiel #5
0
def get_publish_direct_messages(publish_message, publication_id,
                                publisher_connection):
    """
    Return a list of dictionaries containing websocket and what message they
    should receive. This is called from PublishProcessor when it succeeds.

    Sample response:
    [
        {
            "websocket": <tornwamp.WAMPHandler>,
            "message": <tornwamp.messages.Event>
        }
    """
    data = []
    topic_name = publish_message.topic
    topic = tornwamp_topic.topics.get(topic_name)
    if topic:
        for subscription_id, connection in topic.subscribers.items():
            if connection != publisher_connection:
                event_message = EventMessage(
                    subscription_id=subscription_id,
                    publication_id=publication_id,
                    args=publish_message.args,
                    kwargs=publish_message.kwargs,
                )
                item = {
                    "websocket": connection._websocket,
                    "message": event_message
                }
                data.append(item)
    return data
Beispiel #6
0
    def test_receive_message_from_same_node(self):
        handler_mock = mock.MagicMock()

        connection = session.ClientConnection(handler_mock)
        self.run_greenlet(self.topic.add_subscriber, "7", connection)

        event_msg = EventMessage(subscription_id="1",
                                 publication_id="1",
                                 kwargs={"type": "test"})
        msg = BroadcastMessage("test", event_msg, 1)
        self.run_greenlet(self.topic.publish, msg)

        # wait for all futures to execute
        self.wait_for(self.topic._publisher_connection.call("GET", "a"))
        self.io_loop.clear_instance()

        event_msg.subscription_id = "7"
        handler_mock.write_message.assert_called_once_with(event_msg.json)
Beispiel #7
0
 def test_publish_redis_fails(self):
     with self.assertRaises(RedisUnavailableError):
         with mock.patch.object(self.topic._publisher_connection,
                                "is_connected",
                                return_value=False):
             msg = BroadcastMessage(
                 "test",
                 EventMessage(subscription_id="1", publication_id="1"), 1)
             # We use a dummy connection id as we are not testing local delivery
             self.run_greenlet(self.topic.publish, msg)
Beispiel #8
0
 def test_redis_fails_on_publish(self):
     event_msg = EventMessage(subscription_id="1",
                              publication_id="1",
                              kwargs={"type": "test"})
     msg = BroadcastMessage("test", event_msg, 1)
     with self.assertRaises(RedisUnavailableError):
         with mock.patch.object(self.topic._publisher_connection,
                                "is_connected",
                                return_value=False):
             self.run_greenlet(self.topic.publish, msg)
Beispiel #9
0
    def test_pop_message_timeout(self):
        handler_mock = mock.MagicMock()

        connection = session.ClientConnection(handler_mock)
        self.run_greenlet(self.topic.add_subscriber, "7", connection)

        self.io_loop.call_later(1.5, self.stop)
        self.wait()

        event_msg = EventMessage(subscription_id="1",
                                 publication_id="1",
                                 kwargs={"type": "test"})
        msg = BroadcastMessage("test", event_msg, 1)
        msg.publisher_node_id = uuid.uuid4().hex
        self.wait_for(
            self.topic._publisher_connection.call("PUBLISH", self.topic.name,
                                                  msg.json))

        event_msg.subscription_id = "7"
        handler_mock.write_message.assert_called_once_with(event_msg.json)
Beispiel #10
0
    def test_pop_message_timeout(self):
        handler_mock = mock.MagicMock()

        connection = session.ClientConnection(handler_mock)
        self.run_greenlet(self.topic.add_subscriber, "7", connection)

        self.io_loop.call_later(1.5, self.stop)
        self.wait()

        event_msg = EventMessage(subscription_id="1", publication_id="1", kwargs={"type": "test"})
        msg = BroadcastMessage("test", event_msg, 1)
        msg.publisher_node_id = uuid.uuid4().hex
        self.wait_for(self.topic._publisher_connection.call("PUBLISH", self.topic.name, msg.json))

        # force ioloop to run
        self.io_loop.call_later(0.2, self.stop)
        self.wait()

        event_msg.subscription_id = "7"
        handler_mock.write_message.assert_called_once_with(event_msg.json)
Beispiel #11
0
    def test_delivery_callback(self):
        msgs = []

        topic = Topic("another.topic")

        def f(t, msg, publisher_connection_id):
            self.assertEqual(publisher_connection_id, 1)
            self.assertEqual(t, topic)
            msgs.append(msg.json)

        tornwamp_topic.customize.deliver_event_messages = f

        msg = BroadcastMessage("another.topic", EventMessage(subscription_id=1, publication_id=1), 1)

        topic.publish(msg)
        self.assertEqual(msgs, [msg.event_message.json])
Beispiel #12
0
    def test_publish_message(self):
        # We use a dummy connection id as we are not testing local delivery
        msg = BroadcastMessage(
            "test",
            EventMessage(subscription_id="1",
                         publication_id="1",
                         kwargs={"type": "test"}),
            1,
        )
        client = PubSubClient(autoconnect=True, ioloop=IOLoop.current())

        self.wait_for(client.pubsub_subscribe("test"))
        ret = self.run_greenlet(self.topic.publish, msg)
        self.assertTrue(ret)
        type_, topic, received_msg = self.wait_for(client.pubsub_pop_message())
        self.assertEqual(type_.decode("utf-8"), u"message")
        self.assertEqual(topic.decode("utf-8"), u"test")
        received_msg = BroadcastMessage.from_text(received_msg.decode("utf-8"))
        self.assertEqual(received_msg.json, msg.json)
Beispiel #13
0
 def test_skip_publisher(self):
     with patch.object(self.subscriber_connection, "_websocket") as ws:
         tornwamp_topic.customize.deliver_event_messages(self.topic, EventMessage(subscription_id=1837, publication_id=1), self.subscriber_connection.id)
         self.assertFalse(ws.write_message.called)
Beispiel #14
0
 def test_deliver_event_messages_empty_topic(self):
     connection = ClientConnection(None, user_id=7475)
     with patch.object(self.subscriber_connection, "_websocket") as ws:
         tornwamp_topic.customize.deliver_event_messages(Topic("education.second"), EventMessage(subscription_id=1, publication_id=1), connection.id)
         self.assertFalse(ws.write_message.called)
Beispiel #15
0
 def test_deliver_event_messages_by_publishing(self):
     connection = ClientConnection(None, user_id=7475)
     with patch.object(self.subscriber_connection, "_websocket") as ws:
         msg = BroadcastMessage("education.first", EventMessage(subscription_id=1, publication_id=1), connection.id)
         tornwamp_topic.topics.get("education.first").publish(msg)
         ws.write_message.assert_called_once_with(EventMessage(subscription_id=18273, publication_id=1).json)
Beispiel #16
0
 def test_deliver_event_messages_none_publisher_connection_id(self):
     connection = ClientConnection(None, user_id=7475)
     with patch.object(self.subscriber_connection, "_websocket") as ws:
         tornwamp_topic.customize.deliver_event_messages(self.topic, EventMessage(subscription_id=1, publication_id=1))
         ws.write_message.assert_called_once_with(EventMessage(subscription_id=18273, publication_id=1).json)