Browse Source

test: replace synthetic collector test with lifecycle integration and remove unreachable collector branch

Made-with: Cursor
pull/15097/head
essentiaMarco 3 months ago
parent
commit
17f10eb0a8
  1. 4
      fastapi/routing.py
  2. 24
      tests/test_dependency_lifespan_scope.py

4
fastapi/routing.py

@ -212,10 +212,6 @@ def _collect_lifespan_dependants(router: "APIRouter") -> list[Dependant]:
for route in router.routes: for route in router.routes:
if isinstance(route, APIRoute): if isinstance(route, APIRoute):
flat = get_flat_dependant(route.dependant) flat = get_flat_dependant(route.dependant)
if flat.computed_scope == "lifespan":
key = flat.cache_key
if key not in seen:
seen[key] = flat
for d in flat.dependencies: for d in flat.dependencies:
if d.computed_scope == "lifespan": if d.computed_scope == "lifespan":
key = d.cache_key key = d.cache_key

24
tests/test_dependency_lifespan_scope.py

@ -4,7 +4,7 @@ from contextlib import asynccontextmanager
from typing import Annotated from typing import Annotated
import pytest import pytest
from fastapi import Depends, FastAPI from fastapi import APIRouter, Depends, FastAPI
from fastapi.exceptions import DependencyScopeError from fastapi.exceptions import DependencyScopeError
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from starlette.requests import Request from starlette.requests import Request
@ -104,13 +104,14 @@ def test_lifespan_dependency_same_instance_across_requests() -> None:
def test_lifespan_dependency_decorator_level_dependencies_runs_at_startup() -> None: def test_lifespan_dependency_decorator_level_dependencies_runs_at_startup() -> None:
"""Lifespan deps declared in decorator-level dependencies are initialized once at startup.""" """Decorator-level dependencies=[Depends(..., scope='lifespan')] run at startup once."""
events: list[str] = [] started: list[str] = []
stopped: list[str] = []
def lifespan_dep() -> str: def lifespan_dep() -> str:
events.append("start") started.append("lifespan_dep")
yield "ok" yield "ok"
events.append("stop") stopped.append("lifespan_dep")
app = FastAPI() app = FastAPI()
@ -118,15 +119,16 @@ def test_lifespan_dependency_decorator_level_dependencies_runs_at_startup() -> N
def root() -> dict[str, str]: def root() -> dict[str, str]:
return {"ok": "yes"} return {"ok": "yes"}
assert events == []
with TestClient(app) as client: with TestClient(app) as client:
assert events == ["start"] assert started == ["lifespan_dep"]
r1 = client.get("/") r1 = client.get("/")
r2 = client.get("/") r2 = client.get("/")
assert r1.status_code == 200 assert r1.status_code == 200 and r2.status_code == 200
assert r2.status_code == 200 assert r1.json() == {"ok": "yes"}
assert events == ["start"] assert r2.json() == {"ok": "yes"}
assert events == ["start", "stop"] assert started == ["lifespan_dep"]
assert stopped == ["lifespan_dep"]
def test_lifespan_dependency_synthetic_request_receive_send() -> None: def test_lifespan_dependency_synthetic_request_receive_send() -> None:

Loading…
Cancel
Save