SF: Set up libscheduler and libscheduler_test
Fix up dependencies to migrate Timer files for starters.
Bug: 185535769
Test: libscheduler_test
Change-Id: I028cb04f2e355e2096be9efe3c1a80f0dfd75602
diff --git a/services/surfaceflinger/tests/unittests/Android.bp b/services/surfaceflinger/tests/unittests/Android.bp
index c053d43..d3be0ea 100644
--- a/services/surfaceflinger/tests/unittests/Android.bp
+++ b/services/surfaceflinger/tests/unittests/Android.bp
@@ -89,7 +89,6 @@
"RegionSamplingTest.cpp",
"TimeStatsTest.cpp",
"FrameTracerTest.cpp",
- "TimerTest.cpp",
"TransactionApplicationTest.cpp",
"TransactionFrameTracerTest.cpp",
"TransactionProtoParserTest.cpp",
@@ -178,11 +177,12 @@
"server_configurable_flags",
],
header_libs: [
+ "android.hardware.graphics.composer3-command-buffer",
"android.hardware.graphics.composer@2.1-command-buffer",
"android.hardware.graphics.composer@2.2-command-buffer",
"android.hardware.graphics.composer@2.3-command-buffer",
"android.hardware.graphics.composer@2.4-command-buffer",
- "android.hardware.graphics.composer3-command-buffer",
+ "libscheduler_test_headers",
"libsurfaceflinger_headers",
],
}
diff --git a/services/surfaceflinger/tests/unittests/AsyncCallRecorder.h b/services/surfaceflinger/tests/unittests/AsyncCallRecorder.h
deleted file mode 100644
index 8bed766..0000000
--- a/services/surfaceflinger/tests/unittests/AsyncCallRecorder.h
+++ /dev/null
@@ -1,167 +0,0 @@
-/*
- * Copyright (C) 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.
- */
-
-#pragma once
-
-#include <chrono>
-#include <deque>
-#include <mutex>
-#include <optional>
-#include <thread>
-#include <tuple>
-#include <type_traits>
-#include <utility>
-
-#include <android-base/thread_annotations.h>
-
-namespace android {
-
-// This class helps record calls made by another thread when they are made
-// asynchronously, with no other way for the tests to verify that the calls have
-// been made.
-//
-// A normal Google Mock recorder, while thread safe, does not allow you to wait
-// for asynchronous calls to be made.
-//
-// Usage:
-//
-// In the test, use a Google Mock expectation to invoke an instance of the
-// recorder:
-//
-// AsyncCallRecorder<void(int)> recorder;
-//
-// EXPECT_CALL(someMock, someFunction(_)).
-// .WillRepeatedly(Invoke(recorder.getInvocable()));
-//
-// Then you can invoke the functionality being tested:
-//
-// threadUnderTest.doSomethingAsync()
-//
-// And afterwards make a number of assertions using the recorder:
-//
-// // Wait for one call (with reasonable default timeout), and get the args
-// // as a std::tuple inside a std::optional.
-// auto args = recorder.waitForCall();
-// // The returned std::optional will have a value if the recorder function
-// // was called.
-// ASSERT_TRUE(args.has_value());
-// // The arguments can be checked if needed using standard tuple
-// // operations.
-// EXPECT_EQ(123, std::get<0>(args.value()));
-//
-// Alternatively maybe you want to assert that a call was not made.
-//
-// EXPECT_FALSE(recorder.waitForUnexpectedCall().has_value());
-//
-// However this check uses a really short timeout so as not to block the test
-// unnecessarily. And it could be possible for the check to return false and
-// then the recorder could observe a call being made after.
-template <typename Func>
-class AsyncCallRecorder;
-
-template <typename... Args>
-class AsyncCallRecorder<void (*)(Args...)> {
-public:
- // This wait value needs to be large enough to avoid flakes caused by delays
- // scheduling threads, but small enough that tests don't take forever if
- // something really is wrong. Based on some empirical evidence, 100ms should
- // be enough to avoid the former.
- static constexpr std::chrono::milliseconds DEFAULT_CALL_EXPECTED_TIMEOUT{100};
-
- // The wait here is tricky. It's for when We don't expect to record a call,
- // but we don't want to wait forever (or for longer than the typical test
- // function runtime). As even the simplest Google Test can take 1ms (1000us)
- // to run, we wait for half that time.
- static constexpr std::chrono::microseconds UNEXPECTED_CALL_TIMEOUT{500};
-
- using ArgTuple = std::tuple<std::remove_cv_t<std::remove_reference_t<Args>>...>;
-
- void recordCall(Args... args) {
- std::lock_guard<std::mutex> lock(mMutex);
- mCalls.emplace_back(std::make_tuple(args...));
- mCondition.notify_all();
- }
-
- // Returns a functor which can be used with the Google Mock Invoke()
- // function, or as a std::function to record calls.
- auto getInvocable() {
- return [this](Args... args) { recordCall(args...); };
- }
-
- // Returns a set of arguments as a std::optional<std::tuple<...>> for the
- // oldest call, waiting for the given timeout if necessary if there are no
- // arguments in the FIFO.
- std::optional<ArgTuple> waitForCall(
- std::chrono::microseconds timeout = DEFAULT_CALL_EXPECTED_TIMEOUT)
- NO_THREAD_SAFETY_ANALYSIS {
- std::unique_lock<std::mutex> lock(mMutex);
-
- // Wait if necessary for us to have a record from a call.
- mCondition.wait_for(lock, timeout,
- [this]() NO_THREAD_SAFETY_ANALYSIS { return !mCalls.empty(); });
-
- // Return the arguments from the oldest call, if one was made
- bool called = !mCalls.empty();
- std::optional<ArgTuple> result;
- if (called) {
- result.emplace(std::move(mCalls.front()));
- mCalls.pop_front();
- }
- return result;
- }
-
- // Waits using a small default timeout for when a call is not expected to be
- // made. The returned std::optional<std:tuple<...>> should not have a value
- // except if a set of arguments was unexpectedly received because a call was
- // actually made.
- //
- // Note this function uses a small timeout to not block test execution, and
- // it is possible the code under test could make the call AFTER the timeout
- // expires.
- std::optional<ArgTuple> waitForUnexpectedCall() { return waitForCall(UNEXPECTED_CALL_TIMEOUT); }
-
-private:
- std::mutex mMutex;
- std::condition_variable mCondition;
- std::deque<ArgTuple> mCalls GUARDED_BY(mMutex);
-};
-
-// Like AsyncCallRecorder, but for when the function being invoked
-// asynchronously is expected to return a value.
-//
-// This helper allows a single constant return value to be set to be returned by
-// all calls that were made.
-template <typename Func>
-class AsyncCallRecorderWithCannedReturn;
-
-template <typename Ret, typename... Args>
-class AsyncCallRecorderWithCannedReturn<Ret (*)(Args...)>
- : public AsyncCallRecorder<void (*)(Args...)> {
-public:
- explicit AsyncCallRecorderWithCannedReturn(Ret returnvalue) : mReturnValue(returnvalue) {}
-
- auto getInvocable() {
- return [this](Args... args) {
- this->recordCall(args...);
- return mReturnValue;
- };
- }
-
-private:
- const Ret mReturnValue;
-};
-
-} // namespace android
diff --git a/services/surfaceflinger/tests/unittests/DispSyncSourceTest.cpp b/services/surfaceflinger/tests/unittests/DispSyncSourceTest.cpp
index a9ad249..f613e43 100644
--- a/services/surfaceflinger/tests/unittests/DispSyncSourceTest.cpp
+++ b/services/surfaceflinger/tests/unittests/DispSyncSourceTest.cpp
@@ -37,12 +37,11 @@
class MockVSyncDispatch : public scheduler::VSyncDispatch {
public:
- MOCK_METHOD2(registerCallback,
- CallbackToken(std::function<void(nsecs_t, nsecs_t, nsecs_t)> const&, std::string));
- MOCK_METHOD1(unregisterCallback, void(CallbackToken));
- MOCK_METHOD2(schedule, scheduler::ScheduleResult(CallbackToken, ScheduleTiming));
- MOCK_METHOD1(cancel, scheduler::CancelResult(CallbackToken token));
- MOCK_CONST_METHOD1(dump, void(std::string&));
+ MOCK_METHOD(CallbackToken, registerCallback, (Callback, std::string), (override));
+ MOCK_METHOD(void, unregisterCallback, (CallbackToken), (override));
+ MOCK_METHOD(scheduler::ScheduleResult, schedule, (CallbackToken, ScheduleTiming), (override));
+ MOCK_METHOD(scheduler::CancelResult, cancel, (CallbackToken), (override));
+ MOCK_METHOD(void, dump, (std::string&), (const, override));
MockVSyncDispatch() {
ON_CALL(*this, registerCallback)
diff --git a/services/surfaceflinger/tests/unittests/MessageQueueTest.cpp b/services/surfaceflinger/tests/unittests/MessageQueueTest.cpp
index bd4dc59..1dd7dea 100644
--- a/services/surfaceflinger/tests/unittests/MessageQueueTest.cpp
+++ b/services/surfaceflinger/tests/unittests/MessageQueueTest.cpp
@@ -56,12 +56,11 @@
};
struct MockVSyncDispatch : scheduler::VSyncDispatch {
- MOCK_METHOD2(registerCallback,
- CallbackToken(const std::function<void(nsecs_t, nsecs_t, nsecs_t)>&, std::string));
- MOCK_METHOD1(unregisterCallback, void(CallbackToken));
- MOCK_METHOD2(schedule, scheduler::ScheduleResult(CallbackToken, ScheduleTiming));
- MOCK_METHOD1(cancel, scheduler::CancelResult(CallbackToken token));
- MOCK_CONST_METHOD1(dump, void(std::string&));
+ MOCK_METHOD(CallbackToken, registerCallback, (Callback, std::string), (override));
+ MOCK_METHOD(void, unregisterCallback, (CallbackToken), (override));
+ MOCK_METHOD(scheduler::ScheduleResult, schedule, (CallbackToken, ScheduleTiming), (override));
+ MOCK_METHOD(scheduler::CancelResult, cancel, (CallbackToken token), (override));
+ MOCK_METHOD(void, dump, (std::string&), (const, override));
};
struct MockTokenManager : frametimeline::TokenManager {
diff --git a/services/surfaceflinger/tests/unittests/TimerTest.cpp b/services/surfaceflinger/tests/unittests/TimerTest.cpp
deleted file mode 100644
index 0a3639d..0000000
--- a/services/surfaceflinger/tests/unittests/TimerTest.cpp
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright 2020 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.
- */
-
-#include "AsyncCallRecorder.h"
-#include "Scheduler/TimeKeeper.h"
-#include "Scheduler/Timer.h"
-
-#include <gmock/gmock.h>
-#include <gtest/gtest.h>
-
-using namespace testing;
-using namespace std::literals;
-
-namespace android::scheduler {
-
-struct TestableTimer : public Timer {
-public:
- void makeEpollError() {
- // close the epoll file descriptor to cause an epoll error
- close(mEpollFd);
- }
-};
-
-struct TimerTest : testing::Test {
- static constexpr int mIterations = 20;
-
- AsyncCallRecorder<void (*)()> mCallbackRecorder;
- TestableTimer mTimer;
-
- void timerCallback() { mCallbackRecorder.recordCall(); }
-};
-
-TEST_F(TimerTest, callsCallbackIfScheduledInPast) {
- for (int i = 0; i < mIterations; i++) {
- mTimer.alarmAt(std::bind(&TimerTest::timerCallback, this), systemTime() - 10'000'00);
- EXPECT_TRUE(mCallbackRecorder.waitForCall().has_value());
- EXPECT_FALSE(mCallbackRecorder.waitForUnexpectedCall().has_value());
- }
-}
-
-TEST_F(TimerTest, recoversAfterEpollError) {
- for (int i = 0; i < mIterations; i++) {
- mTimer.makeEpollError();
- mTimer.alarmAt(std::bind(&TimerTest::timerCallback, this), systemTime() - 10'000'00);
- EXPECT_TRUE(mCallbackRecorder.waitForCall().has_value());
- EXPECT_FALSE(mCallbackRecorder.waitForUnexpectedCall().has_value());
- }
-}
-
-} // namespace android::scheduler
diff --git a/services/surfaceflinger/tests/unittests/VSyncDispatchRealtimeTest.cpp b/services/surfaceflinger/tests/unittests/VSyncDispatchRealtimeTest.cpp
index 42b1993..2da266b 100644
--- a/services/surfaceflinger/tests/unittests/VSyncDispatchRealtimeTest.cpp
+++ b/services/surfaceflinger/tests/unittests/VSyncDispatchRealtimeTest.cpp
@@ -14,14 +14,15 @@
* limitations under the License.
*/
-#include "Scheduler/TimeKeeper.h"
-#include "Scheduler/Timer.h"
-#include "Scheduler/VSyncDispatchTimerQueue.h"
-#include "Scheduler/VSyncTracker.h"
+#include <thread>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
-#include <thread>
+
+#include <scheduler/Timer.h>
+
+#include "Scheduler/VSyncDispatchTimerQueue.h"
+#include "Scheduler/VSyncTracker.h"
using namespace testing;
using namespace std::literals;
diff --git a/services/surfaceflinger/tests/unittests/VSyncDispatchTimerQueueTest.cpp b/services/surfaceflinger/tests/unittests/VSyncDispatchTimerQueueTest.cpp
index ddc02bf..b7f968d 100644
--- a/services/surfaceflinger/tests/unittests/VSyncDispatchTimerQueueTest.cpp
+++ b/services/surfaceflinger/tests/unittests/VSyncDispatchTimerQueueTest.cpp
@@ -23,16 +23,19 @@
#define LOG_TAG "LibSurfaceFlingerUnittests"
#define LOG_NDEBUG 0
-#include "Scheduler/TimeKeeper.h"
-#include "Scheduler/VSyncDispatchTimerQueue.h"
-#include "Scheduler/VSyncTracker.h"
+#include <thread>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
-#include <thread>
+
+#include <scheduler/TimeKeeper.h>
+
+#include "Scheduler/VSyncDispatchTimerQueue.h"
+#include "Scheduler/VSyncTracker.h"
using namespace testing;
using namespace std::literals;
+
namespace android::scheduler {
class MockVSyncTracker : public VSyncTracker {
@@ -71,10 +74,10 @@
ON_CALL(*this, now()).WillByDefault(Invoke(this, &ControllableClock::fakeTime));
}
- MOCK_CONST_METHOD0(now, nsecs_t());
- MOCK_METHOD2(alarmAt, void(std::function<void()> const&, nsecs_t time));
- MOCK_METHOD0(alarmCancel, void());
- MOCK_CONST_METHOD1(dump, void(std::string&));
+ MOCK_METHOD(nsecs_t, now, (), (const));
+ MOCK_METHOD(void, alarmAt, (std::function<void()>, nsecs_t), (override));
+ MOCK_METHOD(void, alarmCancel, (), (override));
+ MOCK_METHOD(void, dump, (std::string&), (const, override));
void alarmAtDefaultBehavior(std::function<void()> const& callback, nsecs_t time) {
mCallback = callback;
@@ -196,11 +199,14 @@
class TimeKeeperWrapper : public TimeKeeper {
public:
TimeKeeperWrapper(TimeKeeper& control) : mControllableClock(control) {}
- void alarmAt(std::function<void()> const& callback, nsecs_t time) final {
- mControllableClock.alarmAt(callback, time);
- }
- void alarmCancel() final { mControllableClock.alarmCancel(); }
+
nsecs_t now() const final { return mControllableClock.now(); }
+
+ void alarmAt(std::function<void()> callback, nsecs_t time) final {
+ mControllableClock.alarmAt(std::move(callback), time);
+ }
+
+ void alarmCancel() final { mControllableClock.alarmCancel(); }
void dump(std::string&) const final {}
private:
diff --git a/services/surfaceflinger/tests/unittests/VSyncReactorTest.cpp b/services/surfaceflinger/tests/unittests/VSyncReactorTest.cpp
index 5826a9b..4eb9055 100644
--- a/services/surfaceflinger/tests/unittests/VSyncReactorTest.cpp
+++ b/services/surfaceflinger/tests/unittests/VSyncReactorTest.cpp
@@ -22,19 +22,22 @@
#define LOG_TAG "LibSurfaceFlingerUnittests"
#define LOG_NDEBUG 0
-#include "Scheduler/TimeKeeper.h"
-#include "Scheduler/VSyncDispatch.h"
-#include "Scheduler/VSyncReactor.h"
-#include "Scheduler/VSyncTracker.h"
+#include <array>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <ui/Fence.h>
#include <ui/FenceTime.h>
-#include <array>
+
+#include <scheduler/TimeKeeper.h>
+
+#include "Scheduler/VSyncDispatch.h"
+#include "Scheduler/VSyncReactor.h"
+#include "Scheduler/VSyncTracker.h"
using namespace testing;
using namespace std::literals;
+
namespace android::scheduler {
class MockVSyncTracker : public VSyncTracker {
@@ -65,14 +68,12 @@
std::shared_ptr<Clock> const mClock;
};
-class MockVSyncDispatch : public VSyncDispatch {
-public:
- MOCK_METHOD2(registerCallback,
- CallbackToken(std::function<void(nsecs_t, nsecs_t, nsecs_t)> const&, std::string));
- MOCK_METHOD1(unregisterCallback, void(CallbackToken));
- MOCK_METHOD2(schedule, ScheduleResult(CallbackToken, ScheduleTiming));
- MOCK_METHOD1(cancel, CancelResult(CallbackToken token));
- MOCK_CONST_METHOD1(dump, void(std::string&));
+struct MockVSyncDispatch : VSyncDispatch {
+ MOCK_METHOD(CallbackToken, registerCallback, (Callback, std::string), (override));
+ MOCK_METHOD(void, unregisterCallback, (CallbackToken), (override));
+ MOCK_METHOD(ScheduleResult, schedule, (CallbackToken, ScheduleTiming), (override));
+ MOCK_METHOD(CancelResult, cancel, (CallbackToken), (override));
+ MOCK_METHOD(void, dump, (std::string&), (const, override));
};
std::shared_ptr<android::FenceTime> generateInvalidFence() {
@@ -497,4 +498,4 @@
} // namespace android::scheduler
// TODO(b/129481165): remove the #pragma below and fix conversion issues
-#pragma clang diagnostic pop // ignored "-Wextra"
\ No newline at end of file
+#pragma clang diagnostic pop // ignored "-Wextra"