blob: 0a3639db90fe383b03985fe184c23e36eed6199c [file] [log] [blame]
Ady Abrahamb491c902020-08-15 15:47:56 -07001/*
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 "AsyncCallRecorder.h"
18#include "Scheduler/TimeKeeper.h"
19#include "Scheduler/Timer.h"
20
21#include <gmock/gmock.h>
22#include <gtest/gtest.h>
23
24using namespace testing;
25using namespace std::literals;
26
27namespace android::scheduler {
28
Ady Abraham3a3815a2021-12-23 14:32:07 -080029struct TestableTimer : public Timer {
30public:
31 void makeEpollError() {
32 // close the epoll file descriptor to cause an epoll error
33 close(mEpollFd);
34 }
35};
36
Ady Abrahamb491c902020-08-15 15:47:56 -070037struct TimerTest : testing::Test {
38 static constexpr int mIterations = 20;
39
40 AsyncCallRecorder<void (*)()> mCallbackRecorder;
Ady Abraham3a3815a2021-12-23 14:32:07 -080041 TestableTimer mTimer;
Ady Abrahamb491c902020-08-15 15:47:56 -070042
43 void timerCallback() { mCallbackRecorder.recordCall(); }
44};
45
46TEST_F(TimerTest, callsCallbackIfScheduledInPast) {
47 for (int i = 0; i < mIterations; i++) {
48 mTimer.alarmAt(std::bind(&TimerTest::timerCallback, this), systemTime() - 10'000'00);
49 EXPECT_TRUE(mCallbackRecorder.waitForCall().has_value());
50 EXPECT_FALSE(mCallbackRecorder.waitForUnexpectedCall().has_value());
51 }
52}
Ady Abraham3a3815a2021-12-23 14:32:07 -080053
54TEST_F(TimerTest, recoversAfterEpollError) {
55 for (int i = 0; i < mIterations; i++) {
56 mTimer.makeEpollError();
57 mTimer.alarmAt(std::bind(&TimerTest::timerCallback, this), systemTime() - 10'000'00);
58 EXPECT_TRUE(mCallbackRecorder.waitForCall().has_value());
59 EXPECT_FALSE(mCallbackRecorder.waitForUnexpectedCall().has_value());
60 }
61}
62
Ady Abrahamb491c902020-08-15 15:47:56 -070063} // namespace android::scheduler