| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2017-12-11 10:04:40 -05:00
										 |  |  | from test.test_asyncio import utils as test_utils | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 20:34:09 -07:00
										 |  |  | def tearDownModule(): | 
					
						
							|  |  |  |     asyncio.set_event_loop_policy(None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  | class _QueueTestBase(test_utils.TestCase): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2016-11-04 14:29:28 -04:00
										 |  |  |         super().setUp() | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         self.loop = self.new_test_loop() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         loop = self.new_test_loop(gen) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async 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. | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |             loop.create_task(q.get()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             # Let it start waiting. | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |             await asyncio.sleep(0.1) | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             loop.run_until_complete(add_getter()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async 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. | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |             loop.create_task(q.put(2)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             # Let it start waiting. | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |             await asyncio.sleep(0.1) | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             loop.run_until_complete(add_putter()) | 
					
						
							|  |  |  |             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() | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertIs(q._loop, loop) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             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): | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         asyncio.set_event_loop(self.loop) | 
					
						
							| 
									
										
										
										
											2019-09-11 11:20:24 +03:00
										 |  |  |         q = asyncio.Queue() | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         self.assertIs(q._loop, self.loop) | 
					
						
							| 
									
										
										
										
											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): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             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): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertFalse(q.full()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             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): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         loop = self.new_test_loop(gen) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(maxsize=2, loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertEqual(2, q.maxsize) | 
					
						
							|  |  |  |         have_been_put = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def putter(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             for i in range(3): | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |                 await q.put(i) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 have_been_put.append(i) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def test(): | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |             t = loop.create_task(putter()) | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |             await asyncio.sleep(0.01) | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |             await asyncio.sleep(0.01) | 
					
						
							| 
									
										
										
										
											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): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def queue_get(): | 
					
						
							|  |  |  |             return await q.get() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         res = self.loop.run_until_complete(queue_get()) | 
					
						
							|  |  |  |         self.assertEqual(1, res) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_with_putters(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(1, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         waiter = self.loop.create_future() | 
					
						
							| 
									
										
										
										
											2015-08-05 13:52:33 -04:00
										 |  |  |         q._putters.append(waiter) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         loop = self.new_test_loop(gen) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=loop) | 
					
						
							|  |  |  |             started = asyncio.Event(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         finished = False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def queue_get(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             nonlocal finished | 
					
						
							|  |  |  |             started.set() | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             res = await q.get() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             finished = True | 
					
						
							|  |  |  |             return res | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def queue_put(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             loop.call_later(0.01, q.put_nowait, 1) | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |             queue_get_task = loop.create_task(queue_get()) | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             await started.wait() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.assertFalse(finished) | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             res = await queue_get_task | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             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): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             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): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         loop = self.new_test_loop(gen) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def queue_get(): | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |             return await asyncio.wait_for(q.get(), 0.051) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def test(): | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |             get_task = loop.create_task(queue_get()) | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |             await asyncio.sleep(0.01)  # let the task start | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             q.put_nowait(1) | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             return await get_task | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(1, loop.run_until_complete(test())) | 
					
						
							|  |  |  |         self.assertAlmostEqual(0.06, loop.time()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_cancelled_race(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t1 = self.loop.create_task(q.get()) | 
					
						
							|  |  |  |         t2 = self.loop.create_task(q.get()) | 
					
						
							| 
									
										
										
										
											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): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=self.loop, maxsize=1) | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         self.loop.create_task(q.put('a')) | 
					
						
							|  |  |  |         self.loop.create_task(q.put('b')) | 
					
						
							| 
									
										
										
										
											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') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-28 07:42:34 -07:00
										 |  |  |     def test_why_are_getters_waiting(self): | 
					
						
							|  |  |  |         # From issue #268. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def consumer(queue, num_expected): | 
					
						
							| 
									
										
										
										
											2015-09-28 07:42:34 -07:00
										 |  |  |             for _ in range(num_expected): | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |                 await queue.get() | 
					
						
							| 
									
										
										
										
											2015-09-28 07:42:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def producer(queue, num_items): | 
					
						
							| 
									
										
										
										
											2015-09-28 07:42:34 -07:00
										 |  |  |             for i in range(num_items): | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |                 await queue.put(i) | 
					
						
							| 
									
										
										
										
											2015-09-28 07:42:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         queue_size = 1 | 
					
						
							|  |  |  |         producer_num_items = 5 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(queue_size, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2015-09-28 07:42:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete( | 
					
						
							|  |  |  |             asyncio.gather(producer(q, producer_num_items), | 
					
						
							|  |  |  |                            consumer(q, producer_num_items), | 
					
						
							|  |  |  |                            loop=self.loop), | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 09:35:23 -08:00
										 |  |  |     def test_cancelled_getters_not_being_held_in_self_getters(self): | 
					
						
							|  |  |  |         def a_generator(): | 
					
						
							|  |  |  |             yield 0.1 | 
					
						
							|  |  |  |             yield 0.2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop = self.new_test_loop(a_generator) | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         async def consumer(queue): | 
					
						
							| 
									
										
										
										
											2017-11-07 09:35:23 -08:00
										 |  |  |             try: | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |                 item = await asyncio.wait_for(queue.get(), 0.1) | 
					
						
							| 
									
										
										
										
											2017-11-07 09:35:23 -08:00
										 |  |  |             except asyncio.TimeoutError: | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             queue = asyncio.Queue(loop=self.loop, maxsize=5) | 
					
						
							| 
									
										
										
										
											2017-11-07 09:35:23 -08:00
										 |  |  |         self.loop.run_until_complete(self.loop.create_task(consumer(queue))) | 
					
						
							|  |  |  |         self.assertEqual(len(queue._getters), 0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | class QueuePutTests(_QueueTestBase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_blocking_put(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def queue_put(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             # No maxsize, won't block. | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             await q.put(1) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         loop = self.new_test_loop(gen) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(maxsize=1, loop=loop) | 
					
						
							|  |  |  |             started = asyncio.Event(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         finished = False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def queue_put(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             nonlocal finished | 
					
						
							|  |  |  |             started.set() | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             await q.put(1) | 
					
						
							|  |  |  |             await q.put(2) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             finished = True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def queue_get(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             loop.call_later(0.01, q.get_nowait) | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |             queue_put_task = loop.create_task(queue_put()) | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             await started.wait() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.assertFalse(finished) | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             await queue_put_task | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.assertTrue(finished) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop.run_until_complete(queue_get()) | 
					
						
							|  |  |  |         self.assertAlmostEqual(0.01, loop.time()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_nonblocking_put(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  |         self.assertEqual(1, q.get_nowait()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-06 14:03:38 -04:00
										 |  |  |     def test_get_cancel_drop_one_pending_reader(self): | 
					
						
							| 
									
										
										
										
											2015-08-05 13:52:33 -04:00
										 |  |  |         def gen(): | 
					
						
							|  |  |  |             yield 0.01 | 
					
						
							|  |  |  |             yield 0.1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop = self.new_test_loop(gen) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=loop) | 
					
						
							| 
									
										
										
										
											2015-08-05 13:52:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         reader = loop.create_task(q.get()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |         loop.run_until_complete(asyncio.sleep(0.01)) | 
					
						
							| 
									
										
										
										
											2015-08-05 13:52:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  |         q.put_nowait(2) | 
					
						
							|  |  |  |         reader.cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             loop.run_until_complete(reader) | 
					
						
							|  |  |  |         except asyncio.CancelledError: | 
					
						
							|  |  |  |             # try again | 
					
						
							|  |  |  |             reader = loop.create_task(q.get()) | 
					
						
							|  |  |  |             loop.run_until_complete(reader) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         result = reader.result() | 
					
						
							|  |  |  |         # if we get 2, it means 1 got dropped! | 
					
						
							|  |  |  |         self.assertEqual(1, result) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-06 14:03:38 -04:00
										 |  |  |     def test_get_cancel_drop_many_pending_readers(self): | 
					
						
							|  |  |  |         def gen(): | 
					
						
							|  |  |  |             yield 0.01 | 
					
						
							|  |  |  |             yield 0.1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop = self.new_test_loop(gen) | 
					
						
							|  |  |  |         loop.set_debug(True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=loop) | 
					
						
							| 
									
										
										
										
											2015-08-06 14:03:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         reader1 = loop.create_task(q.get()) | 
					
						
							|  |  |  |         reader2 = loop.create_task(q.get()) | 
					
						
							|  |  |  |         reader3 = loop.create_task(q.get()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |         loop.run_until_complete(asyncio.sleep(0.01)) | 
					
						
							| 
									
										
										
										
											2015-08-06 14:03:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  |         q.put_nowait(2) | 
					
						
							|  |  |  |         reader1.cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             loop.run_until_complete(reader1) | 
					
						
							|  |  |  |         except asyncio.CancelledError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop.run_until_complete(reader3) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-28 07:42:34 -07:00
										 |  |  |         # It is undefined in which order concurrent readers receive results. | 
					
						
							|  |  |  |         self.assertEqual({reader2.result(), reader3.result()}, {1, 2}) | 
					
						
							| 
									
										
										
										
											2015-08-06 14:03:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-05 13:52:33 -04:00
										 |  |  |     def test_put_cancel_drop(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def gen(): | 
					
						
							|  |  |  |             yield 0.01 | 
					
						
							|  |  |  |             yield 0.1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop = self.new_test_loop(gen) | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(1, loop=loop) | 
					
						
							| 
									
										
										
										
											2015-08-05 13:52:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # putting a second item in the queue has to block (qsize=1) | 
					
						
							|  |  |  |         writer = loop.create_task(q.put(2)) | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |         loop.run_until_complete(asyncio.sleep(0.01)) | 
					
						
							| 
									
										
										
										
											2015-08-05 13:52:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         value1 = q.get_nowait() | 
					
						
							|  |  |  |         self.assertEqual(value1, 1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         writer.cancel() | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             loop.run_until_complete(writer) | 
					
						
							|  |  |  |         except asyncio.CancelledError: | 
					
						
							|  |  |  |             # try again | 
					
						
							|  |  |  |             writer = loop.create_task(q.put(2)) | 
					
						
							|  |  |  |             loop.run_until_complete(writer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         value2 = q.get_nowait() | 
					
						
							|  |  |  |         self.assertEqual(value2, 2) | 
					
						
							|  |  |  |         self.assertEqual(q.qsize(), 0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def test_nonblocking_put_exception(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-17 23:36:21 +02:00
										 |  |  |     def test_float_maxsize(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(maxsize=1.3, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2014-06-17 23:36:21 +02:00
										 |  |  |         q.put_nowait(1) | 
					
						
							|  |  |  |         q.put_nowait(2) | 
					
						
							|  |  |  |         self.assertTrue(q.full()) | 
					
						
							|  |  |  |         self.assertRaises(asyncio.QueueFull, q.put_nowait, 3) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(maxsize=1.3, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         async def queue_put(): | 
					
						
							|  |  |  |             await q.put(1) | 
					
						
							|  |  |  |             await q.put(2) | 
					
						
							| 
									
										
										
										
											2014-06-17 23:36:21 +02:00
										 |  |  |             self.assertTrue(q.full()) | 
					
						
							|  |  |  |         self.loop.run_until_complete(queue_put()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def test_put_cancelled(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def queue_put(): | 
					
						
							|  |  |  |             await q.put(1) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def test(): | 
					
						
							|  |  |  |             return await q.get() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t = self.loop.create_task(queue_put()) | 
					
						
							| 
									
										
										
										
											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): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=self.loop, maxsize=1) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         put_a = self.loop.create_task(q.put('a')) | 
					
						
							|  |  |  |         put_b = self.loop.create_task(q.put('b')) | 
					
						
							|  |  |  |         put_c = self.loop.create_task(q.put('X')) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							| 
									
										
										
										
											2014-06-25 23:11:21 +02:00
										 |  |  |         self.assertTrue(put_a.done()) | 
					
						
							|  |  |  |         self.assertFalse(put_b.done()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         put_c.cancel() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							| 
									
										
										
										
											2014-06-25 23:11:21 +02:00
										 |  |  |         self.assertTrue(put_c.done()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertEqual(q.get_nowait(), 'a') | 
					
						
							| 
									
										
										
										
											2015-08-05 13:52:33 -04:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							| 
									
										
										
										
											2014-06-25 23:11:21 +02:00
										 |  |  |         self.assertEqual(q.get_nowait(), 'b') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(put_b) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_put_with_waiting_getters(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = asyncio.Queue(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t = self.loop.create_task(q.get()) | 
					
						
							| 
									
										
										
										
											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') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-28 07:42:34 -07:00
										 |  |  |     def test_why_are_putters_waiting(self): | 
					
						
							|  |  |  |         # From issue #265. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             queue = asyncio.Queue(2, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2015-09-28 07:42:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def putter(item): | 
					
						
							|  |  |  |             await queue.put(item) | 
					
						
							| 
									
										
										
										
											2015-09-28 07:42:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def getter(): | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |             await asyncio.sleep(0) | 
					
						
							| 
									
										
										
										
											2015-09-28 07:42:34 -07:00
										 |  |  |             num = queue.qsize() | 
					
						
							|  |  |  |             for _ in range(num): | 
					
						
							|  |  |  |                 item = queue.get_nowait() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         t0 = putter(0) | 
					
						
							|  |  |  |         t1 = putter(1) | 
					
						
							|  |  |  |         t2 = putter(2) | 
					
						
							|  |  |  |         t3 = putter(3) | 
					
						
							|  |  |  |         self.loop.run_until_complete( | 
					
						
							|  |  |  |             asyncio.gather(getter(), t0, t1, t2, t3, loop=self.loop)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 23:45:43 +00:00
										 |  |  |     def test_cancelled_puts_not_being_held_in_self_putters(self): | 
					
						
							|  |  |  |         def a_generator(): | 
					
						
							|  |  |  |             yield 0.01 | 
					
						
							|  |  |  |             yield 0.1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop = self.new_test_loop(a_generator) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Full queue. | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             queue = asyncio.Queue(loop=loop, maxsize=1) | 
					
						
							| 
									
										
										
										
											2018-01-25 23:45:43 +00:00
										 |  |  |         queue.put_nowait(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Task waiting for space to put an item in the queue. | 
					
						
							|  |  |  |         put_task = loop.create_task(queue.put(1)) | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |         loop.run_until_complete(asyncio.sleep(0.01)) | 
					
						
							| 
									
										
										
										
											2018-01-25 23:45:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Check that the putter is correctly removed from queue._putters when | 
					
						
							|  |  |  |         # the task is canceled. | 
					
						
							|  |  |  |         self.assertEqual(len(queue._putters), 1) | 
					
						
							|  |  |  |         put_task.cancel() | 
					
						
							|  |  |  |         with self.assertRaises(asyncio.CancelledError): | 
					
						
							|  |  |  |             loop.run_until_complete(put_task) | 
					
						
							|  |  |  |         self.assertEqual(len(queue._putters), 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_cancelled_put_silence_value_error_exception(self): | 
					
						
							|  |  |  |         def gen(): | 
					
						
							|  |  |  |             yield 0.01 | 
					
						
							|  |  |  |             yield 0.1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loop = self.new_test_loop(gen) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Full Queue. | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             queue = asyncio.Queue(1, loop=loop) | 
					
						
							| 
									
										
										
										
											2018-01-25 23:45:43 +00:00
										 |  |  |         queue.put_nowait(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Task waiting for space to put a item in the queue. | 
					
						
							|  |  |  |         put_task = loop.create_task(queue.put(1)) | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |         loop.run_until_complete(asyncio.sleep(0.01)) | 
					
						
							| 
									
										
										
										
											2018-01-25 23:45:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # get_nowait() remove the future of put_task from queue._putters. | 
					
						
							|  |  |  |         queue.get_nowait() | 
					
						
							|  |  |  |         # When canceled, queue.put is going to remove its future from | 
					
						
							|  |  |  |         # self._putters but it was removed previously by queue.get_nowait(). | 
					
						
							|  |  |  |         put_task.cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # The ValueError exception triggered by queue._putters.remove(putter) | 
					
						
							|  |  |  |         # inside queue.put should be silenced. | 
					
						
							|  |  |  |         # If the ValueError is silenced we should catch a CancelledError. | 
					
						
							|  |  |  |         with self.assertRaises(asyncio.CancelledError): | 
					
						
							|  |  |  |             loop.run_until_complete(put_task) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | class LifoQueueTests(_QueueTestBase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_order(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             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): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 09:24:24 -07:00
										 |  |  | class _QueueJoinTestMixin: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     q_class = None | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_task_done_underflow(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = self.q_class(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertRaises(ValueError, q.task_done) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_task_done(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = self.q_class(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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def worker(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             nonlocal accumulator | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             while running: | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |                 item = await q.get() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 accumulator += item | 
					
						
							|  |  |  |                 q.task_done() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def test(): | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |             tasks = [self.loop.create_task(worker()) | 
					
						
							| 
									
										
										
										
											2014-06-25 23:11:21 +02:00
										 |  |  |                      for index in range(2)] | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             await q.join() | 
					
						
							| 
									
										
										
										
											2014-06-25 23:11:21 +02:00
										 |  |  |             return tasks | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-25 23:11:21 +02:00
										 |  |  |         tasks = self.loop.run_until_complete(test()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertEqual(sum(range(100)), accumulator) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # close running generators | 
					
						
							|  |  |  |         running = False | 
					
						
							| 
									
										
										
										
											2014-06-25 23:11:21 +02:00
										 |  |  |         for i in range(len(tasks)): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             q.put_nowait(0) | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |         self.loop.run_until_complete(asyncio.wait(tasks)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_join_empty_queue(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = self.q_class(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). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def join(): | 
					
						
							|  |  |  |             await q.join() | 
					
						
							|  |  |  |             await q.join() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(join()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_format(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             q = self.q_class(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') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 09:24:24 -07:00
										 |  |  | class QueueJoinTests(_QueueJoinTestMixin, _QueueTestBase): | 
					
						
							|  |  |  |     q_class = asyncio.Queue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class LifoQueueJoinTests(_QueueJoinTestMixin, _QueueTestBase): | 
					
						
							|  |  |  |     q_class = asyncio.LifoQueue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PriorityQueueJoinTests(_QueueJoinTestMixin, _QueueTestBase): | 
					
						
							|  |  |  |     q_class = asyncio.PriorityQueue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main() |