|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 | import uvicorn |
| 7 | +from httpx import ASGITransport, AsyncClient |
7 | 8 | from uvicorn.config import LOGGING_CONFIG |
8 | 9 |
|
| 10 | +from connectrpc.errors import ConnectError |
| 11 | + |
9 | 12 | from .haberdasher_connect import ( |
10 | 13 | Haberdasher, |
11 | 14 | HaberdasherASGIApplication, |
@@ -160,3 +163,37 @@ async def counting_haberdasher(): |
160 | 163 | server.should_exit = True |
161 | 164 | await uvicorn_task |
162 | 165 | assert "Haberdasher failed to shut down" in logs.getvalue() |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.asyncio |
| 169 | +async def test_lifespan_not_supported() -> None: |
| 170 | + class CountingHaberdasher(Haberdasher): |
| 171 | + def __init__(self, counter: Counter) -> None: |
| 172 | + self._counter = counter |
| 173 | + |
| 174 | + async def make_hat(self, request, ctx): |
| 175 | + self._counter["requests"] += 1 |
| 176 | + return Hat(size=request.inches, color="blue") |
| 177 | + |
| 178 | + final_count = None |
| 179 | + |
| 180 | + async def counting_haberdasher(): |
| 181 | + counter = Counter() |
| 182 | + try: |
| 183 | + haberdasher = CountingHaberdasher(counter) |
| 184 | + yield haberdasher |
| 185 | + finally: |
| 186 | + nonlocal final_count |
| 187 | + final_count = counter["requests"] |
| 188 | + |
| 189 | + app = HaberdasherASGIApplication(counting_haberdasher()) |
| 190 | + transport = ASGITransport(app) # pyright:ignore[reportArgumentType] - httpx type is not complete |
| 191 | + async with HaberdasherClient( |
| 192 | + "http://localhost", session=AsyncClient(transport=transport) |
| 193 | + ) as client: |
| 194 | + with pytest.raises(ConnectError) as e: |
| 195 | + await client.make_hat(Size(inches=10)) |
| 196 | + assert ( |
| 197 | + "ASGI server does not support lifespan but async generator passed for service." |
| 198 | + in str(e.value) |
| 199 | + ) |
0 commit comments