"""Shared fixtures for the execution-pool recovery and isolation suites.""" from __future__ import annotations import sqlite3 import sys import time from pathlib import Path import pytest ROOT = Path(__file__).parents[2] SCRIPTS = ROOT / "services/hermes/scripts" SCM_SCRIPTS = ROOT / "services/hermes/scm-common/scripts" sys.path[:0] = [str(SCRIPTS), str(SCM_SCRIPTS)] import execution_pool_maintenance as maintenance # noqa: E402 import execution_pool_store as pool_store # noqa: E402 from testing.tests.test_hermes_execution_pool_coordinator_v2 import ( # noqa: E402 assignment_payload, binding, ) LOCKED = sqlite3.OperationalError("database is locked") @pytest.fixture(autouse=True) def clear_pool_deferrals(): """Keep the bounded deferral report from leaking between tests.""" maintenance.DEFERRALS.clear() yield maintenance.DEFERRALS.clear() def exhausted_store(path, exact=None): """A store whose only assignment has burned every fenced attempt.""" store = pool_store.PoolStore(path) store.add(exact or binding(attempt=3), assignment_payload()) store.offer(int((exact or binding(attempt=3))["worker_ordinal"])) with store._connect() as connection: connection.execute("UPDATE assignments SET lease_until=1") return store def states(store): """Every durable row as (run_id, ordinal, attempt, state).""" return [ tuple(row) for row in store._connect().execute( "SELECT run_id,worker_ordinal,attempt,state FROM assignments ORDER BY run_id" ) ] def age(store, seconds): """Backdate every row so retention-based collection can be exercised.""" with store._connect() as connection: connection.execute("UPDATE assignments SET updated_at=?", (time.time() - seconds,))