|
| 1 | +"""Tests for the AsyncTimeout middleware. |
| 2 | +
|
| 3 | +Calls the middleware directly with an injected `next` callable. Expiry tests use a |
| 4 | +tiny timeout against a long sleep (large margin -> not wall-clock flaky); the |
| 5 | +inner-timeout test raises immediately so no real time passes. |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +import builtins |
| 10 | +import logging |
| 11 | + |
| 12 | +import httpx2 |
| 13 | +import pytest |
| 14 | + |
| 15 | +from httpware.errors import TimeoutError as HttpwareTimeoutError |
| 16 | +from httpware.middleware.resilience.timeout import AsyncTimeout |
| 17 | + |
| 18 | + |
| 19 | +def _request() -> httpx2.Request: |
| 20 | + return httpx2.Request("GET", "https://example.test/x") |
| 21 | + |
| 22 | + |
| 23 | +async def test_passes_through_response_when_under_budget() -> None: |
| 24 | + async def _next(request: httpx2.Request) -> httpx2.Response: |
| 25 | + return httpx2.Response(200, request=request) |
| 26 | + |
| 27 | + middleware = AsyncTimeout(timeout=10.0) |
| 28 | + response = await middleware(_request(), _next) |
| 29 | + assert response.status_code == 200 # noqa: PLR2004 |
| 30 | + |
| 31 | + |
| 32 | +async def test_expiry_raises_httpware_timeout_chained_from_builtin( |
| 33 | + caplog: pytest.LogCaptureFixture, |
| 34 | +) -> None: |
| 35 | + async def _next(request: httpx2.Request) -> httpx2.Response: |
| 36 | + await asyncio.sleep(5.0) |
| 37 | + return httpx2.Response(200, request=request) # pragma: no cover — deadline fires first |
| 38 | + |
| 39 | + middleware = AsyncTimeout(timeout=0.01) |
| 40 | + with ( |
| 41 | + caplog.at_level(logging.WARNING, logger="httpware.timeout"), |
| 42 | + pytest.raises(HttpwareTimeoutError) as info, |
| 43 | + ): |
| 44 | + await middleware(_request(), _next) |
| 45 | + |
| 46 | + assert "overall timeout of 0.01s exceeded" in str(info.value) |
| 47 | + assert isinstance(info.value.__cause__, builtins.TimeoutError) |
| 48 | + |
| 49 | + records = [r for r in caplog.records if r.name == "httpware.timeout"] |
| 50 | + assert len(records) == 1 |
| 51 | + assert records[0].levelno == logging.WARNING |
| 52 | + assert records[0].timeout == 0.01 # noqa: PLR2004 # ty: ignore[unresolved-attribute] |
| 53 | + assert records[0].method == "GET" # ty: ignore[unresolved-attribute] |
| 54 | + assert "example.test/x" in records[0].url # ty: ignore[unresolved-attribute] |
| 55 | + |
| 56 | + |
| 57 | +async def test_inner_timeout_propagates_unchanged() -> None: |
| 58 | + """A TimeoutError from next (not our deadline) is re-raised untouched.""" |
| 59 | + |
| 60 | + async def _next(_request: httpx2.Request) -> httpx2.Response: |
| 61 | + msg = "inner read timeout" |
| 62 | + raise HttpwareTimeoutError(msg) |
| 63 | + |
| 64 | + middleware = AsyncTimeout(timeout=10.0) |
| 65 | + with pytest.raises(HttpwareTimeoutError) as info: |
| 66 | + await middleware(_request(), _next) |
| 67 | + |
| 68 | + assert "inner read timeout" in str(info.value) |
| 69 | + assert "overall timeout" not in str(info.value) |
| 70 | + |
| 71 | + |
| 72 | +def test_zero_timeout_rejected() -> None: |
| 73 | + with pytest.raises(ValueError, match="timeout must be > 0"): |
| 74 | + AsyncTimeout(timeout=0) |
| 75 | + |
| 76 | + |
| 77 | +def test_negative_timeout_rejected() -> None: |
| 78 | + with pytest.raises(ValueError, match="timeout must be > 0"): |
| 79 | + AsyncTimeout(timeout=-1.0) |
0 commit comments