Skip to content

Commit

Permalink
pythongh-119121: Fix and test async.staggered.staggered_race
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed May 19, 2024
1 parent 697465f commit 8d36cd5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Lib/asyncio/staggered.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ async def staggered_race(coro_fns, delay, *, loop=None):
exceptions = []
running_tasks = []

async def run_one_coro(
previous_failed: typing.Optional[locks.Event]) -> None:
async def run_one_coro(previous_failed) -> None:
# Wait for the previous task to finish, or for delay seconds
if previous_failed is not None:
with contextlib.suppress(exceptions_mod.TimeoutError):
Expand Down
93 changes: 93 additions & 0 deletions Lib/test/test_asyncio/test_staggered.py
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)
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.

0 comments on commit 8d36cd5

Please sign in to comment.