async def test_broker_handling_async_ok(): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=AsyncMock(), queue=queue, timeout=1, options={}) broker = AIORedisBroker('redis://localhost') broker._bindings[queue.name] = consumer await broker._handler(queue.name, '{}') consumer.handler.assert_called_once_with()
async def test_broker_rollback_many_attempts_ok(): queue = Queue(name='test_queue') broker = AIORedisBroker('redis://localhost') broker.send = AsyncMock() broker._rollbacks[str(hash(queue.name)) + str(hash('{}'))] = 4 await broker.rollback(queue.name, '{}') broker.send.assert_not_called()
async def test_broker_handler_fail_timeout(kafka_consumer): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=AsyncMock(side_effect=asyncio.TimeoutError), queue=queue, timeout=1, options={}) broker = KafkaBroker('kafka://localhost') await broker._handle(consumer, {'a': 1})
async def test_broker_handler_fail_error(kafka_consumer): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=1, queue=queue, timeout=1, options={}) broker = KafkaBroker('kafka://localhost') await broker._handle(consumer, {'a': 1})
async def test_broker_consume_ok(monkeypatch): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=AsyncMock(), queue=queue, timeout=1, options={}) create_redis = MagicMock(blpop=AsyncMock(side_effect=[('ch', '{}'), None, Exception])) monkeypatch.setattr(aioredis, 'create_redis', AsyncMock(return_value=create_redis)) broker = AIORedisBroker('redis://localhost') broker.BIND_TIME = .01 await broker.bind_consumer(consumer) await asyncio.sleep(.02)
async def test_broker_handling_sync_rollback_ok(): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=Mock(side_effect=Exception), queue=queue, timeout=1, options={}) broker = AIORedisBroker('redis://localhost') broker.send = AsyncMock() broker._bindings[queue.name] = consumer await broker._handler(queue.name, '{}') consumer.handler.assert_called_once() await asyncio.sleep(.01) broker.send.assert_called_once_with(queue.name, '{}')
async def test_broker_send_ok(kafka_producer): queue = Queue(name='test_queue') broker = KafkaBroker('kafka://localhost') await broker.send(queue.name, '{}') broker.producer.start.assert_called() broker.producer.send_and_wait.assert_called_once_with(queue.name, b'{}') broker.producer.stop.assert_called() broker.producer._closed = False await broker.send(queue.name, '{}') broker.producer.start.assert_called()
async def test_broker_consume_fail(amqp_connection): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=AsyncMock(), queue=queue, timeout=1, options={}) channel, connection = amqp_connection channel.basic_consume.side_effect = aioamqp.ChannelClosed(code=500) broker = AMQPBroker('amqp://localhost') with pytest.raises(aioamqp.ChannelClosed): await broker.bind_consumer(consumer)
async def test_broker_wrapper_fail_timeout(amqp_connection): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=AsyncMock(side_effect=asyncio.TimeoutError), queue=queue, timeout=1, options={}) channel, connection = amqp_connection broker = AMQPBroker('amqp://localhost') wrapper = broker._amqp_wrapper(consumer) await wrapper(channel, '{"a":1}', MagicMock(delivery_tag=1), MagicMock()) channel.basic_client_ack.assert_not_called()
async def test_broker_wrapper_fail_empty_body(amqp_connection): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=1, queue=queue, timeout=1, options={}) channel, connection = amqp_connection broker = AMQPBroker('amqp://localhost') wrapper = broker._amqp_wrapper(consumer) await wrapper(channel, 'null', MagicMock(delivery_tag=1), MagicMock()) channel.basic_client_ack.assert_not_called()
async def test_broker_consume_ok(amqp_connection): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=AsyncMock(), queue=queue, timeout=1, options={}) channel, connection = amqp_connection broker = AMQPBroker('amqp://localhost') await broker.bind_consumer(consumer) connection.channel.assert_called() channel.basic_consume.assert_called()
async def test_broker_consume_on_error_ok(amqp_connection): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=AsyncMock(), queue=queue, timeout=1, options={}) channel, connection = amqp_connection broker = AMQPBroker('amqp://localhost') broker.rebind = AsyncMock() await broker.bind_consumer(consumer) broker._on_amqp_error(queue.name, Exception()) broker._on_amqp_error('no_so_queue', Exception()) broker.rebind.assert_called_once_with(queue.name)
async def test_broker_consume_new_channel_ok(amqp_connection): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=AsyncMock(), queue=queue, timeout=1, options={}) channel, connection = amqp_connection channel.basic_consume.side_effect = [aioamqp.ChannelClosed(code=404), None] broker = AMQPBroker('amqp://localhost') await broker.bind_consumer(consumer) connection.channel.assert_called() channel.queue_declare.assert_called_once_with(queue.name) assert channel.basic_consume.call_count == 2
async def test_broker_consume_ok(kafka_consumer): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=AsyncMock(), queue=queue, timeout=1, options={}) broker = KafkaBroker('kafka://localhost') await broker.bind_consumer(consumer) await asyncio.sleep(.01) kafka_consumer.start.assert_called() kafka_consumer.stop.assert_called() assert consumer.handler.call_args.kwargs['a'] == 1 assert consumer.handler.call_args.kwargs['kafka']
async def test_broker_wrapper_noack_ok(amqp_connection): queue = Queue(name='test_queue') consumer = Consumer(agent=None, handler=AsyncMock(), queue=queue, timeout=1, options={'autoack': False}) channel, connection = amqp_connection broker = AMQPBroker('amqp://localhost') wrapper = broker._amqp_wrapper(consumer) await wrapper(channel, '{"a":1}', MagicMock(delivery_tag=1), MagicMock()) consumer.handler.assert_called_once() assert consumer.handler.call_args.kwargs['a'] == 1 assert consumer.handler.call_args.kwargs['amqp'] channel.basic_client_ack.assert_not_called()