From 3cc53f551d724c215302fe9a76671eeb5e7d6ab4 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Tue, 29 Dec 2020 11:00:03 +0100 Subject: [PATCH] MAVSDK tests: Use full system timeout off Depending on loop iterations for timeouts is not accurate, as usleep behavior depends on the system load and asking for a 0 ms sleep can potentially return immediately. --- test/mavsdk_tests/autopilot_tester.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/test/mavsdk_tests/autopilot_tester.h b/test/mavsdk_tests/autopilot_tester.h index ef91982cc1..443e9c77cc 100644 --- a/test/mavsdk_tests/autopilot_tester.h +++ b/test/mavsdk_tests/autopilot_tester.h @@ -128,24 +128,27 @@ private: uint32_t start_time = _info->get_flight_information().second.time_boot_ms; while (!fun()) { - std::this_thread::sleep_for(duration_ms / 1000); + std::this_thread::sleep_for(duration_ms / 100); // This might potentially loop forever and the test needs to be killed by a watchdog outside. // The reason not to include an absolute timeout here is that it can happen if the host is // busy and PX4 doesn't run fast enough. - if (_info->get_flight_information().second.time_boot_ms - start_time > duration_ms.count()) { + const int64_t elapsed_time_ms = _info->get_flight_information().second.time_boot_ms - start_time; + if (elapsed_time_ms > duration_ms.count()) { + std::cout << "Timeout, connected to vehicle but waiting for test for " << elapsed_time_ms / 1000.0 << " seconds" << std::endl; return false; } } } else { // Nothing is connected yet. Use the host time. - unsigned iteration = 0; + const auto start_time = std::chrono::steady_clock::now(); while (!fun()) { - std::this_thread::sleep_for(duration_ms / 1000); - - if (iteration++ >= 1000) { + std::this_thread::sleep_for(duration_ms / 100); + const int64_t elapsed_time_us = std::chrono::duration(std::chrono::steady_clock::now() - start_time).count(); + if (elapsed_time_us > duration_ms.count() * 1000) { + std::cout << "Timeout, waiting for the vehicle for " << elapsed_time_us / 1000000.0 << " seconds" << std::endl; return false; } }