| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | """Tests for lock.py""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2014-02-26 10:25:02 +01:00
										 |  |  | from unittest import mock | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  | STR_RGX_REPR = ( | 
					
						
							|  |  |  |     r'^<(?P<class>.*?) object at (?P<address>.*?)' | 
					
						
							|  |  |  |     r'\[(?P<extras>' | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |     r'(set|unset|locked|unlocked)(, value:\d)?(, waiters:\d+)?' | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |     r')\]>\Z' | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | RGX_REPR = re.compile(STR_RGX_REPR) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 LockTests(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
										 |  |  | 
 | 
					
						
							|  |  |  |     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): | 
					
						
							|  |  |  |             lock = asyncio.Lock(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertIs(lock._loop, loop) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             lock = asyncio.Lock(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertIs(lock._loop, self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_ctor_noloop(self): | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         asyncio.set_event_loop(self.loop) | 
					
						
							|  |  |  |         lock = asyncio.Lock() | 
					
						
							|  |  |  |         self.assertIs(lock._loop, self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_repr(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             lock = asyncio.Lock(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertTrue(repr(lock).endswith('[unlocked]>')) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.assertTrue(RGX_REPR.match(repr(lock))) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 13:12:52 +02:00
										 |  |  |         self.loop.run_until_complete(lock.acquire()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertTrue(repr(lock).endswith('[locked]>')) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.assertTrue(RGX_REPR.match(repr(lock))) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_lock(self): | 
					
						
							| 
									
										
										
										
											2019-05-16 17:52:10 +03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |             lock = asyncio.Lock(loop=self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 17:52:10 +03:00
										 |  |  |             @asyncio.coroutine | 
					
						
							|  |  |  |             def acquire_lock(): | 
					
						
							| 
									
										
										
										
											2020-02-01 13:12:52 +02:00
										 |  |  |                 return (yield from lock) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 13:12:52 +02:00
										 |  |  |         with self.assertRaisesRegex( | 
					
						
							|  |  |  |             TypeError, | 
					
						
							|  |  |  |             "object is not iterable" | 
					
						
							|  |  |  |         ): | 
					
						
							|  |  |  |             self.loop.run_until_complete(acquire_lock()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertFalse(lock.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 20:00:05 +02:00
										 |  |  |     def test_lock_by_with_statement(self): | 
					
						
							|  |  |  |         loop = asyncio.new_event_loop()  # don't use TestLoop quirks | 
					
						
							|  |  |  |         self.set_event_loop(loop) | 
					
						
							| 
									
										
										
										
											2019-05-16 17:52:10 +03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |             primitives = [ | 
					
						
							|  |  |  |                 asyncio.Lock(loop=loop), | 
					
						
							|  |  |  |                 asyncio.Condition(loop=loop), | 
					
						
							|  |  |  |                 asyncio.Semaphore(loop=loop), | 
					
						
							|  |  |  |                 asyncio.BoundedSemaphore(loop=loop), | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 17:52:10 +03:00
										 |  |  |             @asyncio.coroutine | 
					
						
							|  |  |  |             def test(lock): | 
					
						
							|  |  |  |                 yield from asyncio.sleep(0.01) | 
					
						
							| 
									
										
										
										
											2017-12-09 20:00:05 +02:00
										 |  |  |                 self.assertFalse(lock.locked()) | 
					
						
							| 
									
										
										
										
											2020-02-01 13:12:52 +02:00
										 |  |  |                 with self.assertRaisesRegex( | 
					
						
							|  |  |  |                     TypeError, | 
					
						
							|  |  |  |                     "object is not iterable" | 
					
						
							|  |  |  |                 ): | 
					
						
							|  |  |  |                     with (yield from lock): | 
					
						
							|  |  |  |                         pass | 
					
						
							|  |  |  |                 self.assertFalse(lock.locked()) | 
					
						
							| 
									
										
										
										
											2017-12-09 20:00:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for primitive in primitives: | 
					
						
							|  |  |  |             loop.run_until_complete(test(primitive)) | 
					
						
							|  |  |  |             self.assertFalse(primitive.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def test_acquire(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             lock = asyncio.Lock(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         result = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(self.loop.run_until_complete(lock.acquire())) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c1(result): | 
					
						
							|  |  |  |             if await lock.acquire(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(1) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c2(result): | 
					
						
							|  |  |  |             if await lock.acquire(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(2) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c3(result): | 
					
						
							|  |  |  |             if await lock.acquire(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(3) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t1 = self.loop.create_task(c1(result)) | 
					
						
							|  |  |  |         t2 = self.loop.create_task(c2(result)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         lock.release() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1], result) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t3 = self.loop.create_task(c3(result)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         lock.release() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1, 2], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         lock.release() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1, 2, 3], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(t1.done()) | 
					
						
							|  |  |  |         self.assertTrue(t1.result()) | 
					
						
							|  |  |  |         self.assertTrue(t2.done()) | 
					
						
							|  |  |  |         self.assertTrue(t2.result()) | 
					
						
							|  |  |  |         self.assertTrue(t3.done()) | 
					
						
							|  |  |  |         self.assertTrue(t3.result()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_acquire_cancel(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             lock = asyncio.Lock(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertTrue(self.loop.run_until_complete(lock.acquire())) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         task = self.loop.create_task(lock.acquire()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.call_soon(task.cancel) | 
					
						
							|  |  |  |         self.assertRaises( | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             asyncio.CancelledError, | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.loop.run_until_complete, task) | 
					
						
							|  |  |  |         self.assertFalse(lock._waiters) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_cancel_race(self): | 
					
						
							|  |  |  |         # Several tasks: | 
					
						
							|  |  |  |         # - A acquires the lock | 
					
						
							| 
									
										
										
										
											2016-09-07 12:03:06 +00:00
										 |  |  |         # - B is blocked in acquire() | 
					
						
							|  |  |  |         # - C is blocked in acquire() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         # | 
					
						
							|  |  |  |         # Now, concurrently: | 
					
						
							|  |  |  |         # - B is cancelled | 
					
						
							|  |  |  |         # - A releases the lock | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # If B's waiter is marked cancelled but not yet removed from | 
					
						
							|  |  |  |         # _waiters, A's release() call will crash when trying to set | 
					
						
							|  |  |  |         # B's waiter; instead, it should move on to C's waiter. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Setup: A has the lock, b and c are waiting. | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             lock = asyncio.Lock(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def lockit(name, blocker): | 
					
						
							|  |  |  |             await lock.acquire() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 if blocker is not None: | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |                     await blocker | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             finally: | 
					
						
							|  |  |  |                 lock.release() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         fa = self.loop.create_future() | 
					
						
							|  |  |  |         ta = self.loop.create_task(lockit('A', fa)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertTrue(lock.locked()) | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         tb = self.loop.create_task(lockit('B', None)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual(len(lock._waiters), 1) | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         tc = self.loop.create_task(lockit('C', None)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual(len(lock._waiters), 2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Create the race and check. | 
					
						
							|  |  |  |         # Without the fix this failed at the last assert. | 
					
						
							|  |  |  |         fa.set_result(None) | 
					
						
							|  |  |  |         tb.cancel() | 
					
						
							|  |  |  |         self.assertTrue(lock._waiters[0].cancelled()) | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertFalse(lock.locked()) | 
					
						
							|  |  |  |         self.assertTrue(ta.done()) | 
					
						
							|  |  |  |         self.assertTrue(tb.cancelled()) | 
					
						
							|  |  |  |         self.assertTrue(tc.done()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-03 00:04:00 +02:00
										 |  |  |     def test_cancel_release_race(self): | 
					
						
							|  |  |  |         # Issue 32734 | 
					
						
							|  |  |  |         # Acquire 4 locks, cancel second, release first | 
					
						
							|  |  |  |         # and 2 locks are taken at once. | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             lock = asyncio.Lock(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2018-02-03 00:04:00 +02:00
										 |  |  |         lock_count = 0 | 
					
						
							|  |  |  |         call_count = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         async def lockit(): | 
					
						
							|  |  |  |             nonlocal lock_count | 
					
						
							|  |  |  |             nonlocal call_count | 
					
						
							|  |  |  |             call_count += 1 | 
					
						
							|  |  |  |             await lock.acquire() | 
					
						
							|  |  |  |             lock_count += 1 | 
					
						
							| 
									
										
										
										
											2018-02-14 11:18:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-03 00:04:00 +02:00
										 |  |  |         async def lockandtrigger(): | 
					
						
							|  |  |  |             await lock.acquire() | 
					
						
							|  |  |  |             self.loop.call_soon(trigger) | 
					
						
							| 
									
										
										
										
											2018-02-14 11:18:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-03 00:04:00 +02:00
										 |  |  |         def trigger(): | 
					
						
							|  |  |  |             t1.cancel() | 
					
						
							|  |  |  |             lock.release() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         t0 = self.loop.create_task(lockandtrigger()) | 
					
						
							|  |  |  |         t1 = self.loop.create_task(lockit()) | 
					
						
							|  |  |  |         t2 = self.loop.create_task(lockit()) | 
					
						
							|  |  |  |         t3 = self.loop.create_task(lockit()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # First loop acquires all | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertTrue(t0.done()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Second loop calls trigger | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         # Third loop calls cancellation | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Make sure only one lock was taken | 
					
						
							|  |  |  |         self.assertEqual(lock_count, 1) | 
					
						
							|  |  |  |         # While 3 calls were made to lockit() | 
					
						
							|  |  |  |         self.assertEqual(call_count, 3) | 
					
						
							|  |  |  |         self.assertTrue(t1.cancelled() and t2.done()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Cleanup the task that is stuck on acquire. | 
					
						
							|  |  |  |         t3.cancel() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertTrue(t3.cancelled()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 22:17:40 +02:00
										 |  |  |     def test_finished_waiter_cancelled(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             lock = asyncio.Lock(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2017-06-09 22:17:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         ta = self.loop.create_task(lock.acquire()) | 
					
						
							| 
									
										
										
										
											2017-06-09 22:17:40 +02:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertTrue(lock.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         tb = self.loop.create_task(lock.acquire()) | 
					
						
							| 
									
										
										
										
											2017-06-09 22:17:40 +02:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual(len(lock._waiters), 1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Create a second waiter, wake up the first, and cancel it. | 
					
						
							|  |  |  |         # Without the fix, the second was not woken up. | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         tc = self.loop.create_task(lock.acquire()) | 
					
						
							| 
									
										
										
										
											2017-06-09 22:17:40 +02:00
										 |  |  |         lock.release() | 
					
						
							|  |  |  |         tb.cancel() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(lock.locked()) | 
					
						
							|  |  |  |         self.assertTrue(ta.done()) | 
					
						
							|  |  |  |         self.assertTrue(tb.cancelled()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def test_release_not_acquired(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             lock = asyncio.Lock(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertRaises(RuntimeError, lock.release) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_release_no_waiters(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             lock = asyncio.Lock(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.run_until_complete(lock.acquire()) | 
					
						
							|  |  |  |         self.assertTrue(lock.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         lock.release() | 
					
						
							|  |  |  |         self.assertFalse(lock.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_context_manager(self): | 
					
						
							| 
									
										
										
										
											2020-02-01 13:12:52 +02:00
										 |  |  |         async def f(): | 
					
						
							|  |  |  |             lock = asyncio.Lock() | 
					
						
							|  |  |  |             self.assertFalse(lock.locked()) | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 13:12:52 +02:00
										 |  |  |             async with lock: | 
					
						
							|  |  |  |                 self.assertTrue(lock.locked()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 13:12:52 +02:00
										 |  |  |             self.assertFalse(lock.locked()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 13:12:52 +02:00
										 |  |  |         self.loop.run_until_complete(f()) | 
					
						
							| 
									
										
										
										
											2014-01-25 16:51:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  | class EventTests(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
										 |  |  | 
 | 
					
						
							|  |  |  |     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): | 
					
						
							|  |  |  |             ev = asyncio.Event(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertIs(ev._loop, loop) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             ev = asyncio.Event(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertIs(ev._loop, self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_ctor_noloop(self): | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         asyncio.set_event_loop(self.loop) | 
					
						
							|  |  |  |         ev = asyncio.Event() | 
					
						
							|  |  |  |         self.assertIs(ev._loop, self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_repr(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             ev = asyncio.Event(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertTrue(repr(ev).endswith('[unset]>')) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         match = RGX_REPR.match(repr(ev)) | 
					
						
							|  |  |  |         self.assertEqual(match.group('extras'), 'unset') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ev.set() | 
					
						
							|  |  |  |         self.assertTrue(repr(ev).endswith('[set]>')) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.assertTrue(RGX_REPR.match(repr(ev))) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 10:25:02 +01:00
										 |  |  |         ev._waiters.append(mock.Mock()) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.assertTrue('waiters:1' in repr(ev)) | 
					
						
							|  |  |  |         self.assertTrue(RGX_REPR.match(repr(ev))) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_wait(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             ev = asyncio.Event(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertFalse(ev.is_set()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         result = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c1(result): | 
					
						
							|  |  |  |             if await ev.wait(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c2(result): | 
					
						
							|  |  |  |             if await ev.wait(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(2) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c3(result): | 
					
						
							|  |  |  |             if await ev.wait(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(3) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t1 = self.loop.create_task(c1(result)) | 
					
						
							|  |  |  |         t2 = self.loop.create_task(c2(result)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([], result) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t3 = self.loop.create_task(c3(result)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ev.set() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([3, 1, 2], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(t1.done()) | 
					
						
							|  |  |  |         self.assertIsNone(t1.result()) | 
					
						
							|  |  |  |         self.assertTrue(t2.done()) | 
					
						
							|  |  |  |         self.assertIsNone(t2.result()) | 
					
						
							|  |  |  |         self.assertTrue(t3.done()) | 
					
						
							|  |  |  |         self.assertIsNone(t3.result()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_wait_on_set(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             ev = asyncio.Event(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         ev.set() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res = self.loop.run_until_complete(ev.wait()) | 
					
						
							|  |  |  |         self.assertTrue(res) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_wait_cancel(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             ev = asyncio.Event(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         wait = self.loop.create_task(ev.wait()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.call_soon(wait.cancel) | 
					
						
							|  |  |  |         self.assertRaises( | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             asyncio.CancelledError, | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.loop.run_until_complete, wait) | 
					
						
							|  |  |  |         self.assertFalse(ev._waiters) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_clear(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             ev = asyncio.Event(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertFalse(ev.is_set()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ev.set() | 
					
						
							|  |  |  |         self.assertTrue(ev.is_set()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ev.clear() | 
					
						
							|  |  |  |         self.assertFalse(ev.is_set()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_clear_with_waiters(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             ev = asyncio.Event(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         result = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c1(result): | 
					
						
							|  |  |  |             if await ev.wait(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(1) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t = self.loop.create_task(c1(result)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ev.set() | 
					
						
							|  |  |  |         ev.clear() | 
					
						
							|  |  |  |         self.assertFalse(ev.is_set()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ev.set() | 
					
						
							|  |  |  |         ev.set() | 
					
						
							|  |  |  |         self.assertEqual(1, len(ev._waiters)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1], result) | 
					
						
							|  |  |  |         self.assertEqual(0, len(ev._waiters)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(t.done()) | 
					
						
							|  |  |  |         self.assertTrue(t.result()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  | class ConditionTests(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
										 |  |  | 
 | 
					
						
							|  |  |  |     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): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=loop) | 
					
						
							|  |  |  |             self.assertIs(cond._loop, loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							|  |  |  |             self.assertIs(cond._loop, self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_ctor_noloop(self): | 
					
						
							| 
									
										
										
										
											2019-09-11 11:20:24 +03:00
										 |  |  |         asyncio.set_event_loop(self.loop) | 
					
						
							|  |  |  |         cond = asyncio.Condition() | 
					
						
							|  |  |  |         self.assertIs(cond._loop, self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_wait(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         result = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c1(result): | 
					
						
							|  |  |  |             await cond.acquire() | 
					
						
							|  |  |  |             if await cond.wait(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(1) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c2(result): | 
					
						
							|  |  |  |             await cond.acquire() | 
					
						
							|  |  |  |             if await cond.wait(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(2) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c3(result): | 
					
						
							|  |  |  |             await cond.acquire() | 
					
						
							|  |  |  |             if await cond.wait(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(3) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t1 = self.loop.create_task(c1(result)) | 
					
						
							|  |  |  |         t2 = self.loop.create_task(c2(result)) | 
					
						
							|  |  |  |         t3 = self.loop.create_task(c3(result)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([], result) | 
					
						
							|  |  |  |         self.assertFalse(cond.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(self.loop.run_until_complete(cond.acquire())) | 
					
						
							|  |  |  |         cond.notify() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([], result) | 
					
						
							|  |  |  |         self.assertTrue(cond.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cond.release() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1], result) | 
					
						
							|  |  |  |         self.assertTrue(cond.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cond.notify(2) | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1], result) | 
					
						
							|  |  |  |         self.assertTrue(cond.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cond.release() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1, 2], result) | 
					
						
							|  |  |  |         self.assertTrue(cond.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cond.release() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1, 2, 3], result) | 
					
						
							|  |  |  |         self.assertTrue(cond.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(t1.done()) | 
					
						
							|  |  |  |         self.assertTrue(t1.result()) | 
					
						
							|  |  |  |         self.assertTrue(t2.done()) | 
					
						
							|  |  |  |         self.assertTrue(t2.result()) | 
					
						
							|  |  |  |         self.assertTrue(t3.done()) | 
					
						
							|  |  |  |         self.assertTrue(t3.result()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_wait_cancel(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.run_until_complete(cond.acquire()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         wait = self.loop.create_task(cond.wait()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.call_soon(wait.cancel) | 
					
						
							|  |  |  |         self.assertRaises( | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             asyncio.CancelledError, | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.loop.run_until_complete, wait) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.assertFalse(cond._waiters) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertTrue(cond.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-11 12:00:07 -04:00
										 |  |  |     def test_wait_cancel_contested(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2016-06-11 12:00:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(cond.acquire()) | 
					
						
							|  |  |  |         self.assertTrue(cond.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         wait_task = self.loop.create_task(cond.wait()) | 
					
						
							| 
									
										
										
										
											2016-06-11 12:00:07 -04:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertFalse(cond.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Notify, but contest the lock before cancelling | 
					
						
							|  |  |  |         self.loop.run_until_complete(cond.acquire()) | 
					
						
							|  |  |  |         self.assertTrue(cond.locked()) | 
					
						
							|  |  |  |         cond.notify() | 
					
						
							|  |  |  |         self.loop.call_soon(wait_task.cancel) | 
					
						
							|  |  |  |         self.loop.call_soon(cond.release) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             self.loop.run_until_complete(wait_task) | 
					
						
							|  |  |  |         except asyncio.CancelledError: | 
					
						
							|  |  |  |             # Should not happen, since no cancellation points | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(cond.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 11:18:11 +02:00
										 |  |  |     def test_wait_cancel_after_notify(self): | 
					
						
							|  |  |  |         # See bpo-32841 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2018-02-14 11:18:11 +02:00
										 |  |  |         waited = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         async def wait_on_cond(): | 
					
						
							|  |  |  |             nonlocal waited | 
					
						
							|  |  |  |             async with cond: | 
					
						
							|  |  |  |                 waited = True  # Make sure this area was reached | 
					
						
							|  |  |  |                 await cond.wait() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         waiter = asyncio.ensure_future(wait_on_cond(), loop=self.loop) | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop)  # Start waiting | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(cond.acquire()) | 
					
						
							|  |  |  |         cond.notify() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop)  # Get to acquire() | 
					
						
							|  |  |  |         waiter.cancel() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop)  # Activate cancellation | 
					
						
							|  |  |  |         cond.release() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop)  # Cancellation should occur | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(waiter.cancelled()) | 
					
						
							|  |  |  |         self.assertTrue(waited) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def test_wait_unacquired(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertRaises( | 
					
						
							|  |  |  |             RuntimeError, | 
					
						
							|  |  |  |             self.loop.run_until_complete, cond.wait()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_wait_for(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         presult = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def predicate(): | 
					
						
							|  |  |  |             return presult | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         result = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c1(result): | 
					
						
							|  |  |  |             await cond.acquire() | 
					
						
							|  |  |  |             if await cond.wait_for(predicate): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(1) | 
					
						
							|  |  |  |                 cond.release() | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t = self.loop.create_task(c1(result)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(cond.acquire()) | 
					
						
							|  |  |  |         cond.notify() | 
					
						
							|  |  |  |         cond.release() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         presult = True | 
					
						
							|  |  |  |         self.loop.run_until_complete(cond.acquire()) | 
					
						
							|  |  |  |         cond.notify() | 
					
						
							|  |  |  |         cond.release() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(t.done()) | 
					
						
							|  |  |  |         self.assertTrue(t.result()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_wait_for_unacquired(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # predicate can return true immediately | 
					
						
							|  |  |  |         res = self.loop.run_until_complete(cond.wait_for(lambda: [1, 2, 3])) | 
					
						
							|  |  |  |         self.assertEqual([1, 2, 3], res) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertRaises( | 
					
						
							|  |  |  |             RuntimeError, | 
					
						
							|  |  |  |             self.loop.run_until_complete, | 
					
						
							|  |  |  |             cond.wait_for(lambda: False)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_notify(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         result = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c1(result): | 
					
						
							|  |  |  |             await cond.acquire() | 
					
						
							|  |  |  |             if await cond.wait(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(1) | 
					
						
							|  |  |  |                 cond.release() | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c2(result): | 
					
						
							|  |  |  |             await cond.acquire() | 
					
						
							|  |  |  |             if await cond.wait(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(2) | 
					
						
							|  |  |  |                 cond.release() | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c3(result): | 
					
						
							|  |  |  |             await cond.acquire() | 
					
						
							|  |  |  |             if await cond.wait(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(3) | 
					
						
							|  |  |  |                 cond.release() | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t1 = self.loop.create_task(c1(result)) | 
					
						
							|  |  |  |         t2 = self.loop.create_task(c2(result)) | 
					
						
							|  |  |  |         t3 = self.loop.create_task(c3(result)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(cond.acquire()) | 
					
						
							|  |  |  |         cond.notify(1) | 
					
						
							|  |  |  |         cond.release() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(cond.acquire()) | 
					
						
							|  |  |  |         cond.notify(1) | 
					
						
							|  |  |  |         cond.notify(2048) | 
					
						
							|  |  |  |         cond.release() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1, 2, 3], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(t1.done()) | 
					
						
							|  |  |  |         self.assertTrue(t1.result()) | 
					
						
							|  |  |  |         self.assertTrue(t2.done()) | 
					
						
							|  |  |  |         self.assertTrue(t2.result()) | 
					
						
							|  |  |  |         self.assertTrue(t3.done()) | 
					
						
							|  |  |  |         self.assertTrue(t3.result()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_notify_all(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         result = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c1(result): | 
					
						
							|  |  |  |             await cond.acquire() | 
					
						
							|  |  |  |             if await cond.wait(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(1) | 
					
						
							|  |  |  |                 cond.release() | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c2(result): | 
					
						
							|  |  |  |             await cond.acquire() | 
					
						
							|  |  |  |             if await cond.wait(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 result.append(2) | 
					
						
							|  |  |  |                 cond.release() | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t1 = self.loop.create_task(c1(result)) | 
					
						
							|  |  |  |         t2 = self.loop.create_task(c2(result)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(cond.acquire()) | 
					
						
							|  |  |  |         cond.notify_all() | 
					
						
							|  |  |  |         cond.release() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1, 2], result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(t1.done()) | 
					
						
							|  |  |  |         self.assertTrue(t1.result()) | 
					
						
							|  |  |  |         self.assertTrue(t2.done()) | 
					
						
							|  |  |  |         self.assertTrue(t2.result()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_notify_unacquired(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertRaises(RuntimeError, cond.notify) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_notify_all_unacquired(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertRaises(RuntimeError, cond.notify_all) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |     def test_repr(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             cond = asyncio.Condition(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.assertTrue('unlocked' in repr(cond)) | 
					
						
							|  |  |  |         self.assertTrue(RGX_REPR.match(repr(cond))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(cond.acquire()) | 
					
						
							|  |  |  |         self.assertTrue('locked' in repr(cond)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 10:25:02 +01:00
										 |  |  |         cond._waiters.append(mock.Mock()) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.assertTrue('waiters:1' in repr(cond)) | 
					
						
							|  |  |  |         self.assertTrue(RGX_REPR.match(repr(cond))) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 10:25:02 +01:00
										 |  |  |         cond._waiters.append(mock.Mock()) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.assertTrue('waiters:2' in repr(cond)) | 
					
						
							|  |  |  |         self.assertTrue(RGX_REPR.match(repr(cond))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_context_manager(self): | 
					
						
							| 
									
										
										
										
											2020-02-01 13:12:52 +02:00
										 |  |  |         async def f(): | 
					
						
							|  |  |  |             cond = asyncio.Condition() | 
					
						
							|  |  |  |             self.assertFalse(cond.locked()) | 
					
						
							|  |  |  |             async with cond: | 
					
						
							|  |  |  |                 self.assertTrue(cond.locked()) | 
					
						
							|  |  |  |             self.assertFalse(cond.locked()) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 13:12:52 +02:00
										 |  |  |         self.loop.run_until_complete(f()) | 
					
						
							| 
									
										
										
										
											2014-01-25 16:51:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-26 17:54:34 +03:00
										 |  |  |     def test_explicit_lock(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             lock = asyncio.Lock(loop=self.loop) | 
					
						
							|  |  |  |             cond = asyncio.Condition(lock, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2014-07-26 17:54:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-29 12:58:23 +02:00
										 |  |  |         self.assertIs(cond._lock, lock) | 
					
						
							|  |  |  |         self.assertIs(cond._loop, lock._loop) | 
					
						
							| 
									
										
										
										
											2014-07-26 17:54:34 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_ambiguous_loops(self): | 
					
						
							|  |  |  |         loop = self.new_test_loop() | 
					
						
							| 
									
										
										
										
											2014-07-29 12:58:23 +02:00
										 |  |  |         self.addCleanup(loop.close) | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             lock = asyncio.Lock(loop=self.loop) | 
					
						
							|  |  |  |             with self.assertRaises(ValueError): | 
					
						
							|  |  |  |                 asyncio.Condition(lock, loop=loop) | 
					
						
							| 
									
										
										
										
											2014-07-26 17:54:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-29 17:31:01 -04:00
										 |  |  |     def test_timeout_in_block(self): | 
					
						
							|  |  |  |         loop = asyncio.new_event_loop() | 
					
						
							|  |  |  |         self.addCleanup(loop.close) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         async def task_timeout(): | 
					
						
							|  |  |  |             condition = asyncio.Condition(loop=loop) | 
					
						
							|  |  |  |             async with condition: | 
					
						
							|  |  |  |                 with self.assertRaises(asyncio.TimeoutError): | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |                     await asyncio.wait_for(condition.wait(), timeout=0.5) | 
					
						
							| 
									
										
										
										
											2018-05-29 17:31:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             loop.run_until_complete(task_timeout()) | 
					
						
							| 
									
										
										
										
											2018-05-29 17:31:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  | class SemaphoreTests(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
										 |  |  | 
 | 
					
						
							|  |  |  |     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): | 
					
						
							|  |  |  |             sem = asyncio.Semaphore(loop=loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertIs(sem._loop, loop) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             sem = asyncio.Semaphore(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertIs(sem._loop, self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_ctor_noloop(self): | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         asyncio.set_event_loop(self.loop) | 
					
						
							|  |  |  |         sem = asyncio.Semaphore() | 
					
						
							|  |  |  |         self.assertIs(sem._loop, self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-21 11:07:45 -08:00
										 |  |  |     def test_initial_value_zero(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             sem = asyncio.Semaphore(0, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-11-21 11:07:45 -08:00
										 |  |  |         self.assertTrue(sem.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def test_repr(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             sem = asyncio.Semaphore(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |         self.assertTrue(repr(sem).endswith('[unlocked, value:1]>')) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.assertTrue(RGX_REPR.match(repr(sem))) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(sem.acquire()) | 
					
						
							|  |  |  |         self.assertTrue(repr(sem).endswith('[locked]>')) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.assertTrue('waiters' not in repr(sem)) | 
					
						
							|  |  |  |         self.assertTrue(RGX_REPR.match(repr(sem))) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 10:25:02 +01:00
										 |  |  |         sem._waiters.append(mock.Mock()) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.assertTrue('waiters:1' in repr(sem)) | 
					
						
							|  |  |  |         self.assertTrue(RGX_REPR.match(repr(sem))) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 10:25:02 +01:00
										 |  |  |         sem._waiters.append(mock.Mock()) | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.assertTrue('waiters:2' in repr(sem)) | 
					
						
							|  |  |  |         self.assertTrue(RGX_REPR.match(repr(sem))) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_semaphore(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             sem = asyncio.Semaphore(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertEqual(1, sem._value) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 17:52:10 +03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             @asyncio.coroutine | 
					
						
							|  |  |  |             def acquire_lock(): | 
					
						
							| 
									
										
										
										
											2020-02-01 13:12:52 +02:00
										 |  |  |                 return (yield from sem) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 13:12:52 +02:00
										 |  |  |         with self.assertRaisesRegex( | 
					
						
							|  |  |  |             TypeError, | 
					
						
							|  |  |  |             "'Semaphore' object is not iterable", | 
					
						
							|  |  |  |         ): | 
					
						
							|  |  |  |             self.loop.run_until_complete(acquire_lock()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertFalse(sem.locked()) | 
					
						
							|  |  |  |         self.assertEqual(1, sem._value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_semaphore_value(self): | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         self.assertRaises(ValueError, asyncio.Semaphore, -1) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_acquire(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             sem = asyncio.Semaphore(3, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         result = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(self.loop.run_until_complete(sem.acquire())) | 
					
						
							|  |  |  |         self.assertTrue(self.loop.run_until_complete(sem.acquire())) | 
					
						
							|  |  |  |         self.assertFalse(sem.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c1(result): | 
					
						
							|  |  |  |             await sem.acquire() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             result.append(1) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c2(result): | 
					
						
							|  |  |  |             await sem.acquire() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             result.append(2) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c3(result): | 
					
						
							|  |  |  |             await sem.acquire() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             result.append(3) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def c4(result): | 
					
						
							|  |  |  |             await sem.acquire() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             result.append(4) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t1 = self.loop.create_task(c1(result)) | 
					
						
							|  |  |  |         t2 = self.loop.create_task(c2(result)) | 
					
						
							|  |  |  |         t3 = self.loop.create_task(c3(result)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual([1], result) | 
					
						
							|  |  |  |         self.assertTrue(sem.locked()) | 
					
						
							|  |  |  |         self.assertEqual(2, len(sem._waiters)) | 
					
						
							|  |  |  |         self.assertEqual(0, sem._value) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t4 = self.loop.create_task(c4(result)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         sem.release() | 
					
						
							|  |  |  |         sem.release() | 
					
						
							|  |  |  |         self.assertEqual(2, sem._value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertEqual(0, sem._value) | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  |         self.assertEqual(3, len(result)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertTrue(sem.locked()) | 
					
						
							|  |  |  |         self.assertEqual(1, len(sem._waiters)) | 
					
						
							|  |  |  |         self.assertEqual(0, sem._value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertTrue(t1.done()) | 
					
						
							|  |  |  |         self.assertTrue(t1.result()) | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  |         race_tasks = [t2, t3, t4] | 
					
						
							|  |  |  |         done_tasks = [t for t in race_tasks if t.done() and t.result()] | 
					
						
							|  |  |  |         self.assertTrue(2, len(done_tasks)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # cleanup locked semaphore | 
					
						
							|  |  |  |         sem.release() | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  |         self.loop.run_until_complete(asyncio.gather(*race_tasks)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_acquire_cancel(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             sem = asyncio.Semaphore(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.run_until_complete(sem.acquire()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         acquire = self.loop.create_task(sem.acquire()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.call_soon(acquire.cancel) | 
					
						
							|  |  |  |         self.assertRaises( | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             asyncio.CancelledError, | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.loop.run_until_complete, acquire) | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  |         self.assertTrue((not sem._waiters) or | 
					
						
							|  |  |  |                         all(waiter.done() for waiter in sem._waiters)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_acquire_cancel_before_awoken(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             sem = asyncio.Semaphore(value=0, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t1 = self.loop.create_task(sem.acquire()) | 
					
						
							|  |  |  |         t2 = self.loop.create_task(sem.acquire()) | 
					
						
							|  |  |  |         t3 = self.loop.create_task(sem.acquire()) | 
					
						
							|  |  |  |         t4 = self.loop.create_task(sem.acquire()) | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         sem.release() | 
					
						
							|  |  |  |         t1.cancel() | 
					
						
							|  |  |  |         t2.cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         num_done = sum(t.done() for t in [t3, t4]) | 
					
						
							|  |  |  |         self.assertEqual(num_done, 1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         t3.cancel() | 
					
						
							|  |  |  |         t4.cancel() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_acquire_hang(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             sem = asyncio.Semaphore(value=0, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t1 = self.loop.create_task(sem.acquire()) | 
					
						
							|  |  |  |         t2 = self.loop.create_task(sem.acquire()) | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         sem.release() | 
					
						
							|  |  |  |         t1.cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertTrue(sem.locked()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_release_not_acquired(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             sem = asyncio.BoundedSemaphore(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertRaises(ValueError, sem.release) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_release_no_waiters(self): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         with self.assertWarns(DeprecationWarning): | 
					
						
							|  |  |  |             sem = asyncio.Semaphore(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.run_until_complete(sem.acquire()) | 
					
						
							|  |  |  |         self.assertTrue(sem.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         sem.release() | 
					
						
							|  |  |  |         self.assertFalse(sem.locked()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main() |