def test_fifo_ordering(self): """ Test that items maintain order """ channel = Channel(3, loop=self.loop) testitems = ["first", "second", "third"] @asyncio.coroutine def add_items(): for item in testitems: yield from channel.put(item) @asyncio.coroutine def get_items(): out = [] while not channel.empty(): item = yield from channel.get() out.append(item) return out # add and check for full self.ruc(add_items()) self.assertTrue(channel.full()) # retreive and check that everything matches outitems = self.ruc(get_items()) self.assertEqual(outitems, testitems)
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_fifo_ordering(self): """ Test that items maintain order """ channel = Channel(3, loop=self.loop) testitems = [ "first", "second", "third" ] @asyncio.coroutine def add_items(): for item in testitems: yield from channel.put(item) @asyncio.coroutine def get_items(): out = [] while not channel.empty(): item = yield from channel.get() out.append(item) return out # add and check for full self.ruc(add_items()) self.assertTrue(channel.full()) # retreive and check that everything matches outitems = self.ruc(get_items()) self.assertEqual(outitems, testitems)
def test_put_get(self): """ Simple put/get test """ testitem = {"foo": "bar"} channel = Channel(1, loop=self.loop) self.ruc(channel.put(testitem)) self.assertEqual(channel.qsize(), 1) self.assertTrue(channel.full()) self.assertFalse(channel.empty()) item = self.ruc(channel.get()) self.assertEqual(item, testitem) self.assertEqual(channel.qsize(), 0) self.assertFalse(channel.full()) self.assertTrue(channel.empty())
def test_construct(self): """ Test that we can even construct a Channel """ channel = Channel(loop=self.loop) self.assertEqual(channel.maxsize, 0) self.assertFalse(channel.full()) self.assertTrue(channel.empty()) channel = Channel(1, loop=self.loop) self.assertEqual(channel.maxsize, 1) channel = Channel(maxsize=1, loop=self.loop) self.assertEqual(channel.maxsize, 1) self.assertRaises(TypeError, lambda: Channel([], loop=self.loop)) self.assertRaises(TypeError, lambda: Channel(1.0, loop=self.loop)) self.assertRaises(TypeError, lambda: Channel(-1, loop=self.loop))
def test_putter_cancel(self): channel = Channel(1, loop=self.loop) self.ruc(channel.put("foo")) # next put will block as channel is full self.assertTrue(channel.full()) @asyncio.coroutine def test_put(): yield from channel.put("bar") @asyncio.coroutine def test_cancel(): yield from asyncio.sleep(0.01, loop=self.loop) channel._putters[0].cancel() result = self.rucgather(test_put(), test_cancel(), return_exceptions=True) self.assertIsInstance(result[0], asyncio.CancelledError)
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_putter_exception(self): channel = Channel(1, loop=self.loop) self.ruc(channel.put("foo")) # next put will block as channel is full self.assertTrue(channel.full()) @asyncio.coroutine def test_put(): yield from channel.put("bar") @asyncio.coroutine def test_cancel(): yield from asyncio.sleep(0.01, loop=self.loop) channel._maxsize = 2 # For hitting a different code branch in Channel channel._putters[0].set_exception(TypeError('random type error')) result = self.rucgather(test_put(), test_cancel(), return_exceptions=True) self.assertIsInstance(result[0], TypeError)
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)