Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
CzBiX committed May 28, 2024
1 parent e447e65 commit a8ef8d7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ async def handle_async_request(self, request: Request) -> Response:
else:
break # pragma: nocover

except BaseException as exc:
except BaseException:
with self._optional_thread_lock:
# For any exception or cancellation we remove the request from
# the queue, and then re-assign requests to connections.
Expand Down Expand Up @@ -362,7 +362,7 @@ async def __aiter__(self) -> AsyncIterator[bytes]:
try:
async for part in self._stream:
yield part
except BaseException as exc:
except BaseException:
await self.aclose()
raise

Expand Down
4 changes: 2 additions & 2 deletions httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def handle_request(self, request: Request) -> Response:
else:
break # pragma: nocover

except BaseException as exc:
except BaseException:
with self._optional_thread_lock:
# For any exception or cancellation we remove the request from
# the queue, and then re-assign requests to connections.
Expand Down Expand Up @@ -362,7 +362,7 @@ def __iter__(self) -> Iterator[bytes]:
try:
for part in self._stream:
yield part
except BaseException as exc:
except BaseException:
self.close()
raise

Expand Down
8 changes: 6 additions & 2 deletions tests/_async/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ async def test_connection_pool_with_connect_exception():
be returned to the connection pool.
"""

cause_exception = Exception("cause")

class FailedConnectBackend(httpcore.AsyncMockBackend):
async def connect_tcp(
self,
Expand All @@ -421,7 +423,7 @@ async def connect_tcp(
typing.Iterable[httpcore.SOCKET_OPTION]
] = None,
) -> httpcore.AsyncNetworkStream:
raise httpcore.ConnectError("Could not connect")
raise httpcore.ConnectError("Could not connect") from cause_exception

network_backend = FailedConnectBackend([])

Expand All @@ -432,11 +434,13 @@ async def trace(name, kwargs):

async with httpcore.AsyncConnectionPool(network_backend=network_backend) as pool:
# Sending an initial request, which once complete will not return to the pool.
with pytest.raises(Exception):
with pytest.raises(Exception) as exc_info:
await pool.request(
"GET", "https://example.com/", extensions={"trace": trace}
)

assert exc_info.value.__cause__ is cause_exception

info = [repr(c) for c in pool.connections]
assert info == []

Expand Down
8 changes: 6 additions & 2 deletions tests/_sync/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ def test_connection_pool_with_connect_exception():
be returned to the connection pool.
"""

cause_exception = Exception("cause")

class FailedConnectBackend(httpcore.MockBackend):
def connect_tcp(
self,
Expand All @@ -421,7 +423,7 @@ def connect_tcp(
typing.Iterable[httpcore.SOCKET_OPTION]
] = None,
) -> httpcore.NetworkStream:
raise httpcore.ConnectError("Could not connect")
raise httpcore.ConnectError("Could not connect") from cause_exception

network_backend = FailedConnectBackend([])

Expand All @@ -432,11 +434,13 @@ def trace(name, kwargs):

with httpcore.ConnectionPool(network_backend=network_backend) as pool:
# Sending an initial request, which once complete will not return to the pool.
with pytest.raises(Exception):
with pytest.raises(Exception) as exc_info:
pool.request(
"GET", "https://example.com/", extensions={"trace": trace}
)

assert exc_info.value.__cause__ is cause_exception

info = [repr(c) for c in pool.connections]
assert info == []

Expand Down

0 comments on commit a8ef8d7

Please sign in to comment.