gabriel / musehub public
test_heavy_reads.py python
129 lines 4.8 KB
Raw
sha256:30c51e90dfe2486f79752f7305be1faf2f086d5407ee5e1307167b5a8c61549d perf(db): SQL audit pass 2 + Tier A review fixes; local tim… Opus 5.5 minor ⚠ breaking 2 days ago
1 """Heavy-read protections: deferred commit deltas, admission gate, cached symbol counts, web statement timeout."""
2 from __future__ import annotations
3
4 import asyncio
5
6 import pytest
7 from fastapi import HTTPException
8 from sqlalchemy import select
9 from sqlalchemy.dialects import postgresql
10
11 import musehub.main # noqa: F401 (imports every ORM model so the mappers configure)
12 from musehub.db import heavy_reads
13 from musehub.db.musehub_repo_models import light_commit, MusehubCommit
14
15
16 def test_light_commit_leaves_structured_delta_out_of_the_select() -> None:
17 sql = str(select(MusehubCommit).options(*light_commit()).compile(dialect=postgresql.dialect()))
18 assert "structured_delta" not in sql
19 assert "musehub_commits.message" in sql and "musehub_commits.snapshot_id" in sql
20 assert "structured_delta" in str(select(MusehubCommit).compile(dialect=postgresql.dialect()))
21
22
23 async def test_gate_admits_max_concurrent_then_503(monkeypatch: pytest.MonkeyPatch) -> None:
24 monkeypatch.setattr(heavy_reads, "_gate", asyncio.Semaphore(2))
25 release = asyncio.Event()
26
27 async def hold() -> None:
28 async with heavy_reads.heavy_read():
29 await release.wait()
30
31 tasks = [asyncio.create_task(hold()) for _ in range(2)]
32 await asyncio.sleep(0.05)
33 with pytest.raises(HTTPException) as exc:
34 async with heavy_reads.heavy_read(wait_seconds=0.05):
35 pass
36 assert exc.value.status_code == 503 and exc.value.headers == {"Retry-After": str(heavy_reads.RETRY_AFTER_SECONDS)}
37 release.set()
38 await asyncio.gather(*tasks)
39 async with heavy_reads.heavy_read(wait_seconds=0.05): # slots were released
40 pass
41
42
43 async def test_gate_releases_on_error(monkeypatch: pytest.MonkeyPatch) -> None:
44 monkeypatch.setattr(heavy_reads, "_gate", asyncio.Semaphore(1))
45 with pytest.raises(RuntimeError):
46 async with heavy_reads.heavy_read():
47 raise RuntimeError("boom")
48 async with heavy_reads.heavy_read(wait_seconds=0.05):
49 pass
50
51
52 async def test_symbol_counts_are_cached_for_a_short_time(monkeypatch: pytest.MonkeyPatch) -> None:
53 from musehub.api.routes.musehub import ui_symbols as m
54
55 monkeypatch.setattr(m, "_count_cache", {})
56 calls = 0
57
58 async def compute() -> int:
59 nonlocal calls
60 calls += 1
61 return 42
62
63 assert await m._cached_count(("k",), compute) == 42
64 assert await m._cached_count(("k",), compute) == 42
65 assert calls == 1
66 assert await m._cached_count(("other",), compute) == 42 and calls == 2
67 monkeypatch.setattr(m, "_COUNT_TTL_SECONDS", 0.0)
68 await m._cached_count(("k",), compute)
69 assert calls == 3
70
71
72 async def test_gate_caps_its_own_statements_with_set_local(monkeypatch: pytest.MonkeyPatch) -> None:
73 monkeypatch.setattr(heavy_reads, "_gate", asyncio.Semaphore(1))
74 seen: list[str] = []
75
76 class FakeSession:
77 async def execute(self, stmt: object) -> None:
78 seen.append(str(stmt))
79
80 async with heavy_reads.heavy_read(FakeSession()): # type: ignore[arg-type]
81 pass
82 assert seen == [f"SET LOCAL statement_timeout = {heavy_reads.STATEMENT_TIMEOUT_MS}"]
83
84
85 def test_busy_gate_answers_html_to_browsers_and_json_to_clients() -> None:
86 import musehub.main as m
87 from starlette.requests import Request
88
89 def req(accept: str) -> Request:
90 return Request({"type": "http", "method": "GET", "path": "/x", "headers": [(b"accept", accept.encode())], "query_string": b""})
91
92 html = asyncio.run(m._handle_heavy_read_busy(req("text/html,application/xhtml+xml"), heavy_reads.HeavyReadBusy()))
93 assert html.status_code == 503 and html.media_type == "text/html" and html.headers["retry-after"] == str(heavy_reads.RETRY_AFTER_SECONDS)
94 js = asyncio.run(m._handle_heavy_read_busy(req("application/json"), heavy_reads.HeavyReadBusy()))
95 assert js.status_code == 503 and js.media_type == "application/json"
96
97
98 async def test_ttl_cache_reuses_until_expiry_and_is_bounded() -> None:
99 from musehub.services.ttl_cache import TTLCache
100
101 now = [0.0]
102 cache: TTLCache[int] = TTLCache(10.0, max_entries=2, clock=lambda: now[0])
103 calls = 0
104
105 async def compute() -> int:
106 nonlocal calls
107 calls += 1
108 return calls
109
110 assert await cache.get_or_compute("a", compute) == 1
111 assert await cache.get_or_compute("a", compute) == 1
112 now[0] = 11.0
113 assert await cache.get_or_compute("a", compute) == 2 # expired
114 await cache.get_or_compute("b", compute)
115 await cache.get_or_compute("c", compute) # evicts the oldest
116 assert len(cache._data) == 2
117
118
119 async def test_ttl_cache_does_not_cache_failures() -> None:
120 from musehub.services.ttl_cache import TTLCache
121
122 cache: TTLCache[None] = TTLCache(60.0)
123
124 async def boom() -> None:
125 raise RuntimeError("drift")
126
127 for _ in range(2):
128 with pytest.raises(RuntimeError):
129 await cache.get_or_compute("schema", boom)
File History 2 commits
sha256:30c51e90dfe2486f79752f7305be1faf2f086d5407ee5e1307167b5a8c61549d perf(db): SQL audit pass 2 + Tier A review fixes; local tim… Opus 5.5 minor ⚠ 2 days ago
sha256:0759a1c1482fec68223e8017ef8d3124ddba5c489a8524b18c59a1992c37c5b6 fix(web): bound the cost of heavy page reads on a small database Opus 5.5 minor ⚠ 2 days ago