• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #undef LOG_TAG
17 #define LOG_TAG "SchedulerUnittests"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <utils/Log.h>
22 #include <utils/Timers.h>
23 
24 #include "AsyncCallRecorder.h"
25 #include "Scheduler/OneShotTimer.h"
26 #include "fake/FakeClock.h"
27 
28 using namespace std::chrono_literals;
29 
30 namespace android {
31 namespace scheduler {
32 
33 class OneShotTimerTest : public testing::Test {
34 protected:
35     OneShotTimerTest() = default;
36     ~OneShotTimerTest() override = default;
37 
38     AsyncCallRecorder<void (*)()> mResetTimerCallback;
39     AsyncCallRecorder<void (*)()> mExpiredTimerCallback;
40 
41     std::unique_ptr<OneShotTimer> mIdleTimer;
42 
clearPendingCallbacks()43     void clearPendingCallbacks() {
44         while (mExpiredTimerCallback.waitForCall(0us).has_value()) {
45         }
46     }
47 };
48 
49 namespace {
TEST_F(OneShotTimerTest,createAndDestroyTest)50 TEST_F(OneShotTimerTest, createAndDestroyTest) {
51     fake::FakeClock* clock = new fake::FakeClock();
52     mIdleTimer = std::make_unique<scheduler::OneShotTimer>(
53             "TestTimer", 3ms, [] {}, [] {}, std::unique_ptr<fake::FakeClock>(clock));
54 }
55 
TEST_F(OneShotTimerTest,startStopTest)56 TEST_F(OneShotTimerTest, startStopTest) {
57     fake::FakeClock* clock = new fake::FakeClock();
58     mIdleTimer = std::make_unique<scheduler::OneShotTimer>("TestTimer", 1ms,
59                                                            mResetTimerCallback.getInvocable(),
60                                                            mExpiredTimerCallback.getInvocable(),
61                                                            std::unique_ptr<fake::FakeClock>(clock));
62     mIdleTimer->start();
63     EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value());
64     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
65 
66     clock->advanceTime(2ms);
67     EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value());
68 
69     clock->advanceTime(2ms);
70     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
71     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
72     mIdleTimer->stop();
73 }
74 
75 // TODO(b/186417847) This test is flaky. Reenable once fixed.
TEST_F(OneShotTimerTest,DISABLED_resetTest)76 TEST_F(OneShotTimerTest, DISABLED_resetTest) {
77     fake::FakeClock* clock = new fake::FakeClock();
78     mIdleTimer = std::make_unique<scheduler::OneShotTimer>("TestTimer", 1ms,
79                                                            mResetTimerCallback.getInvocable(),
80                                                            mExpiredTimerCallback.getInvocable(),
81                                                            std::unique_ptr<fake::FakeClock>(clock));
82 
83     mIdleTimer->start();
84     EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value());
85     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
86     clock->advanceTime(2ms);
87     EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value());
88     mIdleTimer->reset();
89     EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value());
90     clock->advanceTime(2ms);
91     EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value());
92 
93     clock->advanceTime(2ms);
94     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
95     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
96 }
97 
98 // TODO(b/186417847) This test is flaky. Reenable once fixed.
TEST_F(OneShotTimerTest,DISABLED_resetBackToBackTest)99 TEST_F(OneShotTimerTest, DISABLED_resetBackToBackTest) {
100     fake::FakeClock* clock = new fake::FakeClock();
101     mIdleTimer = std::make_unique<scheduler::OneShotTimer>("TestTimer", 1ms,
102                                                            mResetTimerCallback.getInvocable(),
103                                                            mExpiredTimerCallback.getInvocable(),
104                                                            std::unique_ptr<fake::FakeClock>(clock));
105     mIdleTimer->start();
106     EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value());
107 
108     mIdleTimer->reset();
109     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
110     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
111 
112     mIdleTimer->reset();
113     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
114     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
115 
116     mIdleTimer->reset();
117     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
118     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
119 
120     mIdleTimer->reset();
121     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
122 
123     clock->advanceTime(2ms);
124     EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value());
125 
126     mIdleTimer->stop();
127     clock->advanceTime(2ms);
128     // Final quick check that no more callback were observed.
129     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
130     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
131 }
132 
133 // TODO(b/186417847) This test is new and passes locally, but may be flaky
TEST_F(OneShotTimerTest,DISABLED_resetBackToBackSlowAdvanceTest)134 TEST_F(OneShotTimerTest, DISABLED_resetBackToBackSlowAdvanceTest) {
135     fake::FakeClock* clock = new fake::FakeClock();
136     mIdleTimer = std::make_unique<scheduler::OneShotTimer>("TestTimer", 1ms,
137                                                            mResetTimerCallback.getInvocable(),
138                                                            mExpiredTimerCallback.getInvocable(),
139                                                            std::unique_ptr<fake::FakeClock>(clock));
140     mIdleTimer->start();
141     EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value());
142 
143     mIdleTimer->reset();
144     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
145     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
146 
147     clock->advanceTime(200us);
148     mIdleTimer->reset();
149 
150     // Normally we would check that the timer callbacks weren't invoked here
151     // after resetting the timer, but we need to precisely control the timing of
152     // this test, and checking that callbacks weren't invoked requires non-zero
153     // time.
154 
155     clock->advanceTime(1500us);
156     EXPECT_TRUE(mExpiredTimerCallback.waitForCall(1100us).has_value());
157     mIdleTimer->reset();
158     EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value());
159 
160     mIdleTimer->stop();
161     clock->advanceTime(2ms);
162     // Final quick check that no more callback were observed.
163     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
164     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
165 }
166 
TEST_F(OneShotTimerTest,startNotCalledTest)167 TEST_F(OneShotTimerTest, startNotCalledTest) {
168     fake::FakeClock* clock = new fake::FakeClock();
169     mIdleTimer = std::make_unique<scheduler::OneShotTimer>("TestTimer", 1ms,
170                                                            mResetTimerCallback.getInvocable(),
171                                                            mExpiredTimerCallback.getInvocable(),
172                                                            std::unique_ptr<fake::FakeClock>(clock));
173     // The start hasn't happened, so the callback does not happen.
174     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
175     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
176     mIdleTimer->stop();
177     clock->advanceTime(2ms);
178     // Final quick check that no more callback were observed.
179     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
180     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
181 }
182 
183 // TODO(b/186417847) This test is flaky. Reenable once fixed.
TEST_F(OneShotTimerTest,DISABLED_idleTimerIdlesTest)184 TEST_F(OneShotTimerTest, DISABLED_idleTimerIdlesTest) {
185     fake::FakeClock* clock = new fake::FakeClock();
186     mIdleTimer = std::make_unique<scheduler::OneShotTimer>("TestTimer", 1ms,
187                                                            mResetTimerCallback.getInvocable(),
188                                                            mExpiredTimerCallback.getInvocable(),
189                                                            std::unique_ptr<fake::FakeClock>(clock));
190     mIdleTimer->start();
191     EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value());
192     clock->advanceTime(2ms);
193     EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value());
194 
195     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
196     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
197 
198     mIdleTimer->reset();
199     EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value());
200     clock->advanceTime(2ms);
201     EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value());
202     mIdleTimer->stop();
203     clock->advanceTime(2ms);
204     // Final quick check that no more callback were observed.
205     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
206     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
207 }
208 
209 // TODO(b/186417847) This test is flaky. Reenable once fixed.
TEST_F(OneShotTimerTest,DISABLED_timeoutCallbackExecutionTest)210 TEST_F(OneShotTimerTest, DISABLED_timeoutCallbackExecutionTest) {
211     fake::FakeClock* clock = new fake::FakeClock();
212     mIdleTimer = std::make_unique<scheduler::OneShotTimer>("TestTimer", 1ms,
213                                                            mResetTimerCallback.getInvocable(),
214                                                            mExpiredTimerCallback.getInvocable(),
215                                                            std::unique_ptr<fake::FakeClock>(clock));
216     mIdleTimer->start();
217     EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value());
218 
219     clock->advanceTime(2ms);
220     EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value());
221     mIdleTimer->stop();
222     clock->advanceTime(2ms);
223     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
224     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
225 }
226 
227 // TODO(b/186417847) This test is flaky. Reenable once fixed.
TEST_F(OneShotTimerTest,DISABLED_noCallbacksAfterStopAndResetTest)228 TEST_F(OneShotTimerTest, DISABLED_noCallbacksAfterStopAndResetTest) {
229     fake::FakeClock* clock = new fake::FakeClock();
230     mIdleTimer = std::make_unique<scheduler::OneShotTimer>("TestTimer", 1ms,
231                                                            mResetTimerCallback.getInvocable(),
232                                                            mExpiredTimerCallback.getInvocable(),
233                                                            std::unique_ptr<fake::FakeClock>(clock));
234     mIdleTimer->start();
235     EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value());
236     clock->advanceTime(2ms);
237     EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value());
238 
239     mIdleTimer->stop();
240     mIdleTimer->reset();
241     clock->advanceTime(2ms);
242     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
243     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
244 }
245 
TEST_F(OneShotTimerTest,noCallbacksAfterStopTest)246 TEST_F(OneShotTimerTest, noCallbacksAfterStopTest) {
247     fake::FakeClock* clock = new fake::FakeClock();
248     mIdleTimer = std::make_unique<scheduler::OneShotTimer>("TestTimer", 1ms,
249                                                            mResetTimerCallback.getInvocable(),
250                                                            mExpiredTimerCallback.getInvocable(),
251                                                            std::unique_ptr<fake::FakeClock>(clock));
252     mIdleTimer->start();
253     EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value());
254     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
255 
256     mIdleTimer->stop();
257     mIdleTimer->reset();
258 
259     clock->advanceTime(2ms);
260     // No more idle events should be observed
261     EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value());
262     EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value());
263 }
264 
265 } // namespace
266 } // namespace scheduler
267 } // namespace android
268