From 0a4deea37089f7070047018aeaed0f251b2b0521 Mon Sep 17 00:00:00 2001 From: alberto Date: Sun, 29 Nov 2026 04:47:06 -0600 Subject: [PATCH] Dask scheduler and workers for the test profile and CI; writes-overlap, cancel and remote-mode tests: run concurrency and cancel tests; TEA gate cancels a re-analysis and checks nothing changed --- tests/test_runs_concurrency.py | 104 +++++++++++++++++++++++++++++++++ tests/test_tea.py | 31 ++++++++++ 2 files changed, 135 insertions(+) create mode 100644 tests/test_runs_concurrency.py diff --git a/tests/test_runs_concurrency.py b/tests/test_runs_concurrency.py new file mode 100644 index 0000000..dbba9a4 --- /dev/null +++ b/tests/test_runs_concurrency.py @@ -0,0 +1,104 @@ +"""Concurrency and cancellation through the real API and Dagster daemon.""" + +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): # the table was never created + platform.count("silver.person") + with pytest.raises(DBAPIError): # the table was never created + 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") + first, second = _submit(api), _submit(api) + if _get_run(api, second)["state"] != "queued": + pytest.skip("the second run was not queued") + ok(api.post(f"/runs/{second}/cancel")) + assert _wait_cancelled(api, second)["started"] is None + wait_for(api, first, 300) + + +@pytest.mark.skip(reason="needs killing the run worker; checked by hand") +def test_a_dead_run_does_not_stay_running(): + """The Dagster daemon detects a dead worker via `run_monitoring` in `dagster.yaml`.""" diff --git a/tests/test_tea.py b/tests/test_tea.py index 568a683..e77f7b5 100644 --- a/tests/test_tea.py +++ b/tests/test_tea.py @@ -267,6 +267,37 @@ def test_tea_from_the_hospital_to_indicators(api, scored_hospital, monkeypatch): _preview_matches_the_analysis_and_leaves_bronze_unchanged(scored_hospital, monkeypatch) _reanalysis_by_id_in_local_mode_matches_sync(api, scored_hospital) + _cancel_during_analysis_leaves_bronze_and_gold_untouched(api, scored_hospital) + + +def _cancel_during_analysis_leaves_bronze_and_gold_untouched(api, platform): + def snapshot(): + return ( + platform.query( + "select edf, valid, level, analysed_at from bronze.eeg_analysis order by edf" + ), + platform.count("bronze.eeg_metrics"), + platform.count("gold.tea_eeg_metrics"), + ) + + before = snapshot() + ids = [study["id"] for study in ok(api.get("/tea/eegs")) if study["valid"]] + body = {"scope": {"mode": "ids", "ids": ids}} + run_id = ok(api.post("/tea/eegs/analyze", json=body))["run_id"] + url = f"/runs/{run_id}" + deadline = time.monotonic() + 300 + while (run := ok(api.get(url)))["state"] != "running": + assert run["state"] not in {"succeeded", "failed", "cancelled"}, run + assert time.monotonic() < deadline, run + time.sleep(1) + + ok(api.post(f"{url}/cancel")) + deadline = time.monotonic() + 900 + while (run := ok(api.get(url)))["state"] != "cancelled": + assert run["state"] not in {"succeeded", "failed"}, run + assert time.monotonic() < deadline, run + time.sleep(1) + assert snapshot() == before def _reanalysis_by_id_in_local_mode_matches_sync(api, platform):