Beispiel #1
0
async def test_capacity_next_reblocks():
    """
    GIVEN
        Channel is open and full, and two coroutines are awaiting
        capacity.
    WHEN
        Capacity becomes available, and first unblocked coroutine
        puts an item.
    EXPECT
        Second coroutine remains blocked.
    """
    q = asyncio.Queue(1)
    ch = Channel(q)
    assert ch.offer('a')
    b = 'b'

    async def put_item():
        await ch.capacity()
        ch.offer(b)

    asyncio.get_running_loop().call_later(0.05, ch.poll)
    with pytest.raises(asyncio.TimeoutError):
        await asyncio.wait_for(asyncio.gather(put_item(), ch.capacity()),
                               timeout=0.1)
    # Verify that new item was put and unblock the second coroutine.
    assert await ch.take() == b
Beispiel #2
0
async def test_capacity_unblock_next():
    """
    GIVEN
        Channel is open and full, and two coroutines are awaiting
        capacity.
    WHEN
        Capacity becomes available, and first unblocked coroutine does
        not put item.
    EXPECT
        Second coroutine unblocks.
    """
    q = asyncio.Queue(1)
    ch = Channel(q)
    assert ch.offer('a')
    asyncio.get_running_loop().call_later(0.05, ch.poll)
    await asyncio.wait_for(asyncio.gather(ch.capacity(), ch.capacity()),
                           timeout=0.1)
Beispiel #3
0
async def test_capacity():
    """
    GIVEN
        Channel is open and full.
    WHEN
        Wait for capacity.
    EXPECT
        Blocks.
    """
    q = asyncio.Queue(1)
    ch = Channel(q)
    x = 'x'
    ch.offer(x)
    with pytest.raises(asyncio.TimeoutError):
        await asyncio.wait_for(ch.capacity(), timeout=0.05)