Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1527bf57 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "VibratorHalControllerTest: fix flaky test" into main

parents 6614faf5 08f2962e
Loading
Loading
Loading
Loading
+7 −6
Original line number Diff line number Diff line
@@ -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());
}
+27 −1
Original line number Diff line number Diff line
@@ -85,6 +85,32 @@ 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