Back to Repositories

Testing Time-to-Onroad System Initialization in OpenPilot

A comprehensive test suite designed to validate the time-to-onroad functionality in the OpenPilot system. This test ensures proper initialization and engagement of the self-driving capabilities while monitoring system states and events.

Test Coverage Overview

The test suite provides extensive coverage of OpenPilot’s onroad initialization sequence and engagement readiness.

Key areas tested include:
  • System startup and manager process initialization
  • Device state monitoring and transition to onroad mode
  • Self-drive initialization status
  • Engagement readiness verification
  • Sustained engageability validation

Implementation Analysis

The implementation utilizes pytest framework with a focus on real-time state monitoring and timeout management. The test employs messaging systems to track vehicle states and events, using SubMaster for message handling and custom timeout mechanisms for process control.

Notable patterns include:
  • Asynchronous state monitoring
  • Event-based validation
  • Graceful process management
  • Timeout-controlled test flow

Technical Details

Testing tools and components:
  • pytest framework with TICI marker
  • Cereal messaging system
  • Custom timeout implementation
  • Subprocess management
  • State monitoring via SubMaster
  • Parameter management utilities

Best Practices Demonstrated

The test implementation showcases several testing best practices for autonomous driving systems.

Notable practices include:
  • Proper resource cleanup and process termination
  • Robust timeout handling
  • Continuous state validation
  • Clear event logging and debugging support
  • Isolated test environment setup

commaai/openpilot

selfdrive/test/test_time_to_onroad.py

            
import os
import pytest
import time
import subprocess

from cereal import log
import cereal.messaging as messaging
from openpilot.common.basedir import BASEDIR
from openpilot.common.timeout import Timeout
from openpilot.selfdrive.test.helpers import set_params_enabled

EventName = log.OnroadEvent.EventName


@pytest.mark.tici
def test_time_to_onroad():
  # launch
  set_params_enabled()
  manager_path = os.path.join(BASEDIR, "system/manager/manager.py")
  proc = subprocess.Popen(["python", manager_path])

  start_time = time.monotonic()
  sm = messaging.SubMaster(['selfdriveState', 'deviceState', 'onroadEvents'])
  try:
    # wait for onroad. timeout assumes panda is up to date
    with Timeout(10, "timed out waiting to go onroad"):
      while not sm['deviceState'].started:
        sm.update(100)

    # wait for engageability
    try:
      with Timeout(10, "timed out waiting for engageable"):
        initialized = False
        while True:
          sm.update(100)

          if sm.seen['onroadEvents'] and not any(EventName.selfdriveInitializing == e.name for e in sm['onroadEvents']):
            initialized = True

          if initialized:
            sm.update(100)
            assert sm['selfdriveState'].engageable, f"events: {sm['onroadEvents']}"
            break
    finally:
      print(f"onroad events: {sm['onroadEvents']}")
    print(f"engageable after {time.monotonic() - start_time:.2f}s")

    # once we're enageable, must stay for the next few seconds
    st = time.monotonic()
    while (time.monotonic() - st) < 10.:
      sm.update(100)
      assert sm.all_alive(), sm.alive
      assert sm['selfdriveState'].engageable, f"events: {sm['onroadEvents']}"
  finally:
    proc.terminate()
    if proc.wait(20) is None:
      proc.kill()