test_prod2u_p4_parallel_offline_gate.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/env python3
  2. """Concurrency contract for the Normal v3 strict offline gate."""
  3. from __future__ import annotations
  4. import tempfile
  5. import threading
  6. from pathlib import Path
  7. from unittest.mock import patch
  8. from tools import prod2u_p4_harness as harness
  9. def test_strict_offline_gate_starts_all_five_runners_in_parallel() -> None:
  10. barrier = threading.Barrier(5)
  11. observed_roots: dict[str, Path] = {}
  12. def finish(name: str, work_root: Path) -> dict[str, object]:
  13. observed_roots[name] = Path(work_root).resolve()
  14. barrier.wait(timeout=5)
  15. return {"status": "pass", "work_root": str(work_root)}
  16. def fake_hdl(*, run: bool, backend: str, work_root: Path) -> dict[str, object]:
  17. assert run is True
  18. payload = finish(backend, work_root)
  19. payload.update(
  20. {
  21. "schema": harness.HDL_RECEIPT_SCHEMA,
  22. "verification_layer": "hdl_sim",
  23. "backend": backend,
  24. "tests": [],
  25. "target_manifest": {"status": "pass"},
  26. }
  27. )
  28. return payload
  29. source_state = {
  30. "status": "pass",
  31. "clean": True,
  32. "commit": "bbc1b20f",
  33. "changes": [],
  34. }
  35. with tempfile.TemporaryDirectory(
  36. dir=r"C:\Users\Administrator\CodexTemp"
  37. ) as temporary, patch.object(
  38. harness, "source_state", return_value=source_state
  39. ), patch.object(
  40. harness,
  41. "source_integrity_payload",
  42. return_value={"status": "pass", "source_state": source_state},
  43. ), patch.object(
  44. harness, "unit_payload", side_effect=lambda root: finish("unit", root)
  45. ), patch.object(
  46. harness, "hdl_sim_payload", side_effect=fake_hdl
  47. ), patch.object(
  48. harness,
  49. "formal_payload",
  50. side_effect=lambda *, run, work_root: finish("formal", work_root),
  51. ):
  52. receipt = harness.strict_offline_gate_payload(
  53. work_root=Path(temporary),
  54. verification_run_id="1" * 32,
  55. )
  56. assert receipt["status"] == "pass"
  57. assert set(observed_roots) == {
  58. "unit",
  59. "icarus",
  60. "verilator",
  61. "cocotb",
  62. "formal",
  63. }
  64. assert len(set(observed_roots.values())) == 5
  65. assert all(path.parent == Path(temporary).resolve() for path in observed_roots.values())
  66. assert set(receipt["layers"]["hdl_sim"]["backend_results"]) == {
  67. "icarus",
  68. "verilator",
  69. "cocotb",
  70. }
  71. def test_strict_offline_gate_waits_for_all_runners_and_reports_exceptions() -> None:
  72. barrier = threading.Barrier(5)
  73. completed: set[str] = set()
  74. completed_lock = threading.Lock()
  75. def finish(name: str, work_root: Path, *, status: str = "pass") -> dict[str, object]:
  76. barrier.wait(timeout=5)
  77. with completed_lock:
  78. completed.add(name)
  79. return {"status": status, "work_root": str(work_root)}
  80. def fake_hdl(*, run: bool, backend: str, work_root: Path) -> dict[str, object]:
  81. assert run is True
  82. if backend == "verilator":
  83. barrier.wait(timeout=5)
  84. with completed_lock:
  85. completed.add(backend)
  86. raise RuntimeError("verilator runner exploded")
  87. payload = finish(backend, work_root)
  88. payload.update(
  89. {
  90. "schema": harness.HDL_RECEIPT_SCHEMA,
  91. "verification_layer": "hdl_sim",
  92. "backend": backend,
  93. "tests": [],
  94. "target_manifest": {"status": "pass"},
  95. }
  96. )
  97. return payload
  98. source_state = {
  99. "status": "pass",
  100. "clean": True,
  101. "commit": "bbc1b20f",
  102. "changes": [],
  103. }
  104. with tempfile.TemporaryDirectory(
  105. dir=r"C:\Users\Administrator\CodexTemp"
  106. ) as temporary, patch.object(
  107. harness, "source_state", return_value=source_state
  108. ), patch.object(
  109. harness,
  110. "source_integrity_payload",
  111. return_value={"status": "pass", "source_state": source_state},
  112. ), patch.object(
  113. harness,
  114. "unit_payload",
  115. side_effect=lambda root: finish("unit", root),
  116. ), patch.object(
  117. harness, "hdl_sim_payload", side_effect=fake_hdl
  118. ), patch.object(
  119. harness,
  120. "formal_payload",
  121. side_effect=lambda *, run, work_root: finish(
  122. "formal", work_root, status="fail"
  123. ),
  124. ):
  125. receipt = harness.strict_offline_gate_payload(
  126. work_root=Path(temporary),
  127. verification_run_id="2" * 32,
  128. )
  129. assert completed == {"unit", "icarus", "verilator", "cocotb", "formal"}
  130. assert receipt["status"] == "fail"
  131. assert receipt["layers"]["formal"]["status"] == "fail"
  132. verilator = receipt["layers"]["hdl_sim"]["backend_results"]["verilator"]
  133. assert verilator["status"] == "fail"
  134. assert "RuntimeError: verilator runner exploded" in verilator["reason"]
  135. assert set(receipt["failures"]) == {
  136. "required_layer_not_pass:hdl_sim:fail",
  137. "required_layer_not_pass:formal:fail",
  138. }