Fix flaky VibratorCallbackSchedulerTest
The test is sometimes timing out while asserting the scheduled callbacks
are triggered within the required test timeout.
Update CallbackScheduler to use std::condition_variable_any::wait_for
with calculated durations from using std::chrono::steady_clock, instead
of relying on the timestamps via wait_until.
Update the VibratorCallbackSchedulerTest to also use wait_for.
Bug: 293603710
Test: VibratorCallbackSchedulerTest
Change-Id: Ie84147a9ff686d666d8525a35914571f32d72719
diff --git a/services/vibratorservice/VibratorCallbackScheduler.cpp b/services/vibratorservice/VibratorCallbackScheduler.cpp
index f2870b0..7eda9ef 100644
--- a/services/vibratorservice/VibratorCallbackScheduler.cpp
+++ b/services/vibratorservice/VibratorCallbackScheduler.cpp
@@ -29,8 +29,11 @@
return mExpiration <= std::chrono::steady_clock::now();
}
-DelayedCallback::Timestamp DelayedCallback::getExpiration() const {
- return mExpiration;
+std::chrono::milliseconds DelayedCallback::getWaitForExpirationDuration() const {
+ std::chrono::milliseconds delta = std::chrono::duration_cast<std::chrono::milliseconds>(
+ mExpiration - std::chrono::steady_clock::now());
+ // Return zero if this is already expired.
+ return delta > delta.zero() ? delta : delta.zero();
}
void DelayedCallback::run() const {
@@ -88,7 +91,9 @@
mCondition.wait(mMutex);
} else {
// Wait until next callback expires, or a new one is scheduled.
- mCondition.wait_until(mMutex, mQueue.top().getExpiration());
+ // Use the monotonic steady clock to wait for the measured delay interval via wait_for
+ // instead of using a wall clock via wait_until.
+ mCondition.wait_for(mMutex, mQueue.top().getWaitForExpirationDuration());
}
}
}