-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GH-98388: Add tests for happy eyeballs and its internal workings #98389
Open
twisteroidambassador
wants to merge
11
commits into
python:main
Choose a base branch
from
twisteroidambassador:happy_eyeballs_checkup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d8b4492
Add tests for happy eyeballs and its internal workings.
twisteroidambassador 34a8f6f
Correct typo in filename.
twisteroidambassador c0e179e
Improve readability for _interleave_addrinfos tests.
twisteroidambassador 8270e69
Rename _interleave_addrinfos tests to match the method being tested.
twisteroidambassador afb6abf
Made test_simultaneous_success_fail fail more consistently (I hope).
twisteroidambassador 513f7f9
Replace `wait_for` with `wait`.
twisteroidambassador 079eec8
Delete staggered.py
arhadthedev ec144fb
Merge branch 'main' into happy_eyeballs_checkup
arhadthedev 5ec7d65
Restore staggered.py from `main` branch
arhadthedev 4e9f06a
Address patchcheck report
arhadthedev 62387d6
Merge branch 'main' into happy_eyeballs_checkup
gvanrossum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,115 @@ | ||||||||||||||||||
import asyncio | ||||||||||||||||||
import functools | ||||||||||||||||||
import unittest | ||||||||||||||||||
from asyncio.staggered import staggered_race | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
# To prevent a warning "test altered the execution environment" | ||||||||||||||||||
def tearDownModule(): | ||||||||||||||||||
asyncio.set_event_loop_policy(None) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class TestStaggered(unittest.IsolatedAsyncioTestCase): | ||||||||||||||||||
@staticmethod | ||||||||||||||||||
async def waiting_coroutine(return_value, wait_seconds, success): | ||||||||||||||||||
await asyncio.sleep(wait_seconds) | ||||||||||||||||||
if success: | ||||||||||||||||||
return return_value | ||||||||||||||||||
raise RuntimeError(str(return_value)) | ||||||||||||||||||
|
||||||||||||||||||
def get_waiting_coroutine_factory(self, return_value, wait_seconds, success): | ||||||||||||||||||
return functools.partial(self.waiting_coroutine, return_value, wait_seconds, success) | ||||||||||||||||||
|
||||||||||||||||||
async def test_single_success(self): | ||||||||||||||||||
winner_result, winner_idx, exceptions = await staggered_race( | ||||||||||||||||||
(self.get_waiting_coroutine_factory(0, 0.1, True),), | ||||||||||||||||||
0.1, | ||||||||||||||||||
) | ||||||||||||||||||
self.assertEqual(winner_result, 0) | ||||||||||||||||||
self.assertEqual(winner_idx, 0) | ||||||||||||||||||
self.assertEqual(len(exceptions), 1) | ||||||||||||||||||
self.assertIsNone(exceptions[0]) | ||||||||||||||||||
|
||||||||||||||||||
async def test_single_fail(self): | ||||||||||||||||||
winner_result, winner_idx, exceptions = await staggered_race( | ||||||||||||||||||
(self.get_waiting_coroutine_factory(0, 0.1, False),), | ||||||||||||||||||
0.1, | ||||||||||||||||||
) | ||||||||||||||||||
self.assertEqual(winner_result, None) | ||||||||||||||||||
self.assertEqual(winner_idx, None) | ||||||||||||||||||
self.assertEqual(len(exceptions), 1) | ||||||||||||||||||
self.assertIsInstance(exceptions[0], RuntimeError) | ||||||||||||||||||
|
||||||||||||||||||
async def test_first_win(self): | ||||||||||||||||||
winner_result, winner_idx, exceptions = await staggered_race( | ||||||||||||||||||
( | ||||||||||||||||||
self.get_waiting_coroutine_factory(0, 0.2, True), | ||||||||||||||||||
self.get_waiting_coroutine_factory(1, 0.2, True), | ||||||||||||||||||
), | ||||||||||||||||||
0.1, | ||||||||||||||||||
) | ||||||||||||||||||
self.assertEqual(winner_result, 0) | ||||||||||||||||||
self.assertEqual(winner_idx, 0) | ||||||||||||||||||
self.assertEqual(len(exceptions), 2) | ||||||||||||||||||
self.assertIsNone(exceptions[0]) | ||||||||||||||||||
self.assertIsInstance(exceptions[1], asyncio.CancelledError) | ||||||||||||||||||
|
||||||||||||||||||
async def test_second_win(self): | ||||||||||||||||||
winner_result, winner_idx, exceptions = await staggered_race( | ||||||||||||||||||
( | ||||||||||||||||||
self.get_waiting_coroutine_factory(0, 0.3, True), | ||||||||||||||||||
self.get_waiting_coroutine_factory(1, 0.1, True), | ||||||||||||||||||
), | ||||||||||||||||||
0.1, | ||||||||||||||||||
) | ||||||||||||||||||
self.assertEqual(winner_result, 1) | ||||||||||||||||||
self.assertEqual(winner_idx, 1) | ||||||||||||||||||
self.assertEqual(len(exceptions), 2) | ||||||||||||||||||
self.assertIsInstance(exceptions[0], asyncio.CancelledError) | ||||||||||||||||||
self.assertIsNone(exceptions[1]) | ||||||||||||||||||
|
||||||||||||||||||
async def test_first_fail(self): | ||||||||||||||||||
winner_result, winner_idx, exceptions = await staggered_race( | ||||||||||||||||||
( | ||||||||||||||||||
self.get_waiting_coroutine_factory(0, 0.2, False), | ||||||||||||||||||
self.get_waiting_coroutine_factory(1, 0.2, True), | ||||||||||||||||||
), | ||||||||||||||||||
0.1, | ||||||||||||||||||
) | ||||||||||||||||||
self.assertEqual(winner_result, 1) | ||||||||||||||||||
self.assertEqual(winner_idx, 1) | ||||||||||||||||||
self.assertEqual(len(exceptions), 2) | ||||||||||||||||||
self.assertIsInstance(exceptions[0], RuntimeError) | ||||||||||||||||||
self.assertIsNone(exceptions[1]) | ||||||||||||||||||
|
||||||||||||||||||
async def test_second_fail(self): | ||||||||||||||||||
winner_result, winner_idx, exceptions = await staggered_race( | ||||||||||||||||||
( | ||||||||||||||||||
self.get_waiting_coroutine_factory(0, 0.2, True), | ||||||||||||||||||
self.get_waiting_coroutine_factory(1, 0, False), | ||||||||||||||||||
), | ||||||||||||||||||
0.1, | ||||||||||||||||||
) | ||||||||||||||||||
self.assertEqual(winner_result, 0) | ||||||||||||||||||
self.assertEqual(winner_idx, 0) | ||||||||||||||||||
self.assertEqual(len(exceptions), 2) | ||||||||||||||||||
self.assertIsNone(exceptions[0]) | ||||||||||||||||||
self.assertIsInstance(exceptions[1], RuntimeError) | ||||||||||||||||||
|
||||||||||||||||||
async def test_simultaneous_success_fail(self): | ||||||||||||||||||
# There's a potential race condition here: | ||||||||||||||||||
# https://github.com/python/cpython/issues/86296 | ||||||||||||||||||
# As with any race condition, it can be difficult to reproduce. | ||||||||||||||||||
# This test may not fail every time. | ||||||||||||||||||
Comment on lines
+100
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a note to this comment that the race condition has been fixed in 3.12, e.g.
Suggested change
|
||||||||||||||||||
for i in range(201): | ||||||||||||||||||
time_unit = 0.0001 * i | ||||||||||||||||||
winner_result, winner_idx, exceptions = await staggered_race( | ||||||||||||||||||
( | ||||||||||||||||||
self.get_waiting_coroutine_factory(0, time_unit*2, True), | ||||||||||||||||||
self.get_waiting_coroutine_factory(1, time_unit, False), | ||||||||||||||||||
self.get_waiting_coroutine_factory(2, 0.05, True) | ||||||||||||||||||
), | ||||||||||||||||||
time_unit, | ||||||||||||||||||
) | ||||||||||||||||||
self.assertEqual(winner_result, 0) | ||||||||||||||||||
self.assertEqual(winner_idx, 0) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be a more thorough test if we mixed up the order a bit, e.g. like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM @gvanrossum.