32 lines
993 B
Python
32 lines
993 B
Python
from __future__ import annotations
|
|
|
|
import threading
|
|
import time
|
|
|
|
from lingkong_motor_service.protocol import CanFrame
|
|
|
|
|
|
class FakeTransport:
|
|
def __init__(self) -> None:
|
|
self.frames: list[CanFrame] = []
|
|
self.frame_times_ns: list[int] = []
|
|
self.tx_queue_timeouts_s: list[float | None] = []
|
|
self.is_open = True
|
|
self.fail_next = False
|
|
self.lock = threading.Lock()
|
|
|
|
def send(self, frame: CanFrame, *, tx_queue_timeout_s: float | None = None) -> None:
|
|
with self.lock:
|
|
if self.fail_next:
|
|
self.fail_next = False
|
|
from lingkong_motor_service.transport import CanTransportError
|
|
|
|
raise CanTransportError("injected CAN failure")
|
|
self.frames.append(frame)
|
|
self.frame_times_ns.append(time.monotonic_ns())
|
|
self.tx_queue_timeouts_s.append(tx_queue_timeout_s)
|
|
|
|
def shutdown(self) -> None:
|
|
with self.lock:
|
|
self.is_open = False
|