Ejemplo n.º 1
0
def test_offer_none():
    """
    GIVEN
        A channel.
    WHEN
        Offered None.
    EXPECT
        Raises ValueError.
    """
    q = asyncio.Queue()
    ch = Channel(q)
    with pytest.raises(ValueError, match='None is not allowed on channel'):
        ch.offer(None)
Ejemplo n.º 2
0
async def test_capacity_timeout_unblock():
    """
    GIVEN
        Channel is open and full.
    WHEN
        Wait for capacity, and timeout elapses.
    EXPECT
        Unblocks and returns True.
    """
    q = asyncio.Queue(1)
    ch = Channel(q)
    ch.offer('x')
    assert not await ch.capacity(timeout=0.1)
    assert ch.full()
Ejemplo n.º 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)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
async def test_capacity_unblock():
    """
    GIVEN
        Channel is open and full.
    WHEN
        Wait for capacity, and an item is take while blocked.
    EXPECT
        Unblocks and returns True.
    """
    q = asyncio.Queue(1)
    ch = Channel(q)
    x = 'x'
    ch.offer(x)
    asyncio.get_running_loop().call_later(0.05, ch.poll)
    assert await ch.capacity(timeout=0.1)
    assert ch.offer(x)
Ejemplo n.º 6
0
async def test_capacity_close_unblock():
    """
    GIVEN
        Channel is open and full.
    WHEN
        Wait for capacity, and close channel.
    EXPECT
        Unblocks and returns False.
    """
    q = asyncio.Queue(1)
    ch = Channel(q)
    ch.close()
    assert not await ch.capacity(timeout=0.05)
    assert not ch.offer('x')
Ejemplo n.º 7
0
def test_offer_closed():
    """
    GIVEN
        Channel is closed.
    WHEN
        Offered an item.
    EXPECT
        The item is not added to the queue and offer returns False.
    """
    q = asyncio.Queue()
    ch = Channel(q)
    ch.close()
    x = 'x'
    assert not ch.offer(x)
    assert q.empty()
Ejemplo n.º 8
0
def test_offer():
    """
    GIVEN
        Channel is not full.
    WHEN
        Offered an item.
    EXPECT
        The item is added to the queue and offer returns True.
    """
    q = asyncio.Queue()
    ch = Channel(q)
    x = 'x'
    assert ch.offer(x)
    assert q.qsize() == 1
    assert q.get_nowait() == x
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
def test_offer_full():
    """
    GIVEN
        Channel is full.
    WHEN
        Offered an item.
    EXPECT
        The item is not added to the queue and offer returns False.
    """
    a = 'a'
    q = asyncio.Queue(1)
    q.put_nowait(a)
    ch = Channel(q)
    b = 'b'
    assert not ch.offer(b)
    x = q.get_nowait()
    assert x != b