Skip to content

Commit 1af3889

Browse files
lesnik512claude
andcommitted
test(resilience): use AsyncMock instead of pragma'd stub for never-called sleep
User feedback: # pragma: no cover on a user-defined function body that intentionally never runs is the wrong shape — refactor to use a mock whose body is structural (unittest.mock internals are excluded from coverage measurement). AsyncMock().assert_not_called() expresses the "must never run" assertion cleanly with no coverage gymnastics. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 25db15b commit 1af3889

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

tests/test_bulkhead.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from collections.abc import Callable, Coroutine
1010
from http import HTTPStatus
1111
from typing import Any
12+
from unittest.mock import AsyncMock
1213

1314
import httpx2
1415
import pytest
@@ -369,18 +370,15 @@ async def test_bulkhead_full_error_is_not_retried_by_retry() -> None:
369370
bulkhead = Bulkhead(max_concurrent=_MAX_CONCURRENT_1, acquire_timeout=0)
370371
transport = httpx2.MockTransport(handler)
371372

372-
sleep_calls: list[float] = []
373-
374-
async def _sleep(
375-
delay: float,
376-
) -> None: # pragma: no cover — assert is `sleep_calls == []`, so this body must never run
377-
sleep_calls.append(delay)
373+
# AsyncMock so the never-called assertion is structural — no user-defined
374+
# body that would need # pragma: no cover.
375+
mock_sleep = AsyncMock()
378376

379377
client = AsyncClient(
380378
httpx2_client=httpx2.AsyncClient(transport=transport),
381379
middleware=[
382380
bulkhead,
383-
Retry(_sleep=_sleep, max_attempts=3, base_delay=0.001, max_delay=0.002),
381+
Retry(_sleep=mock_sleep, max_attempts=3, base_delay=0.001, max_delay=0.002),
384382
],
385383
)
386384

@@ -391,7 +389,7 @@ async def _sleep(
391389
# Second call hits a full Bulkhead. Retry must NOT swallow + retry it.
392390
with pytest.raises(BulkheadFullError):
393391
await client.get("https://example.test/rejected")
394-
assert sleep_calls == [] # Retry never slept — it didn't try to retry
392+
mock_sleep.assert_not_called() # Retry never slept — it didn't try to retry
395393

396394
# Cleanup.
397395
first.cancel()

0 commit comments

Comments
 (0)