Skip to content

Commit

Permalink
Run unasync
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed May 12, 2023
1 parent 5078b6a commit 39dccfc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
7 changes: 6 additions & 1 deletion httpcore/_async/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,12 @@ async def _receive_events(
# and should be saved so it can be raised on all streams.
if hasattr(event, "error_code") and event_stream_id == 0:
self._connection_error_event = event
raise RemoteProtocolError(event)
if isinstance(
self._connection_error_event, h2.events.ConnectionTerminated
):
raise ConnectionNotAvailable(self._connection_error_event)
else:
raise RemoteProtocolError(event)

if event_stream_id in self._events:
self._events[event_stream_id].append(event)
Expand Down
19 changes: 14 additions & 5 deletions httpcore/_sync/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,14 @@ def handle_request(self, request: Request) -> Response:
# a GOAWAY event. Other flows of control may then raise
# a protocol error at any point they interact with the 'h2_state'.
#
# In this case we'll have stored the event, and should raise
# it as a RemoteProtocolError.
# In this case we'll have stored the event, and should raise it.
if self._connection_error_event:
raise RemoteProtocolError(self._connection_error_event)
if isinstance(
self._connection_error_event, h2.events.ConnectionTerminated
):
raise ConnectionNotAvailable(self._connection_error_event)
else:
raise RemoteProtocolError(self._connection_error_event)
# If h2 raises a protocol error in some other state then we
# must somehow have made a protocol violation.
raise LocalProtocolError(exc) # pragma: nocover
Expand Down Expand Up @@ -279,7 +283,7 @@ def _receive_events(
) -> None:
with self._read_lock:
if self._connection_error_event is not None: # pragma: nocover
raise RemoteProtocolError(self._connection_error_event)
raise ConnectionNotAvailable(self._connection_error_event)

# This conditional is a bit icky. We don't want to block reading if we've
# actually got an event to return for a given stream. We need to do that
Expand All @@ -303,7 +307,12 @@ def _receive_events(
# and should be saved so it can be raised on all streams.
if hasattr(event, "error_code") and event_stream_id == 0:
self._connection_error_event = event
raise RemoteProtocolError(event)
if isinstance(
self._connection_error_event, h2.events.ConnectionTerminated
):
raise ConnectionNotAvailable(self._connection_error_event)
else:
raise RemoteProtocolError(event)

if event_stream_id in self._events:
self._events[event_stream_id].append(event)
Expand Down
5 changes: 1 addition & 4 deletions tests/_async/test_http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ async def test_http2_connection_closed():
) as conn:
await conn.request("GET", "https://example.com/")

with pytest.raises(RemoteProtocolError):
await conn.request("GET", "https://example.com/")

with pytest.raises(ConnectionNotAvailable):
await conn.request("GET", "https://example.com/")

Expand Down Expand Up @@ -220,7 +217,7 @@ async def test_http2_connection_with_goaway():
]
)
async with AsyncHTTP2Connection(origin=origin, stream=stream) as conn:
with pytest.raises(RemoteProtocolError):
with pytest.raises(ConnectionNotAvailable):
await conn.request("GET", "https://example.com/")
with pytest.raises(ConnectionNotAvailable):
await conn.request("GET", "https://example.com/")
Expand Down
6 changes: 3 additions & 3 deletions tests/_sync/test_http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_http2_connection_closed():
) as conn:
conn.request("GET", "https://example.com/")

with pytest.raises(RemoteProtocolError):
with pytest.raises(ConnectionNotAvailable):
conn.request("GET", "https://example.com/")

assert not conn.is_available()
Expand Down Expand Up @@ -217,9 +217,9 @@ def test_http2_connection_with_goaway():
]
)
with HTTP2Connection(origin=origin, stream=stream) as conn:
with pytest.raises(RemoteProtocolError):
with pytest.raises(ConnectionNotAvailable):
conn.request("GET", "https://example.com/")
with pytest.raises(RemoteProtocolError):
with pytest.raises(ConnectionNotAvailable):
conn.request("GET", "https://example.com/")


Expand Down

0 comments on commit 39dccfc

Please sign in to comment.