From 08f2962efe392cc6bd1e2fcc817b70edaf3a61fb Mon Sep 17 00:00:00 2001 From: Adrian Roos Date: Mon, 8 Apr 2024 15:20:33 +0000 Subject: VibratorHalControllerTest: fix flaky test Fixes a flaky test that was caused by sleeping instead of explicitly waiting for the condition to become true. Fixes: 332278497 Test: atest 'libvibratorservice_test:VibratorHalControllerTest#TestScheduledCallbackSurvivesReconnection' Change-Id: Id34a6237ca2deb3653a7336a04d181fa9d1ebb08 --- .../test/VibratorHalControllerTest.cpp | 13 +++++----- services/vibratorservice/test/test_utils.h | 28 +++++++++++++++++++++- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/services/vibratorservice/test/VibratorHalControllerTest.cpp b/services/vibratorservice/test/VibratorHalControllerTest.cpp index 9b95d74899..15fde914f6 100644 --- a/services/vibratorservice/test/VibratorHalControllerTest.cpp +++ b/services/vibratorservice/test/VibratorHalControllerTest.cpp @@ -255,16 +255,17 @@ TEST_F(VibratorHalControllerTest, TestScheduledCallbackSurvivesReconnection) { .WillRepeatedly(Return(vibrator::HalResult::transactionFailed("message"))); } - std::unique_ptr callbackCounter = std::make_unique(); - auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get()); + auto counter = vibrator::TestCounter(0); - auto onFn = [&](vibrator::HalWrapper* hal) { return hal->on(10ms, callback); }; + auto onFn = [&](vibrator::HalWrapper* hal) { + return hal->on(10ms, [&counter] { counter.increment(); }); + }; ASSERT_TRUE(mController->doWithRetry(onFn, "on").isOk()); ASSERT_TRUE(mController->doWithRetry(PING_FN, "ping").isFailed()); mMockHal.reset(); - ASSERT_EQ(0, *callbackCounter.get()); + ASSERT_EQ(0, counter.get()); // Callback triggered even after HalWrapper was reconnected. - std::this_thread::sleep_for(15ms); - ASSERT_EQ(1, *callbackCounter.get()); + counter.tryWaitUntilCountIsAtLeast(1, 500ms); + ASSERT_EQ(1, counter.get()); } diff --git a/services/vibratorservice/test/test_utils.h b/services/vibratorservice/test/test_utils.h index 1933a118ef..b584f64bbf 100644 --- a/services/vibratorservice/test/test_utils.h +++ b/services/vibratorservice/test/test_utils.h @@ -85,10 +85,36 @@ private: ~TestFactory() = delete; }; +class TestCounter { +public: + TestCounter(int32_t init) : mCount(init), mMutex(), mCondVar() {} + + int32_t get() { + std::unique_lock lock(mMutex); + return mCount; + } + + void increment() { + std::unique_lock lock(mMutex); + mCount += 1; + mCondVar.notify_all(); + } + + void tryWaitUntilCountIsAtLeast(int32_t count, std::chrono::milliseconds timeout) { + std::unique_lock lock(mMutex); + mCondVar.wait_for(lock, timeout, [&] { return mCount >= count; }); + } + +private: + int32_t mCount; + std::mutex mMutex; + std::condition_variable mCondVar; +}; + // ------------------------------------------------------------------------------------------------- } // namespace vibrator } // namespace android -#endif // VIBRATORSERVICE_UNITTEST_UTIL_H_ \ No newline at end of file +#endif // VIBRATORSERVICE_UNITTEST_UTIL_H_ -- cgit v1.2.3-59-g8ed1b