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
Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 1–9why, one step back1import asyncio23import pytest4from uvicorn.config import Config5from uvicorn.main import Server678async def app(scope, receive, send):9 assert scope["type"] == "http"Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 10–16why, one step back10 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):Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 17–26why, one step back17 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!"})2526Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 27–38why, one step back27async 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!"})3738Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 39–48why, one step back39@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:Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 49–50why, one step back49 server.should_exit = True50 await task