You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					104 lines
				
				3.2 KiB
			
		
		
			
		
	
	
					104 lines
				
				3.2 KiB
			| 
								 
											2 years ago
										 
									 | 
							
								#!/usr/bin/env python3
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								import pytest
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								import subprocess
							 | 
						||
| 
								 | 
							
								import threading
							 | 
						||
| 
								 | 
							
								import time
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from typing import cast
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from openpilot.common.params import Params
							 | 
						||
| 
								 | 
							
								from openpilot.common.timeout import Timeout
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								from openpilot.system.athena import athenad
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from openpilot.selfdrive.manager.helpers import write_onroad_params
							 | 
						||
| 
								 | 
							
								from openpilot.system.hardware import TICI
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								TIMEOUT_TOLERANCE = 20  # seconds
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								def wifi_radio(on: bool) -> None:
							 | 
						||
| 
								 | 
							
								  if not TICI:
							 | 
						||
| 
								 | 
							
								    return
							 | 
						||
| 
								 | 
							
								  print(f"wifi {'on' if on else 'off'}")
							 | 
						||
| 
								 | 
							
								  subprocess.run(["nmcli", "radio", "wifi", "on" if on else "off"], check=True)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								class TestAthenadPing:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  params: Params
							 | 
						||
| 
								 | 
							
								  dongle_id: str
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  athenad: threading.Thread
							 | 
						||
| 
								 | 
							
								  exit_event: threading.Event
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  def _get_ping_time(self) -> str | None:
							 | 
						||
| 
								 | 
							
								    return cast(str | None, self.params.get("LastAthenaPingTime", encoding="utf-8"))
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  def _clear_ping_time(self) -> None:
							 | 
						||
| 
								 | 
							
								    self.params.remove("LastAthenaPingTime")
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def _received_ping(self) -> bool:
							 | 
						||
| 
								 | 
							
								    return self._get_ping_time() is not None
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  @classmethod
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  def teardown_class(cls) -> None:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    wifi_radio(True)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  def setup_method(self) -> None:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    self.params = Params()
							 | 
						||
| 
								 | 
							
								    self.dongle_id = self.params.get("DongleId", encoding="utf-8")
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    wifi_radio(True)
							 | 
						||
| 
								 | 
							
								    self._clear_ping_time()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    self.exit_event = threading.Event()
							 | 
						||
| 
								 | 
							
								    self.athenad = threading.Thread(target=athenad.main, args=(self.exit_event,))
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  def teardown_method(self) -> None:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    if self.athenad.is_alive():
							 | 
						||
| 
								 | 
							
								      self.exit_event.set()
							 | 
						||
| 
								 | 
							
								      self.athenad.join()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  def assertTimeout(self, reconnect_time: float, subtests, mocker) -> None:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    self.athenad.start()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								    mock_create_connection = mocker.patch('openpilot.system.athena.athenad.create_connection',
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								                                          new_callable=lambda: mocker.MagicMock(wraps=athenad.create_connection))
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    time.sleep(1)
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    mock_create_connection.assert_called_once()
							 | 
						||
| 
								 | 
							
								    mock_create_connection.reset_mock()
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    # check normal behaviour, server pings on connection
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								    with subtests.test("Wi-Fi: receives ping"), Timeout(70, "no ping received"):
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      while not self._received_ping():
							 | 
						||
| 
								 | 
							
								        time.sleep(0.1)
							 | 
						||
| 
								 | 
							
								      print("ping received")
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    mock_create_connection.assert_not_called()
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    # websocket should attempt reconnect after short time
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								    with subtests.test("LTE: attempt reconnect"):
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      wifi_radio(False)
							 | 
						||
| 
								 | 
							
								      print("waiting for reconnect attempt")
							 | 
						||
| 
								 | 
							
								      start_time = time.monotonic()
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      with Timeout(reconnect_time, "no reconnect attempt"):
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								        while not mock_create_connection.called:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								          time.sleep(0.1)
							 | 
						||
| 
								 | 
							
								        print(f"reconnect attempt after {time.monotonic() - start_time:.2f}s")
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    self._clear_ping_time()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    # check ping received after reconnect
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								    with subtests.test("LTE: receives ping"), Timeout(70, "no ping received"):
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      while not self._received_ping():
							 | 
						||
| 
								 | 
							
								        time.sleep(0.1)
							 | 
						||
| 
								 | 
							
								      print("ping received")
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  @pytest.mark.skipif(not TICI, reason="only run on desk")
							 | 
						||
| 
								 | 
							
								  def test_offroad(self, subtests, mocker) -> None:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    write_onroad_params(False, self.params)
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								    self.assertTimeout(60 + TIMEOUT_TOLERANCE, subtests, mocker)  # based using TCP keepalive settings
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								  @pytest.mark.skipif(not TICI, reason="only run on desk")
							 | 
						||
| 
								 | 
							
								  def test_onroad(self, subtests, mocker) -> None:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    write_onroad_params(True, self.params)
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								    self.assertTimeout(21 + TIMEOUT_TOLERANCE, subtests, mocker)
							 |