Skip to content

Commit ed8e87d

Browse files
committed
test: keep the async straggler harness covered on Python 3.11
On 3.11, awaiting a task that ended with an exception drops coverage line events in the awaiting frame until its next suspension. The harness tasks now swallow the breaker's error themselves.
1 parent bb43abb commit ed8e87d

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

‎tests/test_circuit_breaker.py‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ class _HeldCalls:
717717
def __init__(self, breaker: AsyncCircuitBreaker) -> None:
718718
self._breaker = breaker
719719
self._futures: list[asyncio.Future[int]] = []
720-
self._tasks: list[asyncio.Task[httpx2.Response]] = []
720+
self._tasks: list[asyncio.Task[None]] = []
721721
self.probes_in_flight = 0
722722
self.max_probes_in_flight = 0
723723

@@ -737,23 +737,29 @@ async def _next(self, request: httpx2.Request) -> httpx2.Response:
737737
raise InternalServerError(httpx2.Response(status, request=request))
738738
return httpx2.Response(status, request=request)
739739

740+
async def _call(self) -> None:
741+
with contextlib.suppress(InternalServerError, CircuitOpenError):
742+
await self._breaker(httpx2.Request("GET", "https://example.test/x"), self._next)
743+
740744
async def start(self) -> int:
741-
"""Start a call; return its index once it is blocked in `next` (or already rejected)."""
742-
task = asyncio.create_task(self._breaker(httpx2.Request("GET", "https://example.test/x"), self._next))
743-
self._tasks.append(task)
745+
"""Start a call; return its index once it is blocked in `next` (or already rejected).
746+
747+
The task swallows the breaker's error itself: on 3.11, awaiting a task that failed loses
748+
coverage line events in the awaiting test.
749+
"""
750+
self._tasks.append(asyncio.create_task(self._call()))
744751
await asyncio.sleep(0)
745752
return len(self._tasks) - 1
746753

747754
async def finish(self, index: int, status: int) -> None:
748755
self._futures[index].set_result(status)
749-
with contextlib.suppress(InternalServerError):
750-
await self._tasks[index]
756+
await self._tasks[index]
751757

752758
async def drain(self) -> None:
753759
for future in self._futures:
754760
if not future.done():
755761
future.set_result(HTTPStatus.OK)
756-
await asyncio.gather(*self._tasks, return_exceptions=True)
762+
await asyncio.gather(*self._tasks)
757763

758764

759765
async def _open_now(breaker: AsyncCircuitBreaker) -> None:

0 commit comments

Comments
 (0)