-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: rewrite staggered_race to be race safe (#101)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
c8f1fa9
commit 9db617a
Showing
7 changed files
with
492 additions
and
40 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 was deleted.
Oops, something went wrong.
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,32 @@ | ||
"""Configuration for the tests.""" | ||
|
||
import asyncio | ||
import threading | ||
from typing import Generator | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def verify_threads_ended(): | ||
"""Verify that the threads are not running after the test.""" | ||
threads_before = frozenset(threading.enumerate()) | ||
yield | ||
threads = frozenset(threading.enumerate()) - threads_before | ||
assert not threads | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def verify_no_lingering_tasks( | ||
event_loop: asyncio.AbstractEventLoop, | ||
) -> Generator[None, None, None]: | ||
"""Verify that all tasks are cleaned up.""" | ||
tasks_before = asyncio.all_tasks(event_loop) | ||
yield | ||
|
||
tasks = asyncio.all_tasks(event_loop) - tasks_before | ||
for task in tasks: | ||
pytest.fail(f"Task still running: {task!r}") | ||
task.cancel() | ||
if tasks: | ||
event_loop.run_until_complete(asyncio.wait(tasks)) |
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,86 @@ | ||
import asyncio | ||
import sys | ||
from functools import partial | ||
|
||
import pytest | ||
|
||
from aiohappyeyeballs._staggered import staggered_race | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_one_winners(): | ||
"""Test that there is only one winner when there is no await in the coro.""" | ||
winners = [] | ||
|
||
async def coro(idx): | ||
winners.append(idx) | ||
return idx | ||
|
||
coros = [partial(coro, idx) for idx in range(4)] | ||
|
||
winner, index, excs = await staggered_race( | ||
coros, | ||
delay=None, | ||
) | ||
assert len(winners) == 1 | ||
assert winners == [0] | ||
assert winner == 0 | ||
assert index == 0 | ||
assert excs == [None] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_multiple_winners(): | ||
"""Test multiple winners are handled correctly.""" | ||
loop = asyncio.get_running_loop() | ||
winners = [] | ||
finish = loop.create_future() | ||
|
||
async def coro(idx): | ||
await finish | ||
winners.append(idx) | ||
return idx | ||
|
||
coros = [partial(coro, idx) for idx in range(4)] | ||
|
||
task = loop.create_task(staggered_race(coros, delay=0.00001)) | ||
await asyncio.sleep(0.1) | ||
loop.call_soon(finish.set_result, None) | ||
winner, index, excs = await task | ||
assert len(winners) == 4 | ||
assert winners == [0, 1, 2, 3] | ||
assert winner == 0 | ||
assert index == 0 | ||
assert excs == [None, None, None, None] | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info < (3, 12), reason="requires python3.12 or higher") | ||
def test_multiple_winners_eager_task_factory(): | ||
"""Test multiple winners are handled correctly.""" | ||
loop = asyncio.new_event_loop() | ||
eager_task_factory = asyncio.create_eager_task_factory(asyncio.Task) | ||
loop.set_task_factory(eager_task_factory) | ||
asyncio.set_event_loop(None) | ||
|
||
async def run(): | ||
winners = [] | ||
finish = loop.create_future() | ||
|
||
async def coro(idx): | ||
await finish | ||
winners.append(idx) | ||
return idx | ||
|
||
coros = [partial(coro, idx) for idx in range(4)] | ||
|
||
task = loop.create_task(staggered_race(coros, delay=0.00001)) | ||
await asyncio.sleep(0.1) | ||
loop.call_soon(finish.set_result, None) | ||
winner, index, excs = await task | ||
assert len(winners) == 4 | ||
assert winners == [0, 1, 2, 3] | ||
assert winner == 0 | ||
assert index == 0 | ||
assert excs == [None, None, None, None] | ||
|
||
loop.run_until_complete(run()) |
Oops, something went wrong.