You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
5.5 KiB
Python
114 lines
5.5 KiB
Python
"""Test the additive patch against the pinned PR10 status.py without HTTP."""
|
|
|
|
import asyncio
|
|
from hashlib import sha256
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
from tempfile import TemporaryDirectory
|
|
from types import ModuleType, SimpleNamespace
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
SOURCE = Path(sys.argv.pop(1)) if __name__ == "__main__" else None
|
|
|
|
|
|
@unittest.skipIf(SOURCE is None, "Pass the path to materialized PR10 status.py")
|
|
class FieldStatusTests(unittest.IsolatedAsyncioTestCase):
|
|
def setUp(self):
|
|
data = SOURCE.read_bytes()
|
|
pins = json.loads((HERE / "pins.json").read_bytes())["status.py"]
|
|
self.assertEqual(sha256(data).hexdigest(), pins["before"])
|
|
with TemporaryDirectory() as root:
|
|
path = Path(root) / "status.py"
|
|
path.write_bytes(data)
|
|
git = ["git", "-c", "core.autocrlf=false", "apply"]
|
|
subprocess.run([*git, str(HERE / "status.patch")], cwd=root, check=True)
|
|
candidate = path.read_bytes()
|
|
self.assertEqual(sha256(candidate).hexdigest(), pins["after"])
|
|
subprocess.run([*git, "-R", str(HERE / "status.patch")], cwd=root, check=True)
|
|
self.assertEqual(path.read_bytes(), data)
|
|
self.config = SimpleNamespace(PTZ_STATUS_HZ=4., PTZ_CAM_IDS=[0, 2, 4, 6, 8, 10],
|
|
CAMERA_CONFIG={c: {"ptz_status_track_hz": 15.}
|
|
for c in (0, 2, 4, 6, 8, 10)})
|
|
self.state = SimpleNamespace(shutdown=False, ptz_states={}, ptz_status={})
|
|
self.events = []
|
|
package = ModuleType("pilot_test")
|
|
package.config, package.state = self.config, self.state
|
|
mod = ModuleType("pilot_test.status")
|
|
mod.__package__ = package.__name__
|
|
values = {"pilot_test": package,
|
|
"pilot_test.utils": SimpleNamespace(clipf=lambda x, low, high: max(low, min(high, x)),
|
|
wrap_deg=lambda x: x),
|
|
"pilot_test.ptz_io": SimpleNamespace(get_ptz_status_full=None,
|
|
confirm_zoom_step_optics_feedback=None, _zoom_command_lock=None),
|
|
"pilot_test.zoom_controller": SimpleNamespace(
|
|
observe_calibration=None, _episode_telemetry=lambda *a, **kw:self.events.append((a, kw)),
|
|
_owner=lambda s:(s.get("ptz_generation", 0), s.get("track_started_at", 0.)))}
|
|
with patch.dict(sys.modules, values):
|
|
exec(compile(candidate, "status.py", "exec"), vars(mod))
|
|
self.status = mod
|
|
|
|
def test_only_valid_opt_in_track_accelerates(self):
|
|
for cam in self.config.PTZ_CAM_IDS:
|
|
cfg = self.config.CAMERA_CONFIG[cam]
|
|
self.assertAlmostEqual(self.status._track_status_period(cfg, {"mode":"TRACK", "tracking_active":True}), 1/15)
|
|
self.assertIsNone(self.status._track_status_period(cfg, {"mode":"RETURN", "tracking_active":True}))
|
|
for invalid in (None, 9, 16, True, "15", float("nan")):
|
|
self.assertIsNone(self.status._track_status_period({"ptz_status_track_hz":invalid},
|
|
{"mode":"TRACK", "tracking_active":True}))
|
|
|
|
async def test_one_request_per_camera_and_all_six_can_accelerate(self):
|
|
calls = dict.fromkeys(self.config.PTZ_CAM_IDS, 0)
|
|
inflight = set()
|
|
for cam in self.config.PTZ_CAM_IDS:
|
|
self.state.ptz_states[cam] = dict(mode="TRACK", tracking_active=True)
|
|
|
|
async def poll(cam, *args):
|
|
self.assertNotIn(cam, inflight)
|
|
inflight.add(cam)
|
|
calls[cam] += 1
|
|
await asyncio.sleep(.105 if cam == 0 else .002)
|
|
inflight.remove(cam)
|
|
|
|
self.status._poll_camera_status_once = poll
|
|
task = asyncio.create_task(self.status.ptz_status_poller())
|
|
try:
|
|
await asyncio.sleep(.42)
|
|
finally:
|
|
self.state.shutdown = True
|
|
await asyncio.wait_for(task, .5)
|
|
self.assertGreaterEqual(min(calls.values()), 3)
|
|
self.assertGreater(calls[2], calls[0])
|
|
|
|
async def test_late_reply_is_not_logged_for_new_track_and_cache_is_marked(self):
|
|
from threading import RLock
|
|
self.status._zoom_command_lock = lambda _: RLock()
|
|
self.status._update_runtime_state = lambda *args, **kwargs: None
|
|
self.state.ptz_states[8] = dict(mode="TRACK", tracking_active=True,
|
|
ptz_generation=1, track_started_at=100.)
|
|
value = SimpleNamespace(pan=0., tilt=0., zoom_ratio=2., hfov_deg=20.,
|
|
zoom_raw=20., zoom_tag="test", zoom_age_sec=.05)
|
|
async def late(*_):
|
|
self.state.ptz_states[8]["ptz_generation"] = 2
|
|
return value
|
|
async def current(*_):
|
|
return value
|
|
cfg = self.config.CAMERA_CONFIG[8]
|
|
with patch.object(self.status.asyncio, "to_thread", late):
|
|
await self.status._poll_camera_status_once(8, cfg, 0)
|
|
self.assertEqual(self.events, [])
|
|
with patch.object(self.status.asyncio, "to_thread", current):
|
|
await self.status._poll_camera_status_once(8, cfg, 0)
|
|
args, fields = self.events[0]
|
|
self.assertEqual(args[1:4], (8, (2, 100.), "status_sample"))
|
|
self.assertTrue(fields["status_ok"])
|
|
self.assertFalse(fields["optics_fresh"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|