Example #1
0
    def test_not_equal_messages(self):
        msg1 = AMQPMessage(
            connection=Mock(),
            channel=Mock(),
            queue_name=Mock(),
            serialized_data=Mock(),
            delivery_tag=Mock(),
            envelope=Mock(),
            properties=Mock(),
            deserialization_method=Mock(),
            queue=Mock(),
        )

        msg2 = AMQPMessage(
            connection=msg1.connection,
            channel=Mock(),
            queue_name=msg1.queue_name,
            serialized_data=msg1.serialized_data,
            delivery_tag=msg1.delivery_tag,
            envelope=msg1._envelope,
            properties=msg1._properties,
            deserialization_method=msg1._deserialization_method,
            queue=Mock(),
        )
        self.assertNotEqual(msg1, msg2)
Example #2
0
    async def test_it_calls_on_queue_message_with_the_message_body_wrapped_as_a_AMQPMessage_instance(
        self
    ):
        content = {
            "artist": "Caetano Veloso",
            "song": "Não enche",
            "album": "Livro",
        }
        body = json.dumps(content).encode("utf-8")
        with patch.object(
            self.handler, "_handle_callback", CoroutineMock()
        ) as _handle_callback:
            await self.handler.handle_message(
                channel=self.queue.connection.channel,
                body=body,
                envelope=self.envelope,
                properties=self.properties,
            )

            _handle_callback.assert_called_once_with(
                self.handler.delegate.on_queue_message,
                msg=AMQPMessage(
                    connection=self.queue.connection,
                    channel=self.queue.connection.channel,
                    queue=self.queue,
                    envelope=self.envelope,
                    properties=self.properties,
                    delivery_tag=self.envelope.delivery_tag,
                    deserialization_method=self.queue.deserialize,
                    queue_name=self.test_queue_name,
                    serialized_data=body,
                ),
            )
Example #3
0
    async def test_it_acks_messages(self):
        msg = AMQPMessage(
            connection=Mock(),
            channel=Mock(basic_client_ack=CoroutineMock()),
            queue_name=Mock(),
            serialized_data=Mock(),
            delivery_tag=Mock(),
            envelope=Mock(),
            properties=Mock(),
            deserialization_method=Mock(),
            queue=Mock(),
        )
        await msg.ack()

        msg.channel.basic_client_ack.assert_awaited_once_with(msg.delivery_tag)
Example #4
0
    def test_successful_deserialization(self):
        data = b'["Xablau"]'
        deserializer = Mock(return_value=["Xablau"])

        msg = AMQPMessage(
            connection=Mock(),
            channel=Mock(),
            queue_name=Mock(),
            serialized_data=data,
            delivery_tag=Mock(),
            envelope=Mock(),
            properties=Mock(),
            deserialization_method=deserializer,
            queue=Mock(),
        )

        self.assertEqual(msg.deserialized_data, ["Xablau"])
Example #5
0
    async def test_it_rejects_messages_with_requeue(self):
        msg = AMQPMessage(
            connection=Mock(),
            channel=Mock(basic_reject=CoroutineMock()),
            queue_name=Mock(),
            serialized_data=Mock(),
            delivery_tag=Mock(),
            envelope=Mock(),
            properties=Mock(),
            deserialization_method=Mock(),
            queue=Mock(),
        )

        await msg.reject(requeue=True)
        msg.channel.basic_reject.assert_awaited_once_with(
            delivery_tag=msg.delivery_tag, requeue=True
        )
Example #6
0
 def test_body_property(self):
     amqp_message = AMQPMessage(
         connection=Mock(),
         channel=Mock(),
         queue_name=Mock(),
         serialized_data='["Xablau", "Xena"]',
         delivery_tag=Mock(),
         envelope=Mock(),
         properties=Mock(),
         deserialization_method=json.loads,
         queue=Mock(),
     )
     message = RabbitMQMessage(
         delivery_tag=10,
         on_exception=Actions.REQUEUE,
         amqp_message=amqp_message,
     )
     self.assertEqual(message.body, ["Xablau", "Xena"])
Example #7
0
 def test_serialized_data_property(self):
     amqp_message = AMQPMessage(
         connection=Mock(),
         channel=Mock(),
         queue_name=Mock(),
         serialized_data=Mock(),
         delivery_tag=Mock(),
         envelope=Mock(),
         properties=Mock(),
         deserialization_method=Mock(),
         queue=Mock(),
     )
     message = RabbitMQMessage(
         delivery_tag=10,
         on_exception=Actions.REQUEUE,
         amqp_message=amqp_message,
     )
     self.assertEqual(message.serialized_data, amqp_message.serialized_data)
Example #8
0
    def test_deserialization_is_only_called_once(self):
        data = b'["Xablau"]'
        deserializer = Mock(return_value=["Xablau"])

        msg = AMQPMessage(
            queue=Mock(),
            connection=Mock(),
            channel=Mock(),
            queue_name=Mock(),
            serialized_data=data,
            delivery_tag=Mock(),
            envelope=Mock(),
            properties=Mock(),
            deserialization_method=deserializer,
        )

        _ = [msg.deserialized_data for _ in range(10)]

        deserializer.assert_called_once_with(data)
Example #9
0
    def test_lazy_deserialization_raises_an_error_if_deserialization_fails(
        self
    ):
        data = b"Xablau"
        deserializer = Mock(side_effect=ValueError)

        msg = AMQPMessage(
            connection=Mock(),
            channel=Mock(),
            queue_name=Mock(),
            serialized_data=data,
            delivery_tag=Mock(),
            envelope=Mock(),
            properties=Mock(),
            deserialization_method=deserializer,
            queue=Mock(),
        )

        with self.assertRaises(UndecodableMessageException):
            _ = msg.deserialized_data

        deserializer.assert_called_once_with(data)
Example #10
0
    async def handle_message(
        self,
        channel: Channel,
        body: bytes,
        envelope: Envelope,
        properties: Properties,
    ) -> Task:
        msg = AMQPMessage(
            connection=self.queue.connection,
            channel=channel,
            queue=self.queue,
            envelope=envelope,
            properties=properties,
            delivery_tag=envelope.delivery_tag,
            deserialization_method=self.queue.deserialize,
            queue_name=self.queue_name,
            serialized_data=body,
        )

        callback = self._handle_callback(
            self.delegate.on_queue_message, msg=msg  # type: ignore
        )
        return self.loop.create_task(callback)