-
Notifications
You must be signed in to change notification settings - Fork 160
Support async cancellations #719
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
Closed
karpetrosyan
wants to merge
17
commits into
encode:master
from
karpetrosyan:support-async-cancellations
Closed
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
dceaaf6
Add red tests
karpetrosyan 853e799
Typo
karpetrosyan 41aca77
Use top level `import httpcore`
karpetrosyan f6c9131
Decrease timeout
karpetrosyan b8ab756
Use async with
karpetrosyan 7a5eb9b
Merge branch 'master' into support-async-cancellations
lovelydinosaur e3837f2
Add tests for the `_response_closed` method
karpetrosyan a2d10dd
Merge branch 'support-async-cancellations' of github.com:karosis88/ht…
karpetrosyan 7f07c53
Testing with the public API
karpetrosyan f533ab9
Simplify tests
karpetrosyan 26a4455
Little changes
karpetrosyan 9e56efa
Add test for the bytestream cancellation
karpetrosyan 20c591a
Change elipsis to `pass`
karpetrosyan 2aab3dc
Remove useless test
karpetrosyan d2ce00b
Remove print
karpetrosyan 0216681
Yet another print fix
karpetrosyan 2e48b78
Change unneded stream to request
karpetrosyan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import typing | ||
| from typing import Optional | ||
|
|
||
| import anyio | ||
| import pytest | ||
|
|
||
| import httpcore | ||
| from httpcore._async.http2 import HTTPConnectionState as HTTP2ConnectionState | ||
| from httpcore._async.http11 import HTTPConnectionState as HTTP11ConnectionState | ||
| from httpcore._synchronization import AsyncSemaphore | ||
|
|
||
|
|
||
| class SlowStream(httpcore.AsyncNetworkStream): | ||
| async def write( | ||
| self, buffer: bytes, timeout: typing.Optional[float] = None | ||
| ) -> None: | ||
| await httpcore._backends.auto.AutoBackend().sleep(2) | ||
|
karpetrosyan marked this conversation as resolved.
Outdated
|
||
|
|
||
| async def aclose(self) -> None: | ||
| ... | ||
|
|
||
|
|
||
| class SlowBackend(httpcore.AsyncNetworkBackend): | ||
| async def connect_tcp( | ||
| self, | ||
| host: str, | ||
| port: int, | ||
| timeout: Optional[float] = None, | ||
| local_address: Optional[str] = None, | ||
| socket_options: typing.Optional[typing.Iterable[httpcore.SOCKET_OPTION]] = None, | ||
| ) -> httpcore.AsyncNetworkStream: | ||
| return SlowStream() | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_async_cancellation(): | ||
| async with httpcore.AsyncConnectionPool(network_backend=SlowBackend()) as pool: | ||
| with anyio.move_on_after(0.001): | ||
| await pool.request("GET", "http://example.com") | ||
| assert not pool.connections | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_h11_response_closed(): | ||
| origin = httpcore.Origin(b"https", b"example.com", 443) | ||
| stream = httpcore.AsyncMockStream([]) | ||
| async with httpcore.AsyncHTTP11Connection(origin, stream) as conn: | ||
| # mock | ||
| conn._state = HTTP11ConnectionState.ACTIVE | ||
|
|
||
| with anyio.CancelScope() as cancel_scope: | ||
| async with conn._state_lock: | ||
| cancel_scope.cancel() | ||
| await conn._response_closed() | ||
| assert conn._state != HTTP11ConnectionState.ACTIVE | ||
| assert conn._state in (HTTP11ConnectionState.IDLE, HTTP11ConnectionState.CLOSED) | ||
|
karpetrosyan marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_h2_response_closed(): | ||
| origin = httpcore.Origin(b"https", b"example.com", 443) | ||
| stream = httpcore.AsyncMockStream([]) | ||
| events = {0: None} | ||
| async with httpcore.AsyncHTTP2Connection(origin, stream) as conn: | ||
| # mock | ||
| conn._state = HTTP2ConnectionState.ACTIVE | ||
| conn._max_streams_semaphore = AsyncSemaphore(1) | ||
| conn._events = events | ||
| await conn._max_streams_semaphore.acquire() | ||
|
|
||
| with anyio.CancelScope() as cancel_scope: | ||
| async with conn._state_lock: | ||
| cancel_scope.cancel() | ||
| await conn._response_closed(0) | ||
| assert conn._state != HTTP2ConnectionState.ACTIVE | ||
| assert conn._state in (HTTP2ConnectionState.IDLE, HTTP2ConnectionState.CLOSED) | ||
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.