"""Restarts, shutdown and concurrency limits.""" import asyncio import time import pytest from estimator.config.sections import DatabaseCfg from estimator.domain.enums import Region from estimator.domain.estimates import NewVersion from estimator.infra.db import SqlEstimateRepo, make_engine, make_session_factory from tests.fakes import AutoGateway from tests.unit.api.test_estimates_api import A, create, make_client, wait def db(dsn, coro_fn): """Run an async function against the test database, outside the app.""" async def go(): engine = make_engine(DatabaseCfg(dsn=dsn)) try: return await coro_fn(SqlEstimateRepo(make_session_factory(engine))) finally: await engine.dispose() return asyncio.run(go()) def seed(dsn, status): async def go(repo): eid, *_ = await repo.create( client="client_a", idempotency_key=None, request_hash="h", data=NewVersion(requirements="бот после рестарта", region=Region.RU, currency="RUB"), ) if status != "pending": await repo.update_version(eid, 1, status=status, step="estimate") return eid return db(dsn, go) def test_restart_requeues_pending_and_fails_interrupted(monkeypatch, backend_dsn): pending_id = seed(backend_dsn, "pending") running_id = seed(backend_dsn, "running") gw = AutoGateway() with make_client(monkeypatch, backend_dsn, gw) as c: assert wait(c, pending_id)["status"] == "done" lost = c.get(f"/api/estimates/{running_id}", headers=A).json() assert lost["status"] == "failed" and lost["error"]["code"] == "interrupted" # the interrupted one is not silently re-run (that could burn tokens in a crash loop) assert gw.steps().count("clarify") == 1 # and it can be recalculated on purpose assert c.post(f"/api/estimates/{running_id}/recalculate", headers=A).status_code == 202 assert wait(c, running_id)["status"] == "done" def test_shutdown_marks_unfinished_jobs_as_interrupted(monkeypatch, backend_dsn): gw = AutoGateway() gw.block() # never released with make_client(monkeypatch, backend_dsn, gw, EST_JOBS__SHUTDOWN_GRACE_S="0.1") as c: eid = create(c)["id"] wait(c, eid, until=("running",)) rec = db(backend_dsn, lambda repo: repo.get("client_a", eid)) assert rec.status.value == "failed" and rec.error_code == "interrupted" def test_job_concurrency_limit_is_applied(monkeypatch, backend_dsn): gw = AutoGateway() gate = gw.block() with make_client(monkeypatch, backend_dsn, gw, EST_JOBS__MAX_CONCURRENT="1") as c: first = create(c)["id"] second = create(c, {"requirements": "второй бот"})["id"] wait(c, first, until=("running",)) time.sleep(0.2) assert c.get(f"/api/estimates/{second}", headers=A).json()["status"] == "pending" gate.set() assert wait(c, first)["status"] == "done" and wait(c, second)["status"] == "done" def test_service_refuses_to_start_without_a_schema(monkeypatch, tmp_path): gw = AutoGateway() empty = f"sqlite:///{tmp_path / 'empty.db'}" # no tables with pytest.raises(RuntimeError, match="migrate"), make_client(monkeypatch, empty, gw): pass def test_migrate_command_creates_the_schema(monkeypatch, tmp_path): from estimator.__main__ import main from estimator.config import get_settings dsn = f"sqlite:///{tmp_path / 'cli.db'}" monkeypatch.setenv("EST_DATABASE__DSN", dsn) monkeypatch.setenv("EST_LLM__ANTHROPIC__API_KEY", "sk-test") get_settings.cache_clear() try: assert main(["migrate"]) == 0 finally: get_settings.cache_clear() with make_client(monkeypatch, dsn, AutoGateway()) as c: assert c.get("/health").status_code == 200 # the service starts on a migrated database