forked from python/cpython
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-119121: Fix and test
async.staggered.staggered_race
- Loading branch information
Showing
3 changed files
with
96 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import asyncio | ||
import unittest | ||
from asyncio.staggered import staggered_race | ||
|
||
|
||
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.assertEqual(winner, None) | ||
self.assertEqual(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.assertEqual(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.assertEqual(excs[1], None) | ||
|
||
async def test_none_successful(self): | ||
async def coro(index): | ||
raise ValueError(index) | ||
|
||
winner, index, excs = await staggered_race( | ||
[ | ||
lambda: coro(0), | ||
lambda: coro(1), | ||
], | ||
delay=None, | ||
) | ||
|
||
self.assertEqual(winner, None) | ||
self.assertEqual(index, None) | ||
self.assertEqual(len(excs), 2) | ||
self.assertIsInstance(excs[0], ValueError) | ||
self.assertIsInstance(excs[1], ValueError) |
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2024-05-19-13-05-59.gh-issue-119121.P1gnh1.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,2 @@ | ||
Fix a NameError happening in ``asyncio.staggered.staggered_race``. This | ||
function is now tested. |