Ejemplo n.º 1
0
async def test_item_timeout_unblock():
    """
    GIVEN
        Channel is open and empty.
    WHEN
        Wait for item, and timeout elapses.
    EXPECT
        Unblocks and returns False.
    """
    q = asyncio.Queue()
    ch = Channel(q)
    assert not await ch.item(timeout=0.05)
    assert ch.empty()
Ejemplo n.º 2
0
async def test_item_close_unblock():
    """
    GIVEN
        Channel is open and empty.
    WHEN
        Wait for item, and close channel.
    EXPECT
        Unblocks and returns False.
    """
    q = asyncio.Queue()
    ch = Channel(q)
    asyncio.get_running_loop().call_later(0.05, ch.close)
    assert not await ch.item(timeout=0.1)
    assert ch.empty()
Ejemplo n.º 3
0
async def test_item_next_reblocks():
    """
    GIVEN
        Channel is open and empty, and two coroutines are awaiting
        an item.
    WHEN
        An item becomes available, and first unblocked coroutine
        takes it.
    EXPECT
        Second coroutine remains blocked.
    """
    q = asyncio.Queue(1)
    ch = Channel(q)

    async def take_item():
        await ch.item()
        ch.poll()

    asyncio.get_running_loop().call_later(0.05, ch.offer, 'a')
    with pytest.raises(asyncio.TimeoutError):
        await asyncio.wait_for(asyncio.gather(take_item(), ch.item()),
                               timeout=0.1)
    assert ch.empty()
    await ch.put('b')  # Unblock the second coroutine.