105 lines
3.7 KiB
Python
105 lines
3.7 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")
|
|
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`."""
|