| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #!/usr/bin/env python3
- """Concurrency contract for the Normal v3 strict offline gate."""
- from __future__ import annotations
- import tempfile
- import threading
- from pathlib import Path
- from unittest.mock import patch
- from tools import prod2u_p4_harness as harness
- def test_strict_offline_gate_starts_all_five_runners_in_parallel() -> None:
- barrier = threading.Barrier(5)
- observed_roots: dict[str, Path] = {}
- def finish(name: str, work_root: Path) -> dict[str, object]:
- observed_roots[name] = Path(work_root).resolve()
- barrier.wait(timeout=5)
- return {"status": "pass", "work_root": str(work_root)}
- def fake_hdl(*, run: bool, backend: str, work_root: Path) -> dict[str, object]:
- assert run is True
- payload = finish(backend, work_root)
- payload.update(
- {
- "schema": harness.HDL_RECEIPT_SCHEMA,
- "verification_layer": "hdl_sim",
- "backend": backend,
- "tests": [],
- "target_manifest": {"status": "pass"},
- }
- )
- return payload
- source_state = {
- "status": "pass",
- "clean": True,
- "commit": "bbc1b20f",
- "changes": [],
- }
- with tempfile.TemporaryDirectory(
- dir=r"C:\Users\Administrator\CodexTemp"
- ) as temporary, patch.object(
- harness, "source_state", return_value=source_state
- ), patch.object(
- harness,
- "source_integrity_payload",
- return_value={"status": "pass", "source_state": source_state},
- ), patch.object(
- harness, "unit_payload", side_effect=lambda root: finish("unit", root)
- ), patch.object(
- harness, "hdl_sim_payload", side_effect=fake_hdl
- ), patch.object(
- harness,
- "formal_payload",
- side_effect=lambda *, run, work_root: finish("formal", work_root),
- ):
- receipt = harness.strict_offline_gate_payload(
- work_root=Path(temporary),
- verification_run_id="1" * 32,
- )
- assert receipt["status"] == "pass"
- assert set(observed_roots) == {
- "unit",
- "icarus",
- "verilator",
- "cocotb",
- "formal",
- }
- assert len(set(observed_roots.values())) == 5
- assert all(path.parent == Path(temporary).resolve() for path in observed_roots.values())
- assert set(receipt["layers"]["hdl_sim"]["backend_results"]) == {
- "icarus",
- "verilator",
- "cocotb",
- }
- def test_strict_offline_gate_waits_for_all_runners_and_reports_exceptions() -> None:
- barrier = threading.Barrier(5)
- completed: set[str] = set()
- completed_lock = threading.Lock()
- def finish(name: str, work_root: Path, *, status: str = "pass") -> dict[str, object]:
- barrier.wait(timeout=5)
- with completed_lock:
- completed.add(name)
- return {"status": status, "work_root": str(work_root)}
- def fake_hdl(*, run: bool, backend: str, work_root: Path) -> dict[str, object]:
- assert run is True
- if backend == "verilator":
- barrier.wait(timeout=5)
- with completed_lock:
- completed.add(backend)
- raise RuntimeError("verilator runner exploded")
- payload = finish(backend, work_root)
- payload.update(
- {
- "schema": harness.HDL_RECEIPT_SCHEMA,
- "verification_layer": "hdl_sim",
- "backend": backend,
- "tests": [],
- "target_manifest": {"status": "pass"},
- }
- )
- return payload
- source_state = {
- "status": "pass",
- "clean": True,
- "commit": "bbc1b20f",
- "changes": [],
- }
- with tempfile.TemporaryDirectory(
- dir=r"C:\Users\Administrator\CodexTemp"
- ) as temporary, patch.object(
- harness, "source_state", return_value=source_state
- ), patch.object(
- harness,
- "source_integrity_payload",
- return_value={"status": "pass", "source_state": source_state},
- ), patch.object(
- harness,
- "unit_payload",
- side_effect=lambda root: finish("unit", root),
- ), patch.object(
- harness, "hdl_sim_payload", side_effect=fake_hdl
- ), patch.object(
- harness,
- "formal_payload",
- side_effect=lambda *, run, work_root: finish(
- "formal", work_root, status="fail"
- ),
- ):
- receipt = harness.strict_offline_gate_payload(
- work_root=Path(temporary),
- verification_run_id="2" * 32,
- )
- assert completed == {"unit", "icarus", "verilator", "cocotb", "formal"}
- assert receipt["status"] == "fail"
- assert receipt["layers"]["formal"]["status"] == "fail"
- verilator = receipt["layers"]["hdl_sim"]["backend_results"]["verilator"]
- assert verilator["status"] == "fail"
- assert "RuntimeError: verilator runner exploded" in verilator["reason"]
- assert set(receipt["failures"]) == {
- "required_layer_not_pass:hdl_sim:fail",
- "required_layer_not_pass:formal:fail",
- }
|