-
Notifications
You must be signed in to change notification settings - Fork 63
feat: handle CancelledError - cancel if no other waiters #697
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
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
865db04
feat: handle CancelledError - cancel if no other waiters
BobTheBuidler 9244bc7
Update __init__.py
BobTheBuidler 8c2b878
Create test_cancel.py
BobTheBuidler b283118
Update test_cancel.py
BobTheBuidler f8ceb96
Update test_cancel.py
Dreamsorcerer 27100f3
fix: name error CancelledError
BobTheBuidler 19b1b3b
feat(test): more cancel tests
BobTheBuidler e85e099
finish up impl
BobTheBuidler 2c4d654
Update test_cancel.py
BobTheBuidler f9cfbf9
lint
BobTheBuidler 6441a1d
Update __init__.py
BobTheBuidler 94c303b
Update __init__.py
BobTheBuidler 1dec1b4
chore: refactor out __task and fut
BobTheBuidler 85f2f51
Update __init__.py
BobTheBuidler 094a3cc
Update __init__.py
BobTheBuidler 0b06d9b
fix missing import
BobTheBuidler b439eca
Update __init__.py
BobTheBuidler 83f921c
Merge branch 'master' into refactored
BobTheBuidler 4495e48
Merge branch 'master' into patch-3
BobTheBuidler ca5a166
Update test_cancel.py
BobTheBuidler 829a077
lint
BobTheBuidler 4d239ef
lint
BobTheBuidler 83a6b53
Update __init__.py
BobTheBuidler f2152ee
Update __init__.py
BobTheBuidler 4bbab74
Update test_cancel.py
BobTheBuidler 157c7d5
Update benchmark.py
BobTheBuidler 23daa05
Update test_internals.py
BobTheBuidler 3972c0f
Update test_basic.py
BobTheBuidler 4878867
Update test_internals.py
BobTheBuidler 3943555
Update __init__.py
BobTheBuidler d76b537
Update __init__.py
BobTheBuidler 9ed3a42
Update __init__.py
BobTheBuidler 4675c7a
Update test_internals.py
BobTheBuidler 33e5dca
Update test_internals.py
BobTheBuidler 9d1f4f8
Merge branch 'refactored' into patch-3
BobTheBuidler b131609
reduce sleep time
BobTheBuidler b42e31d
Update test_cancel.py
BobTheBuidler be24f51
Merge branch 'master' into patch-3
BobTheBuidler 28a108a
remove unnecessary check
BobTheBuidler 8b818e8
refactor with _shield_and_handle_cancelled_error
BobTheBuidler ea13dac
Update __init__.py
BobTheBuidler b0e7572
Update __init__.py
BobTheBuidler 928531f
Tweak coverage
Dreamsorcerer 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,58 @@ | ||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
| from async_lru import alru_cache | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("num_to_cancel", (0, 1, 2, 3)) | ||
| async def test_cancel(num_to_cancel: int) -> None: | ||
| @alru_cache | ||
| async def coro(val: int) -> int: | ||
| # I am a long running coro function | ||
| await asyncio.sleep(0.1) | ||
| return val | ||
|
|
||
| # create 3 tasks for the cached function using the same key | ||
| tasks = [asyncio.create_task(coro(i)) for i in range(3)] | ||
|
|
||
| # force the event loop to run once so the tasks can begin | ||
| await asyncio.sleep(0) | ||
|
|
||
| # maybe cancel some tasks | ||
| for i in range(num_to_cancel): | ||
| tasks[i].cancel() | ||
|
|
||
| # allow enough time for the non-cancelled tasks to complete | ||
| await asyncio.sleep(0.2) | ||
|
|
||
| # check tasks are properly cancelled | ||
| for i in range(num_to_cancel): | ||
| assert tasks[i].cancelled() | ||
|
|
||
| # check non-cancelled tasks return expected outputs | ||
| for i in range(num_to_cancel, 3): | ||
| assert await tasks[i] == i | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_cancel_single_waiter_triggers_handle_cancelled_error() -> None: | ||
| # This test ensures the _handle_cancelled_error path (waiters == 1) is exercised. | ||
| cache_item_task_finished = False | ||
|
|
||
| @alru_cache | ||
| async def coro(val: int) -> None: | ||
| nonlocal cache_item_task_finished | ||
| await asyncio.sleep(2) | ||
| cache_item_task_finished = True # pragma: no cover | ||
|
|
||
| task = asyncio.create_task(coro(42)) | ||
| await asyncio.sleep(0) | ||
| task.cancel() | ||
| try: | ||
| await task | ||
| except asyncio.CancelledError: | ||
| pass | ||
|
|
||
| # The underlying coroutine should be cancelled, so the flag should remain False | ||
| assert cache_item_task_finished is False | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.