-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3.12] gh-124309: Modernize the
staggered_race
implementation to su…
…pport e… (#124574) gh-124309: Modernize the `staggered_race` implementation to support eager task factories (#124390) Co-authored-by: Thomas Grainger <tagrain@gmail.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Carol Willing <carolcode@willingconsulting.com> Co-authored-by: Kumar Aditya <kumaraditya@python.org> (cherry picked from commit de929f3) Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
- Loading branch information
1 parent
48359c5
commit 2b54a4e
Showing
5 changed files
with
193 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import asyncio | ||
import unittest | ||
from asyncio.staggered import staggered_race | ||
|
||
from test import support | ||
|
||
support.requires_working_socket(module=True) | ||
|
||
|
||
def tearDownModule(): | ||
asyncio.set_event_loop_policy(None) | ||
|
||
|
||
class StaggeredTests(unittest.IsolatedAsyncioTestCase): | ||
async def test_empty(self): | ||
winner, index, excs = await staggered_race( | ||
[], | ||
delay=None, | ||
) | ||
|
||
self.assertIs(winner, None) | ||
self.assertIs(index, None) | ||
self.assertEqual(excs, []) | ||
|
||
async def test_one_successful(self): | ||
async def coro(index): | ||
return f'Res: {index}' | ||
|
||
winner, index, excs = await staggered_race( | ||
[ | ||
lambda: coro(0), | ||
lambda: coro(1), | ||
], | ||
delay=None, | ||
) | ||
|
||
self.assertEqual(winner, 'Res: 0') | ||
self.assertEqual(index, 0) | ||
self.assertEqual(excs, [None]) | ||
|
||
async def test_first_error_second_successful(self): | ||
async def coro(index): | ||
if index == 0: | ||
raise ValueError(index) | ||
return f'Res: {index}' | ||
|
||
winner, index, excs = await staggered_race( | ||
[ | ||
lambda: coro(0), | ||
lambda: coro(1), | ||
], | ||
delay=None, | ||
) | ||
|
||
self.assertEqual(winner, 'Res: 1') | ||
self.assertEqual(index, 1) | ||
self.assertEqual(len(excs), 2) | ||
self.assertIsInstance(excs[0], ValueError) | ||
self.assertIs(excs[1], None) | ||
|
||
async def test_first_timeout_second_successful(self): | ||
async def coro(index): | ||
if index == 0: | ||
await asyncio.sleep(10) # much bigger than delay | ||
return f'Res: {index}' | ||
|
||
winner, index, excs = await staggered_race( | ||
[ | ||
lambda: coro(0), | ||
lambda: coro(1), | ||
], | ||
delay=0.1, | ||
) | ||
|
||
self.assertEqual(winner, 'Res: 1') | ||
self.assertEqual(index, 1) | ||
self.assertEqual(len(excs), 2) | ||
self.assertIsInstance(excs[0], asyncio.CancelledError) | ||
self.assertIs(excs[1], None) | ||
|
||
async def test_none_successful(self): | ||
async def coro(index): | ||
raise ValueError(index) | ||
|
||
for delay in [None, 0, 0.1, 1]: | ||
with self.subTest(delay=delay): | ||
winner, index, excs = await staggered_race( | ||
[ | ||
lambda: coro(0), | ||
lambda: coro(1), | ||
], | ||
delay=delay, | ||
) | ||
|
||
self.assertIs(winner, None) | ||
self.assertIs(index, None) | ||
self.assertEqual(len(excs), 2) | ||
self.assertIsInstance(excs[0], ValueError) | ||
self.assertIsInstance(excs[1], ValueError) | ||
|
||
async def test_long_delay_early_failure(self): | ||
async def coro(index): | ||
await asyncio.sleep(0) # Dummy coroutine for the 1 case | ||
if index == 0: | ||
await asyncio.sleep(0.1) # Dummy coroutine | ||
raise ValueError(index) | ||
|
||
return f'Res: {index}' | ||
|
||
winner, index, excs = await staggered_race( | ||
[ | ||
lambda: coro(0), | ||
lambda: coro(1), | ||
], | ||
delay=10, | ||
) | ||
|
||
self.assertEqual(winner, 'Res: 1') | ||
self.assertEqual(index, 1) | ||
self.assertEqual(len(excs), 2) | ||
self.assertIsInstance(excs[0], ValueError) | ||
self.assertIsNone(excs[1]) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2024-09-23-18-18-23.gh-issue-124309.iFcarA.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed :exc:`AssertionError` when using :func:`!asyncio.staggered.staggered_race` with :attr:`asyncio.eager_task_factory`. |