Example #1
0
    def test_send_call_message(self):
        message = pika_drv_msg.RpcPikaOutgoingMessage(
            self._pika_engine, self._message, self._context
        )

        expiration = 1
        stopwatch = timeutils.StopWatch(duration=expiration).start()

        result = "it is a result"
        reply_queue_name = "reply_queue_name"

        future = futures.Future()
        future.set_result(result)
        reply_listener = mock.Mock()
        reply_listener.register_reply_waiter.return_value = future
        reply_listener.get_reply_qname.return_value = reply_queue_name

        res = message.send(
            exchange=self._exchange,
            routing_key=self._routing_key,
            reply_listener=reply_listener,
            stopwatch=stopwatch,
            retrier=None
        )

        self.assertEqual(result, res)

        self._pika_engine.connection_with_confirmation_pool.acquire(
        ).__enter__().channel.publish.assert_called_once_with(
            body=mock.ANY,
            exchange=self._exchange, mandatory=True,
            properties=mock.ANY,
            routing_key=self._routing_key
        )

        body = self._pika_engine.connection_with_confirmation_pool.acquire(
        ).__enter__().channel.publish.call_args[1]["body"]

        self.assertEqual(
            b'{"_$_request_id": 555, "_$_token": "it is a token", '
            b'"msg_str": "hello", "msg_type": 1}',
            body
        )

        props = self._pika_engine.connection_with_confirmation_pool.acquire(
        ).__enter__().channel.publish.call_args[1]["properties"]

        self.assertEqual(props.content_encoding, 'utf-8')
        self.assertEqual(props.content_type, 'application/json')
        self.assertEqual(props.delivery_mode, 1)
        self.assertTrue(expiration * 1000 - float(props.expiration) < 100)
        self.assertEqual(props.headers, {'version': '1.0'})
        self.assertEqual(props.correlation_id, message.msg_id)
        self.assertEqual(props.reply_to, reply_queue_name)
        self.assertTrue(props.message_id)
    def test_send_cast_message(self):
        message = pika_drv_msg.RpcPikaOutgoingMessage(self._pika_engine,
                                                      self._message,
                                                      self._context)

        expiration = 1
        expiration_time = time.time() + expiration

        message.send(target=oslo_messaging.Target(exchange=self._exchange,
                                                  topic=self._routing_key),
                     reply_listener=None,
                     expiration_time=expiration_time,
                     retrier=None)

        self._pika_engine.connection_with_confirmation_pool.acquire(
        ).__enter__().channel.publish.assert_called_once_with(
            body=mock.ANY,
            exchange=self._exchange,
            mandatory=True,
            properties=mock.ANY,
            routing_key=self._routing_key)

        body = self._pika_engine.connection_with_confirmation_pool.acquire(
        ).__enter__().channel.publish.call_args[1]["body"]

        self.assertEqual(
            b'{"_$_request_id": 555, "_$_token": "it is a token", '
            b'"msg_str": "hello", "msg_type": 1}', body)

        props = self._pika_engine.connection_with_confirmation_pool.acquire(
        ).__enter__().channel.publish.call_args[1]["properties"]

        self.assertEqual(props.content_encoding, 'utf-8')
        self.assertEqual(props.content_type, 'application/json')
        self.assertEqual(props.delivery_mode, 1)
        self.assertTrue(expiration * 1000 - float(props.expiration) < 100)
        self.assertEqual(props.headers, {'version': '1.0'})
        self.assertIsNone(props.correlation_id)
        self.assertIsNone(props.reply_to)
        self.assertTrue(props.message_id)
Example #3
0
    def test_send_cast_message(self):
        message = pika_drv_msg.RpcPikaOutgoingMessage(self._pika_engine,
                                                      self._message,
                                                      self._context)

        expiration = 1
        stopwatch = timeutils.StopWatch(duration=expiration).start()

        message.send(exchange=self._exchange,
                     routing_key=self._routing_key,
                     reply_listener=None,
                     stopwatch=stopwatch,
                     retrier=None)

        self._pika_engine.connection_with_confirmation_pool.acquire(
        ).__enter__().channel.publish.assert_called_once_with(
            body=mock.ANY,
            exchange=self._exchange,
            mandatory=True,
            properties=mock.ANY,
            routing_key=self._routing_key)

        body = self._pika_engine.connection_with_confirmation_pool.acquire(
        ).__enter__().channel.publish.call_args[1]["body"]

        self.assertEqual(
            b'{"_$_request_id": 555, "_$_token": "it is a token", '
            b'"msg_str": "hello", "msg_type": 1}', body)

        props = self._pika_engine.connection_with_confirmation_pool.acquire(
        ).__enter__().channel.publish.call_args[1]["properties"]

        self.assertEqual('utf-8', props.content_encoding)
        self.assertEqual('application/json', props.content_type)
        self.assertEqual(1, props.delivery_mode)
        self.assertTrue(expiration * 1000 - float(props.expiration) < 100)
        self.assertEqual({'version': '1.0'}, props.headers)
        self.assertIsNone(props.correlation_id)
        self.assertIsNone(props.reply_to)
        self.assertTrue(props.message_id)
Example #4
0
    def send(self,
             target,
             ctxt,
             message,
             wait_for_reply=None,
             timeout=None,
             retry=None):
        expiration_time = None if timeout is None else time.time() + timeout

        if retry is None:
            retry = self._pika_engine.default_rpc_retry_attempts

        def on_exception(ex):
            if isinstance(ex, (pika_drv_exc.ConnectionException,
                               exceptions.MessageDeliveryFailure)):
                LOG.warn("Problem during message sending. %s", ex)
                return True
            else:
                return False

        retrier = (None if retry == 0 else retrying.retry(
            stop_max_attempt_number=(None if retry == -1 else retry),
            retry_on_exception=on_exception,
            wait_fixed=self._pika_engine.rpc_retry_delay * 1000,
        ))

        msg = pika_drv_msg.RpcPikaOutgoingMessage(self._pika_engine, message,
                                                  ctxt)
        reply = msg.send(
            target,
            reply_listener=self._reply_listener if wait_for_reply else None,
            expiration_time=expiration_time,
            retrier=retrier)

        if reply is not None:
            if reply.failure is not None:
                raise reply.failure

            return reply.result
Example #5
0
    def send(self,
             target,
             ctxt,
             message,
             wait_for_reply=None,
             timeout=None,
             retry=None):
        with timeutils.StopWatch(duration=timeout) as stopwatch:
            if retry is None:
                retry = self._pika_engine.default_rpc_retry_attempts

            exchange = self._pika_engine.get_rpc_exchange_name(target.exchange)

            def on_exception(ex):
                if isinstance(ex, pika_drv_exc.ExchangeNotFoundException):
                    # it is desired to create exchange because if we sent to
                    # exchange which is not exists, we get ChannelClosed
                    # exception and need to reconnect
                    try:
                        self._declare_rpc_exchange(exchange, stopwatch)
                    except pika_drv_exc.ConnectionException as e:
                        LOG.warning("Problem during declaring exchange. %s", e)
                    return True
                elif isinstance(ex, (pika_drv_exc.ConnectionException,
                                     exceptions.MessageDeliveryFailure)):
                    LOG.warning("Problem during message sending. %s", ex)
                    return True
                else:
                    return False

            retrier = (None if retry == 0 else retrying.retry(
                stop_max_attempt_number=(None if retry == -1 else retry),
                retry_on_exception=on_exception,
                wait_fixed=self._pika_engine.rpc_retry_delay * 1000,
            ))

            if target.fanout:
                return self.cast_all_workers(exchange, target.topic, ctxt,
                                             message, stopwatch, retrier)

            routing_key = self._pika_engine.get_rpc_queue_name(
                target.topic, target.server, retrier is None)

            msg = pika_drv_msg.RpcPikaOutgoingMessage(self._pika_engine,
                                                      message, ctxt)
            try:
                reply = msg.send(exchange=exchange,
                                 routing_key=routing_key,
                                 reply_listener=(self._reply_listener
                                                 if wait_for_reply else None),
                                 stopwatch=stopwatch,
                                 retrier=retrier)
            except pika_drv_exc.ExchangeNotFoundException as ex:
                try:
                    self._declare_rpc_exchange(exchange, stopwatch)
                except pika_drv_exc.ConnectionException as e:
                    LOG.warning("Problem during declaring exchange. %s", e)
                raise ex

            if reply is not None:
                if reply.failure is not None:
                    raise reply.failure

                return reply.result