feat: serialize public hardware execution

This commit is contained in:
vee
2026-08-03 09:25:38 +08:00
parent a6d974cb29
commit 85b4740d29
10 changed files with 308 additions and 16 deletions
@@ -0,0 +1,42 @@
from __future__ import annotations
import threading
import unittest
from cognitive_agent.infrastructure.can.hardware import (
HardwareBusyError,
HardwareExecutionCoordinator,
)
class HardwareExecutionCoordinatorTests(unittest.TestCase):
def test_rejects_overlapping_owner_and_releases_after_completion(self) -> None:
coordinator = HardwareExecutionCoordinator(retry_after_seconds=7)
entered = threading.Event()
release = threading.Event()
def hold_bench() -> None:
with coordinator.reserve():
entered.set()
self.assertTrue(release.wait(timeout=3))
owner = threading.Thread(target=hold_bench)
owner.start()
self.assertTrue(entered.wait(timeout=2))
self.assertTrue(coordinator.public_info()["busy"])
with self.assertRaises(HardwareBusyError) as caught:
with coordinator.reserve():
self.fail("a concurrent request must not enter the physical bench")
self.assertEqual(caught.exception.retry_after_seconds, 7)
release.set()
owner.join(timeout=3)
self.assertFalse(owner.is_alive())
self.assertFalse(coordinator.public_info()["busy"])
with coordinator.reserve():
self.assertTrue(coordinator.public_info()["busy"])
if __name__ == "__main__":
unittest.main()