Example #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)
Example #2
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)
Example #3
0
 def open(self):
     """
     Responsible for authorizing or aborting WebSocket connection.
     It calls 'authorize' method and, based on its response, sends
     a ABORT message to the client.
     """
     authorized, details, error_msg = self.authorize()
     if authorized:
         self.connection = session.ClientConnection(self, **details)
         self.register_connection()
     else:
         abort(self, error_msg, details)
Example #4
0
    def test_pop_message_timeout_drop_subscribers(self):
        handler_mock = mock.MagicMock()

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

        self.topic._subscriber_connection = None

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

        self.assertTrue(handler_mock.close.called)
        self.assertEqual(len(self.topic.subscribers), 0)
Example #5
0
    def test_disconnect_redis_drop_subscribers(self):
        handler_mock = mock.MagicMock()

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

        self.topic._subscriber_connection.disconnect()

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

        self.assertTrue(handler_mock.close.called)
        self.assertEqual(len(self.topic.subscribers), 0)
Example #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)
Example #7
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)
Example #8
0
 def test_redis_fails_to_connect(self):
     connection = session.ClientConnection(mock.MagicMock())
     with self.assertRaises(RedisUnavailableError):
         with mock.patch("socket.socket.connect", side_effect=socket.error):
             self.run_greenlet(self.topic.add_subscriber, "7", connection)
Example #9
0
 def test_redis_fails_on_subscribe(self):
     connection = session.ClientConnection(mock.MagicMock())
     with self.assertRaises(RedisUnavailableError):
         with mock.patch("tornadis.PubSubClient.is_connected",
                         return_value=False):
             self.run_greenlet(self.topic.add_subscriber, "7", connection)