Exemplo n.º 1
0
async def test_receive_multiple_specific_prefixes(channel_layer):
    """
    Makes sure we receive on multiple real channels
    """
    channel_layer = RedisChannelLayer(capacity=10)
    channel1 = await channel_layer.new_channel()
    channel2 = await channel_layer.new_channel(prefix="thing")
    r1, _, r2 = tasks = [
        asyncio.ensure_future(x) for x in (
            channel_layer.receive(channel1),
            channel_layer.send(channel2, {"type": "message"}),
            channel_layer.receive(channel2),
        )
    ]
    await asyncio.wait(tasks, timeout=0.5)

    assert not r1.done()
    assert r2.done() and r2.result()["type"] == "message"
    r1.cancel()
Exemplo n.º 2
0
async def test_receive_cancel(channel_layer):
    """
    Makes sure we can cancel a receive without blocking
    """
    channel_layer = RedisChannelLayer(capacity=10)
    channel = await channel_layer.new_channel()
    delay = 0
    while delay < 0.01:
        await channel_layer.send(channel, {"type": "test.message", "text": "Ahoy-hoy!"})

        task = asyncio.ensure_future(channel_layer.receive(channel))
        await asyncio.sleep(delay)
        task.cancel()
        delay += 0.001

        try:
            await asyncio.wait_for(task, None)
        except asyncio.CancelledError:
            pass