Skip to content

Commit 634cf82

Browse files
committed
Add test for lifespan required but not available
Signed-off-by: Anuraag Agrawal <[email protected]>
1 parent 5377386 commit 634cf82

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

test/test_lifespan.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
import pytest
66
import uvicorn
7+
from httpx import ASGITransport, AsyncClient
78
from uvicorn.config import LOGGING_CONFIG
89

10+
from connectrpc.errors import ConnectError
11+
912
from .haberdasher_connect import (
1013
Haberdasher,
1114
HaberdasherASGIApplication,
@@ -160,3 +163,37 @@ async def counting_haberdasher():
160163
server.should_exit = True
161164
await uvicorn_task
162165
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

Comments
 (0)