Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 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 | |
| 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "SchedulerUnittests" |
| 19 | |
| 20 | #include <gmock/gmock.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | #include <utils/Log.h> |
| 23 | |
| 24 | #include "AsyncCallRecorder.h" |
| 25 | #include "Scheduler/IdleTimer.h" |
| 26 | |
| 27 | using namespace std::chrono_literals; |
| 28 | |
| 29 | namespace android { |
| 30 | namespace scheduler { |
| 31 | |
| 32 | class IdleTimerTest : public testing::Test { |
| 33 | protected: |
| 34 | IdleTimerTest() = default; |
| 35 | ~IdleTimerTest() override = default; |
| 36 | |
| 37 | AsyncCallRecorder<void (*)()> mExpiredTimerCallback; |
| 38 | |
| 39 | std::unique_ptr<IdleTimer> mIdleTimer; |
| 40 | }; |
| 41 | |
| 42 | namespace { |
| 43 | TEST_F(IdleTimerTest, createAndDestroyTest) { |
| 44 | mIdleTimer = std::make_unique<scheduler::IdleTimer>(30ms, [] {}); |
| 45 | } |
| 46 | |
| 47 | TEST_F(IdleTimerTest, startStopTest) { |
| 48 | mIdleTimer = std::make_unique<scheduler::IdleTimer>(30ms, mExpiredTimerCallback.getInvocable()); |
| 49 | mIdleTimer->start(); |
| 50 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 51 | // The timer expires after 30 ms, so the call to the callback should not happen. |
| 52 | EXPECT_FALSE(mExpiredTimerCallback.waitForCall().has_value()); |
| 53 | mIdleTimer->stop(); |
| 54 | } |
| 55 | |
| 56 | TEST_F(IdleTimerTest, resetTest) { |
| 57 | mIdleTimer = std::make_unique<scheduler::IdleTimer>(20ms, mExpiredTimerCallback.getInvocable()); |
| 58 | mIdleTimer->start(); |
| 59 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 60 | // The timer expires after 30 ms, so the call to the callback should not happen. |
| 61 | EXPECT_FALSE(mExpiredTimerCallback.waitForCall(1us).has_value()); |
| 62 | mIdleTimer->reset(); |
| 63 | // The timer was reset, so the call to the callback should not happen. |
| 64 | std::this_thread::sleep_for(std::chrono::milliseconds(15)); |
| 65 | EXPECT_FALSE(mExpiredTimerCallback.waitForCall(1us).has_value()); |
| 66 | mIdleTimer->stop(); |
| 67 | } |
| 68 | |
| 69 | TEST_F(IdleTimerTest, startNotCalledTest) { |
| 70 | mIdleTimer = std::make_unique<scheduler::IdleTimer>(3ms, mExpiredTimerCallback.getInvocable()); |
| 71 | std::this_thread::sleep_for(6ms); |
| 72 | // The start hasn't happened, so the callback does not happen. |
| 73 | EXPECT_FALSE(mExpiredTimerCallback.waitForCall(1us).has_value()); |
| 74 | mIdleTimer->stop(); |
| 75 | } |
| 76 | |
| 77 | TEST_F(IdleTimerTest, idleTimerIdlesTest) { |
| 78 | mIdleTimer = std::make_unique<scheduler::IdleTimer>(3ms, mExpiredTimerCallback.getInvocable()); |
| 79 | mIdleTimer->start(); |
| 80 | std::this_thread::sleep_for(6ms); |
| 81 | // The timer expires after 3 ms, so the call to the callback happens. |
| 82 | EXPECT_TRUE(mExpiredTimerCallback.waitForCall(1us).has_value()); |
| 83 | std::this_thread::sleep_for(6ms); |
| 84 | // Timer can be idle. |
| 85 | EXPECT_FALSE(mExpiredTimerCallback.waitForCall(1us).has_value()); |
| 86 | // Timer can be reset. |
| 87 | mIdleTimer->reset(); |
| 88 | std::this_thread::sleep_for(6ms); |
| 89 | // Timer fires again. |
| 90 | EXPECT_TRUE(mExpiredTimerCallback.waitForCall(1us).has_value()); |
| 91 | mIdleTimer->stop(); |
| 92 | } |
| 93 | |
| 94 | TEST_F(IdleTimerTest, timeoutCallbackExecutionTest) { |
| 95 | mIdleTimer = std::make_unique<scheduler::IdleTimer>(3ms, mExpiredTimerCallback.getInvocable()); |
| 96 | |
| 97 | mIdleTimer->start(); |
| 98 | std::this_thread::sleep_for(6ms); |
| 99 | // The timer expires after 3 ms, so the call to the callback should happen. |
| 100 | EXPECT_TRUE(mExpiredTimerCallback.waitForCall(1us).has_value()); |
| 101 | mIdleTimer->stop(); |
| 102 | } |
| 103 | |
| 104 | TEST_F(IdleTimerTest, noCallbacksAfterStopAndResetTest) { |
| 105 | mIdleTimer = std::make_unique<scheduler::IdleTimer>(3ms, mExpiredTimerCallback.getInvocable()); |
| 106 | mIdleTimer->start(); |
| 107 | std::this_thread::sleep_for(6ms); |
| 108 | EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value()); |
| 109 | mIdleTimer->stop(); |
| 110 | mIdleTimer->reset(); |
| 111 | std::this_thread::sleep_for(6ms); |
| 112 | EXPECT_FALSE(mExpiredTimerCallback.waitForCall().has_value()); |
| 113 | } |
| 114 | |
| 115 | TEST_F(IdleTimerTest, noCallbacksAfterStopTest) { |
| 116 | mIdleTimer = std::make_unique<scheduler::IdleTimer>(3ms, mExpiredTimerCallback.getInvocable()); |
| 117 | mIdleTimer->start(); |
| 118 | std::this_thread::sleep_for(1ms); |
| 119 | mIdleTimer->stop(); |
| 120 | std::this_thread::sleep_for(3ms); |
| 121 | EXPECT_FALSE(mExpiredTimerCallback.waitForCall(1us).has_value()); |
| 122 | } |
| 123 | |
| 124 | } // namespace |
| 125 | } // namespace scheduler |
| 126 | } // namespace android |