def test_get_nowait_raises_closed(self): channel = Channel(1, loop=self.loop) channel.put_nowait("foo") channel.close() item = channel.get_nowait() self.assertEqual(item, "foo") self.assertRaises(ChannelClosed, lambda: channel.get_nowait())
def test_get_nowait_raises_closed(self): channel = Channel(1, loop=self.loop) channel.put_nowait("foo") channel.close() item = channel.get_nowait() self.assertEqual(item, "foo") self.assertRaises(ChannelClosed, lambda: channel.get_nowait())
def test_join(self): """ Test that a channel is joinable (when closed, and queue empty) """ channel = Channel(1000, loop=self.loop) [channel.put_nowait(i) for i in range(1000)] self.assertTrue(channel.full()) # create 1000 gets, should complete the queue gets = [channel.get() for _ in range(1000)] @asyncio.coroutine def runner(): # sleep a bit, then call 1000 gets on channel, calling channel.close() in the middle yield from asyncio.sleep(0.01, loop=self.loop) n = 0 for c in gets: n += 1 if n == 500: channel.close() yield from c @asyncio.coroutine def test(): self.loop.create_task(runner()) # run the getters in the backgrund yield from asyncio.wait_for(channel.join(), timeout=2, loop=self.loop) self.ruc(test())
def test_join(self): """ Test that a channel is joinable (when closed, and queue empty) """ channel = Channel(1000, loop=self.loop) [channel.put_nowait(i) for i in range(1000)] self.assertTrue(channel.full()) # create 1000 gets, should complete the queue gets = [channel.get() for _ in range(1000)] @asyncio.coroutine def runner(): # sleep a bit, then call 1000 gets on channel, calling channel.close() in the middle yield from asyncio.sleep(0.01, loop=self.loop) n = 0 for c in gets: n += 1 if n == 500: channel.close() yield from c @asyncio.coroutine def test(): self.loop.create_task(runner()) # run the getters in the backgrund yield from asyncio.wait_for(channel.join(), timeout=2, loop=self.loop) self.ruc(test())
def test_put_throws_channel_closed(self): """ Test that when a put blocks, and a channel is closed, the put will throw a ChannelClosed instead of waiting to add to channel """ channel = Channel(1, loop=self.loop) channel.put_nowait("foo") self.assertTrue(channel.full()) @asyncio.coroutine def wait_close(): yield from asyncio.sleep(0.01, loop=self.loop) channel.close() (put_return, _) = self.rucgather(channel.put("bar"), wait_close(), return_exceptions=True) self.assertIsInstance(put_return, ChannelClosed) self.assertTrue(channel.closed())
def test_put_throws_channel_closed(self): """ Test that when a put blocks, and a channel is closed, the put will throw a ChannelClosed instead of waiting to add to channel """ channel = Channel(1, loop=self.loop) channel.put_nowait("foo") self.assertTrue(channel.full()) @asyncio.coroutine def wait_close(): yield from asyncio.sleep(0.01, loop=self.loop) channel.close() (put_return, _) = self.rucgather(channel.put("bar"), wait_close(), return_exceptions=True) self.assertIsInstance(put_return, ChannelClosed) self.assertTrue(channel.closed())
def test_multiple_blocking_puts(self): """ Test that a channel with multiple running put() still works out fine when the channel is closed """ channel = Channel(1, loop=self.loop) channel.put_nowait("foo") self.assertTrue(channel.full()) @asyncio.coroutine def wait_close(): yield from asyncio.sleep(0.01, loop=self.loop) channel.close() futures = [channel.put(i) for i in range(100)] futures.insert(50, wait_close()) result = self.rucgather(*futures, return_exceptions=True) result.pop(50) # pop the result for wait_close() for res in result: self.assertIsInstance(res, ChannelClosed)
def test_multiple_blocking_puts(self): """ Test that a channel with multiple running put() still works out fine when the channel is closed """ channel = Channel(1, loop=self.loop) channel.put_nowait("foo") self.assertTrue(channel.full()) @asyncio.coroutine def wait_close(): yield from asyncio.sleep(0.01, loop=self.loop) channel.close() futures = [channel.put(i) for i in range(100)] futures.insert(50, wait_close()) result = self.rucgather(*futures, return_exceptions=True) result.pop(50) # pop the result for wait_close() for res in result: self.assertIsInstance(res, ChannelClosed)
def test_async_iterator(self): """ Test that we can even construct a Channel """ channel = Channel(loop=self.loop) [channel.put_nowait(i) for i in range(10)] channel.close() async def test(): s = 0 async for item in channel: s += item return s result = self.ruc(test()) self.assertEqual(result, sum(range(10)))
def test_put_nowait_get_nowait(self): channel = Channel(1, loop=self.loop) channel.put_nowait("foo") self.assertRaises(ChannelFull, lambda: channel.put_nowait("bar")) self.assertEqual("foo", channel.get_nowait()) self.assertRaises(ChannelEmpty, lambda: channel.get_nowait())
def test_iter(self): channel = Channel(loop=self.loop) [channel.put_nowait(n) for n in range(5)] self.assertEqual(list(range(5)), list(channel))
def test_put_nowait_get_nowait(self): channel = Channel(1, loop=self.loop) channel.put_nowait("foo") self.assertRaises(ChannelFull, lambda: channel.put_nowait("bar")) self.assertEqual("foo", channel.get_nowait()) self.assertRaises(ChannelEmpty, lambda: channel.get_nowait())
def test_iter(self): channel = Channel(loop=self.loop) [channel.put_nowait(n) for n in range(5)] self.assertEqual(list(range(5)), list(channel))