Browse Source

🎨 Auto format

pull/15977/head
pre-commit-ci-lite[bot] 1 week ago
committed by GitHub
parent
commit
5c24dc0b17
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      fastapi/routing.py
  2. 13
      tests/test_thread_safety.py

4
fastapi/routing.py

@ -1589,7 +1589,7 @@ class _IncludedRouter(BaseRoute):
with self._rebuild_lock:
if routes_version == self._effective_candidates_version:
return self._effective_candidates
new_candidates: list["_EffectiveRouteContext | _IncludedRouter"] = []
new_candidates: list[_EffectiveRouteContext | _IncludedRouter] = []
candidates = self.original_router.routes
for route in candidates:
if isinstance(route, _IncludedRouter):
@ -1614,7 +1614,7 @@ class _IncludedRouter(BaseRoute):
with self._rebuild_lock:
if routes_version == self._effective_low_priority_routes_version:
return self._effective_low_priority_routes
new_low_priority: list["_EffectiveRouteContext"] = []
new_low_priority: list[_EffectiveRouteContext] = []
for route in self.original_router._low_priority_routes:
route_context = self._build_effective_context(route)
if route_context is not None:

13
tests/test_thread_safety.py

@ -1,4 +1,5 @@
"""Test thread safety of _IncludedRouter cache rebuild."""
import sys
import threading
@ -15,8 +16,10 @@ def test_effective_candidates_thread_safety():
app = FastAPI()
router = APIRouter()
for i in range(N_ROUTES):
def endpoint(i: int = i):
return {"i": i}
router.add_api_route(f"/r{i}", endpoint, methods=["GET"])
app.include_router(router)
@ -42,9 +45,7 @@ def test_effective_candidates_thread_safety():
assert not errors, f"Errors during concurrent requests: {errors}"
included = next(
r for r in app.router.routes if isinstance(r, _IncludedRouter)
)
included = next(r for r in app.router.routes if isinstance(r, _IncludedRouter))
n_candidates = len(included._effective_candidates)
assert n_candidates == N_ROUTES, (
f"Expected {N_ROUTES} cached candidates, got {n_candidates}"
@ -59,9 +60,13 @@ def test_effective_low_priority_thread_safety():
app = FastAPI()
router = APIRouter()
for i in range(N_ROUTES):
def endpoint(i: int = i):
return {"i": i}
router.add_api_route(f"/r{i}", endpoint, methods=["GET"], include_in_schema=False)
router.add_api_route(
f"/r{i}", endpoint, methods=["GET"], include_in_schema=False
)
app.include_router(router)
sys.setswitchinterval(1e-5)

Loading…
Cancel
Save