conftest.py
This file
Path
tests/conftest.py
50 lines · 1.2 KiB · Python
Last changed by
Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
History of this fileTake it
1import asyncio23import pytest4from uvicorn.config import Config5from uvicorn.main import Server678async def app(scope, receive, send):9 assert scope["type"] == "http"10 if scope["path"] == "/slow_response":11 await slow_response(scope, receive, send)12 else:13 await hello_world(scope, receive, send)141516async def hello_world(scope, receive, send):17 await send(18 {19 "type": "http.response.start",20 "status": 200,21 "headers": [[b"content-type", b"text/plain"]],22 }23 )24 await send({"type": "http.response.body", "body": b"Hello, world!"})252627async def slow_response(scope, receive, send):28 await asyncio.sleep(0.01)29 await send(30 {31 "type": "http.response.start",32 "status": 200,33 "headers": [[b"content-type", b"text/plain"]],34 }35 )36 await send({"type": "http.response.body", "body": b"Hello, world!"})373839@pytest.fixture40async def server():41 config = Config(app=app, lifespan="off")42 server = Server(config=config)43 task = asyncio.ensure_future(server.serve())44 try:45 while not server.started:46 await asyncio.sleep(0.0001)47 yield server48 finally:49 server.should_exit = True50 await task