| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | """Tests for queues.py""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2014-02-26 10:25:02 +01:00
										 |  |  | from unittest import mock | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  | import asyncio | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | from asyncio import test_utils | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _QueueTestBase(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         self.loop = test_utils.TestLoop() | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         asyncio.set_event_loop(None) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         self.loop.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class QueueBasicTests(_QueueTestBase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _test_repr_or_str(self, fn, expect_id): | 
					
						
							|  |  |  |         """Test Queue's repr or str.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         fn is repr or str. expect_id is True if we expect the Queue's id to | 
					
						
							|  |  |  |         appear in fn(Queue()). | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         def gen(): | 
					
						
							|  |  |  |             when = yield | 
					
						
							|  |  |  |             self.assertAlmostEqual(0.1, when) | 
					
						
							|  |  |  |             when = yield 0.1 | 
					
						
							|  |  |  |             self.assertAlmostEqual(0.2, when) | 
					
						
							|  |  |  |             yield 0.1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop = test_utils.TestLoop(gen) | 
					
						
							|  |  |  |         self.addCleanup(loop.close) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertTrue(fn(q).startswith('<Queue'), fn(q)) | 
					
						
							|  |  |  |         id_is_present = hex(id(q)) in fn(q) | 
					
						
							|  |  |  |         self.assertEqual(expect_id, id_is_present) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def add_getter(): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             q = asyncio.Queue(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             # Start a task that waits to get. | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             asyncio.Task(q.get(), loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             # Let it start waiting. | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             yield from asyncio.sleep(0.1, loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.assertTrue('_getters[1]' in fn(q)) | 
					
						
							|  |  |  |             # resume q.get coroutine to finish generator | 
					
						
							|  |  |  |             q.put_nowait(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop.run_until_complete(add_getter()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def add_putter(): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             q = asyncio.Queue(maxsize=1, loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             q.put_nowait(1) | 
					
						
							|  |  |  |             # Start a task that waits to put. | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             asyncio.Task(q.put(2), loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             # Let it start waiting. | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             yield from asyncio.sleep(0.1, loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.assertTrue('_putters[1]' in fn(q)) | 
					
						
							|  |  |  |             # resume q.put coroutine to finish generator | 
					
						
							|  |  |  |             q.get_nowait() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop.run_until_complete(add_putter()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  |         self.assertTrue('_queue=[1]' in fn(q)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_ctor_loop(self): | 
					
						
							| 
									
										
										
										
											2014-02-26 10:25:02 +01:00
										 |  |  |         loop = mock.Mock() | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertIs(q._loop, loop) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertIs(q._loop, self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_ctor_noloop(self): | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             asyncio.set_event_loop(self.loop) | 
					
						
							|  |  |  |             q = asyncio.Queue() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.assertIs(q._loop, self.loop) | 
					
						
							|  |  |  |         finally: | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             asyncio.set_event_loop(None) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_repr(self): | 
					
						
							|  |  |  |         self._test_repr_or_str(repr, True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_str(self): | 
					
						
							|  |  |  |         self._test_repr_or_str(str, False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_empty(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertTrue(q.empty()) | 
					
						
							|  |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  |         self.assertFalse(q.empty()) | 
					
						
							|  |  |  |         self.assertEqual(1, q.get_nowait()) | 
					
						
							|  |  |  |         self.assertTrue(q.empty()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_full(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertFalse(q.full()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(maxsize=1, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  |         self.assertTrue(q.full()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_order(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         for i in [1, 3, 2]: | 
					
						
							|  |  |  |             q.put_nowait(i) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         items = [q.get_nowait() for _ in range(3)] | 
					
						
							|  |  |  |         self.assertEqual([1, 3, 2], items) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_maxsize(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def gen(): | 
					
						
							|  |  |  |             when = yield | 
					
						
							|  |  |  |             self.assertAlmostEqual(0.01, when) | 
					
						
							|  |  |  |             when = yield 0.01 | 
					
						
							|  |  |  |             self.assertAlmostEqual(0.02, when) | 
					
						
							|  |  |  |             yield 0.01 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop = test_utils.TestLoop(gen) | 
					
						
							|  |  |  |         self.addCleanup(loop.close) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(maxsize=2, loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertEqual(2, q.maxsize) | 
					
						
							|  |  |  |         have_been_put = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def putter(): | 
					
						
							|  |  |  |             for i in range(3): | 
					
						
							|  |  |  |                 yield from q.put(i) | 
					
						
							|  |  |  |                 have_been_put.append(i) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def test(): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             t = asyncio.Task(putter(), loop=loop) | 
					
						
							|  |  |  |             yield from asyncio.sleep(0.01, loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # The putter is blocked after putting two items. | 
					
						
							|  |  |  |             self.assertEqual([0, 1], have_been_put) | 
					
						
							|  |  |  |             self.assertEqual(0, q.get_nowait()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # Let the putter resume and put last item. | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             yield from asyncio.sleep(0.01, loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.assertEqual([0, 1, 2], have_been_put) | 
					
						
							|  |  |  |             self.assertEqual(1, q.get_nowait()) | 
					
						
							|  |  |  |             self.assertEqual(2, q.get_nowait()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             self.assertTrue(t.done()) | 
					
						
							|  |  |  |             self.assertTrue(t.result()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop.run_until_complete(test()) | 
					
						
							|  |  |  |         self.assertAlmostEqual(0.02, loop.time()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class QueueGetTests(_QueueTestBase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_blocking_get(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def queue_get(): | 
					
						
							|  |  |  |             return (yield from q.get()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res = self.loop.run_until_complete(queue_get()) | 
					
						
							|  |  |  |         self.assertEqual(1, res) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_with_putters(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(1, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         waiter = asyncio.Future(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         q._putters.append((2, waiter)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res = self.loop.run_until_complete(q.get()) | 
					
						
							|  |  |  |         self.assertEqual(1, res) | 
					
						
							|  |  |  |         self.assertTrue(waiter.done()) | 
					
						
							|  |  |  |         self.assertIsNone(waiter.result()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_blocking_get_wait(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def gen(): | 
					
						
							|  |  |  |             when = yield | 
					
						
							|  |  |  |             self.assertAlmostEqual(0.01, when) | 
					
						
							|  |  |  |             yield 0.01 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop = test_utils.TestLoop(gen) | 
					
						
							|  |  |  |         self.addCleanup(loop.close) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=loop) | 
					
						
							|  |  |  |         started = asyncio.Event(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         finished = False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def queue_get(): | 
					
						
							|  |  |  |             nonlocal finished | 
					
						
							|  |  |  |             started.set() | 
					
						
							|  |  |  |             res = yield from q.get() | 
					
						
							|  |  |  |             finished = True | 
					
						
							|  |  |  |             return res | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def queue_put(): | 
					
						
							|  |  |  |             loop.call_later(0.01, q.put_nowait, 1) | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             queue_get_task = asyncio.Task(queue_get(), loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             yield from started.wait() | 
					
						
							|  |  |  |             self.assertFalse(finished) | 
					
						
							|  |  |  |             res = yield from queue_get_task | 
					
						
							|  |  |  |             self.assertTrue(finished) | 
					
						
							|  |  |  |             return res | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res = loop.run_until_complete(queue_put()) | 
					
						
							|  |  |  |         self.assertEqual(1, res) | 
					
						
							|  |  |  |         self.assertAlmostEqual(0.01, loop.time()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_nonblocking_get(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  |         self.assertEqual(1, q.get_nowait()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_nonblocking_get_exception(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2014-01-25 17:24:51 -08:00
										 |  |  |         self.assertRaises(asyncio.QueueEmpty, q.get_nowait) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_cancelled(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def gen(): | 
					
						
							|  |  |  |             when = yield | 
					
						
							|  |  |  |             self.assertAlmostEqual(0.01, when) | 
					
						
							|  |  |  |             when = yield 0.01 | 
					
						
							|  |  |  |             self.assertAlmostEqual(0.061, when) | 
					
						
							|  |  |  |             yield 0.05 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop = test_utils.TestLoop(gen) | 
					
						
							|  |  |  |         self.addCleanup(loop.close) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def queue_get(): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             return (yield from asyncio.wait_for(q.get(), 0.051, loop=loop)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def test(): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             get_task = asyncio.Task(queue_get(), loop=loop) | 
					
						
							|  |  |  |             yield from asyncio.sleep(0.01, loop=loop)  # let the task start | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             q.put_nowait(1) | 
					
						
							|  |  |  |             return (yield from get_task) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(1, loop.run_until_complete(test())) | 
					
						
							|  |  |  |         self.assertAlmostEqual(0.06, loop.time()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_cancelled_race(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         t1 = asyncio.Task(q.get(), loop=self.loop) | 
					
						
							|  |  |  |         t2 = asyncio.Task(q.get(), loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         t1.cancel() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertTrue(t1.done()) | 
					
						
							|  |  |  |         q.put_nowait('a') | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual(t2.result(), 'a') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_with_waiting_putters(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop, maxsize=1) | 
					
						
							|  |  |  |         asyncio.Task(q.put('a'), loop=self.loop) | 
					
						
							|  |  |  |         asyncio.Task(q.put('b'), loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual(self.loop.run_until_complete(q.get()), 'a') | 
					
						
							|  |  |  |         self.assertEqual(self.loop.run_until_complete(q.get()), 'b') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class QueuePutTests(_QueueTestBase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_blocking_put(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def queue_put(): | 
					
						
							|  |  |  |             # No maxsize, won't block. | 
					
						
							|  |  |  |             yield from q.put(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(queue_put()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_blocking_put_wait(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def gen(): | 
					
						
							|  |  |  |             when = yield | 
					
						
							|  |  |  |             self.assertAlmostEqual(0.01, when) | 
					
						
							|  |  |  |             yield 0.01 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop = test_utils.TestLoop(gen) | 
					
						
							|  |  |  |         self.addCleanup(loop.close) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(maxsize=1, loop=loop) | 
					
						
							|  |  |  |         started = asyncio.Event(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         finished = False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def queue_put(): | 
					
						
							|  |  |  |             nonlocal finished | 
					
						
							|  |  |  |             started.set() | 
					
						
							|  |  |  |             yield from q.put(1) | 
					
						
							|  |  |  |             yield from q.put(2) | 
					
						
							|  |  |  |             finished = True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def queue_get(): | 
					
						
							|  |  |  |             loop.call_later(0.01, q.get_nowait) | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             queue_put_task = asyncio.Task(queue_put(), loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             yield from started.wait() | 
					
						
							|  |  |  |             self.assertFalse(finished) | 
					
						
							|  |  |  |             yield from queue_put_task | 
					
						
							|  |  |  |             self.assertTrue(finished) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop.run_until_complete(queue_get()) | 
					
						
							|  |  |  |         self.assertAlmostEqual(0.01, loop.time()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_nonblocking_put(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  |         self.assertEqual(1, q.get_nowait()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_nonblocking_put_exception(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(maxsize=1, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         q.put_nowait(1) | 
					
						
							| 
									
										
										
										
											2014-01-25 17:24:51 -08:00
										 |  |  |         self.assertRaises(asyncio.QueueFull, q.put_nowait, 2) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_put_cancelled(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def queue_put(): | 
					
						
							|  |  |  |             yield from q.put(1) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def test(): | 
					
						
							|  |  |  |             return (yield from q.get()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         t = asyncio.Task(queue_put(), loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertEqual(1, self.loop.run_until_complete(test())) | 
					
						
							|  |  |  |         self.assertTrue(t.done()) | 
					
						
							|  |  |  |         self.assertTrue(t.result()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_put_cancelled_race(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop, maxsize=1) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         asyncio.Task(q.put('a'), loop=self.loop) | 
					
						
							|  |  |  |         asyncio.Task(q.put('c'), loop=self.loop) | 
					
						
							|  |  |  |         t = asyncio.Task(q.put('b'), loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         t.cancel() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertTrue(t.done()) | 
					
						
							|  |  |  |         self.assertEqual(q.get_nowait(), 'a') | 
					
						
							|  |  |  |         self.assertEqual(q.get_nowait(), 'c') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_put_with_waiting_getters(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.Queue(loop=self.loop) | 
					
						
							|  |  |  |         t = asyncio.Task(q.get(), loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.loop.run_until_complete(q.put('a')) | 
					
						
							|  |  |  |         self.assertEqual(self.loop.run_until_complete(t), 'a') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class LifoQueueTests(_QueueTestBase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_order(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.LifoQueue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         for i in [1, 3, 2]: | 
					
						
							|  |  |  |             q.put_nowait(i) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         items = [q.get_nowait() for _ in range(3)] | 
					
						
							|  |  |  |         self.assertEqual([2, 3, 1], items) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PriorityQueueTests(_QueueTestBase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_order(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.PriorityQueue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         for i in [1, 3, 2]: | 
					
						
							|  |  |  |             q.put_nowait(i) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         items = [q.get_nowait() for _ in range(3)] | 
					
						
							|  |  |  |         self.assertEqual([1, 2, 3], items) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class JoinableQueueTests(_QueueTestBase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_task_done_underflow(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.JoinableQueue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertRaises(ValueError, q.task_done) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_task_done(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.JoinableQueue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         for i in range(100): | 
					
						
							|  |  |  |             q.put_nowait(i) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         accumulator = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Two workers get items from the queue and call task_done after each. | 
					
						
							|  |  |  |         # Join the queue and assert all items have been processed. | 
					
						
							|  |  |  |         running = True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def worker(): | 
					
						
							|  |  |  |             nonlocal accumulator | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             while running: | 
					
						
							|  |  |  |                 item = yield from q.get() | 
					
						
							|  |  |  |                 accumulator += item | 
					
						
							|  |  |  |                 q.task_done() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def test(): | 
					
						
							|  |  |  |             for _ in range(2): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |                 asyncio.Task(worker(), loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             yield from q.join() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(test()) | 
					
						
							|  |  |  |         self.assertEqual(sum(range(100)), accumulator) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # close running generators | 
					
						
							|  |  |  |         running = False | 
					
						
							|  |  |  |         for i in range(2): | 
					
						
							|  |  |  |             q.put_nowait(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_join_empty_queue(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.JoinableQueue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Test that a queue join()s successfully, and before anything else | 
					
						
							|  |  |  |         # (done twice for insurance). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         @asyncio.coroutine | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def join(): | 
					
						
							|  |  |  |             yield from q.join() | 
					
						
							|  |  |  |             yield from q.join() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(join()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_format(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         q = asyncio.JoinableQueue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertEqual(q._format(), 'maxsize=0') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         q._unfinished_tasks = 2 | 
					
						
							|  |  |  |         self.assertEqual(q._format(), 'maxsize=0 tasks=2') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main() |