Ejemplo 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

    events = []

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

    task1 = asyncio.ensure_future(co_consume("my_event1", "cg1"))
    task2 = asyncio.ensure_future(co_consume("my_event2", "cg2"))
    task3 = asyncio.ensure_future(co_consume("my_event3", "cg3"))
    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"}
Ejemplo n.º 2
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"',
    }