Ejemplo n.º 1
0
async def test_initialize_declare_exchange_race_condition():
    service = MessageQueueService()

    async def keep_declaring_exchanges():
        i = 0
        while service._channel is None:
            await service.declare_exchange(f"exchange_{i}")
            await asyncio.sleep(0)
            i += 1

    await asyncio.gather(keep_declaring_exchanges(), service.initialize())
Ejemplo n.º 2
0
async def message_queue_service():
    service = MessageQueueService()
    await service.initialize()

    yield service

    await service.shutdown()
Ejemplo n.º 3
0
async def test_incorrect_vhost(mocker, caplog):
    mocker.patch("server.message_queue_service.config.MQ_VHOST", "bad_vhost")
    service = MessageQueueService()

    await service.initialize()

    assert any("Incorrect vhost?" in rec.message for rec in caplog.records)
Ejemplo n.º 4
0
async def test_incorrect_credentials(mocker, caplog):
    mocker.patch("server.message_queue_service.config.MQ_PASSWORD",
                 "bad_password")
    service = MessageQueueService()

    await service.initialize()
    expected_warning = "Unable to connect to RabbitMQ. Incorrect credentials?"
    assert expected_warning in [rec.message for rec in caplog.records]
    assert service._is_ready is False
    caplog.clear()

    await service.declare_exchange("test_exchange")
    expected_warning = "Not connected to RabbitMQ, unable to declare exchange."
    assert expected_warning in [rec.message for rec in caplog.records]
    caplog.clear()

    payload = {"msg": "test message"}
    exchange_name = "test_exchange"
    routing_key = "test.routing.key"
    delivery_mode = aio_pika.DeliveryMode.NOT_PERSISTENT
    await service.publish(exchange_name, routing_key, payload, delivery_mode)
    expected_warning = "Not connected to RabbitMQ, unable to publish message."
    assert expected_warning in [rec.message for rec in caplog.records]

    await service.shutdown()
async def test_incorrect_port(mocker, caplog):
    mocker.patch("server.message_queue_service.config.MQ_PORT", 1)
    service = MessageQueueService()

    await service.initialize()

    expected_warning = "Unable to connect to RabbitMQ. Is it running?"
    assert expected_warning in [rec.message for rec in caplog.records]
Ejemplo n.º 6
0
async def test_incorrect_username(mocker, caplog):
    mocker.patch("server.message_queue_service.config.MQ_USER", "bad_user")
    service = MessageQueueService()

    await service.initialize()

    expected_warning = "Unable to connect to RabbitMQ. Incorrect credentials?"
    assert expected_warning in [rec.message for rec in caplog.records]
async def mq_service():
    service = MessageQueueService()
    await service.initialize()

    await service.declare_exchange("test_exchange")

    yield service

    await service.shutdown()
Ejemplo n.º 8
0
async def test_declaring_exchange_without_initialization():
    service = MessageQueueService()
    exchange_name = "test_exchange"

    assert service._is_ready is False
    assert service._connection is None

    await service.declare_exchange(exchange_name)

    assert service._is_ready
    assert service._connection is not None
    assert service._exchanges.get(exchange_name) is not None
async def test_several_initializations_connect_only_once():
    service = MessageQueueService()

    def set_mock_connection(*args, **kwargs):
        service._connection = mock.Mock()
        service._channel = mock.Mock()
        service._channel.declare_exchange = CoroutineMock()

    service._connect = CoroutineMock(side_effect=set_mock_connection)

    await asyncio.gather(
        service.declare_exchange("exchange_one"),
        service.initialize(),
        service.declare_exchange("exchange_two"),
        service.initialize(),
    )

    service._connect.assert_called_once()
async def test_initialize():
    service = MessageQueueService()
    await service.initialize()
    await service.shutdown()