Exemple #1
0
    def testOnMessage_withDynamicTargetUrl(self, mock_request_ctor,
                                           mock_urlopen):
        message_puller = rabbitmq_puller.MessagePuller(self.conn_params,
                                                       'queue',
                                                       'target_url/{queue}')
        method = mock.MagicMock(pika.spec.Basic.Deliver, autospec=True)
        method.delivery_tag = 1234
        properties = pika.spec.BasicProperties(headers={})
        body = json.dumps({
            'name':
            'name',
            'payload':
            base64.b64encode('payload'.encode('ascii')).decode('ascii')
        })

        message_puller.on_message(self.mock_conn, self.mock_channel, method,
                                  properties, body)

        mock_request_ctor.assert_called_with(url='target_url/queue',
                                             headers={
                                                 'X-AppEngine-TaskName':
                                                 'name',
                                                 'X-AppEngine-TaskRetryCount':
                                                 0
                                             },
                                             data='payload'.encode('ascii'))
        mock_urlopen.assert_called_with(
            mock_request_ctor.return_value,
            timeout=rabbitmq_puller.INVOKE_TIMEOUT_SECONDS)
Exemple #2
0
    def testDelayMessage(self):
        message_puller = rabbitmq_puller.MessagePuller(self.conn_params,
                                                       'queue', 'target_url')
        properties = pika.spec.BasicProperties(headers={})
        body = json.dumps({
            'name':
            'name',
            'payload':
            base64.b64encode('payload'.encode('ascii')).decode('ascii')
        })

        message_puller._delay_message(self.mock_channel, properties, body,
                                      1234)

        self.mock_channel.queue_declare.assert_called_with(
            queue='queue.1234000',
            arguments={
                'x-dead-letter-exchange':
                rabbitmq_puller.DEFAULT_EXCHANGE,
                'x-dead-letter-routing-key':
                'queue',
                'x-expires':
                (1234000 + rabbitmq_puller.DELAY_QUEUE_TTL_SECONDS * 1000),
            })
        self.mock_channel.queue_bind.assert_called_with(
            queue='queue.1234000', exchange=rabbitmq_puller.DELAY_EXCHANGE)
        self.mock_channel.basic_publish.assert_called_with(
            exchange=rabbitmq_puller.DELAY_EXCHANGE,
            routing_key='queue.1234000',
            body=body,
            properties=properties)
Exemple #3
0
    def testOnMessage_messageWithETA(self, mock_datetime, mock_delay_message,
                                     mock_request_ctor, mock_urlopen):
        epoch = datetime.datetime(1970, 1, 1)
        now = datetime.datetime(2020, 9, 1)
        mock_datetime.utcnow = mock.MagicMock(return_value=now)
        eta = now + datetime.timedelta(seconds=1234)
        message_puller = rabbitmq_puller.MessagePuller(self.conn_params,
                                                       'queue', 'target_url')
        method = mock.MagicMock(pika.spec.Basic.Deliver, autospec=True)
        method.delivery_tag = 4321
        properties = pika.spec.BasicProperties(
            headers={
                # ETA in epoch millis
                'x-eta-millis': str(int((eta - epoch).total_seconds() * 1000))
            })
        body = json.dumps({
            'name':
            'name',
            'payload':
            base64.b64encode('payload'.encode('ascii')).decode('ascii')
        })

        message_puller.on_message(self.mock_conn, self.mock_channel, method,
                                  properties, body)

        mock_delay_message.assert_called_with(self.mock_conn,
                                              self.mock_channel, properties,
                                              body, 1234)
        mock_request_ctor.assert_not_called()
        mock_urlopen.assert_not_called()
Exemple #4
0
    def testRun(self):
        message_puller = rabbitmq_puller.MessagePuller(self.conn_params,
                                                       'queue', 'target_url')

        message_puller.run()

        self.mock_conn_ctor.assert_called_with(self.conn_params)
        self.mock_channel.assert_has_calls([
            mock.call.queue_declare(queue='queue'),
            mock.call.queue_purge(queue='queue'),
            mock.call.basic_consume('queue', mock.ANY, auto_ack=True),
            mock.call.start_consuming(),
            mock.call.stop_consuming(),
        ])