From 0a2c2a6b280a5779bfaedeaa55a548493cd3ad16 Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Mon, 16 May 2022 16:25:52 +0200 Subject: [PATCH] Add unittest for alert callbacks (#24551) add alert callback test old-commit-hash: 31af13101ba2ad3b6282140c70c07e5298ccffce --- selfdrive/controls/tests/test_alerts.py | 12 ++++++++++-- selfdrive/test/process_replay/process_replay.py | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/selfdrive/controls/tests/test_alerts.py b/selfdrive/controls/tests/test_alerts.py index 2502bed06b..f340049d06 100755 --- a/selfdrive/controls/tests/test_alerts.py +++ b/selfdrive/controls/tests/test_alerts.py @@ -10,6 +10,7 @@ from common.basedir import BASEDIR from common.params import Params from selfdrive.controls.lib.events import Alert, EVENTS, ET from selfdrive.controls.lib.alertmanager import set_offroad_alert +from selfdrive.test.process_replay.process_replay import FakeSubMaster, CONFIGS AlertSize = log.ControlsState.AlertSize @@ -19,8 +20,7 @@ OFFROAD_ALERTS_PATH = os.path.join(BASEDIR, "selfdrive/controls/lib/alerts_offro ALERTS = [] for event_types in EVENTS.values(): for alert in event_types.values(): - if isinstance(alert, Alert): - ALERTS.append(alert) + ALERTS.append(alert) class TestAlerts(unittest.TestCase): @@ -30,6 +30,11 @@ class TestAlerts(unittest.TestCase): with open(OFFROAD_ALERTS_PATH) as f: cls.offroad_alerts = json.loads(f.read()) + # Create fake objects for callback + cls.CP = car.CarParams.new_message() + cfg = [c for c in CONFIGS if c.proc_name == 'controlsd'][0] + cls.sm = FakeSubMaster(cfg.pub_sub.keys()) + def test_events_defined(self): # Ensure all events in capnp schema are defined in events.py events = car.CarEvent.EventName.schema.enumerants @@ -59,6 +64,9 @@ class TestAlerts(unittest.TestCase): } for alert in ALERTS: + if not isinstance(alert, Alert): + alert = alert(self.CP, self.sm, metric=False, soft_disable_time=100) + # for full size alerts, both text fields wrap the text, # so it's unlikely that they would go past the max width if alert.alert_size in (AlertSize.none, AlertSize.full): diff --git a/selfdrive/test/process_replay/process_replay.py b/selfdrive/test/process_replay/process_replay.py index d7b922863a..03d882591f 100755 --- a/selfdrive/test/process_replay/process_replay.py +++ b/selfdrive/test/process_replay/process_replay.py @@ -242,7 +242,7 @@ CONFIGS = [ pub_sub={ "can": ["controlsState", "carState", "carControl", "sendcan", "carEvents", "carParams"], "deviceState": [], "pandaStates": [], "peripheralState": [], "liveCalibration": [], "driverMonitoringState": [], "longitudinalPlan": [], "lateralPlan": [], "liveLocationKalman": [], "liveParameters": [], "radarState": [], - "modelV2": [], "driverCameraState": [], "roadCameraState": [], "managerState": [], + "modelV2": [], "driverCameraState": [], "roadCameraState": [], "managerState": [], "testJoystick": [], }, ignore=["logMonoTime", "valid", "controlsState.startMonoTime", "controlsState.cumLagMs"], init_callback=fingerprint,