Ejemplo n.º 1
0
 def test_SubscribeBadSingleQueueCreate(self, pikaMock):
     # ConnectionClosed
     pikaMock.return_value.channel.return_value.queue_declare.side_effect=[ConnectionClosed(1, "Boom"), pikaMock.return_value.channel.return_value.queue_declare.return_value]
     self.broker = self._getBroker()
     
     exchange = "testExchange"
     topic1 = "topic.one"
     topics = [topic1]
     self.broker.subscribe(self.testCallback, topics, exchange)
     self.broker.unsubscribe()
     self.broker.disconnect()
     
     pikaMock.return_value.channel.return_value.queue_declare.side_effect=[ChannelClosed(1, "Boom"), pikaMock.return_value.channel.return_value.queue_declare.return_value]
     self.broker.subscribe(self.testCallback, topics, exchange)
     self.broker.unsubscribe()
     self.broker.disconnect()
      
     pikaMock.return_value.channel.return_value.queue_declare.side_effect=[ChannelError("Boom"), pikaMock.return_value.channel.return_value.queue_declare.return_value]
     self.broker.subscribe(self.testCallback, topics, exchange)
     self.broker.unsubscribe()
     self.broker.disconnect()
     
     pikaMock.return_value.channel.return_value.queue_declare.side_effect=[AMQPConnectionError("Boom"), pikaMock.return_value.channel.return_value.queue_declare.return_value]
     self.broker.subscribe(self.testCallback, topics, exchange)
     self.broker.unsubscribe()
     self.broker.disconnect()
     
     pikaMock.return_value.channel.return_value.queue_declare.side_effect=[AttributeError("Boom"), pikaMock.return_value.channel.return_value.queue_declare.return_value]
     self.broker.subscribe(self.testCallback, topics, exchange)
     self.broker.unsubscribe()
     self.broker.disconnect()    
Ejemplo n.º 2
0
 def test_SubscribeBadDoubleQueueCreate(self, pikaMock):
     pikaMock.return_value.channel.return_value.queue_declare.side_effect=[ConnectionClosed(1, "Boom"), ConnectionClosed(1, "Boom2")]
     self.broker = self._getBroker()
     
     exchange = "testExchange"
     topic1 = "topic.one"
     topics = [topic1]
     with self.assertRaises(ConnectionClosed):
         self.broker.subscribe(self.testCallback, topics, exchange)
     self.broker.disconnect()
     
     pikaMock.return_value.channel.return_value.queue_declare.side_effect=[ChannelClosed(1, "Boom"), ChannelClosed(1, "Boom2")]
     with self.assertRaises(ChannelClosed):
         self.broker.subscribe(self.testCallback, topics, exchange)
     self.broker.disconnect()
     
     pikaMock.return_value.channel.return_value.queue_declare.side_effect=[ChannelError("Boom"), ChannelError("Boom2")]
     with self.assertRaises(ChannelError):
         self.broker.subscribe(self.testCallback, topics, exchange)
     self.broker.disconnect()
     
     pikaMock.return_value.channel.return_value.queue_declare.side_effect=[AMQPConnectionError("Boom"), AMQPConnectionError("Boom2")]
     with self.assertRaises(AMQPConnectionError):
         self.broker.subscribe(self.testCallback, topics, exchange)
     self.broker.disconnect()
     
     pikaMock.return_value.channel.return_value.queue_declare.side_effect=[AttributeError("Boom"), AttributeError("Boom2")]
     with self.assertRaises(AttributeError):
         self.broker.subscribe(self.testCallback, topics, exchange)
     self.broker.disconnect()
Ejemplo n.º 3
0
    def send_message(self,
                     message: Union[str, Dict[str, Any]],
                     key: str = '',
                     exchange: str = None):
        """
        Sends message to specified routing key/ exchange combination
        if exchange is not passed as an argument then
        the last declared exchange will be used.

        Exceptions
        ----------
        Raises:
            AssertionError.
                if exchanges equals to None in basic_publish call.
            pika.exceptions.ChannelClosed:
                if exchange is not open.
        """
        if exchange is None:
            exchange = self._exchange

        if isinstance(message, dict):
            message = json.dumps(message)

        try:
            self.channel_connection.basic_publish(exchange=exchange,
                                                  routing_key=key,
                                                  body=message)
        except ChannelClosed as e:
            self.logger.error(traceback.format_exc())
            raise ChannelClosed(
                f"Failure sending message: {e} on exchange:{exchange}")
Ejemplo n.º 4
0
    def _on_close(self, method_frame):

        super(LayerChannel, self)._on_close(method_frame)
        if self.on_callback_error_callback:
            self.on_callback_error_callback(
                ChannelClosed(
                    method_frame.method.reply_code,
                    method_frame.method.reply_text,
                ), )
Ejemplo n.º 5
0
    def test_run_queue_not_declared_send_correct_notifcation(
        self, monitor: Monitor
    ) -> None:
        """Test correct notifications are sent when the Q is not found."""
        monitor.connect_to_queue = Mock(side_effect=ChannelClosed(404, "NOT FOUND"))

        monitor.run()

        monitor.notifiers[0].notify.assert_called_once_with(
            "Queue does not exist", 'Queue "test_queue_1" has not been declared'
        )
Ejemplo n.º 6
0
    def test_reject_queue_handles_exception_while_nack_message(self):
        # mock the deserialize method and force it to raise an exception
        channel = mock.MagicMock()
        channel.basic_nack = mock.MagicMock(side_effect=ChannelClosed('foo'))

        # regenerate the command class being tested as we mocked an extra module above
        self.command = classify.Command()

        # call the method being tested
        self.command.reject_queue_item(channel, mock.MagicMock())
        # error was logged
        self.logger.error.assert_any_call('Classifier could not nack message.')
Ejemplo n.º 7
0
    def test_get_channel_handles_exception_when_connecting_to_broker(self):
        # mock connection creation method and force it to raise an exception
        classify.pika.ConnectionParameters = mock.MagicMock(
            side_effect=ChannelClosed('foo'))

        # call the method being tested
        channel = self.command.get_channel()

        # the connection parameters are set correctly
        classify.pika.ConnectionParameters.assert_called_once_with(
            host=settings.QUEUE_HOSTNAME,
            heartbeat_interval=600,
            blocked_connection_timeout=300)
        # error was logged
        self.logger.error.assert_any_call('foo')
        # no broker channel is returned
        self.assertEquals(None, channel)
Ejemplo n.º 8
0
    def test_train_consumer_handles_exception_when_starting_consuming_the_queue(
            self):
        # mock channel and force to raise an exception
        channel = mock.MagicMock()
        channel.start_consuming = mock.MagicMock(
            side_effect=ChannelClosed('foo'))
        self.command.get_consumer = mock.MagicMock(return_value=channel)
        # mock the callback called when a message is consumed from the queue
        self.command.train_callback = mock.MagicMock()

        # call the method being tested
        self.command.train_consumer()

        # the queue consumer was tried to get fetched
        self.command.get_consumer.assert_called_once_with(
            settings.QUEUE_NAME_TRAIN, self.command.train_callback)
        # consuming messages from queue was started
        channel.start_consuming.assert_called_once()
        # error was logged
        self.logger.error.assert_any_call('foo')
Ejemplo n.º 9
0
    def test_close_with_channel_closed_exception(self,
                                                 select_connection_class_mock):
        select_connection_class_mock.return_value.is_closed = False

        with mock.patch.object(blocking_connection.BlockingConnection,
                               '_process_io_for_connection_setup'):
            connection = blocking_connection.BlockingConnection('params')

        channel1_mock = mock.Mock(
            is_open=True,
            close=mock.Mock(side_effect=ChannelClosed(-1, 'Just because'),
                            spec_set=pika.channel.Channel.close),
            spec_set=blocking_connection.BlockingChannel)

        channel2_mock = mock.Mock(is_open=True,
                                  spec_set=blocking_connection.BlockingChannel)

        connection._impl._channels = {
            1:
            mock.Mock(_get_cookie=mock.Mock(
                return_value=channel1_mock,
                spec_set=pika.channel.Channel._get_cookie),
                      spec_set=pika.channel.Channel),
            2:
            mock.Mock(_get_cookie=mock.Mock(
                return_value=channel2_mock,
                spec_set=pika.channel.Channel._get_cookie),
                      spec_set=pika.channel.Channel)
        }

        with mock.patch.object(
                blocking_connection.BlockingConnection,
                '_flush_output',
                spec_set=blocking_connection.BlockingConnection._flush_output):
            connection._closed_result.signal_once()
            connection.close(200, 'text')

            channel1_mock.close.assert_called_once_with(200, 'text')
            channel2_mock.close.assert_called_once_with(200, 'text')
        select_connection_class_mock.return_value.close.assert_called_once_with(
            200, 'text')
Ejemplo n.º 10
0
class WhenChannelClosedRaised(_BaseProcessingErrorTestCase):
    exc = ChannelClosed(sentinel.arg0, sentinel.arg1)

    def should_reconnect(self):
        self.agent.reconnect.assert_called_once_with()
Ejemplo n.º 11
0
Archivo: amqp.py Proyecto: xdrew/twoost
 def _on_safewrite_channel_closed(self, channel, reply_code, reply_text):
     yield self._open_safewrite_channel()
     self._fail_published_messages(ChannelClosed(reply_code, reply_text))