Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add race test
  • Loading branch information
belm0 committed Apr 23, 2026
commit 8710b95065d5f529a39e1d116c4d1d428945e86b
37 changes: 37 additions & 0 deletions Lib/test/test_asyncio/test_taskgroups.py
Comment thread
belm0 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,43 @@ async def test_taskgroup_body_cancel_after_exception(self):
finally:
tg.cancel()

async def test_taskgroup_cancel_one_winner(self):
async def race(*fns):
outcome = None
async def run(fn):
nonlocal outcome
outcome = await fn()
tg.cancel()

async with asyncio.TaskGroup() as tg:
for fn in fns:
tg.create_task(run(fn))
return outcome

event = asyncio.Event()
record = []
async def fn_1():
record.append("1 started")
await event.wait()
record.append("1 finished")
return 1

async def fn_2():
record.append("2 started")
await event.wait()
record.append("2 finished")
return 2

async def fn_3():
record.append("3 started")
event.set()
await asyncio.sleep(10)
record.append("3 finished")
return 3

self.assertEqual(await race(fn_1, fn_2, fn_3), 1)
self.assertListEqual(record, ["1 started", "2 started", "3 started", "1 finished"])


class TestTaskGroup(BaseTestTaskGroup, unittest.IsolatedAsyncioTestCase):
loop_factory = asyncio.EventLoop
Expand Down
Loading