Skip to content

Commit

Permalink
cr: revert changes in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitagashkov committed Dec 7, 2024
1 parent 91ebef3 commit 4693336
Showing 1 changed file with 12 additions and 55 deletions.
67 changes: 12 additions & 55 deletions tests/middleware/test_base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import annotations

import contextvars
import gc
from contextlib import AsyncExitStack
from typing import Any, AsyncGenerator, AsyncIterator, Generator, NoReturn
from typing import Any, AsyncGenerator, AsyncIterator, Generator

import anyio
import pytest
Expand Down Expand Up @@ -211,17 +210,24 @@ async def dispatch(
) -> Response:
ctxvar.set("set by middleware")
resp = await call_next(request)
# BaseHTTPMiddleware creates a TaskGroup which copies the context
# and erases any changes to it made within the TaskGroup
assert ctxvar.get() == "set by middleware"
assert ctxvar.get() == "set by endpoint"
return resp # pragma: no cover


@pytest.mark.parametrize(
"middleware_cls",
[
CustomMiddlewareWithoutBaseHTTPMiddleware,
CustomMiddlewareUsingBaseHTTPMiddleware,
pytest.param(
CustomMiddlewareUsingBaseHTTPMiddleware,
marks=pytest.mark.xfail(
reason=(
"BaseHTTPMiddleware creates a TaskGroup which copies the context"
"and erases any changes to it made within the TaskGroup"
),
raises=AssertionError,
),
),
],
)
def test_contextvars(
Expand Down Expand Up @@ -1147,52 +1153,3 @@ async def send(message: Message) -> None:
{"type": "http.response.body", "body": b"good!", "more_body": True},
{"type": "http.response.body", "body": b"", "more_body": False},
]


# XXX: Do we even need `test_anyio_streams_cleanup_*`?
# 1. Warning is triggered in other tests for unhandled exceptions. These tests
# are a bit special because they trigger `gc.collect()` so, warnings trigger
# within a test and not when GC runs.
# 2. Tests will become evergreen if `anyio` decides to stop emitting warnings
# on non-closed streams deletion: https://github.com/agronholm/anyio/blob/4.6.2.post1/src/anyio/streams/memory.py#L183


def test_anyio_streams_cleanup_exc_in_route(test_client_factory: TestClientFactory) -> None:
async def error(_: Request) -> NoReturn:
raise RuntimeError("Oops!")

class NoopMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
return await call_next(request)

app = Starlette(
routes=[Route("/error", endpoint=error)],
middleware=[Middleware(NoopMiddleware)],
)
client = test_client_factory(app, raise_server_exceptions=False)
resp = client.get("/error")
assert resp.status_code == 500
assert resp.text == "Internal Server Error"

gc.collect() # to get warning right away instead of waiting for GC


def test_anyio_streams_cleanup_exc_in_middleware(test_client_factory: TestClientFactory) -> None:
async def ok(_: Request) -> PlainTextResponse:
return PlainTextResponse("OK")

class BreakingMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> NoReturn:
await call_next(request)
raise RuntimeError("Oops!")

app = Starlette(
routes=[Route("/ok", endpoint=ok)],
middleware=[Middleware(BreakingMiddleware)],
)
client = test_client_factory(app, raise_server_exceptions=False)
resp = client.get("/ok")
assert resp.status_code == 500
assert resp.text == "Internal Server Error"

gc.collect() # to get warning right away instead of waiting for GC

0 comments on commit 4693336

Please sign in to comment.