async def test_put(): """ GIVEN Channel is open and not full. WHEN Put an item. EXPECT Item is added to the Channel and returns True. """ q = asyncio.Queue() ch = Channel(q) x = 'x' assert await asyncio.wait_for(ch.put(x), timeout=0.05) assert q.get_nowait() == x
async def test_put_closed(): """ GIVEN Closed Channel. WHEN Put an item. EXPECT Item is not added to the Channel and returns False. """ q = asyncio.Queue() ch = Channel(q) ch.close() x = 'x' result = await asyncio.wait_for(ch.put(x), timeout=0.05) assert result == False assert q.empty()
async def test_put_full_close(): """ GIVEN Channel is open and full. WHEN Put an item. While blocked, the Channel is closed. EXPECT Item is not added to Channel and returns False. """ a = 'a' q = asyncio.Queue(1) q.put_nowait(a) ch = Channel(q) asyncio.get_running_loop().call_later(0.05, ch.close) b = 'b' assert not await asyncio.wait_for(ch.put(b), timeout=0.1) assert q.get_nowait() == a
async def test_put_full_unblock(): """ GIVEN Channe is open and full. WHEN Put and item, and an item is taken while blocked. EXPECT Item is added to Channel and returns True. """ a = 'a' q = asyncio.Queue(1) q.put_nowait(a) ch = Channel(q) asyncio.get_running_loop().call_later(0.05, ch.poll) b = 'b' assert await asyncio.wait_for(ch.put(b), timeout=0.1) assert q.get_nowait() == b
async def test_put_full(): """ GIVEN Channel is open and full. WHEN Put an item. EXPECT Put blocks. """ a = 'a' q = asyncio.Queue(1) q.put_nowait(a) ch = Channel(q) b = 'b' with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(ch.put(b), timeout=0.05) assert q.get_nowait() == a