bpo-45997: Fix asyncio.Semaphore re-acquiring order (GH-31910)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
This commit is contained in:
Andrew Svetlov 2022-03-22 16:02:51 +02:00 committed by GitHub
parent 673755bfba
commit 32e77154dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 6 deletions

View file

@ -917,6 +917,31 @@ class SemaphoreTests(unittest.IsolatedAsyncioTestCase):
sem.release()
self.assertFalse(sem.locked())
async def test_acquire_fifo_order(self):
sem = asyncio.Semaphore(1)
result = []
async def coro(tag):
await sem.acquire()
result.append(f'{tag}_1')
await asyncio.sleep(0.01)
sem.release()
await sem.acquire()
result.append(f'{tag}_2')
await asyncio.sleep(0.01)
sem.release()
async with asyncio.TaskGroup() as tg:
tg.create_task(coro('c1'))
tg.create_task(coro('c2'))
tg.create_task(coro('c3'))
self.assertEqual(
['c1_1', 'c2_1', 'c3_1', 'c1_2', 'c2_2', 'c3_2'],
result
)
if __name__ == '__main__':
unittest.main()