|
| 1 | +import time |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from pymavlink.mavutil import mavlink |
3 | 5 |
|
|
6 | 8 | WaitForMessageReturnsNone, |
7 | 9 | ) |
8 | 10 |
|
| 11 | +FLIGHT_MODE_SET_RETRY_MAX_ATTEMPTS = 5 |
| 12 | +FLIGHT_MODE_SET_RETRY_DELAY_SECS = 1 |
| 13 | + |
| 14 | + |
| 15 | +def set_flight_mode_with_retries( |
| 16 | + droneStatus, mode_number: int, flight_mode: int |
| 17 | +) -> dict: |
| 18 | + """Retry setting a flight mode to reduce intermittent SITL timing flakes.""" |
| 19 | + last_response = {"success": False, "message": "No response"} |
| 20 | + for _ in range(FLIGHT_MODE_SET_RETRY_MAX_ATTEMPTS): |
| 21 | + last_response = droneStatus.drone.flightModesController.setFlightMode( |
| 22 | + mode_number, flight_mode |
| 23 | + ) |
| 24 | + if last_response.get("success"): |
| 25 | + return last_response |
| 26 | + |
| 27 | + time.sleep(FLIGHT_MODE_SET_RETRY_DELAY_SECS) |
| 28 | + |
| 29 | + return last_response |
| 30 | + |
9 | 31 |
|
10 | 32 | @pytest.fixture(scope="module", autouse=True) |
11 | 33 | def run_once_after_all_tests(): |
@@ -155,7 +177,7 @@ def test_setFlightMode_invalidData_copter(droneStatus): |
155 | 177 |
|
156 | 178 | @pytest.mark.copter_only |
157 | 179 | def test_setFlightMode_success_copter(droneStatus): |
158 | | - response = droneStatus.drone.flightModesController.setFlightMode(1, 2) |
| 180 | + response = set_flight_mode_with_retries(droneStatus, 1, 2) |
159 | 181 | assert response == { |
160 | 182 | "success": True, |
161 | 183 | "message": "Flight mode 1 set to COPTER_MODE_ALT_HOLD", |
|
0 commit comments