blob: dc63260f4d854ed7400deb93e9bf641cc3f6ad9f [file] [log] [blame]
Ana Krulecfb772822018-11-30 10:44:07 +01001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef LOG_TAG
18#define LOG_TAG "SchedulerUnittests"
19
20#include <gmock/gmock.h>
21#include <gtest/gtest.h>
22#include <utils/Log.h>
23
24#include "AsyncCallRecorder.h"
25#include "Scheduler/IdleTimer.h"
26
27using namespace std::chrono_literals;
28
29namespace android {
30namespace scheduler {
31
32class IdleTimerTest : public testing::Test {
33protected:
34 IdleTimerTest() = default;
35 ~IdleTimerTest() override = default;
36
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080037 // This timeout should be used when a 3ms callback is expected.
38 // While the tests typically request a callback after 3ms, the scheduler
39 // does not always cooperate, at it can take significantly longer (observed
40 // 30ms).
41 static constexpr auto waitTimeForExpected3msCallback = 100ms;
42
43 // This timeout should be used when an 3ms callback is not expected.
44 // Note that there can be false-negatives if the callback happens later.
45 static constexpr auto waitTimeForUnexpected3msCallback = 6ms;
46
Ana Krulecfb772822018-11-30 10:44:07 +010047 AsyncCallRecorder<void (*)()> mExpiredTimerCallback;
48
49 std::unique_ptr<IdleTimer> mIdleTimer;
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080050
51 void clearPendingCallbacks() {
52 while (mExpiredTimerCallback.waitForCall(0us).has_value()) {
53 }
54 }
Ana Krulecfb772822018-11-30 10:44:07 +010055};
56
57namespace {
58TEST_F(IdleTimerTest, createAndDestroyTest) {
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080059 mIdleTimer = std::make_unique<scheduler::IdleTimer>(3ms, [] {});
Ana Krulecfb772822018-11-30 10:44:07 +010060}
61
62TEST_F(IdleTimerTest, startStopTest) {
63 mIdleTimer = std::make_unique<scheduler::IdleTimer>(30ms, mExpiredTimerCallback.getInvocable());
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080064 auto startTime = std::chrono::steady_clock::now();
Ana Krulecfb772822018-11-30 10:44:07 +010065 mIdleTimer->start();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080066 // The idle timer fires after 30ms, so there should be no callback within
67 // 25ms (waiting for a ballback for the full 30ms would be problematic).
68 bool callbackCalled = mExpiredTimerCallback.waitForCall(25ms).has_value();
69 // Under ideal conditions there should be no event. But occasionally
70 // it is possible that the wait just prior takes more than 30ms, and
71 // a callback is observed. We check the elapsed time since before the IdleTimer
72 // thread was started as a sanity check to not have a flakey test.
73 EXPECT_FALSE(callbackCalled && std::chrono::steady_clock::now() - startTime < 30ms);
Ana Krulecfb772822018-11-30 10:44:07 +010074 mIdleTimer->stop();
75}
76
77TEST_F(IdleTimerTest, resetTest) {
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080078 mIdleTimer = std::make_unique<scheduler::IdleTimer>(30ms, mExpiredTimerCallback.getInvocable());
Ana Krulecfb772822018-11-30 10:44:07 +010079 mIdleTimer->start();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080080 // Observe any event that happens in about 25ms. We don't care if one was
81 // observed or not.
82 mExpiredTimerCallback.waitForCall(25ms).has_value();
Ana Krulecfb772822018-11-30 10:44:07 +010083 mIdleTimer->reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080084 // There may have been a race with the reset. Clear any callbacks we
85 // received right afterwards.
86 clearPendingCallbacks();
87 // A single callback should be generated after 30ms
88 EXPECT_TRUE(
89 mExpiredTimerCallback.waitForCall(waitTimeForExpected3msCallback + 30ms).has_value());
90 // After one event, it should be idle, and not generate another.
91 EXPECT_FALSE(
92 mExpiredTimerCallback.waitForCall(waitTimeForUnexpected3msCallback * 10).has_value());
Ana Krulecfb772822018-11-30 10:44:07 +010093 mIdleTimer->stop();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080094 // Final quick check that no more callback were observed.
95 EXPECT_FALSE(mExpiredTimerCallback.waitForCall(0ms).has_value());
Ana Krulecfb772822018-11-30 10:44:07 +010096}
97
98TEST_F(IdleTimerTest, startNotCalledTest) {
99 mIdleTimer = std::make_unique<scheduler::IdleTimer>(3ms, mExpiredTimerCallback.getInvocable());
Ana Krulecfb772822018-11-30 10:44:07 +0100100 // The start hasn't happened, so the callback does not happen.
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800101 EXPECT_FALSE(mExpiredTimerCallback.waitForCall(waitTimeForUnexpected3msCallback).has_value());
Ana Krulecfb772822018-11-30 10:44:07 +0100102 mIdleTimer->stop();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800103 // Final quick check that no more callback were observed.
104 EXPECT_FALSE(mExpiredTimerCallback.waitForCall(0ms).has_value());
Ana Krulecfb772822018-11-30 10:44:07 +0100105}
106
107TEST_F(IdleTimerTest, idleTimerIdlesTest) {
108 mIdleTimer = std::make_unique<scheduler::IdleTimer>(3ms, mExpiredTimerCallback.getInvocable());
109 mIdleTimer->start();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800110 // A callback should be generated after 3ms
111 EXPECT_TRUE(mExpiredTimerCallback.waitForCall(waitTimeForExpected3msCallback).has_value());
112 // After one event, it should be idle, and not generate another.
113 EXPECT_FALSE(mExpiredTimerCallback.waitForCall(waitTimeForUnexpected3msCallback).has_value());
114 // Once reset, it should generate another
Ana Krulecfb772822018-11-30 10:44:07 +0100115 mIdleTimer->reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800116 EXPECT_TRUE(mExpiredTimerCallback.waitForCall(waitTimeForExpected3msCallback).has_value());
Ana Krulecfb772822018-11-30 10:44:07 +0100117 mIdleTimer->stop();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800118 // Final quick check that no more callback were observed.
119 EXPECT_FALSE(mExpiredTimerCallback.waitForCall(0ms).has_value());
Ana Krulecfb772822018-11-30 10:44:07 +0100120}
121
122TEST_F(IdleTimerTest, timeoutCallbackExecutionTest) {
123 mIdleTimer = std::make_unique<scheduler::IdleTimer>(3ms, mExpiredTimerCallback.getInvocable());
124
125 mIdleTimer->start();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800126 EXPECT_TRUE(mExpiredTimerCallback.waitForCall(waitTimeForExpected3msCallback).has_value());
Ana Krulecfb772822018-11-30 10:44:07 +0100127 mIdleTimer->stop();
128}
129
130TEST_F(IdleTimerTest, noCallbacksAfterStopAndResetTest) {
131 mIdleTimer = std::make_unique<scheduler::IdleTimer>(3ms, mExpiredTimerCallback.getInvocable());
132 mIdleTimer->start();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800133 EXPECT_TRUE(mExpiredTimerCallback.waitForCall(waitTimeForExpected3msCallback).has_value());
Ana Krulecfb772822018-11-30 10:44:07 +0100134 mIdleTimer->stop();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800135 clearPendingCallbacks();
Ana Krulecfb772822018-11-30 10:44:07 +0100136 mIdleTimer->reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800137 EXPECT_FALSE(mExpiredTimerCallback.waitForCall(waitTimeForUnexpected3msCallback).has_value());
Ana Krulecfb772822018-11-30 10:44:07 +0100138}
139
140TEST_F(IdleTimerTest, noCallbacksAfterStopTest) {
141 mIdleTimer = std::make_unique<scheduler::IdleTimer>(3ms, mExpiredTimerCallback.getInvocable());
142 mIdleTimer->start();
Ana Krulecfb772822018-11-30 10:44:07 +0100143 mIdleTimer->stop();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800144 clearPendingCallbacks();
145 mIdleTimer->reset();
146 // No more idle events should be observed
147 EXPECT_FALSE(mExpiredTimerCallback.waitForCall(waitTimeForUnexpected3msCallback).has_value());
Ana Krulecfb772822018-11-30 10:44:07 +0100148}
149
150} // namespace
151} // namespace scheduler
152} // namespace android