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_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)
def test_put_when_closed(self): channel = Channel(1, loop=self.loop) channel.close() self.assertRaises(ChannelClosed, lambda: self.ruc(channel.put("foo")))