blob: 47d968c94eec7fa065df77342ba73e8c9177bb57 [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
Ady Abrahamb491c902020-08-15 15:47:56 -070017#include <gmock/gmock.h>
18#include <gtest/gtest.h>
19
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080020#include <scheduler/TimeKeeper.h>
21#include <scheduler/Timer.h>
22
23#include "AsyncCallRecorder.h"
Ady Abrahamb491c902020-08-15 15:47:56 -070024
25namespace android::scheduler {
26
Ady Abraham3a3815a2021-12-23 14:32:07 -080027struct TestableTimer : public Timer {
28public:
29 void makeEpollError() {
30 // close the epoll file descriptor to cause an epoll error
31 close(mEpollFd);
32 }
33};
34
Ady Abrahamb491c902020-08-15 15:47:56 -070035struct TimerTest : testing::Test {
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080036 static constexpr int kIterations = 20;
Ady Abrahamb491c902020-08-15 15:47:56 -070037
38 AsyncCallRecorder<void (*)()> mCallbackRecorder;
Ady Abraham3a3815a2021-12-23 14:32:07 -080039 TestableTimer mTimer;
Ady Abrahamb491c902020-08-15 15:47:56 -070040
41 void timerCallback() { mCallbackRecorder.recordCall(); }
42};
43
44TEST_F(TimerTest, callsCallbackIfScheduledInPast) {
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080045 for (int i = 0; i < kIterations; i++) {
46 mTimer.alarmAt(std::bind(&TimerTest::timerCallback, this), systemTime() - 1'000'000);
Ady Abrahamb491c902020-08-15 15:47:56 -070047 EXPECT_TRUE(mCallbackRecorder.waitForCall().has_value());
48 EXPECT_FALSE(mCallbackRecorder.waitForUnexpectedCall().has_value());
49 }
50}
Ady Abraham3a3815a2021-12-23 14:32:07 -080051
52TEST_F(TimerTest, recoversAfterEpollError) {
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080053 for (int i = 0; i < kIterations; i++) {
Ady Abraham3a3815a2021-12-23 14:32:07 -080054 mTimer.makeEpollError();
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080055 mTimer.alarmAt(std::bind(&TimerTest::timerCallback, this), systemTime() - 1'000'000);
Ady Abraham3a3815a2021-12-23 14:32:07 -080056 EXPECT_TRUE(mCallbackRecorder.waitForCall().has_value());
57 EXPECT_FALSE(mCallbackRecorder.waitForUnexpectedCall().has_value());
58 }
59}
60
Ady Abrahamb491c902020-08-15 15:47:56 -070061} // namespace android::scheduler