diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-11-29 00:37:34 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-29 00:37:34 -0800 |
commit | 305236e03a274850be8ed399ea3390ee71519ef4 (patch) | |
tree | 13a7127844ffa458aebe69aa3eb6deb0471575a1 /Lib/test/test_asyncio/test_tasks.py | |
parent | 8d1a5800645575ec876932bbb9aed3aa65d18f46 (diff) | |
download | cpython-git-305236e03a274850be8ed399ea3390ee71519ef4.tar.gz |
bpo-37658: Actually return result in race condition (GH-29202)
(cherry picked from commit 934a82623793e9d52b85f74d5395d65927a52205)
Co-authored-by: Sam Bull <aa6bs0@sambull.org>
Diffstat (limited to 'Lib/test/test_asyncio/test_tasks.py')
-rw-r--r-- | Lib/test/test_asyncio/test_tasks.py | 38 |
1 files changed, 8 insertions, 30 deletions
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index ff627a6980..773d5057fe 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -1148,20 +1148,16 @@ class BaseTaskTests: self.assertEqual(res, "ok") def test_wait_for_cancellation_race_condition(self): - def gen(): - yield 0.1 - yield 0.1 - yield 0.1 - yield 0.1 + async def inner(): + with contextlib.suppress(asyncio.CancelledError): + await asyncio.sleep(1) + return 1 - loop = self.new_test_loop(gen) + async def main(): + result = await asyncio.wait_for(inner(), timeout=.01) + assert result == 1 - fut = self.new_future(loop) - loop.call_later(0.1, fut.set_result, "ok") - task = loop.create_task(asyncio.wait_for(fut, timeout=1)) - loop.call_later(0.1, task.cancel) - res = loop.run_until_complete(task) - self.assertEqual(res, "ok") + asyncio.run(main()) def test_wait_for_waits_for_task_cancellation(self): loop = asyncio.new_event_loop() @@ -1240,24 +1236,6 @@ class BaseTaskTests: with self.assertRaises(FooException): loop.run_until_complete(foo()) - def test_wait_for_raises_timeout_error_if_returned_during_cancellation(self): - loop = asyncio.new_event_loop() - self.addCleanup(loop.close) - - async def foo(): - async def inner(): - try: - await asyncio.sleep(0.2) - except asyncio.CancelledError: - return 42 - - inner_task = self.new_task(loop, inner()) - - await asyncio.wait_for(inner_task, timeout=_EPSILON) - - with self.assertRaises(asyncio.TimeoutError): - loop.run_until_complete(foo()) - def test_wait_for_self_cancellation(self): loop = asyncio.new_event_loop() self.addCleanup(loop.close) |