Skip to content

Commit a8ef8d7

Browse files
committed
Add test case
1 parent e447e65 commit a8ef8d7

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

httpcore/_async/connection_pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ async def handle_async_request(self, request: Request) -> Response:
205205
else:
206206
break # pragma: nocover
207207

208-
except BaseException as exc:
208+
except BaseException:
209209
with self._optional_thread_lock:
210210
# For any exception or cancellation we remove the request from
211211
# the queue, and then re-assign requests to connections.
@@ -362,7 +362,7 @@ async def __aiter__(self) -> AsyncIterator[bytes]:
362362
try:
363363
async for part in self._stream:
364364
yield part
365-
except BaseException as exc:
365+
except BaseException:
366366
await self.aclose()
367367
raise
368368

httpcore/_sync/connection_pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def handle_request(self, request: Request) -> Response:
205205
else:
206206
break # pragma: nocover
207207

208-
except BaseException as exc:
208+
except BaseException:
209209
with self._optional_thread_lock:
210210
# For any exception or cancellation we remove the request from
211211
# the queue, and then re-assign requests to connections.
@@ -362,7 +362,7 @@ def __iter__(self) -> Iterator[bytes]:
362362
try:
363363
for part in self._stream:
364364
yield part
365-
except BaseException as exc:
365+
except BaseException:
366366
self.close()
367367
raise
368368

tests/_async/test_connection_pool.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ async def test_connection_pool_with_connect_exception():
410410
be returned to the connection pool.
411411
"""
412412

413+
cause_exception = Exception("cause")
414+
413415
class FailedConnectBackend(httpcore.AsyncMockBackend):
414416
async def connect_tcp(
415417
self,
@@ -421,7 +423,7 @@ async def connect_tcp(
421423
typing.Iterable[httpcore.SOCKET_OPTION]
422424
] = None,
423425
) -> httpcore.AsyncNetworkStream:
424-
raise httpcore.ConnectError("Could not connect")
426+
raise httpcore.ConnectError("Could not connect") from cause_exception
425427

426428
network_backend = FailedConnectBackend([])
427429

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

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

442+
assert exc_info.value.__cause__ is cause_exception
443+
440444
info = [repr(c) for c in pool.connections]
441445
assert info == []
442446

tests/_sync/test_connection_pool.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ def test_connection_pool_with_connect_exception():
410410
be returned to the connection pool.
411411
"""
412412

413+
cause_exception = Exception("cause")
414+
413415
class FailedConnectBackend(httpcore.MockBackend):
414416
def connect_tcp(
415417
self,
@@ -421,7 +423,7 @@ def connect_tcp(
421423
typing.Iterable[httpcore.SOCKET_OPTION]
422424
] = None,
423425
) -> httpcore.NetworkStream:
424-
raise httpcore.ConnectError("Could not connect")
426+
raise httpcore.ConnectError("Could not connect") from cause_exception
425427

426428
network_backend = FailedConnectBackend([])
427429

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

433435
with httpcore.ConnectionPool(network_backend=network_backend) as pool:
434436
# Sending an initial request, which once complete will not return to the pool.
435-
with pytest.raises(Exception):
437+
with pytest.raises(Exception) as exc_info:
436438
pool.request(
437439
"GET", "https://example.com/", extensions={"trace": trace}
438440
)
439441

442+
assert exc_info.value.__cause__ is cause_exception
443+
440444
info = [repr(c) for c in pool.connections]
441445
assert info == []
442446

0 commit comments

Comments
 (0)