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

Commit ed5082b3 authored by Yu Shan's avatar Yu Shan
Browse files

Make RecurrentTimerTest more stable.

Decrease the event frequency from 1000/s to 10/s. This should increase the test stability.

Change-Id: Ibd5d348e317be62ae2526100eaa4663d051c99ed
Flag: EXEMPT test fix
Tests: Presubmit
Bug: 389799306
parent 074a9a21
Loading
Loading
Loading
Loading
+17 −17
Original line number Diff line number Diff line
@@ -39,37 +39,37 @@ TEST(RecurrentTimerTest, oneInterval) {
        counterRef.get()++;
    });

    timer.registerRecurrentEvent(milliseconds(1), 0xdead);
    std::this_thread::sleep_for(milliseconds(100));
    // This test is unstable, so set the tolerance to 50.
    ASSERT_EQ_WITH_TOLERANCE(100, counter.load(), 50);
    timer.registerRecurrentEvent(milliseconds(100), 0xdead);
    std::this_thread::sleep_for(milliseconds(1000));
    // This test is unstable, so set the tolerance to 5.
    ASSERT_EQ_WITH_TOLERANCE(10, counter.load(), 5);
}

TEST(RecurrentTimerTest, multipleIntervals) {
    std::atomic<int64_t> counter1ms { 0L };
    std::atomic<int64_t> counter5ms { 0L };
    auto counter1msRef = std::ref(counter1ms);
    auto counter5msRef = std::ref(counter5ms);
    std::atomic<int64_t> counter100ms { 0L };
    std::atomic<int64_t> counter50ms { 0L };
    auto counter100msRef = std::ref(counter100ms);
    auto counter50msRef = std::ref(counter50ms);
    RecurrentTimer timer(
            [&counter1msRef, &counter5msRef](const std::vector<int32_t>& cookies) {
            [&counter100msRef, &counter50msRef](const std::vector<int32_t>& cookies) {
        for (int32_t cookie : cookies) {
            if (cookie == 0xdead) {
                counter1msRef.get()++;
                counter100msRef.get()++;
            } else if (cookie == 0xbeef) {
                counter5msRef.get()++;
                counter50msRef.get()++;
            } else {
                FAIL();
            }
        }
    });

    timer.registerRecurrentEvent(milliseconds(1), 0xdead);
    timer.registerRecurrentEvent(milliseconds(5), 0xbeef);
    timer.registerRecurrentEvent(milliseconds(100), 0xdead);
    timer.registerRecurrentEvent(milliseconds(50), 0xbeef);

    std::this_thread::sleep_for(milliseconds(100));
    // This test is unstable, so set the tolerance to 50.
    ASSERT_EQ_WITH_TOLERANCE(100, counter1ms.load(), 50);
    ASSERT_EQ_WITH_TOLERANCE(20, counter5ms.load(), 10);
    std::this_thread::sleep_for(milliseconds(1000));
    // This test is unstable, so set the tolerance to 5.
    ASSERT_EQ_WITH_TOLERANCE(10, counter100ms.load(), 5);
    ASSERT_EQ_WITH_TOLERANCE(20, counter50ms.load(), 10);
}

}  // anonymous namespace