1 /*
2 * Copyright 2020 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 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19
20 #include <scheduler/TimeKeeper.h>
21 #include <scheduler/Timer.h>
22
23 #include "AsyncCallRecorder.h"
24
25 namespace android::scheduler {
26
27 struct TestableTimer : public Timer {
28 public:
makeEpollErrorandroid::scheduler::TestableTimer29 void makeEpollError() {
30 // close the epoll file descriptor to cause an epoll error
31 close(mEpollFd);
32 }
33 };
34
35 struct TimerTest : testing::Test {
36 static constexpr int kIterations = 20;
37
38 AsyncCallRecorder<void (*)()> mCallbackRecorder;
39 TestableTimer mTimer;
40
timerCallbackandroid::scheduler::TimerTest41 void timerCallback() { mCallbackRecorder.recordCall(); }
42 };
43
TEST_F(TimerTest,callsCallbackIfScheduledInPast)44 TEST_F(TimerTest, callsCallbackIfScheduledInPast) {
45 for (int i = 0; i < kIterations; i++) {
46 mTimer.alarmAt(std::bind(&TimerTest::timerCallback, this), systemTime() - 1'000'000);
47 EXPECT_TRUE(mCallbackRecorder.waitForCall().has_value());
48 EXPECT_FALSE(mCallbackRecorder.waitForUnexpectedCall().has_value());
49 }
50 }
51
TEST_F(TimerTest,recoversAfterEpollError)52 TEST_F(TimerTest, recoversAfterEpollError) {
53 for (int i = 0; i < kIterations; i++) {
54 mTimer.makeEpollError();
55 mTimer.alarmAt(std::bind(&TimerTest::timerCallback, this), systemTime() - 1'000'000);
56 EXPECT_TRUE(mCallbackRecorder.waitForCall().has_value());
57 EXPECT_FALSE(mCallbackRecorder.waitForUnexpectedCall().has_value());
58 }
59 }
60
61 } // namespace android::scheduler
62