diff options
author | 2024-04-08 16:26:34 +0000 | |
---|---|---|
committer | 2024-04-08 16:26:34 +0000 | |
commit | 1527bf57a7a8697fedc7d04c6f54656347402a7f (patch) | |
tree | 17d387a4af7e37fc6f7444074b12451041f38ab2 /services/vibratorservice | |
parent | 6614faf5ad28a6edaa805aaa21567e7bd7a16db2 (diff) | |
parent | 08f2962efe392cc6bd1e2fcc817b70edaf3a61fb (diff) |
Merge "VibratorHalControllerTest: fix flaky test" into main
Diffstat (limited to 'services/vibratorservice')
-rw-r--r-- | services/vibratorservice/test/VibratorHalControllerTest.cpp | 13 | ||||
-rw-r--r-- | 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<void>::transactionFailed("message"))); } - std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>(); - 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<void>(onFn, "on").isOk()); ASSERT_TRUE(mController->doWithRetry<void>(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<std::mutex> lock(mMutex); + return mCount; + } + + void increment() { + std::unique_lock<std::mutex> lock(mMutex); + mCount += 1; + mCondVar.notify_all(); + } + + void tryWaitUntilCountIsAtLeast(int32_t count, std::chrono::milliseconds timeout) { + std::unique_lock<std::mutex> 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_ |