Files
WebApi/tests/test_runs_concurrency.py
T

117 lines
4.4 KiB
Python

"""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")
runs = [_submit(api), _submit(api)]
# Dagster launches one of the two (its queue sorts by priority, not by submission order);
# the other waits behind the `writes` limit: cancel that one, whichever it is.
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"))
# Dagster stamps a start time on a queued run it cancels; what matters is that nothing ran
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():
"""The Dagster daemon detects a dead worker via `run_monitoring` in `dagster.yaml`."""