def test_delivery_not_acked_leads_to_delivery_error(self):
        """
        Verify that if a delivery confirmation is NOT an ack, a delivery error
        is propagated to the delivery notify callback.
        """
        # Prep
        got_error = False

        def on_confirm(error):
            nonlocal got_error
            got_error = True
            if isinstance(error, DeliveryError):
                self.assertEqual("123", error.publish_key)

        self.producer.activate_confirm_mode(on_confirm)
        self.producer._unacked_publishes[1] = "123"

        # Test
        frame = Mock()
        frame.method = Basic.Nack()
        frame.method.delivery_tag = 1
        self.producer.on_delivery_confirmed(frame)

        # Assert
        self.assertTrue(got_error)
示例#2
0
    def configure(self):
        self.consumer_tag = 'R2'
        self.delivery_tag = '1138'
        self.exchange = 'droids'
        self.redelivered = True
        self.routing_key = 'dejarik'

        self.app_id = '3PO'
        self.content_encoding = 'GZIP'
        self.content_type = 'application/JSON'
        self.correlation_id = 'dockingbay94'
        self.expiration = '60000'  # 1 minute TTL
        self.headers = {'tags': ['best practices']}
        self.message_id = '1234'
        self.priority = 9
        self.reply_to = 'reply_to_address'
        self.timestamp = '1234567890'
        self.type = 'message_type_name'
        self.user_id = 'amqp_user_id'

        self.channel = pika.channel.Channel(pika.connection.Connection(),
                                            channel_number=1)
        self.method = Basic.Deliver(**dict([(x, getattr(self, x))
                                            for x in self.method_delegates]))
        self.body = '{"strategy": "Let the Wookie win."}'
        self.header = BasicProperties(
            **dict([(x, getattr(self, x)) for x in self.properties_delegates]))
示例#3
0
    def test_execute_rabbit_is_not_called_when_exception_raised(self):
        empty_body_as_bytes = b'{}'
        self.worker.event_handler = Mock()

        # When
        self.worker.on_message(None, Basic.GetOk(), None, empty_body_as_bytes)

        # Then
        assert not self.worker.event_handler.execute_rabbit.called
示例#4
0
    def test_raise_when_error_occurs_in_decode_message_content(self):
        empty_body_as_bytes = b'{}'
        self.worker.event_handler = Mock()

        # When
        self.worker.on_message(None, Basic.GetOk(), None, empty_body_as_bytes)

        # Then
        assert not self.worker.event_handler.execute_rabbit.called
示例#5
0
    def test_envelope(self):
        prop = BasicProperties(content_type='application/json',
                               content_encoding='utf-8',
                               headers=None,
                               delivery_mode=2,
                               priority=7,
                               correlation_id='some-correlation-id',
                               reply_to='reply_to_name',
                               expiration='some-expiration',
                               message_id='some-message-id',
                               timestamp='123456789',
                               type='message-type-a',
                               user_id='user-id',
                               app_id='app-id',
                               cluster_id='cluster-id')
        payload = 'some data'
        delivery_info = Basic.Deliver(consumer_tag=34,
                                      delivery_tag=132,
                                      redelivered=True,
                                      exchange='exchange1',
                                      routing_key='routing_key1')
        envelope = Envelope(prop, payload, delivery_info)

        self.assertEqual(envelope.payload, payload)
        self.assertEqual(envelope.properties, prop)
        self.assertEqual(envelope.delivery_info, delivery_info)
        self.assertEqual(envelope.consumer_tag, delivery_info.consumer_tag)
        self.assertEqual(envelope.delivery_tag, delivery_info.delivery_tag)
        self.assertEqual(envelope.redelivered, delivery_info.redelivered)
        self.assertEqual(envelope.routing_key, delivery_info.routing_key)
        self.assertEqual(envelope.exchange, delivery_info.exchange)
        self.assertEqual(envelope.content_type, prop.content_type)
        self.assertEqual(envelope.content_encoding, prop.content_encoding)
        self.assertEqual(envelope.headers, prop.headers)
        self.assertEqual(envelope.delivery_mode, prop.delivery_mode)
        self.assertEqual(envelope.priority, prop.priority)
        self.assertEqual(envelope.correlation_id, prop.correlation_id)
        self.assertEqual(envelope.reply_to, prop.reply_to)
        self.assertEqual(envelope.expiration, prop.expiration)
        self.assertEqual(envelope.message_id, prop.message_id)
        self.assertEqual(envelope.timestamp, prop.timestamp)
        self.assertEqual(envelope.type, prop.type)
        self.assertEqual(envelope.user_id, prop.user_id)
        self.assertEqual(envelope.app_id, prop.app_id)
        self.assertEqual(envelope.cluster_id, prop.cluster_id)
        self.assertEqual(envelope.headers, prop.headers)
        self.assertIsNone(envelope.get_header('some_header'))
        envelope.set_header('some_header', 50)
        envelope.set_header('another_header', 'hello')
        self.assertEqual(envelope.headers,
                         dict(some_header=50, another_header='hello'))
        self.assertEqual(envelope.get_header('some_header'), 50)
        self.assertEqual(envelope.get_header('another_header'), 'hello')
        self.assertIsNone(envelope.get_header('non_exist_header'))
    def test_confirm_mode_publish_key_returned_by_publish(self):
        """
        Verify that a publish key is generated and returned by publish if
        confirm mode is activated.
        """
        # Prep
        publish_key = None

        def on_confirm(delivery_notification):
            if delivery_notification == publish_key:
                return
            elif isinstance(delivery_notification, ConfirmModeOK):
                return

            self.fail("delivery notification did not match the generated "
                      "publish key")

        self.producer.activate_confirm_mode(on_confirm)
        self.producer.confirm_delivery.assert_called_with(
            self.producer.on_delivery_confirmed,
            callback=self.producer.on_confirm_select_ok)

        self.producer.on_confirm_select_ok(None)
        exchange_params = ExchangeParams("exchange")

        # Test
        publish_key = self.producer.publish(b"body",
                                            exchange_params=exchange_params)

        self.producer.declare_exchange.assert_called()
        self.producer.on_exchange_declared(b"body",
                                           exchange_params,
                                           None,
                                           publish_key=publish_key)
        self.producer.basic_publish.assert_called_with(
            b"body",
            exchange=exchange_params.exchange,
            routing_key="",
            publish_params=ANY)
        # 1 is the delivery tag
        self.assertEqual(self.producer._unacked_publishes[1], publish_key)

        frame = Mock()
        ack = Basic.Ack()
        ack.delivery_tag = 1
        frame.method = ack
        self.producer.on_delivery_confirmed(frame)
        self.assertEqual(len(self.producer._unacked_publishes.keys()), 0)
示例#7
0
def create_envelope(payload='some payload'):
    prop = BasicProperties(content_type='application/json',
                           content_encoding='utf-8',
                           headers=None,
                           delivery_mode=2,
                           priority=7,
                           correlation_id='some-correlation-id',
                           reply_to='reply_to_name',
                           expiration='some-expiration',
                           message_id='some-message-id',
                           timestamp='123456789',
                           type='message-type-a',
                           user_id='user-id',
                           app_id='app-id',
                           cluster_id='cluster-id')
    payload = payload or 'some data'
    delivery_info = Basic.Deliver(consumer_tag=34,
                                  delivery_tag=132,
                                  redelivered=True,
                                  exchange='exchange1',
                                  routing_key='routing_key1')
    envelope = Envelope(prop, payload, delivery_info)
    return envelope