SurfaceFlinger: Mock Clock for OneShotTimerTest
Use a mock clock for OneShotTimerTest instead of a real time clock
to remove flakiness in the test.
Bug: 185150860
Test: adb shell /data/nativetest64/libsurfaceflinger_unittest/libsurfaceflinger_unittest
Change-Id: Iaea32548319eae29c6dee62efbf4ba2efc827267
diff --git a/services/surfaceflinger/Scheduler/OneShotTimer.h b/services/surfaceflinger/Scheduler/OneShotTimer.h
index 3690ce7..7285427 100644
--- a/services/surfaceflinger/Scheduler/OneShotTimer.h
+++ b/services/surfaceflinger/Scheduler/OneShotTimer.h
@@ -36,8 +36,17 @@
using ResetCallback = std::function<void()>;
using TimeoutCallback = std::function<void()>;
+ class Clock {
+ public:
+ Clock() = default;
+ virtual ~Clock() = default;
+
+ virtual std::chrono::steady_clock::time_point now() const;
+ };
+
OneShotTimer(std::string name, const Interval& interval, const ResetCallback& resetCallback,
- const TimeoutCallback& timeoutCallback);
+ const TimeoutCallback& timeoutCallback,
+ std::unique_ptr<OneShotTimer::Clock> = std::make_unique<OneShotTimer::Clock>());
~OneShotTimer();
// Initializes and turns on the idle timer.
@@ -78,6 +87,9 @@
// Thread waiting for timer to expire.
std::thread mThread;
+ // Clock object for the timer. Mocked in unit tests.
+ std::unique_ptr<Clock> mClock;
+
// Semaphore to keep mThread synchronized.
sem_t mSemaphore;