Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 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 | |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 17 | #include <gmock/gmock.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 20 | #include <scheduler/TimeKeeper.h> |
| 21 | #include <scheduler/Timer.h> |
| 22 | |
| 23 | #include "AsyncCallRecorder.h" |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android::scheduler { |
| 26 | |
Ady Abraham | 3a3815a | 2021-12-23 14:32:07 -0800 | [diff] [blame] | 27 | struct TestableTimer : public Timer { |
| 28 | public: |
| 29 | void makeEpollError() { |
| 30 | // close the epoll file descriptor to cause an epoll error |
| 31 | close(mEpollFd); |
| 32 | } |
| 33 | }; |
| 34 | |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 35 | struct TimerTest : testing::Test { |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 36 | static constexpr int kIterations = 20; |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 37 | |
| 38 | AsyncCallRecorder<void (*)()> mCallbackRecorder; |
Ady Abraham | 3a3815a | 2021-12-23 14:32:07 -0800 | [diff] [blame] | 39 | TestableTimer mTimer; |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 40 | |
| 41 | void timerCallback() { mCallbackRecorder.recordCall(); } |
| 42 | }; |
| 43 | |
| 44 | TEST_F(TimerTest, callsCallbackIfScheduledInPast) { |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 45 | for (int i = 0; i < kIterations; i++) { |
| 46 | mTimer.alarmAt(std::bind(&TimerTest::timerCallback, this), systemTime() - 1'000'000); |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 47 | EXPECT_TRUE(mCallbackRecorder.waitForCall().has_value()); |
| 48 | EXPECT_FALSE(mCallbackRecorder.waitForUnexpectedCall().has_value()); |
| 49 | } |
| 50 | } |
Ady Abraham | 3a3815a | 2021-12-23 14:32:07 -0800 | [diff] [blame] | 51 | |
| 52 | TEST_F(TimerTest, recoversAfterEpollError) { |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 53 | for (int i = 0; i < kIterations; i++) { |
Ady Abraham | 3a3815a | 2021-12-23 14:32:07 -0800 | [diff] [blame] | 54 | mTimer.makeEpollError(); |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 55 | mTimer.alarmAt(std::bind(&TimerTest::timerCallback, this), systemTime() - 1'000'000); |
Ady Abraham | 3a3815a | 2021-12-23 14:32:07 -0800 | [diff] [blame] | 56 | EXPECT_TRUE(mCallbackRecorder.waitForCall().has_value()); |
| 57 | EXPECT_FALSE(mCallbackRecorder.waitForUnexpectedCall().has_value()); |
| 58 | } |
| 59 | } |
| 60 | |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 61 | } // namespace android::scheduler |