|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
| 17 | +#include <cstdlib> |
| 18 | +#include <functional> |
| 19 | +#include <string> |
| 20 | + |
17 | 21 | #include <pw_unit_test/framework.h> |
18 | 22 | #include <system/SystemConfig.h> |
19 | 23 |
|
|
25 | 29 | #include <lib/support/CodeUtils.h> |
26 | 30 | #include <platform/CHIPDeviceLayer.h> |
27 | 31 |
|
28 | | -#include <functional> |
29 | | -#include <string> |
30 | | - |
31 | 32 | using namespace chip; |
32 | 33 | using namespace chip::System::Clock; |
33 | 34 | using namespace chip::System::Clock::Literals; |
@@ -139,9 +140,20 @@ TEST_F(TestEventLoopHandler, EventLoopHandlerWake) |
139 | 140 | SystemLayer().RemoveLoopHandler(loopHandler); |
140 | 141 | cancelFallback(); // avoid leaking the fallback timer |
141 | 142 |
|
| 143 | + // Get the upper bound of the sleep duration from the environment, so we can |
| 144 | + // adjust it if the test machine is under heavy load, e.g. in CI or on a slow VM. |
| 145 | + // By default, we expect the sleep duration to be close to the requested 400ms. |
| 146 | + unsigned int expectedMaxDuration = 500u; // allow some slack for test machine load |
| 147 | + const char * maxDurationEnv = std::getenv("CHIP_TEST_EVENT_LOOP_HANDLER_MAX_DURATION_MS"); |
| 148 | + if (maxDurationEnv != nullptr) |
| 149 | + { |
| 150 | + ChipLogDetail(Test, "CHIP_TEST_EVENT_LOOP_HANDLER_MAX_DURATION_MS=%s", maxDurationEnv); |
| 151 | + expectedMaxDuration = static_cast<unsigned int>(std::stoul(maxDurationEnv)); |
| 152 | + } |
| 153 | + |
142 | 154 | Timestamp sleepDuration = loopHandler.wakeTimestamp - loopHandler.startTimestamp; |
143 | 155 | EXPECT_GE(sleepDuration.count(), 400u); // loopHandler requested wake-up after 400ms |
144 | | - EXPECT_LE(sleepDuration.count(), 500u); // allow some slack for test machine load |
| 156 | + EXPECT_LE(sleepDuration.count(), expectedMaxDuration); |
145 | 157 | } |
146 | 158 |
|
147 | 159 | #endif // !CHIP_DEVICE_LAYER_TARGET_FAKE |
|
0 commit comments