Esempio n. 1
0
async def test_consume_events_per_api_stream(
        loop, redis_event_transport: RedisEventTransport, redis_client,
        dummy_api):
    redis_event_transport.stream_use = StreamUse.PER_API

    event_names = []

    async def co_consume(event_name):
        consumer = redis_event_transport.consume([("my.dummy", event_name)],
                                                 {}, loop)
        async for message_ in consumer:
            event_names.append(message_.event_name)
            await consumer.__anext__()

    task1 = asyncio.ensure_future(co_consume("my_event1"))
    task2 = asyncio.ensure_future(co_consume("my_event2"))
    task3 = asyncio.ensure_future(co_consume("my_event3"))
    await asyncio.sleep(0.1)

    await redis_client.xadd(
        "my.dummy.*:stream",
        fields={
            b"api_name": b"my.dummy",
            b"event_name": b"my_event1",
            b"id": b"1",
            b"version": b"1",
            b":field": b'"value"',
        },
    )
    await redis_client.xadd(
        "my.dummy.*:stream",
        fields={
            b"api_name": b"my.dummy",
            b"event_name": b"my_event2",
            b"id": b"2",
            b"version": b"1",
            b":field": b'"value"',
        },
    )
    await redis_client.xadd(
        "my.dummy.*:stream",
        fields={
            b"api_name": b"my.dummy",
            b"event_name": b"my_event3",
            b"id": b"3",
            b"version": b"1",
            b":field": b'"value"',
        },
    )
    await asyncio.sleep(0.1)

    assert len(event_names) == 3
    assert set(event_names) == {"my_event1", "my_event2", "my_event3"}

    await cancel(task1, task2, task3)
Esempio n. 2
0
async def test_consume_events_per_api_stream(
    loop, redis_event_transport: RedisEventTransport, redis_client, dummy_api
):
    redis_event_transport.stream_use = StreamUse.PER_API

    events = []

    async def co_consume(event_name):
        consumer = redis_event_transport.consume([("my.dummy", event_name)], "cg", bus_client=None)
        async for messages in consumer:
            events.extend(messages)

    task1 = asyncio.ensure_future(co_consume("my_event1"))
    task2 = asyncio.ensure_future(co_consume("my_event2"))
    task3 = asyncio.ensure_future(co_consume("my_event3"))
    await asyncio.sleep(0.2)

    await redis_client.xadd(
        "my.dummy.*:stream",
        fields={
            b"api_name": b"my.dummy",
            b"event_name": b"my_event1",
            b"id": b"1",
            b"version": b"1",
            b":field": b'"value"',
        },
    )
    await redis_client.xadd(
        "my.dummy.*:stream",
        fields={
            b"api_name": b"my.dummy",
            b"event_name": b"my_event2",
            b"id": b"2",
            b"version": b"1",
            b":field": b'"value"',
        },
    )
    await redis_client.xadd(
        "my.dummy.*:stream",
        fields={
            b"api_name": b"my.dummy",
            b"event_name": b"my_event3",
            b"id": b"3",
            b"version": b"1",
            b":field": b'"value"',
        },
    )
    await asyncio.sleep(0.2)
    await cancel(task1, task2, task3)

    assert set([e.event_name for e in events]) == {"my_event1", "my_event2", "my_event3"}
Esempio n. 3
0
async def test_send_event_per_api_stream(redis_event_transport: RedisEventTransport, redis_client):
    redis_event_transport.stream_use = StreamUse.PER_API
    await redis_event_transport.send_event(
        EventMessage(api_name="my.api", event_name="my_event", kwargs={"field": "value"}, id="123"),
        options={},
    )
    messages = await redis_client.xrange("my.api.*:stream")
    assert len(messages) == 1
    assert messages[0][1] == {
        b"api_name": b"my.api",
        b"event_name": b"my_event",
        b"id": b"123",
        b"version": b"1",
        b":field": b'"value"',
    }