/* * Copyright 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #undef LOG_TAG #define LOG_TAG "SchedulerUnittests" #include #include #include #include #include "AsyncCallRecorder.h" #include "Scheduler/OneShotTimer.h" #include "fake/FakeClock.h" using namespace std::chrono_literals; namespace android { namespace scheduler { class OneShotTimerTest : public testing::Test { protected: OneShotTimerTest() = default; ~OneShotTimerTest() override = default; AsyncCallRecorder mResetTimerCallback; AsyncCallRecorder mExpiredTimerCallback; std::unique_ptr mIdleTimer; void clearPendingCallbacks() { while (mExpiredTimerCallback.waitForCall(0us).has_value()) { } } }; namespace { TEST_F(OneShotTimerTest, createAndDestroyTest) { fake::FakeClock* clock = new fake::FakeClock(); mIdleTimer = std::make_unique( "TestTimer", 3ms, [] {}, [] {}, std::unique_ptr(clock)); } TEST_F(OneShotTimerTest, startStopTest) { fake::FakeClock* clock = new fake::FakeClock(); mIdleTimer = std::make_unique("TestTimer", 1ms, mResetTimerCallback.getInvocable(), mExpiredTimerCallback.getInvocable(), std::unique_ptr(clock)); mIdleTimer->start(); EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value()); EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); clock->advanceTime(2ms); EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value()); clock->advanceTime(2ms); EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); mIdleTimer->stop(); } TEST_F(OneShotTimerTest, resetTest) { fake::FakeClock* clock = new fake::FakeClock(); mIdleTimer = std::make_unique("TestTimer", 1ms, mResetTimerCallback.getInvocable(), mExpiredTimerCallback.getInvocable(), std::unique_ptr(clock)); mIdleTimer->start(); EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value()); EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); clock->advanceTime(2ms); EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value()); mIdleTimer->reset(); EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value()); clock->advanceTime(2ms); EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value()); clock->advanceTime(2ms); EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); } TEST_F(OneShotTimerTest, resetBackToBackTest) { fake::FakeClock* clock = new fake::FakeClock(); mIdleTimer = std::make_unique("TestTimer", 1ms, mResetTimerCallback.getInvocable(), mExpiredTimerCallback.getInvocable(), std::unique_ptr(clock)); mIdleTimer->start(); EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value()); mIdleTimer->reset(); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); mIdleTimer->reset(); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); mIdleTimer->reset(); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); mIdleTimer->reset(); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); clock->advanceTime(2ms); EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value()); mIdleTimer->stop(); clock->advanceTime(2ms); // Final quick check that no more callback were observed. EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); } TEST_F(OneShotTimerTest, startNotCalledTest) { fake::FakeClock* clock = new fake::FakeClock(); mIdleTimer = std::make_unique("TestTimer", 1ms, mResetTimerCallback.getInvocable(), mExpiredTimerCallback.getInvocable(), std::unique_ptr(clock)); // The start hasn't happened, so the callback does not happen. EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); mIdleTimer->stop(); clock->advanceTime(2ms); // Final quick check that no more callback were observed. EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); } TEST_F(OneShotTimerTest, idleTimerIdlesTest) { fake::FakeClock* clock = new fake::FakeClock(); mIdleTimer = std::make_unique("TestTimer", 1ms, mResetTimerCallback.getInvocable(), mExpiredTimerCallback.getInvocable(), std::unique_ptr(clock)); mIdleTimer->start(); EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value()); clock->advanceTime(2ms); EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value()); EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); mIdleTimer->reset(); EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value()); clock->advanceTime(2ms); EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value()); mIdleTimer->stop(); clock->advanceTime(2ms); // Final quick check that no more callback were observed. EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); } TEST_F(OneShotTimerTest, timeoutCallbackExecutionTest) { fake::FakeClock* clock = new fake::FakeClock(); mIdleTimer = std::make_unique("TestTimer", 1ms, mResetTimerCallback.getInvocable(), mExpiredTimerCallback.getInvocable(), std::unique_ptr(clock)); mIdleTimer->start(); EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value()); clock->advanceTime(2ms); EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value()); mIdleTimer->stop(); clock->advanceTime(2ms); EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); } TEST_F(OneShotTimerTest, noCallbacksAfterStopAndResetTest) { fake::FakeClock* clock = new fake::FakeClock(); mIdleTimer = std::make_unique("TestTimer", 1ms, mResetTimerCallback.getInvocable(), mExpiredTimerCallback.getInvocable(), std::unique_ptr(clock)); mIdleTimer->start(); EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value()); clock->advanceTime(2ms); EXPECT_TRUE(mExpiredTimerCallback.waitForCall().has_value()); mIdleTimer->stop(); mIdleTimer->reset(); clock->advanceTime(2ms); EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); } TEST_F(OneShotTimerTest, noCallbacksAfterStopTest) { fake::FakeClock* clock = new fake::FakeClock(); mIdleTimer = std::make_unique("TestTimer", 1ms, mResetTimerCallback.getInvocable(), mExpiredTimerCallback.getInvocable(), std::unique_ptr(clock)); mIdleTimer->start(); EXPECT_TRUE(mResetTimerCallback.waitForCall().has_value()); EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); mIdleTimer->stop(); mIdleTimer->reset(); clock->advanceTime(2ms); // No more idle events should be observed EXPECT_FALSE(mExpiredTimerCallback.waitForUnexpectedCall().has_value()); EXPECT_FALSE(mResetTimerCallback.waitForUnexpectedCall().has_value()); } } // namespace } // namespace scheduler } // namespace android