gabriel / musehub public
test_ui_session_in_order_265.py python
36 lines 1.4 KB
Raw
sha256:141d2a1e63256ef4fa78fe8e13e81febdb37c203a5a4c19665b80c025ca19054 fix(ui): never run database coroutines concurrently on the … Opus 5.5 minor ⚠ breaking 1 day ago
1 """musehub#265: UI handlers never run database coroutines concurrently on the request session.
2
3 ``asyncio.gather`` over coroutines that share one ``AsyncSession`` interleaves statements on a single connection. The
4 UI routes and the OpenGraph card loader await their queries in order (``musehub.db.in_order``).
5 """
6 from __future__ import annotations
7
8 import asyncio
9 import pathlib
10
11 import pytest
12
13 from musehub.db.in_order import in_order
14
15 _ROOT = pathlib.Path(__file__).resolve().parents[1] / "musehub"
16 _SCOPE = [*sorted((_ROOT / "api" / "routes" / "musehub").glob("ui*.py")), _ROOT / "api" / "routes" / "musehub" / "_ui_helpers.py",
17 _ROOT / "services" / "opengraph_cards.py"]
18
19
20 @pytest.mark.parametrize("path", _SCOPE, ids=lambda p: p.name)
21 def test_no_ui_handler_gathers_database_work(path: pathlib.Path) -> None:
22 assert "asyncio.gather(" not in path.read_text(), f"{path.name}: use musehub.db.in_order on the request session"
23
24
25 def test_in_order_awaits_sequentially_and_keeps_order() -> None:
26 log: list[str] = []
27
28 async def step(name: str, delay: float) -> str:
29 log.append(f"start {name}")
30 await asyncio.sleep(delay)
31 log.append(f"end {name}")
32 return name
33
34 result = asyncio.run(in_order(step("a", 0.02), step("b", 0.0)))
35 assert result == ("a", "b")
36 assert log == ["start a", "end a", "start b", "end b"] # never interleaved
File History 1 commit
sha256:141d2a1e63256ef4fa78fe8e13e81febdb37c203a5a4c19665b80c025ca19054 fix(ui): never run database coroutines concurrently on the … Opus 5.5 minor ⚠ 1 day ago