Files
WebApi/tests/test_runs_concurrency.py
T
alberto ec5d945f71 Cleanup of the TEA parity work: no prose in code, no hacks, everything in its place
1. No prose in code
- Remove parity-added Python comments and docstrings across the EEG, FHIR,
  execution, preview, training and run code and tests. Preserve endpoint
  descriptions as explicit FastAPI schema metadata.

2. No hacks, no glued code
- Read the declared TEA patient filters and training inputs directly and
  use the same TEA check for exposing and validating reference options.
- Give the Dagster GraphQL request one helper. Keep lock backoff and
  cancellation polling because their timing and error behavior are
  covered by run tests and they handle different Dagster states.
- Use the renamed FHIR report ID helper in the hospital test. Keep
  TEA_GATE_SLOW local to that test and preserve the test assertions.

3. Everything in its place
- Keep endpoint and run handling in WebApi and reuse Core and Persistance
  for domain rules and reads. Leave tests in the existing testpaths.
2026-11-30 13:41:49 -06:00

112 lines
3.9 KiB
Python

import json
import time
from pathlib import Path
import pytest
from sqlalchemy.exc import DBAPIError
from test_conectar import ok, wait_for
from conftest import put_concept_map
FIXTURES = Path(__file__).parents[2] / "tests" / "fixtures"
TERMINAL = {"succeeded", "failed", "cancelled"}
def load(api, domain):
put_concept_map(api, f"hospital_{domain}")
drawn = (FIXTURES / "pipelines" / f"hospital_{domain}.json").read_text()
pipeline = json.loads(drawn.replace("{hospital}", str(FIXTURES / "hospital")))
ok(api.put(f"/ingest/pipelines/{domain}", json=pipeline))
def _submit(api):
return ok(api.post("/ingest/pipelines/cacu/runs", json={}))["run_id"]
def _get_run(api, run_id):
return ok(api.get(f"/runs/{run_id}"))
def _wait_cancelled(api, run_id):
deadline = time.monotonic() + 300
while (run := _get_run(api, run_id))["state"] not in TERMINAL:
assert time.monotonic() < deadline, run
time.sleep(1)
assert run["state"] == "cancelled", run
return run
@pytest.mark.parametrize("platform", ["sqlite"], indirect=True)
def test_writing_runs_never_overlap(api, platform):
load(api, "cacu")
first, second = _submit(api), _submit(api)
queued = False
deadline = time.monotonic() + 300
while True:
first_run, second_run = _get_run(api, first), _get_run(api, second)
queued |= first_run["state"] == "running" and second_run["state"] == "queued"
for run in (first_run, second_run):
assert run["state"] not in {"failed", "cancelled"}, run
if first_run["state"] == second_run["state"] == "succeeded":
break
assert time.monotonic() < deadline, (first_run, second_run)
time.sleep(1)
earlier, later = sorted((first_run, second_run), key=lambda run: run["started"])
assert later["started"] >= earlier["ended"] - 1
if not queued:
pytest.skip("the first run ended before the second was submitted")
@pytest.mark.parametrize("platform", ["sqlite"], indirect=True)
def test_cancelling_an_ingest_run_leaves_no_silver_or_gold(api, platform):
load(api, "cacu")
run_id = _submit(api)
deadline = time.monotonic() + 300
while True:
run = _get_run(api, run_id)
if run["state"] == "running" and run["info"]:
break
assert run["state"] not in TERMINAL, run
assert time.monotonic() < deadline, run
time.sleep(1)
response = api.post(f"/runs/{run_id}/cancel")
if response.status_code == 409:
pytest.skip("the run ended before the cancel")
ok(response)
cancelled = _wait_cancelled(api, run_id)
assert cancelled["status"] == "CANCELED"
assert cancelled["error"] is None
with pytest.raises(DBAPIError):
platform.count("silver.person")
with pytest.raises(DBAPIError):
platform.count("gold.cacu_risk")
wait_for(api, _submit(api), 300)
assert platform.count("silver.person") > 0
@pytest.mark.parametrize("platform", ["sqlite"], indirect=True)
def test_cancelling_a_queued_run_never_starts_it(api, platform):
load(api, "cacu")
runs = [_submit(api), _submit(api)]
deadline = time.monotonic() + 120
while True:
states = {run_id: _get_run(api, run_id)["state"] for run_id in runs}
running = [r for r, state in states.items() if state == "running"]
queued = [r for r, state in states.items() if state == "queued"]
if len(running) == 1 and len(queued) == 1:
break
if any(state in TERMINAL for state in states.values()):
pytest.skip(f"a run ended before the other was seen queued: {states}")
assert time.monotonic() < deadline, states
time.sleep(0.5)
ok(api.post(f"/runs/{queued[0]}/cancel"))
assert _wait_cancelled(api, queued[0])["steps"] == []
wait_for(api, running[0], 300)
@pytest.mark.skip(reason="needs killing the run worker; checked by hand")
def test_a_dead_run_does_not_stay_running():
pass