Files
NODEDC_MISSION_CORE/tests/test_infrastructure_queue.py
T
DCCONSTRUCTIONS f48411f8fe feat(simulation): integrate infrastructure jobs and semantic scene editing
Integrate infrastructure preparation and qualified map delivery with local scenes. Add semantic editing, object provenance, survey caching and operator scene tools.
2026-10-29 09:10:01 +03:00

124 lines
4.7 KiB
Python

"""Twenty-job FIFO, crash recovery and cancellation use the real durable store."""
import concurrent.futures
import pytest
from test_infrastructure_jobs import world
from k1link.simulation.ai_polygon.infrastructure_jobs import InfrastructureJobs
@pytest.fixture
def store(tmp_path, monkeypatch):
monkeypatch.setattr(
"k1link.simulation.ai_polygon.infrastructure_jobs.provider_identity", lambda: "f" * 64
)
return InfrastructureJobs(tmp_path)
def scene(index):
result = world()
result.update(world_id="aiworld-" + format(index, "032x"), sha256=format(index, "064x"))
return result
def test_twenty_jobs_run_fifo_after_failure_cancel_and_restart(store):
jobs = [store.request(scene(i)) for i in range(1, 21)]
assert [r["key"] for r in store.queue_rows()] == [r["key"] for r in jobs]
assert store.present(jobs[-1])["queue"]["position"] == 20
# A caller cannot jump the queue, even if it knows an identity.
with pytest.raises(RuntimeError):
store.claim(jobs[4]["key"], "worker")
first = jobs[0]["key"]
store.claim(first, "worker")
store.update(first, dict(action="fail"), "worker")
assert store.pending()["key"] == first
with pytest.raises(RuntimeError):
store.claim(jobs[1]["key"], "worker")
store.update(first, dict(action="release"), "worker")
assert store.pending()["key"] == jobs[1]["key"]
second = jobs[1]["key"]
store.claim(second, "worker")
store.cancel(second)
with pytest.raises(RuntimeError):
store.update(
second, dict(action="progress", phase="survey", completed=0, total=91), "worker"
)
with pytest.raises(RuntimeError):
store.update(second, dict(action="complete", result={}), "worker")
assert store.pending()["key"] == second
# Persisted cancellation and cleanup barrier survive a coordinator restart.
restored = InfrastructureJobs(store.root.parents[1])
assert restored.get(second)["cancel_requested"]
restored.update(second, dict(action="release", previous_owner="worker"), "new-worker")
assert restored.get(second)["status"] == "cancelled"
# Removing a waiting job never acquires a worker or delays its successor.
restored.cancel(jobs[4]["key"])
seen = []
while row := restored.pending():
seen.append(row["key"])
restored.claim(row["key"], "new-worker")
restored.update(row["key"], dict(action="complete", result={}), "new-worker")
assert restored.pending()["key"] == row["key"]
restored.update(row["key"], dict(action="release"), "new-worker")
assert seen == [j["key"] for i, j in enumerate(jobs) if i not in (0, 1, 4)]
assert restored.queue_rows() == []
def test_parallel_clicks_deduplicate_and_failed_retry_has_new_identity(store):
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool:
rows = list(pool.map(lambda _: store.request(scene(1), True, "a" * 32), range(20)))
assert len({r["key"] for r in rows}) == 1 and len(store.queue_rows()) == 1
key = rows[0]["key"]
store.cancel(key)
new = store.request(scene(1))
assert new["key"] != key and store.get(key)["status"] == "cancelled"
def test_pending_limit_is_explicit_and_never_discards_jobs(store):
for i in range(1, 33):
store.request(scene(i))
with pytest.raises(RuntimeError, match="Очередь заполнена"):
store.request(scene(33))
assert len(store.queue_rows()) == 32
def test_cancel_during_dispatch_cannot_strand_a_gpu_reservation(store):
import threading
from types import SimpleNamespace
row = store.request(scene(1))
entered = threading.Event()
cancel_started = threading.Event()
leases = []
def reserve(owner):
entered.set()
assert cancel_started.wait(3)
leases.append(owner)
def cancel():
assert entered.wait(3)
cancel_started.set()
return store.cancel(row["key"])
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool:
dispatched = pool.submit(
store.dispatch, "worker", SimpleNamespace(reserve_simulation=reserve), "f" * 64
)
cancelled = pool.submit(cancel)
assert dispatched.result(timeout=5)["status"] == "running"
assert cancelled.result(timeout=5)["status"] == "running"
assert leases == ["aicache-" + row["key"]]
assert store.get(row["key"])["cancel_requested"]
store.update(row["key"], dict(action="release"), "worker")
assert store.pending() is None
# Cancel before dispatch must not take any reservation.
waiting = store.request(scene(2))
store.cancel(waiting["key"])
assert (
store.dispatch("worker", SimpleNamespace(reserve_simulation=leases.append), "f" * 64)
is None
)
assert len(leases) == 1