async def test_take_empty(): """ GIVEN Channel is open and empty. WHEN Get an item. EXPECT Get blocks. """ q = asyncio.Queue() ch = Channel(q) with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(ch.take(), timeout=0.05)
async def test_take_closed(): """ GIVEN Channel is closed. WHEN Take an item. EXPECT Returns None. """ q = asyncio.Queue() ch = Channel(q) ch.close() result = await asyncio.wait_for(ch.take(), timeout=0.05) assert result is None
async def test_take_unblock(): """ GIVEN Channel is open and empty. WHEN Get an item, and an item is added while blocked. EXPECT Returns the added item. """ q = asyncio.Queue() ch = Channel(q) x = 'x' asyncio.get_running_loop().call_later(0.05, ch.offer, x) result = await asyncio.wait_for(ch.take(), timeout=0.1) assert result == x
async def test_take_closed_default(): """ GIVEN Channel is closed. WHEN Take an item, and default value is given. EXPECT Returns the given default value. """ q = asyncio.Queue() ch = Channel(q) ch.close() x = 'x' result = await asyncio.wait_for(ch.take(default=x), timeout=0.05) assert result == x
async def test_take_empty_close_default(): """ GIVEN Channel is open and empty. WHEN Get an item, a default value is given, and the Channel is closed while blocked. EXPECT Returns given default value. """ q = asyncio.Queue() ch = Channel(q) asyncio.get_running_loop().call_later(0.05, ch.close) x = 'x' result = await asyncio.wait_for(ch.take(default=x), timeout=0.1) assert result == x
async def test_take(): """ GIVEN Channel is open and not empty. WHEN Take an item. EXPECT Returns first item added to the Channel. """ a = 'a' b = 'b' q = asyncio.Queue() q.put_nowait(a) q.put_nowait(b) ch = Channel(q) result = await asyncio.wait_for(ch.take(), timeout=0.05) assert result == a