48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
"""Deterministic fake clock for the record/replay harness (plan todo 16).
|
|
|
|
Replay is driven by this clock, never by ``time.sleep`` / wall-clock waits, so a
|
|
replayed session reproduces the exact recorded frame sequence and timing in zero
|
|
real time (the plan's "NO real sleeps/waits in tests" guardrail). The clock only
|
|
ever moves forward; a backwards move is a bug (recorded Kinect timestamps are
|
|
monotonic per nui-contracts.md §3.6) and raises rather than silently reordering.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
class FakeClock:
|
|
"""A monotonic virtual clock measured in microseconds.
|
|
|
|
It carries no real time at all: ``advance``/``set`` are the only way the
|
|
value moves, so replay pacing is fully deterministic and instantaneous.
|
|
"""
|
|
|
|
__slots__ = ("_now",)
|
|
|
|
def __init__(self, start_micros: int = 0) -> None:
|
|
if start_micros < 0:
|
|
raise ValueError(f"start_micros must be >= 0, got {start_micros}")
|
|
self._now = int(start_micros)
|
|
|
|
def now_micros(self) -> int:
|
|
"""Current virtual time, microseconds."""
|
|
return self._now
|
|
|
|
def advance(self, delta_micros: int) -> int:
|
|
"""Move the clock forward by ``delta_micros`` (must be >= 0)."""
|
|
delta = int(delta_micros)
|
|
if delta < 0:
|
|
raise ValueError(f"cannot advance a monotonic clock backwards: {delta}")
|
|
self._now += delta
|
|
return self._now
|
|
|
|
def set(self, micros: int) -> int:
|
|
"""Jump the clock to ``micros`` (must not be earlier than now)."""
|
|
target = int(micros)
|
|
if target < self._now:
|
|
raise ValueError(
|
|
f"cannot move a monotonic clock backwards: now={self._now} target={target}"
|
|
)
|
|
self._now = target
|
|
return self._now
|