Files
FabledGrimoire/tests/test_startup_survives_a_slow_database.py
bvandeusenandClaude Opus 5.5 0f5ef0be63
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 56s
CI & Build / integration (push) Successful in 1m4s
CI & Build / Python tests (push) Successful in 1m55s
CI & Build / Build & push image (push) Successful in 44s
rename(547): the server is grimoire — package, image, DB defaults, backups
Step 2 of the Scribe -> Grimoire rename.

- src/scribe -> src/grimoire, and every import, patch target, module path,
  hypercorn entry point and `python -m grimoire.restore` with it; pyproject,
  uv.lock and the frontend package are named grimoire.
- Image git.fabledsword.com/bvandeusen/fabledgrimoire; CI's Postgres service
  runs as grimoire / grimoire_test.
- Compose files default the DB user, database and (prod) volume to grimoire,
  each overridable from .env, so an install created as Scribe keeps its data
  by setting POSTGRES_USER, POSTGRES_DB and PGDATA_VOLUME.
- Backups are written as grimoire-<ts>.tar with format grimoire-backup-tar.
  Archives named scribe-* with the scribe-backup-tar manifest still restore
  and are still counted at the destination (folder and S3), with tests.
- Exports, the MCP auth realm, the ASGI scope key and the persistent logger
  carry the new name.

Kept on purpose: the secret-sealing key label stays "scribe.secrets.v1",
because every stored secret was derived under it.

Plugin identity (plugin name, hooks, env vars, .scribe marker, skills) is
step 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:50:45 -04:00

218 lines
8.9 KiB
Python

"""A sick database must not stop the app from starting.
THE FAILURE THESE GUARD. A storage stall can make Postgres answer in minutes
instead of milliseconds. If the app starts into one, a startup read with no
deadline hangs; Hypercorn kills the worker at its 60-second lifespan timeout;
and nothing retries a failed lifespan. A short disk hiccup becomes an outage
that only a human restart can clear.
Every assertion here is about the SHAPE that makes that possible, not about
the stall, which no test can reproduce and no code can prevent:
- the startup read returns on a schedule of its own rather than the
database's;
- the backfill cannot run while startup is still running;
- the flag that releases it is released even when startup fails, because an
undeadlined wait is only safe if the wake-up is unmissable.
"""
import asyncio
from unittest.mock import AsyncMock, patch
import pytest
# ── the startup read is bounded ──────────────────────────────────────────────
@pytest.mark.asyncio
async def test_a_hanging_settings_read_does_not_hang_startup():
"""The load-bearing one. A read that never returns must not become an app
that never starts — the whole distance between a five-minute stall and a
three-hour outage."""
from grimoire.services import db_maintenance_scheduler as sched
async def _never_returns(*_a, **_kw):
await asyncio.Event().wait() # exactly what the disk stall did
with patch.object(sched, "get_admin_setting", _never_returns), \
patch.object(sched, "_STARTUP_READ_TIMEOUT", 0.05):
hour = await asyncio.wait_for(sched.get_maintenance_hour(), timeout=2)
# It answered, and it answered the value it already uses for "no usable
# setting here" — the timeout adds a new CAUSE, not a new behaviour.
assert hour == sched._DEFAULT_HOUR
@pytest.mark.asyncio
async def test_the_timeout_is_logged_loudly_enough_to_find():
"""Without a WARNING the app logs a few scheduler lines and stops, and the
cause is only recoverable from Postgres's own log. This is the breadcrumb
that names it."""
from grimoire.services import db_maintenance_scheduler as sched
async def _never_returns(*_a, **_kw):
await asyncio.Event().wait()
with patch.object(sched, "get_admin_setting", _never_returns), \
patch.object(sched, "_STARTUP_READ_TIMEOUT", 0.05), \
patch.object(sched.logger, "warning") as warn:
# Bounded here too — a test that awaits a call which is supposed to
# have a deadline must not be the thing without one.
await asyncio.wait_for(sched.get_maintenance_hour(), timeout=2)
assert warn.called
said = " ".join(str(a) for a in warn.call_args.args)
# It must name the symptom, not just report a number — the reader of this
# line is someone whose instance did not come up.
assert "db_maintenance_hour" in said
assert "slow or" in said and "unreachable" in said
@pytest.mark.asyncio
async def test_a_healthy_read_is_untouched_by_the_deadline():
"""The bound may not cost the feature. A configured hour still wins."""
from grimoire.services import db_maintenance_scheduler as sched
with patch.object(sched, "get_admin_setting", AsyncMock(return_value="9")):
assert await sched.get_maintenance_hour() == 9
@pytest.mark.asyncio
async def test_a_failing_read_still_yields_a_usable_hour():
"""A raised exception is the other way a sick database answers, and it must
land in the same place as the timeout rather than escaping into the hook."""
from grimoire.services import db_maintenance_scheduler as sched
with patch.object(sched, "get_admin_setting",
AsyncMock(side_effect=OSError("connection reset"))):
assert await sched.get_maintenance_hour() == sched._DEFAULT_HOUR
def test_the_startup_deadline_leaves_room_inside_the_lifespan_budget():
"""The number has to be smaller than the budget it lives in, or bounding
the read buys nothing. Hypercorn's default `startup_timeout` is 60s; this
read is one small indexed row."""
from grimoire.services.db_maintenance_scheduler import _STARTUP_READ_TIMEOUT
assert 0 < _STARTUP_READ_TIMEOUT <= 10
# ── the backfill does not race startup ───────────────────────────────────────
@pytest.mark.asyncio
async def test_the_deferred_backfill_waits_for_the_startup_flag():
""""Never blocks the server from accepting requests" is true of requests
and false of startup: a backfill that starts before startup finishes
competes with it for one connection pool.
Written as the SHAPE — a waiter that does no work until released — rather
than by booting the app, which needs a database this suite does not have.
"""
released = asyncio.Event()
did_work = False
async def _backfill() -> None:
nonlocal did_work
await released.wait()
did_work = True
task = asyncio.create_task(_backfill())
await asyncio.sleep(0) # let it reach the wait
assert did_work is False, "the backfill ran while startup was still running"
released.set()
await asyncio.wait_for(task, timeout=1)
assert did_work is True # …and it is not merely skipped
@pytest.mark.asyncio
async def test_the_flag_is_released_even_when_startup_fails():
"""The waiter has no deadline of its own, so
the release has to be unmissable: a `finally`, never a last line. Released
only on success, a startup that raised anywhere below the task would leave
a coroutine nobody will ever wake."""
flag = asyncio.Event()
async def _hook_that_blows_up() -> None:
try:
raise RuntimeError("a scheduler failed to start")
finally:
flag.set()
with pytest.raises(RuntimeError):
await _hook_that_blows_up()
assert flag.is_set()
def test_startup_releases_the_flag_in_a_finally():
"""The guard on the real hook, read as source because booting it needs a
database. Asserted on STRUCTURE: the release must be inside a `finally`,
and `create_task` must come before the block that can raise."""
import inspect
from grimoire import app as app_module
src = inspect.getsource(app_module.create_app)
assert "_startup_finished.set()" in src
body = src[src.index("asyncio.create_task(_delayed_backfill())"):]
finally_at = body.index("finally:")
assert finally_at < body.index("_startup_finished.set()"), (
"the flag is released after the work rather than in a finally — a "
"startup that raises would strand the backfill forever"
)
assert "await _startup_finished.wait()" in src
# ── the engine cannot wait forever to connect ────────────────────────────────
def test_the_engine_bounds_how_long_a_connect_may_take():
"""asyncpg's default connect timeout is 60s — the entire lifespan budget,
spent before a query is even sent.
Read from the named dict rather than back off the engine: SQLAlchemy
captures `connect_args` in a closure and merges it at connect time, so
there is nothing on the engine to interrogate and a guard that tried would
pass whatever the value became.
"""
from grimoire.models import _CONNECT_ARGS
timeout = _CONNECT_ARGS.get("timeout")
assert timeout is not None, "a connect with no deadline"
assert 0 < timeout < 60
def test_no_blanket_command_timeout_was_added_with_it():
"""The deliberate omission, guarded so nobody adds it as an obvious
follow-up. `command_timeout` applies to EVERY statement, and this app runs
long ones on purpose — the embedding backfills, VACUUM ANALYZE. It would
trade a hung startup for killed maintenance."""
from grimoire.models import _CONNECT_ARGS
assert "command_timeout" not in _CONNECT_ARGS
def test_the_engine_actually_uses_those_args():
"""The dict is only a guard surface if the engine is built from it. Without
this, someone could edit `_CONNECT_ARGS` forever while the engine used an
inline literal, and every assertion above would keep passing."""
import inspect
from grimoire import models
src = inspect.getsource(models)
assert "connect_args=_CONNECT_ARGS" in src
def test_the_startup_guards_can_fail():
"""Each assertion above has to be able to bite."""
from grimoire.services.db_maintenance_scheduler import _STARTUP_READ_TIMEOUT
# An unbounded read is the thing being prevented.
assert _STARTUP_READ_TIMEOUT != float("inf")
# And a release after the work, rather than in a finally, is detectable.
after_the_work = "try:\n work()\nfinally:\n pass\nflag.set()"
assert after_the_work.index("finally:") > after_the_work.index("try:")
assert after_the_work.index("flag.set()") > after_the_work.index("finally:")