Exemple #1
0
async def test_consume_events(transactional_bus: BusPath,
                              aiopg_connection_factory, dummy_api):
    events = []

    def listener(event_message, *, field):
        events.append(event_message)

    connection1 = await aiopg_connection_factory()
    async with lightbus_set_database(transactional_bus,
                                     connection1,
                                     apis=["my.dummy"]):
        listener_co = await transactional_bus.my.dummy.my_event.listen_async(
            listener)

    task = asyncio.ensure_future(listener_co)
    await asyncio.sleep(0.2)

    connection2 = await aiopg_connection_factory()

    print("connection 1", connection1)
    print("connection 2", connection2)

    async with lightbus_set_database(transactional_bus,
                                     connection2,
                                     apis=["my.dummy"]):
        await transactional_bus.my.dummy.my_event.fire_async(field=1)

    await asyncio.sleep(0.2)

    await cancel(task)

    assert len(events) == 1
def test_init_multiple_transports(dummy_bus: BusPath, aiopg_connection):
    registry = dummy_bus.client.transport_registry
    registry.set_event_transport(
        "another_api", TransactionalEventTransport(DebugEventTransport()))

    with pytest.raises(ApisMustUseSameTransport):
        lightbus_set_database(dummy_bus,
                              aiopg_connection,
                              apis=["some_api", "another_api"])
def atomic_context(dummy_bus: BusPath, aiopg_connection):
    registry = dummy_bus.client.transport_registry
    current_event_transport = registry.get_event_transport("default")
    registry.set_event_transport(
        "default", TransactionalEventTransport(child_transport=current_event_transport)
    )
    return lightbus_set_database(dummy_bus, aiopg_connection, apis=["some_api", "some_api2"])
Exemple #4
0
    def __call__(self, request):
        connection = connections["default"]
        if connection.in_atomic_block:
            start_transaction = False
        elif connection.autocommit:
            start_transaction = True
        else:
            start_transaction = None

        lightbus_transaction_context = lightbus_set_database(
            self.bus, connection)
        block(lightbus_transaction_context.__aenter__(), self.loop, timeout=5)

        response = self.get_response(request)

        if 500 <= response.status_code < 600:
            block(lightbus_transaction_context.__aexit__(True, True, True),
                  self.loop,
                  timeout=5)
        else:
            block(lightbus_transaction_context.__aexit__(None, None, None),
                  self.loop,
                  timeout=5)

        return response
Exemple #5
0
    async def start_firing(number):

        async with aiopg.connect(loop=loop, **pg_kwargs) as connection:
            async with connection.cursor(
                    cursor_factory=cursor_factory) as cursor:
                bus = await transactional_bus_factory()

                for x in range(0, 50):
                    async with lightbus_set_database(bus,
                                                     connection,
                                                     apis=["my.dummy"]):
                        await bus.my.dummy.my_event.fire_async(field=1)
                        await cursor.execute(
                            "INSERT INTO test_table VALUES (%s)",
                            [f"{number}-{x}"])
Exemple #6
0
async def test_fire_events_all_ok(
    transactional_bus,
    aiopg_connection,
    test_table,
    dummy_api,
    aiopg_cursor,
    messages_in_redis,
    get_outbox,
):
    async with lightbus_set_database(transactional_bus,
                                     aiopg_connection,
                                     apis=["foo", "bar"]):
        await transactional_bus.my.dummy.my_event.fire_async(field=1)
        await aiopg_cursor.execute("INSERT INTO test_table VALUES ('hey')")

    assert await test_table.total_rows() == 1  # Test table has data
    assert len(await
               get_outbox()) == 0  # Sent messages are removed from outbox
    assert len(await messages_in_redis("my.dummy",
                                       "my_event")) == 1  # Message in redis
Exemple #7
0
async def test_fire_events_exception(
    transactional_bus,
    aiopg_connection,
    test_table,
    dummy_api,
    aiopg_cursor,
    messages_in_redis,
    get_outbox,
):
    class OhNo(Exception):
        pass

    with pytest.raises(OhNo):
        async with lightbus_set_database(transactional_bus,
                                         aiopg_connection,
                                         apis=["foo", "bar"]):
            await transactional_bus.my.dummy.my_event.fire_async(field=1)
            await aiopg_cursor.execute("INSERT INTO test_table VALUES ('hey')")
            raise OhNo()

    assert await test_table.total_rows() == 0  # No data in tests table
    assert len(await get_outbox()) == 0  # No messages sat in outbox
    assert len(await messages_in_redis("my.dummy",
                                       "my_event")) == 0  # Nothing in redis
def test_init_bad_transport(dummy_bus: BusPath, aiopg_connection):
    with pytest.raises(ApisMustUseTransactionalTransport):
        lightbus_set_database(dummy_bus, aiopg_connection, apis=["some_api"])
def test_init_no_apis(dummy_bus: BusPath, aiopg_connection):
    with pytest.raises(NoApisSpecified):
        lightbus_set_database(dummy_bus, aiopg_connection, apis=[])