def test_basic_publish_channel_error(self, mocker, mock_rabbit_adaptor): client = self._test_client(rabbit_adaptor=mock_rabbit_adaptor) with client.application.app_context(): mock_connection = mocker.Mock() mock_channel = mocker.Mock() mock_channel.basic_publish = mocker.Mock( side_effect=[pika.exceptions.AMQPChannelError]) mock_connection.channel = mocker.Mock(return_value=mock_channel) mock_pika = mocker.patch('pika.BlockingConnection', return_value=mock_connection) mock_pika.channel = mocker.Mock() rabbit = RabbitAdaptor("amqp://test.com") rabbit.basic_publish("exchange1", "my_queue", "{my: body}") mock_pika.assert_called() mock_connection.channel.assert_called() assert mock_channel.basic_publish.call_count == 1 assert mock_pika.call_count == 1 # No retry of connection
def test_setup_exchange_connection_error(self, mocker, mock_rabbit_adaptor): client = self._test_client(rabbit_adaptor=mock_rabbit_adaptor) with client.application.app_context(): mock_connection = mocker.Mock() mock_channel = mocker.Mock() mock_channel.exchange_declare = mocker.Mock( side_effect=[pika.exceptions.AMQPConnectionError, "ok"]) mock_connection.channel = mocker.Mock(return_value=mock_channel) mock_pika = mocker.patch('pika.BlockingConnection', return_value=mock_connection) mock_pika.channel = mocker.Mock() rabbit = RabbitAdaptor("amqp://test.com") rabbit.setup_exchange("exchange1") mock_pika.assert_called() mock_connection.channel.assert_called() assert mock_channel.exchange_declare.call_count == 2 assert mock_pika.call_count == 2
def test_basic_publish(self, mocker, mock_rabbit_adaptor): client = self._test_client(rabbit_adaptor=mock_rabbit_adaptor) with client.application.app_context(): mock_connection = mocker.Mock() mock_channel = mocker.Mock() mock_channel.basic_publish = mocker.Mock() mock_connection.channel = mocker.Mock(return_value=mock_channel) mock_pika = mocker.patch('pika.BlockingConnection', return_value=mock_connection) mock_pika.channel = mocker.Mock() rabbit = RabbitAdaptor("amqp://test.com") rabbit.basic_publish("exchange1", "my_queue", "{my: body}") mock_pika.assert_called() mock_connection.channel.assert_called() mock_channel.basic_publish.assert_called_with( exchange="exchange1", mandatory=True, routing_key='my_queue', properties=pika.BasicProperties(delivery_mode=1), body="{my: body}")
def test_bind_queue_to_exchange_connection_error(self, mocker, mock_rabbit_adaptor): client = self._test_client(rabbit_adaptor=mock_rabbit_adaptor) with client.application.app_context(): mock_connection = mocker.Mock() mock_channel = mocker.Mock() mock_channel.queue_bind = mocker.Mock() mock_channel.queue_bind = mocker.Mock( side_effect=[pika.exceptions.AMQPConnectionError, "ok"]) mock_connection.channel = mocker.Mock(return_value=mock_channel) mock_pika = mocker.patch('pika.BlockingConnection', return_value=mock_connection) mock_pika.channel = mocker.Mock() rabbit = RabbitAdaptor("amqp://test.com") rabbit.bind_queue_to_exchange("exchange1", "my_queue") mock_pika.assert_called() mock_connection.channel.assert_called() mock_channel.queue_bind.assert_called_with(exchange="exchange1", queue="my_queue", routing_key='my_queue') assert mock_channel.queue_bind.call_count == 2 assert mock_pika.call_count == 2 # Retried the connection
def test_channel_acessro(self, mocker, mock_rabbit_adaptor): client = self._test_client(rabbit_adaptor=mock_rabbit_adaptor) with client.application.app_context(): mock_connection = mocker.Mock() mock_connection.close = mocker.Mock() mock_channel = mocker.Mock() mock_connection.channel = mocker.Mock(return_value=mock_channel) mock_pika = mocker.patch('pika.BlockingConnection', return_value=mock_connection) mock_pika.channel = mocker.Mock() rabbit = RabbitAdaptor("amqp://test.com") assert rabbit.channel == mock_channel mock_pika.assert_called() mock_connection.channel.assert_called() # Test that subsequent call doesn't open a new channel mock_pika.reset_mock() mock_connection.reset_mock() assert rabbit.channel == mock_channel mock_pika.assert_not_called() mock_connection.assert_not_called()
def test_close_channel(self, mocker, mock_rabbit_adaptor): client = self._test_client(rabbit_adaptor=mock_rabbit_adaptor) with client.application.app_context(): mock_connection = mocker.Mock() mock_channel = mocker.Mock() mock_channel.close = mocker.Mock() mock_connection.channel = mocker.Mock(return_value=mock_channel) mock_pika = mocker.patch('pika.BlockingConnection', return_value=mock_connection) mock_pika.channel = mocker.Mock() rabbit = RabbitAdaptor("amqp://test.com") # If channel not open then do nothing rabbit.close_channel() mock_channel.close.assert_not_called() rabbit.channel rabbit.close_channel() mock_channel.close.assert_called()
def test_setup_queue(self, mocker, mock_rabbit_adaptor): client = self._test_client(rabbit_adaptor=mock_rabbit_adaptor) with client.application.app_context(): mock_connection = mocker.Mock() mock_channel = mocker.Mock() mock_channel.queue_declare = mocker.Mock() mock_connection.channel = mocker.Mock(return_value=mock_channel) mock_pika = mocker.patch('pika.BlockingConnection', return_value=mock_connection) mock_pika.channel = mocker.Mock() rabbit = RabbitAdaptor("amqp://test.com") # rabbit.connect() rabbit.setup_queue("my_queue") mock_pika.assert_called() mock_connection.channel.assert_called() mock_channel.queue_declare.assert_called_with(queue="my_queue") # Verify that second call doesn't open channel again mock_connection.channel.reset_mock() rabbit.setup_queue("my_queue") mock_connection.channel.assert_not_called()