Skip to content

Commit

Permalink
Replace pytest-asyncio marker by anyio (#1155)
Browse files Browse the repository at this point in the history
* drop pytest-asyncio

* add event_loop fixture for MockLoop tests

* REVERTME: install the latest pypa toolchain

* use a patched anyio that grabs an excgroup of failed tasks

* setuptools_scm!!!

* python -m pip

* python3 isn't installed on GHA?

* ah it's bash quoting

* log task that raised the exception

* Add missing markers

* simplify fixture

* avoid hacking asyncio._set_running_loop in test_http.py

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
  • Loading branch information
graingert and Kludex committed Jun 17, 2022
1 parent f4bb5ea commit d2b14ee
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 214 deletions.
2 changes: 0 additions & 2 deletions requirements.txt
Expand Up @@ -22,8 +22,6 @@ cryptography==3.4.8
coverage==6.4
coverage-conditional-plugin==0.5.0
httpx==1.0.0b0
pytest-asyncio==0.15.1


# Documentation
mkdocs==1.3.0
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Expand Up @@ -138,6 +138,11 @@ def reload_directory_structure(tmp_path_factory: pytest.TempPathFactory):
yield root


@pytest.fixture
def anyio_backend() -> str:
return "asyncio"


@pytest.fixture(scope="function")
def logging_config() -> dict:
return deepcopy(LOGGING_CONFIG)
Expand Down
8 changes: 4 additions & 4 deletions tests/middleware/test_debug.py
Expand Up @@ -4,7 +4,7 @@
from uvicorn.middleware.debug import DebugMiddleware


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_debug_text():
async def app(scope, receive, send):
raise RuntimeError("Something went wrong")
Expand All @@ -24,7 +24,7 @@ async def app(scope, receive, send):
assert "RuntimeError" in response.text


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_debug_html():
async def app(scope, receive, send):
raise RuntimeError("Something went wrong")
Expand All @@ -43,7 +43,7 @@ async def app(scope, receive, send):
assert "RuntimeError" in response.text


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_debug_after_response_sent():
async def app(scope, receive, send):
await send({"type": "http.response.start", "status": 204, "headers": []})
Expand All @@ -63,7 +63,7 @@ async def app(scope, receive, send):
assert response.content == b""


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_debug_not_http():
async def app(scope, send, receive):
raise RuntimeError("Something went wrong")
Expand Down
12 changes: 6 additions & 6 deletions tests/middleware/test_logging.py
Expand Up @@ -27,7 +27,7 @@ async def app(scope, receive, send):
await send({"type": "http.response.body", "body": b"", "more_body": False})


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_trace_logging(caplog, logging_config):
config = Config(
app=app, log_level="trace", log_config=logging_config, lifespan="auto"
Expand All @@ -48,7 +48,7 @@ async def test_trace_logging(caplog, logging_config):
assert "ASGI [2] Completed" in messages.pop(0)


@pytest.mark.asyncio
@pytest.mark.anyio
@pytest.mark.parametrize("http_protocol", [("h11"), ("httptools")])
async def test_trace_logging_on_http_protocol(http_protocol, caplog, logging_config):
config = Config(
Expand All @@ -71,7 +71,7 @@ async def test_trace_logging_on_http_protocol(http_protocol, caplog, logging_con
assert any(" - HTTP connection lost" in message for message in messages)


@pytest.mark.asyncio
@pytest.mark.anyio
@pytest.mark.parametrize("ws_protocol", [("websockets"), ("wsproto")])
async def test_trace_logging_on_ws_protocol(ws_protocol, caplog, logging_config):
async def websocket_app(scope, receive, send):
Expand Down Expand Up @@ -107,7 +107,7 @@ async def open_connection(url):
assert any(" - WebSocket connection lost" in message for message in messages)


@pytest.mark.asyncio
@pytest.mark.anyio
@pytest.mark.parametrize("use_colors", [(True), (False), (None)])
async def test_access_logging(use_colors, caplog, logging_config):
config = Config(app=app, use_colors=use_colors, log_config=logging_config)
Expand All @@ -125,7 +125,7 @@ async def test_access_logging(use_colors, caplog, logging_config):
assert '"GET / HTTP/1.1" 204' in messages.pop()


@pytest.mark.asyncio
@pytest.mark.anyio
@pytest.mark.parametrize("use_colors", [(True), (False)])
async def test_default_logging(use_colors, caplog, logging_config):
config = Config(app=app, use_colors=use_colors, log_config=logging_config)
Expand All @@ -146,7 +146,7 @@ async def test_default_logging(use_colors, caplog, logging_config):
assert "Shutting down" in messages.pop(0)


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_unknown_status_code(caplog):
async def app(scope, receive, send):
assert scope["type"] == "http"
Expand Down
4 changes: 2 additions & 2 deletions tests/middleware/test_message_logger.py
Expand Up @@ -6,7 +6,7 @@
from uvicorn.middleware.message_logger import MessageLoggerMiddleware


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_message_logger(caplog):
async def app(scope, receive, send):
await receive()
Expand All @@ -31,7 +31,7 @@ async def app(scope, receive, send):
)


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_message_logger_exc(caplog):
async def app(scope, receive, send):
raise RuntimeError()
Expand Down
6 changes: 3 additions & 3 deletions tests/middleware/test_proxy_headers.py
Expand Up @@ -20,7 +20,7 @@ async def app(
await response(scope, receive, send)


@pytest.mark.asyncio
@pytest.mark.anyio
@pytest.mark.parametrize(
("trusted_hosts", "response_text"),
[
Expand Down Expand Up @@ -50,7 +50,7 @@ async def test_proxy_headers_trusted_hosts(
assert response.text == response_text


@pytest.mark.asyncio
@pytest.mark.anyio
@pytest.mark.parametrize(
("trusted_hosts", "response_text"),
[
Expand Down Expand Up @@ -87,7 +87,7 @@ async def test_proxy_headers_multiple_proxies(
assert response.text == response_text


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_proxy_headers_invalid_x_forwarded_for() -> None:
app_with_middleware = ProxyHeadersMiddleware(app, trusted_hosts="*")
async with httpx.AsyncClient(
Expand Down
10 changes: 5 additions & 5 deletions tests/middleware/test_wsgi.py
Expand Up @@ -50,7 +50,7 @@ def return_exc_info(environ: Environ, start_response: StartResponse) -> List[byt
return [output]


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_wsgi_get() -> None:
app = WSGIMiddleware(hello_world)
async with httpx.AsyncClient(app=app, base_url="http://testserver") as client:
Expand All @@ -59,7 +59,7 @@ async def test_wsgi_get() -> None:
assert response.text == "Hello World!\n"


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_wsgi_post() -> None:
app = WSGIMiddleware(echo_body)
async with httpx.AsyncClient(app=app, base_url="http://testserver") as client:
Expand All @@ -68,7 +68,7 @@ async def test_wsgi_post() -> None:
assert response.text == '{"example": 123}'


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_wsgi_put_more_body() -> None:
async def generate_body() -> AsyncGenerator[bytes, None]:
for _ in range(1024):
Expand All @@ -81,7 +81,7 @@ async def generate_body() -> AsyncGenerator[bytes, None]:
assert response.text == "123456789abcdef\n" * 64 * 1024


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_wsgi_exception() -> None:
# Note that we're testing the WSGI app directly here.
# The HTTP protocol implementations would catch this error and return 500.
Expand All @@ -91,7 +91,7 @@ async def test_wsgi_exception() -> None:
await client.get("/")


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_wsgi_exc_info() -> None:
# Note that we're testing the WSGI app directly here.
# The HTTP protocol implementations would catch this error and return 500.
Expand Down

0 comments on commit d2b14ee

Please sign in to comment.