"""Persisted independent jobs: lost HTTP replies, cancellation and large queues.""" # ruff: noqa: F811 import json import time import pytest from test_equipment_network import context # noqa: F401 from k1link.fleet import equipment_command_dispatch as dispatch from k1link.fleet import equipment_command_journal as journal from k1link.fleet import equipment_command_runtime as runtime from k1link.fleet.independent_equipment import EquipmentStore @pytest.fixture def ready(context): equipment, hosts, row = context for host in hosts.rows(): host["status"]["version"] = "0.1.43" host["status"]["capabilities"] = [dispatch.CAPABILITY, "arnavi-b2-durable-operations-v1"] hosts.save(host) return equipment, hosts, row def test_outage_and_restart_keep_exact_job_body_and_id_without_deadline(ready, monkeypatch): equipment, hosts, row = ready created = runtime.enqueue(hosts, row["id"], "1" * 32, "pin4-off") assert created["durable"] and "expires_at" not in created and "queue_deadline" not in created sent = [] def lost(host, method, path, body=None): sent.append((method, path, body)) raise OSError("reply lost") monkeypatch.setattr(dispatch, "request", lost) runtime.pump(hosts) assert sent[0][0:2] == ("POST", "/v1/operations") assert sent[0][2]["priority"] == "manual" assert "not_after" not in sent[0][2] hosts.equipment = EquipmentStore(equipment.root.parent, {}) later = time.time() + 86400 monkeypatch.setattr(time, "time", lambda: later) runtime.pump(hosts) assert sent[1] == sent[0] assert journal.listing(hosts.equipment, row["id"])["items"][0]["state"] == "submitting" assert "password" not in json.dumps(journal.listing(hosts.equipment, row["id"])) def test_all_jobs_reach_vps_despite_pending_background_and_results_project_by_id( ready, monkeypatch ): equipment, hosts, row = ready with equipment.db() as db: current = equipment._find(db, "instances", row["id"]) current["network_operation"] = { "id": "background", "state": "queued", "kind": "request-position", } db.execute("update instances set body=? where id=?", (json.dumps(current), row["id"])) for n, preset in enumerate(("pin4-off", "pin5-off", "pin4-on")): runtime.enqueue(hosts, row["id"], f"{n:032x}", preset) requests = {} def send(host, method, path, body=None): if method == "POST": requests[body["id"]] = body return dict(id=body["id"], kind=body["kind"], state="queued", durable=True) identifier = path.rsplit("/", 1)[1] return dict( id=identifier, kind=requests[identifier]["kind"], state="delivered", durable=True, delivered_at=time.time(), delivery_seconds=1, attempt_count=1, ) monkeypatch.setattr(dispatch, "request", send) runtime.pump(hosts) assert len(requests) == 3 later = time.time() + 3 monkeypatch.setattr(time, "time", lambda: later) runtime.pump(hosts) entries = journal.listing(equipment, row["id"])["items"] assert all(x["state"] == "delivered" and x["delivered_at"] == later for x in entries) with equipment.db() as db: assert ( equipment._find(db, "instances", row["id"])["network_operation"]["id"] == "background" ) assert db.execute("select count(*) from command_dispatch").fetchone()[0] == 0 def test_cancel_after_lost_admission_sends_tombstone_not_another_command(ready, monkeypatch): equipment, hosts, row = ready runtime.enqueue(hosts, row["id"], "1" * 32, "pin5-pulse") monkeypatch.setattr(dispatch, "request", lambda *a, **k: (_ for _ in ()).throw(OSError())) runtime.pump(hosts) runtime.cancel(equipment, row["id"], "1" * 32, hosts) later = time.time() + 3 monkeypatch.setattr(time, "time", lambda: later) seen = [] def stop(host, method, path, body=None): seen.append((method, path, body)) return dict( id="1" * 32, kind="pulse-output", state="stopped", durable=True, stop_requested=True ) monkeypatch.setattr(dispatch, "request", stop) runtime.pump(hosts) assert seen[0][1].endswith("/cancel") assert journal.listing(equipment, row["id"])["items"][0]["state"] == "stopped" def test_v2_queue_exceeds_old_limit_but_history_response_stays_bounded(ready): equipment, hosts, row = ready for n in range(80): runtime.enqueue(hosts, row["id"], f"{n:032x}", "pin4-off") response = journal.listing(equipment, row["id"]) assert response["counts"]["waiting"] == 80 assert len(response["items"]) == 25 assert len(response["tracked_items"]) <= 64