Files
IQ.Pilot/iqpilot/common/tests/test_realtime.py
2026-08-18 07:14:33 -05:00

50 lines
1.1 KiB
Python

#!/usr/bin/env python3
import pytest
from iqpilot.common.realtime import Ratekeeper
class MonotonicClock:
def __init__(self) -> None:
self.now = 0.
def advance(self, seconds: float) -> None:
self.now += seconds
def __call__(self) -> float:
return self.now
def test_ratekeeper_reset_discards_accumulated_lag(monkeypatch):
clock = MonotonicClock()
monkeypatch.setattr("iqpilot.common.realtime.time.monotonic", clock)
rk = Ratekeeper(100)
rk.monitor_time()
clock.advance(0.075)
rk.monitor_time()
assert rk.remaining == pytest.approx(-0.055)
assert rk.lag == pytest.approx(0.055)
rk.reset()
assert rk.remaining == 0.
assert rk.lag == 0.
rk.monitor_time()
assert rk.remaining == pytest.approx(0.01)
assert rk.lag == 0.
def test_ratekeeper_reset_preserves_frame_count(monkeypatch):
clock = MonotonicClock()
monkeypatch.setattr("iqpilot.common.realtime.time.monotonic", clock)
rk = Ratekeeper(100)
rk.monitor_time()
clock.advance(0.01)
rk.monitor_time()
frame = rk.frame
rk.reset()
assert rk.frame == frame