8000 gh-119121: Fix and test `async.staggered.staggered_race` (#119173) · python/cpython@16b46eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 16b46eb

Browse files
authored
gh-119121: Fix and test async.staggered.staggered_race (#119173)
1 parent 357f5a1 commit 16b46eb

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

Lib/asyncio/staggered.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ async def staggered_race(coro_fns, delay, *, loop=None):
6969
exceptions = []
7070
running_tasks = []
7171

72-
async def run_one_coro(
73-
previous_failed: typing.Optional[locks.Event]) -> None:
72+
async def run_one_coro(previous_failed) -> None:
7473
# Wait for the previous task to finish, or for delay seconds
7574
if previous_failed is not None:
7675
with contextlib.suppress(exceptions_mod.TimeoutError):
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import asyncio
2+
import unittest
3+
from asyncio.staggered import staggered_race
4+
5+
from test import support
6+
7+
support.requires_working_socket(module=True)
8+
9+
10+
def tearDownModule():
11+
asyncio.set_event_loop_policy(None)
12+
13+
14+
class StaggeredTests(unittest.IsolatedAsyncioTestCase):
15+
async def test_empty(self):
16+
winner, index, excs = await staggered_race(
17+
[],
18+
delay=None,
19+
)
20+
21+
self.assertIs(winner, None)
22+
self.assertIs(index, None)
23+
self.assertEqual(excs, [])
24+
25+
async def test_one_successful(self):
26+
async def coro(index):
27+
return f'Res: {index}'
28+
29+
winner, index, excs = await staggered_race(
30+
[
31+
lambda: coro(0),
32+
lambda: coro(1),
33+
],
34+
delay=None,
35+
)
36+
37+
self.assertEqual(winner, 'Res: 0')
38+
self.assertEqual(index, 0)
39+
self.assertEqual(excs, [None])
40+
41+
async def test_first_error_second_successful(self):
42+
async def coro(index):
43+
if index == 0:
44+
raise ValueError(index)
45+
return f'Res: {index}'
46+
47+
winner, index, excs = await staggered_race(
48+
[
49+
lambda: coro(0),
50+
lambda: coro(1),
51+
],
52+
delay=None,
53+
)
54+
55+
self.assertEqual(winner, 'Res: 1')
56+
self.assertEqual(index, 1)
57+
self.assertEqual(len(excs), 2)
58+
self.assertIsInstance(excs[0], ValueError)
59+
self.assertIs(excs[1], None)
60+
61+
async def test_first_timeout_second_successful(self):
62+
async def coro(index):
63+
if index == 0:
64+
await asyncio.sleep(10) # much bigger than delay
65+
return f'Res: {index}'
66+
67+
winner, index, excs = await staggered_race(
68+
[
69+
lambda: coro(0),
70+
lambda: coro(1),
71+
],
72+
delay=0.1,
73+
)
74+
75+
self.assertEqual(winner, 'Res: 1')
76+
self.assertEqual(index, 1)
77+
self.assertEqual(len(excs), 2)
78+
self.assertIsInstance(excs[0], asyncio.CancelledError)
79+
self.assertIs(excs[1], None)
80+
81+
async def test_none_successful(self):
82+
async def coro(index):
83+
raise ValueError(index)
84+
85+
winner, index, excs = await staggered_race(
86+
[
87+
lambda: coro(0),
88+
lambda: coro(1),
89+
],
90+
delay=None,
91+
)
92+
93+
self.assertIs(winner, None)
94+
self.assertIs(index, None)
95+
self.assertEqual(len(excs), 2)
96+
self.assertIsInstance(excs[0], ValueError)
97+
self.assertIsInstance(excs[1], ValueError)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a NameError happening in ``asyncio.staggered.staggered_race``. This
2+
function is now tested.

0 commit comments

Comments
 (0)
0