Back to Repositories

Testing Sound Daemon Timeout Alerts in OpenPilot

This test suite validates the sound daemon functionality in the OpenPilot system, specifically focusing on selfdrive state timeout alerts. It ensures proper audio alert handling when the selfdrive state experiences communication timeouts.

Test Coverage Overview

The test coverage focuses on validating the selfdrive state timeout alert mechanism in OpenPilot’s sound system.

Key areas tested include:
  • Selfdrive state message handling
  • Timeout detection logic
  • Alert triggering conditions
  • Message publishing and subscription

Implementation Analysis

The testing approach utilizes Python’s testing framework with SubMaster and PubMaster for message handling. The implementation simulates real-world scenarios by publishing selfdrive state messages and validating timeout conditions through timed sequences.

Key patterns include:
  • Message simulation using cereal messaging system
  • Time-based state verification
  • Asynchronous message processing

Technical Details

Testing tools and components:
  • cereal messaging framework
  • SubMaster/PubMaster for pub/sub messaging
  • Python time module for delay simulation
  • CarControl HUDControl for audible alerts

Best Practices Demonstrated

The test suite demonstrates several testing best practices in automotive software validation.

Notable practices include:
  • Systematic timeout verification
  • Clear separation of message publishing and subscription
  • Proper use of assertion statements
  • Simulation of real-world timing scenarios

commaai/openpilot

selfdrive/ui/tests/test_soundd.py

            
from cereal import car
from cereal import messaging
from cereal.messaging import SubMaster, PubMaster
from openpilot.selfdrive.ui.soundd import SELFDRIVE_STATE_TIMEOUT, check_selfdrive_timeout_alert

import time

AudibleAlert = car.CarControl.HUDControl.AudibleAlert


class TestSoundd:
  def test_check_selfdrive_timeout_alert(self):
    sm = SubMaster(['selfdriveState'])
    pm = PubMaster(['selfdriveState'])

    for _ in range(100):
      cs = messaging.new_message('selfdriveState')
      cs.selfdriveState.enabled = True

      pm.send("selfdriveState", cs)

      time.sleep(0.01)

      sm.update(0)

      assert not check_selfdrive_timeout_alert(sm)

    for _ in range(SELFDRIVE_STATE_TIMEOUT * 110):
      sm.update(0)
      time.sleep(0.01)

    assert check_selfdrive_timeout_alert(sm)

  # TODO: add test with micd for checking that soundd actually outputs sounds