blob: 14a28603780cdeeac8be4c67a7ebc2e3b14035f0 [file] [log] [blame]
Kevin DuBois305bef12019-10-09 13:23:27 -07001/*
2 * Copyright 2019 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 Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010020#pragma clang diagnostic ignored "-Wextra"
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080021
Kevin DuBois305bef12019-10-09 13:23:27 -070022#undef LOG_TAG
23#define LOG_TAG "LibSurfaceFlingerUnittests"
24#define LOG_NDEBUG 0
25
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080026#include <thread>
Kevin DuBois305bef12019-10-09 13:23:27 -070027
28#include <gmock/gmock.h>
29#include <gtest/gtest.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080030
31#include <scheduler/TimeKeeper.h>
32
33#include "Scheduler/VSyncDispatchTimerQueue.h"
34#include "Scheduler/VSyncTracker.h"
Kevin DuBois305bef12019-10-09 13:23:27 -070035
36using namespace testing;
37using namespace std::literals;
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080038
Kevin DuBois305bef12019-10-09 13:23:27 -070039namespace android::scheduler {
40
41class MockVSyncTracker : public VSyncTracker {
42public:
43 MockVSyncTracker(nsecs_t period) : mPeriod{period} {
44 ON_CALL(*this, nextAnticipatedVSyncTimeFrom(_))
45 .WillByDefault(Invoke(this, &MockVSyncTracker::nextVSyncTime));
Kevin DuBois02d5ed92020-01-27 11:05:46 -080046 ON_CALL(*this, addVsyncTimestamp(_)).WillByDefault(Return(true));
Ady Abraham3fcfd8b2022-07-12 12:31:00 -070047 ON_CALL(*this, currentPeriod())
48 .WillByDefault(Invoke(this, &MockVSyncTracker::getCurrentPeriod));
Kevin DuBois305bef12019-10-09 13:23:27 -070049 }
50
Kevin DuBois02d5ed92020-01-27 11:05:46 -080051 MOCK_METHOD1(addVsyncTimestamp, bool(nsecs_t));
Kevin DuBois305bef12019-10-09 13:23:27 -070052 MOCK_CONST_METHOD1(nextAnticipatedVSyncTimeFrom, nsecs_t(nsecs_t));
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080053 MOCK_CONST_METHOD0(currentPeriod, nsecs_t());
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080054 MOCK_METHOD1(setPeriod, void(nsecs_t));
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080055 MOCK_METHOD0(resetModel, void());
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070056 MOCK_CONST_METHOD0(needsMoreSamples, bool());
Ady Abraham5cc2e262021-03-25 13:09:17 -070057 MOCK_CONST_METHOD2(isVSyncInPhase, bool(nsecs_t, Fps));
Ady Abrahamace3d052022-11-17 16:25:05 -080058 MOCK_METHOD(void, setDivisor, (unsigned), (override));
Ady Abraham5e7371c2020-03-24 14:47:24 -070059 MOCK_CONST_METHOD1(dump, void(std::string&));
Kevin DuBois305bef12019-10-09 13:23:27 -070060
61 nsecs_t nextVSyncTime(nsecs_t timePoint) const {
62 if (timePoint % mPeriod == 0) {
63 return timePoint;
64 }
65 return (timePoint - (timePoint % mPeriod) + mPeriod);
66 }
67
Ady Abraham3fcfd8b2022-07-12 12:31:00 -070068 nsecs_t getCurrentPeriod() const { return mPeriod; }
69
Kevin DuBois305bef12019-10-09 13:23:27 -070070protected:
71 nsecs_t const mPeriod;
72};
73
74class ControllableClock : public TimeKeeper {
75public:
76 ControllableClock() {
Ady Abrahamb491c902020-08-15 15:47:56 -070077 ON_CALL(*this, alarmAt(_, _))
78 .WillByDefault(Invoke(this, &ControllableClock::alarmAtDefaultBehavior));
Kevin DuBois305bef12019-10-09 13:23:27 -070079 ON_CALL(*this, now()).WillByDefault(Invoke(this, &ControllableClock::fakeTime));
80 }
81
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080082 MOCK_METHOD(nsecs_t, now, (), (const));
83 MOCK_METHOD(void, alarmAt, (std::function<void()>, nsecs_t), (override));
84 MOCK_METHOD(void, alarmCancel, (), (override));
85 MOCK_METHOD(void, dump, (std::string&), (const, override));
Kevin DuBois305bef12019-10-09 13:23:27 -070086
Ady Abrahamb491c902020-08-15 15:47:56 -070087 void alarmAtDefaultBehavior(std::function<void()> const& callback, nsecs_t time) {
Kevin DuBois305bef12019-10-09 13:23:27 -070088 mCallback = callback;
Ady Abrahamb491c902020-08-15 15:47:56 -070089 mNextCallbackTime = time;
Kevin DuBois305bef12019-10-09 13:23:27 -070090 }
91
92 nsecs_t fakeTime() const { return mCurrentTime; }
93
94 void advanceToNextCallback() {
95 mCurrentTime = mNextCallbackTime;
96 if (mCallback) {
97 mCallback();
98 }
99 }
100
101 void advanceBy(nsecs_t advancement) {
102 mCurrentTime += advancement;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700103 if (mCurrentTime >= (mNextCallbackTime + mLag) && mCallback) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700104 mCallback();
105 }
106 };
107
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700108 void setLag(nsecs_t lag) { mLag = lag; }
109
Kevin DuBois305bef12019-10-09 13:23:27 -0700110private:
111 std::function<void()> mCallback;
112 nsecs_t mNextCallbackTime = 0;
113 nsecs_t mCurrentTime = 0;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700114 nsecs_t mLag = 0;
Kevin DuBois305bef12019-10-09 13:23:27 -0700115};
116
117class CountingCallback {
118public:
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500119 CountingCallback(VSyncDispatch& dispatch)
120 : mDispatch(dispatch),
121 mToken(dispatch.registerCallback(std::bind(&CountingCallback::counter, this,
122 std::placeholders::_1, std::placeholders::_2,
123 std::placeholders::_3),
124 "test")) {}
125 ~CountingCallback() { mDispatch.unregisterCallback(mToken); }
Kevin DuBois305bef12019-10-09 13:23:27 -0700126
127 operator VSyncDispatch::CallbackToken() const { return mToken; }
128
Ady Abraham9c53ee72020-07-22 21:16:18 -0700129 void counter(nsecs_t time, nsecs_t wakeup_time, nsecs_t readyTime) {
Kevin DuBoisf9477832020-07-16 10:21:36 -0700130 mCalls.push_back(time);
131 mWakeupTime.push_back(wakeup_time);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700132 mReadyTime.push_back(readyTime);
Kevin DuBoisf9477832020-07-16 10:21:36 -0700133 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700134
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500135 VSyncDispatch& mDispatch;
Kevin DuBois305bef12019-10-09 13:23:27 -0700136 VSyncDispatch::CallbackToken mToken;
137 std::vector<nsecs_t> mCalls;
Kevin DuBoisf9477832020-07-16 10:21:36 -0700138 std::vector<nsecs_t> mWakeupTime;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700139 std::vector<nsecs_t> mReadyTime;
Kevin DuBois305bef12019-10-09 13:23:27 -0700140};
141
142class PausingCallback {
143public:
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500144 PausingCallback(VSyncDispatch& dispatch, std::chrono::milliseconds pauseAmount)
145 : mDispatch(dispatch),
146 mToken(dispatch.registerCallback(std::bind(&PausingCallback::pause, this,
147 std::placeholders::_1,
148 std::placeholders::_2),
149 "test")),
Kevin DuBois305bef12019-10-09 13:23:27 -0700150 mRegistered(true),
151 mPauseAmount(pauseAmount) {}
152 ~PausingCallback() { unregister(); }
153
154 operator VSyncDispatch::CallbackToken() const { return mToken; }
155
Kevin DuBois2968afc2020-01-14 09:48:50 -0800156 void pause(nsecs_t, nsecs_t) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700157 std::unique_lock lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700158 mPause = true;
159 mCv.notify_all();
160
Ady Abraham8cb21882020-08-26 18:22:05 -0700161 mCv.wait_for(lock, mPauseAmount, [this] { return !mPause; });
Kevin DuBois305bef12019-10-09 13:23:27 -0700162
163 mResourcePresent = (mResource.lock() != nullptr);
164 }
165
166 bool waitForPause() {
Ady Abraham8cb21882020-08-26 18:22:05 -0700167 std::unique_lock lock(mMutex);
168 auto waiting = mCv.wait_for(lock, 10s, [this] { return mPause; });
Kevin DuBois305bef12019-10-09 13:23:27 -0700169 return waiting;
170 }
171
172 void stashResource(std::weak_ptr<void> const& resource) { mResource = resource; }
173
174 bool resourcePresent() { return mResourcePresent; }
175
176 void unpause() {
Ady Abraham8cb21882020-08-26 18:22:05 -0700177 std::unique_lock lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700178 mPause = false;
179 mCv.notify_all();
180 }
181
182 void unregister() {
183 if (mRegistered) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500184 mDispatch.unregisterCallback(mToken);
Kevin DuBois305bef12019-10-09 13:23:27 -0700185 mRegistered = false;
186 }
187 }
188
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500189 VSyncDispatch& mDispatch;
Kevin DuBois305bef12019-10-09 13:23:27 -0700190 VSyncDispatch::CallbackToken mToken;
191 bool mRegistered = true;
192
193 std::mutex mMutex;
194 std::condition_variable mCv;
195 bool mPause = false;
196 std::weak_ptr<void> mResource;
197 bool mResourcePresent = false;
198 std::chrono::milliseconds const mPauseAmount;
199};
200
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800201class VSyncDispatchTimerQueueTest : public testing::Test {
Kevin DuBois305bef12019-10-09 13:23:27 -0700202protected:
203 std::unique_ptr<TimeKeeper> createTimeKeeper() {
204 class TimeKeeperWrapper : public TimeKeeper {
205 public:
206 TimeKeeperWrapper(TimeKeeper& control) : mControllableClock(control) {}
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800207
Kevin DuBois305bef12019-10-09 13:23:27 -0700208 nsecs_t now() const final { return mControllableClock.now(); }
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800209
210 void alarmAt(std::function<void()> callback, nsecs_t time) final {
211 mControllableClock.alarmAt(std::move(callback), time);
212 }
213
214 void alarmCancel() final { mControllableClock.alarmCancel(); }
Ady Abraham75398722020-04-07 14:08:45 -0700215 void dump(std::string&) const final {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700216
217 private:
218 TimeKeeper& mControllableClock;
219 };
220 return std::make_unique<TimeKeeperWrapper>(mMockClock);
221 }
222
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800223 ~VSyncDispatchTimerQueueTest() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700224 // destructor of dispatch will cancelAlarm(). Ignore final cancel in common test.
225 Mock::VerifyAndClearExpectations(&mMockClock);
226 }
227
228 void advanceToNextCallback() { mMockClock.advanceToNextCallback(); }
229
230 NiceMock<ControllableClock> mMockClock;
231 static nsecs_t constexpr mDispatchGroupThreshold = 5;
232 nsecs_t const mPeriod = 1000;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800233 nsecs_t const mVsyncMoveThreshold = 300;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500234 NiceMock<MockVSyncTracker> mStubTracker{mPeriod};
235 VSyncDispatchTimerQueue mDispatch{createTimeKeeper(), mStubTracker, mDispatchGroupThreshold,
236 mVsyncMoveThreshold};
Kevin DuBois305bef12019-10-09 13:23:27 -0700237};
238
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800239TEST_F(VSyncDispatchTimerQueueTest, unregistersSetAlarmOnDestruction) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700240 EXPECT_CALL(mMockClock, alarmAt(_, 900));
Kevin DuBois305bef12019-10-09 13:23:27 -0700241 EXPECT_CALL(mMockClock, alarmCancel());
242 {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500243 VSyncDispatchTimerQueue mDispatch{createTimeKeeper(), mStubTracker, mDispatchGroupThreshold,
244 mVsyncMoveThreshold};
Kevin DuBois305bef12019-10-09 13:23:27 -0700245 CountingCallback cb(mDispatch);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500246 const auto result = mDispatch.schedule(cb,
247 {.workDuration = 100,
248 .readyDuration = 0,
249 .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700250 EXPECT_TRUE(result.has_value());
251 EXPECT_EQ(900, *result);
Kevin DuBois305bef12019-10-09 13:23:27 -0700252 }
253}
254
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800255TEST_F(VSyncDispatchTimerQueueTest, basicAlarmSettingFuture) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700256 auto intended = mPeriod - 230;
Ady Abrahamb491c902020-08-15 15:47:56 -0700257 EXPECT_CALL(mMockClock, alarmAt(_, 900));
Kevin DuBois305bef12019-10-09 13:23:27 -0700258
259 CountingCallback cb(mDispatch);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500260 const auto result = mDispatch.schedule(cb,
261 {.workDuration = 100,
262 .readyDuration = 0,
263 .earliestVsync = intended});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700264 EXPECT_TRUE(result.has_value());
265 EXPECT_EQ(900, *result);
266
Kevin DuBois305bef12019-10-09 13:23:27 -0700267 advanceToNextCallback();
268
269 ASSERT_THAT(cb.mCalls.size(), Eq(1));
270 EXPECT_THAT(cb.mCalls[0], Eq(mPeriod));
271}
272
Ady Abraham011f8ba2022-11-22 15:09:07 -0800273TEST_F(VSyncDispatchTimerQueueTest, updateAlarmSettingFuture) {
274 auto intended = mPeriod - 230;
275 Sequence seq;
276 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
277 EXPECT_CALL(mMockClock, alarmAt(_, 700)).InSequence(seq);
278
279 CountingCallback cb(mDispatch);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500280 auto result = mDispatch.schedule(cb,
281 {.workDuration = 100,
282 .readyDuration = 0,
283 .earliestVsync = intended});
Ady Abraham011f8ba2022-11-22 15:09:07 -0800284 EXPECT_TRUE(result.has_value());
285 EXPECT_EQ(900, *result);
286
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500287 result = mDispatch.update(cb,
Ady Abraham011f8ba2022-11-22 15:09:07 -0800288 {.workDuration = 300, .readyDuration = 0, .earliestVsync = intended});
289 EXPECT_TRUE(result.has_value());
290 EXPECT_EQ(700, *result);
291
292 advanceToNextCallback();
293
294 ASSERT_THAT(cb.mCalls.size(), Eq(1));
295 EXPECT_THAT(cb.mCalls[0], Eq(mPeriod));
296 EXPECT_THAT(cb.mWakeupTime[0], Eq(700));
297}
298
299TEST_F(VSyncDispatchTimerQueueTest, updateDoesntSchedule) {
300 auto intended = mPeriod - 230;
301 EXPECT_CALL(mMockClock, alarmAt(_, _)).Times(0);
302
303 CountingCallback cb(mDispatch);
304 const auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500305 mDispatch.update(cb,
306 {.workDuration = 300, .readyDuration = 0, .earliestVsync = intended});
Ady Abraham011f8ba2022-11-22 15:09:07 -0800307 EXPECT_FALSE(result.has_value());
308}
309
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800310TEST_F(VSyncDispatchTimerQueueTest, basicAlarmSettingFutureWithAdjustmentToTrueVsync) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500311 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(1000)).WillOnce(Return(1150));
Ady Abrahamb491c902020-08-15 15:47:56 -0700312 EXPECT_CALL(mMockClock, alarmAt(_, 1050));
Kevin DuBois305bef12019-10-09 13:23:27 -0700313
314 CountingCallback cb(mDispatch);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500315 mDispatch.schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = mPeriod});
Kevin DuBois305bef12019-10-09 13:23:27 -0700316 advanceToNextCallback();
317
318 ASSERT_THAT(cb.mCalls.size(), Eq(1));
319 EXPECT_THAT(cb.mCalls[0], Eq(1150));
320}
321
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800322TEST_F(VSyncDispatchTimerQueueTest, basicAlarmSettingAdjustmentPast) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700323 auto const now = 234;
324 mMockClock.advanceBy(234);
325 auto const workDuration = 10 * mPeriod;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500326 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(now + workDuration))
Kevin DuBois305bef12019-10-09 13:23:27 -0700327 .WillOnce(Return(mPeriod * 11));
Ady Abrahamb491c902020-08-15 15:47:56 -0700328 EXPECT_CALL(mMockClock, alarmAt(_, mPeriod));
Kevin DuBois305bef12019-10-09 13:23:27 -0700329
330 CountingCallback cb(mDispatch);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500331 const auto result = mDispatch.schedule(cb,
332 {.workDuration = workDuration,
333 .readyDuration = 0,
334 .earliestVsync = mPeriod});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700335 EXPECT_TRUE(result.has_value());
336 EXPECT_EQ(mPeriod, *result);
Kevin DuBois305bef12019-10-09 13:23:27 -0700337}
338
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800339TEST_F(VSyncDispatchTimerQueueTest, basicAlarmCancel) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700340 EXPECT_CALL(mMockClock, alarmAt(_, 900));
Kevin DuBois305bef12019-10-09 13:23:27 -0700341 EXPECT_CALL(mMockClock, alarmCancel());
342
343 CountingCallback cb(mDispatch);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500344 const auto result =
345 mDispatch.schedule(cb,
346 {.workDuration = 100, .readyDuration = 0, .earliestVsync = mPeriod});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700347 EXPECT_TRUE(result.has_value());
348 EXPECT_EQ(mPeriod - 100, *result);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500349 EXPECT_EQ(mDispatch.cancel(cb), CancelResult::Cancelled);
Kevin DuBois305bef12019-10-09 13:23:27 -0700350}
351
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800352TEST_F(VSyncDispatchTimerQueueTest, basicAlarmCancelTooLate) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700353 EXPECT_CALL(mMockClock, alarmAt(_, 900));
Kevin DuBois305bef12019-10-09 13:23:27 -0700354 EXPECT_CALL(mMockClock, alarmCancel());
355
356 CountingCallback cb(mDispatch);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500357 const auto result =
358 mDispatch.schedule(cb,
359 {.workDuration = 100, .readyDuration = 0, .earliestVsync = mPeriod});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700360 EXPECT_TRUE(result.has_value());
361 EXPECT_EQ(mPeriod - 100, *result);
Kevin DuBois305bef12019-10-09 13:23:27 -0700362 mMockClock.advanceBy(950);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500363 EXPECT_EQ(mDispatch.cancel(cb), CancelResult::TooLate);
Kevin DuBois305bef12019-10-09 13:23:27 -0700364}
365
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800366TEST_F(VSyncDispatchTimerQueueTest, basicAlarmCancelTooLateWhenRunning) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700367 EXPECT_CALL(mMockClock, alarmAt(_, 900));
Kevin DuBois305bef12019-10-09 13:23:27 -0700368 EXPECT_CALL(mMockClock, alarmCancel());
369
370 PausingCallback cb(mDispatch, std::chrono::duration_cast<std::chrono::milliseconds>(1s));
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500371 const auto result =
372 mDispatch.schedule(cb,
373 {.workDuration = 100, .readyDuration = 0, .earliestVsync = mPeriod});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700374 EXPECT_TRUE(result.has_value());
375 EXPECT_EQ(mPeriod - 100, *result);
Kevin DuBois305bef12019-10-09 13:23:27 -0700376
377 std::thread pausingThread([&] { mMockClock.advanceToNextCallback(); });
378 EXPECT_TRUE(cb.waitForPause());
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500379 EXPECT_EQ(mDispatch.cancel(cb), CancelResult::TooLate);
Kevin DuBois305bef12019-10-09 13:23:27 -0700380 cb.unpause();
381 pausingThread.join();
382}
383
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800384TEST_F(VSyncDispatchTimerQueueTest, unregisterSynchronizes) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700385 EXPECT_CALL(mMockClock, alarmAt(_, 900));
Kevin DuBois305bef12019-10-09 13:23:27 -0700386 EXPECT_CALL(mMockClock, alarmCancel());
387
388 auto resource = std::make_shared<int>(110);
389
390 PausingCallback cb(mDispatch, 50ms);
391 cb.stashResource(resource);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500392 const auto result =
393 mDispatch.schedule(cb,
394 {.workDuration = 100, .readyDuration = 0, .earliestVsync = mPeriod});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700395 EXPECT_TRUE(result.has_value());
396 EXPECT_EQ(mPeriod - 100, *result);
Kevin DuBois305bef12019-10-09 13:23:27 -0700397
398 std::thread pausingThread([&] { mMockClock.advanceToNextCallback(); });
399 EXPECT_TRUE(cb.waitForPause());
400
401 cb.unregister();
402 resource.reset();
403
404 cb.unpause();
405 pausingThread.join();
406
407 EXPECT_TRUE(cb.resourcePresent());
408}
409
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800410TEST_F(VSyncDispatchTimerQueueTest, basicTwoAlarmSetting) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500411 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(1000))
Kevin DuBois305bef12019-10-09 13:23:27 -0700412 .Times(4)
413 .WillOnce(Return(1055))
414 .WillOnce(Return(1063))
415 .WillOnce(Return(1063))
416 .WillOnce(Return(1075));
417
418 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700419 EXPECT_CALL(mMockClock, alarmAt(_, 955)).InSequence(seq);
420 EXPECT_CALL(mMockClock, alarmAt(_, 813)).InSequence(seq);
421 EXPECT_CALL(mMockClock, alarmAt(_, 975)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700422
423 CountingCallback cb0(mDispatch);
424 CountingCallback cb1(mDispatch);
425
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500426 mDispatch.schedule(cb0, {.workDuration = 100, .readyDuration = 0, .earliestVsync = mPeriod});
427 mDispatch.schedule(cb1, {.workDuration = 250, .readyDuration = 0, .earliestVsync = mPeriod});
Kevin DuBois305bef12019-10-09 13:23:27 -0700428
429 advanceToNextCallback();
430 advanceToNextCallback();
431
432 ASSERT_THAT(cb0.mCalls.size(), Eq(1));
433 EXPECT_THAT(cb0.mCalls[0], Eq(1075));
434 ASSERT_THAT(cb1.mCalls.size(), Eq(1));
435 EXPECT_THAT(cb1.mCalls[0], Eq(1063));
436}
437
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700438TEST_F(VSyncDispatchTimerQueueTest, noCloseCallbacksAfterPeriodChange) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500439 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(_))
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700440 .Times(4)
441 .WillOnce(Return(1000))
442 .WillOnce(Return(2000))
443 .WillOnce(Return(2500))
444 .WillOnce(Return(4000));
445
446 Sequence seq;
447 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
448 EXPECT_CALL(mMockClock, alarmAt(_, 1900)).InSequence(seq);
449 EXPECT_CALL(mMockClock, alarmAt(_, 3900)).InSequence(seq);
450
451 CountingCallback cb(mDispatch);
452
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500453 mDispatch.schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 0});
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700454
455 advanceToNextCallback();
456
457 ASSERT_THAT(cb.mCalls.size(), Eq(1));
458 EXPECT_THAT(cb.mCalls[0], Eq(1000));
459
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500460 mDispatch.schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700461
462 advanceToNextCallback();
463
464 ASSERT_THAT(cb.mCalls.size(), Eq(2));
465 EXPECT_THAT(cb.mCalls[1], Eq(2000));
466
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500467 mDispatch.schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 2000});
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700468
469 advanceToNextCallback();
470
471 ASSERT_THAT(cb.mCalls.size(), Eq(3));
472 EXPECT_THAT(cb.mCalls[2], Eq(4000));
473}
474
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800475TEST_F(VSyncDispatchTimerQueueTest, rearmsFaroutTimeoutWhenCancellingCloseOne) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500476 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(_))
Kevin DuBois305bef12019-10-09 13:23:27 -0700477 .Times(4)
478 .WillOnce(Return(10000))
479 .WillOnce(Return(1000))
480 .WillOnce(Return(10000))
481 .WillOnce(Return(10000));
482
483 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700484 EXPECT_CALL(mMockClock, alarmAt(_, 9900)).InSequence(seq);
485 EXPECT_CALL(mMockClock, alarmAt(_, 750)).InSequence(seq);
486 EXPECT_CALL(mMockClock, alarmAt(_, 9900)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700487
488 CountingCallback cb0(mDispatch);
489 CountingCallback cb1(mDispatch);
490
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500491 mDispatch.schedule(cb0,
492 {.workDuration = 100, .readyDuration = 0, .earliestVsync = mPeriod * 10});
493 mDispatch.schedule(cb1, {.workDuration = 250, .readyDuration = 0, .earliestVsync = mPeriod});
494 mDispatch.cancel(cb1);
Kevin DuBois305bef12019-10-09 13:23:27 -0700495}
496
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800497TEST_F(VSyncDispatchTimerQueueTest, noUnnecessaryRearmsWhenRescheduling) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700498 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700499 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
500 EXPECT_CALL(mMockClock, alarmAt(_, 700)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700501
502 CountingCallback cb0(mDispatch);
503 CountingCallback cb1(mDispatch);
504
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500505 mDispatch.schedule(cb0, {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
506 mDispatch.schedule(cb1, {.workDuration = 200, .readyDuration = 0, .earliestVsync = 1000});
507 mDispatch.schedule(cb1, {.workDuration = 300, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700508 advanceToNextCallback();
509}
510
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800511TEST_F(VSyncDispatchTimerQueueTest, necessaryRearmsWhenModifying) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700512 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700513 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
514 EXPECT_CALL(mMockClock, alarmAt(_, 500)).InSequence(seq);
515 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700516
517 CountingCallback cb0(mDispatch);
518 CountingCallback cb1(mDispatch);
519
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500520 mDispatch.schedule(cb0, {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
521 mDispatch.schedule(cb1, {.workDuration = 200, .readyDuration = 0, .earliestVsync = 1000});
522 mDispatch.schedule(cb1, {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700523 advanceToNextCallback();
524}
525
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800526TEST_F(VSyncDispatchTimerQueueTest, modifyIntoGroup) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700527 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700528 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
529 EXPECT_CALL(mMockClock, alarmAt(_, 1600)).InSequence(seq);
530 EXPECT_CALL(mMockClock, alarmAt(_, 1590)).InSequence(seq);
531 EXPECT_CALL(mMockClock, alarmAt(_, 1600)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700532
533 auto offset = 400;
534 auto closeOffset = offset + mDispatchGroupThreshold - 1;
535 auto notCloseOffset = offset + 2 * mDispatchGroupThreshold;
536
537 CountingCallback cb0(mDispatch);
538 CountingCallback cb1(mDispatch);
539
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500540 mDispatch.schedule(cb0, {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
541 mDispatch.schedule(cb1, {.workDuration = 200, .readyDuration = 0, .earliestVsync = 1000});
542 mDispatch.schedule(cb1,
543 {.workDuration = closeOffset, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700544
545 advanceToNextCallback();
546 ASSERT_THAT(cb0.mCalls.size(), Eq(1));
547 EXPECT_THAT(cb0.mCalls[0], Eq(mPeriod));
548 ASSERT_THAT(cb1.mCalls.size(), Eq(1));
549 EXPECT_THAT(cb1.mCalls[0], Eq(mPeriod));
550
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500551 mDispatch.schedule(cb0, {.workDuration = 400, .readyDuration = 0, .earliestVsync = 2000});
552 mDispatch.schedule(cb1,
553 {.workDuration = notCloseOffset, .readyDuration = 0, .earliestVsync = 2000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700554 advanceToNextCallback();
555 ASSERT_THAT(cb1.mCalls.size(), Eq(2));
556 EXPECT_THAT(cb1.mCalls[1], Eq(2000));
557
558 advanceToNextCallback();
559 ASSERT_THAT(cb0.mCalls.size(), Eq(2));
560 EXPECT_THAT(cb0.mCalls[1], Eq(2000));
561}
562
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800563TEST_F(VSyncDispatchTimerQueueTest, rearmsWhenEndingAndDoesntCancel) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700564 Sequence seq;
565 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
566 EXPECT_CALL(mMockClock, alarmAt(_, 800)).InSequence(seq);
567 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700568 EXPECT_CALL(mMockClock, alarmCancel());
569
570 CountingCallback cb0(mDispatch);
571 CountingCallback cb1(mDispatch);
572
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500573 mDispatch.schedule(cb0, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
574 mDispatch.schedule(cb1, {.workDuration = 200, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700575 advanceToNextCallback();
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500576 EXPECT_EQ(mDispatch.cancel(cb0), CancelResult::Cancelled);
Kevin DuBois305bef12019-10-09 13:23:27 -0700577}
578
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800579TEST_F(VSyncDispatchTimerQueueTest, setAlarmCallsAtCorrectTimeWithChangingVsync) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500580 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(_))
Kevin DuBois305bef12019-10-09 13:23:27 -0700581 .Times(3)
582 .WillOnce(Return(950))
583 .WillOnce(Return(1975))
584 .WillOnce(Return(2950));
585
586 CountingCallback cb(mDispatch);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500587 mDispatch.schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 920});
Kevin DuBois305bef12019-10-09 13:23:27 -0700588
589 mMockClock.advanceBy(850);
590 EXPECT_THAT(cb.mCalls.size(), Eq(1));
591
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500592 mDispatch.schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1900});
Kevin DuBois305bef12019-10-09 13:23:27 -0700593 mMockClock.advanceBy(900);
594 EXPECT_THAT(cb.mCalls.size(), Eq(1));
595 mMockClock.advanceBy(125);
596 EXPECT_THAT(cb.mCalls.size(), Eq(2));
597
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500598 mDispatch.schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 2900});
Kevin DuBois305bef12019-10-09 13:23:27 -0700599 mMockClock.advanceBy(975);
600 EXPECT_THAT(cb.mCalls.size(), Eq(3));
601}
602
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800603TEST_F(VSyncDispatchTimerQueueTest, callbackReentrancy) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700604 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700605 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
606 EXPECT_CALL(mMockClock, alarmAt(_, 1900)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700607
608 VSyncDispatch::CallbackToken tmp;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500609 tmp = mDispatch.registerCallback(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700610 [&](auto, auto, auto) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500611 mDispatch.schedule(tmp,
612 {.workDuration = 100,
613 .readyDuration = 0,
614 .earliestVsync = 2000});
Ady Abraham9c53ee72020-07-22 21:16:18 -0700615 },
616 "o.o");
Kevin DuBois305bef12019-10-09 13:23:27 -0700617
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500618 mDispatch.schedule(tmp, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700619 advanceToNextCallback();
620}
621
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800622TEST_F(VSyncDispatchTimerQueueTest, callbackReentrantWithPastWakeup) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700623 VSyncDispatch::CallbackToken tmp;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800624 std::optional<nsecs_t> lastTarget;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500625 tmp = mDispatch.registerCallback(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700626 [&](auto timestamp, auto, auto) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700627 auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500628 mDispatch.schedule(tmp,
629 {.workDuration = 400,
630 .readyDuration = 0,
631 .earliestVsync = timestamp - mVsyncMoveThreshold});
632 EXPECT_TRUE(result.has_value());
633 EXPECT_EQ(mPeriod + timestamp - 400, *result);
634 result = mDispatch.schedule(tmp,
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700635 {.workDuration = 400,
636 .readyDuration = 0,
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500637 .earliestVsync = timestamp});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700638 EXPECT_TRUE(result.has_value());
639 EXPECT_EQ(mPeriod + timestamp - 400, *result);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500640 result = mDispatch.schedule(tmp,
641 {.workDuration = 400,
642 .readyDuration = 0,
643 .earliestVsync = timestamp + mVsyncMoveThreshold});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700644 EXPECT_TRUE(result.has_value());
645 EXPECT_EQ(mPeriod + timestamp - 400, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800646 lastTarget = timestamp;
Kevin DuBois305bef12019-10-09 13:23:27 -0700647 },
648 "oo");
649
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500650 mDispatch.schedule(tmp, {.workDuration = 999, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700651 advanceToNextCallback();
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800652 EXPECT_THAT(lastTarget, Eq(1000));
653
654 advanceToNextCallback();
655 EXPECT_THAT(lastTarget, Eq(2000));
Kevin DuBois305bef12019-10-09 13:23:27 -0700656}
657
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800658TEST_F(VSyncDispatchTimerQueueTest, modificationsAroundVsyncTime) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700659 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700660 EXPECT_CALL(mMockClock, alarmAt(_, 1000)).InSequence(seq);
661 EXPECT_CALL(mMockClock, alarmAt(_, 950)).InSequence(seq);
662 EXPECT_CALL(mMockClock, alarmAt(_, 1950)).InSequence(seq);
663 EXPECT_CALL(mMockClock, alarmAt(_, 1900)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700664
665 CountingCallback cb(mDispatch);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500666 mDispatch.schedule(cb, {.workDuration = 0, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700667
668 mMockClock.advanceBy(750);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500669 mDispatch.schedule(cb, {.workDuration = 50, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700670
671 advanceToNextCallback();
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500672 mDispatch.schedule(cb, {.workDuration = 50, .readyDuration = 0, .earliestVsync = 2000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700673
674 mMockClock.advanceBy(800);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500675 mDispatch.schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 2000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700676}
677
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800678TEST_F(VSyncDispatchTimerQueueTest, lateModifications) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700679 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700680 EXPECT_CALL(mMockClock, alarmAt(_, 500)).InSequence(seq);
681 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
682 EXPECT_CALL(mMockClock, alarmAt(_, 850)).InSequence(seq);
683 EXPECT_CALL(mMockClock, alarmAt(_, 1800)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700684
685 CountingCallback cb0(mDispatch);
686 CountingCallback cb1(mDispatch);
687
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500688 mDispatch.schedule(cb0, {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
689 mDispatch.schedule(cb1, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700690
691 advanceToNextCallback();
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500692 mDispatch.schedule(cb0, {.workDuration = 200, .readyDuration = 0, .earliestVsync = 2000});
693 mDispatch.schedule(cb1, {.workDuration = 150, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700694
695 advanceToNextCallback();
696 advanceToNextCallback();
697}
698
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800699TEST_F(VSyncDispatchTimerQueueTest, doesntCancelPriorValidTimerForFutureMod) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700700 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700701 EXPECT_CALL(mMockClock, alarmAt(_, 500)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700702
703 CountingCallback cb0(mDispatch);
704 CountingCallback cb1(mDispatch);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500705 mDispatch.schedule(cb0, {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
706 mDispatch.schedule(cb1, {.workDuration = 500, .readyDuration = 0, .earliestVsync = 20000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700707}
708
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800709TEST_F(VSyncDispatchTimerQueueTest, setsTimerAfterCancellation) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700710 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700711 EXPECT_CALL(mMockClock, alarmAt(_, 500)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700712 EXPECT_CALL(mMockClock, alarmCancel()).InSequence(seq);
Ady Abrahamb491c902020-08-15 15:47:56 -0700713 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700714
715 CountingCallback cb0(mDispatch);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500716 mDispatch.schedule(cb0, {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
717 mDispatch.cancel(cb0);
718 mDispatch.schedule(cb0, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700719}
720
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800721TEST_F(VSyncDispatchTimerQueueTest, makingUpIdsError) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700722 VSyncDispatch::CallbackToken token(100);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500723 EXPECT_FALSE(mDispatch
724 .schedule(token,
725 {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000})
726 .has_value());
727 EXPECT_THAT(mDispatch.cancel(token), Eq(CancelResult::Error));
Kevin DuBois305bef12019-10-09 13:23:27 -0700728}
729
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800730TEST_F(VSyncDispatchTimerQueueTest, canMoveCallbackBackwardsInTime) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700731 CountingCallback cb0(mDispatch);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700732 auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500733 mDispatch.schedule(cb0,
734 {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700735 EXPECT_TRUE(result.has_value());
736 EXPECT_EQ(500, *result);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500737 result = mDispatch.schedule(cb0,
738 {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700739 EXPECT_TRUE(result.has_value());
740 EXPECT_EQ(900, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800741}
742
743// b/1450138150
744TEST_F(VSyncDispatchTimerQueueTest, doesNotMoveCallbackBackwardsAndSkipAScheduledTargetVSync) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700745 EXPECT_CALL(mMockClock, alarmAt(_, 500));
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800746 CountingCallback cb(mDispatch);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700747 auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500748 mDispatch.schedule(cb,
749 {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700750 EXPECT_TRUE(result.has_value());
751 EXPECT_EQ(500, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800752 mMockClock.advanceBy(400);
753
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500754 result = mDispatch.schedule(cb,
755 {.workDuration = 800, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700756 EXPECT_TRUE(result.has_value());
757 EXPECT_EQ(1200, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800758 advanceToNextCallback();
759 ASSERT_THAT(cb.mCalls.size(), Eq(1));
760}
761
762TEST_F(VSyncDispatchTimerQueueTest, targetOffsetMovingBackALittleCanStillSchedule) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500763 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(1000))
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800764 .Times(2)
765 .WillOnce(Return(1000))
766 .WillOnce(Return(1002));
767 CountingCallback cb(mDispatch);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700768 auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500769 mDispatch.schedule(cb,
770 {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700771 EXPECT_TRUE(result.has_value());
772 EXPECT_EQ(500, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800773 mMockClock.advanceBy(400);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500774 result = mDispatch.schedule(cb,
775 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700776 EXPECT_TRUE(result.has_value());
777 EXPECT_EQ(602, *result);
Kevin DuBois305bef12019-10-09 13:23:27 -0700778}
779
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800780TEST_F(VSyncDispatchTimerQueueTest, canScheduleNegativeOffsetAgainstDifferentPeriods) {
781 CountingCallback cb0(mDispatch);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700782 auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500783 mDispatch.schedule(cb0,
784 {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700785 EXPECT_TRUE(result.has_value());
786 EXPECT_EQ(500, *result);
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800787 advanceToNextCallback();
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500788 result = mDispatch.schedule(cb0,
789 {.workDuration = 1100, .readyDuration = 0, .earliestVsync = 2000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700790 EXPECT_TRUE(result.has_value());
791 EXPECT_EQ(900, *result);
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800792}
793
794TEST_F(VSyncDispatchTimerQueueTest, canScheduleLargeNegativeOffset) {
795 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700796 EXPECT_CALL(mMockClock, alarmAt(_, 500)).InSequence(seq);
797 EXPECT_CALL(mMockClock, alarmAt(_, 1100)).InSequence(seq);
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800798 CountingCallback cb0(mDispatch);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700799 auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500800 mDispatch.schedule(cb0,
801 {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700802 EXPECT_TRUE(result.has_value());
803 EXPECT_EQ(500, *result);
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800804 advanceToNextCallback();
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500805 result = mDispatch.schedule(cb0,
806 {.workDuration = 1900, .readyDuration = 0, .earliestVsync = 2000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700807 EXPECT_TRUE(result.has_value());
808 EXPECT_EQ(1100, *result);
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800809}
810
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800811TEST_F(VSyncDispatchTimerQueueTest, scheduleUpdatesDoesNotAffectSchedulingState) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700812 EXPECT_CALL(mMockClock, alarmAt(_, 600));
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800813
814 CountingCallback cb(mDispatch);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700815 auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500816 mDispatch.schedule(cb,
817 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700818 EXPECT_TRUE(result.has_value());
819 EXPECT_EQ(600, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800820
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500821 result = mDispatch.schedule(cb,
822 {.workDuration = 1400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700823 EXPECT_TRUE(result.has_value());
824 EXPECT_EQ(600, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800825
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800826 advanceToNextCallback();
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800827}
828
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800829TEST_F(VSyncDispatchTimerQueueTest, helperMove) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700830 EXPECT_CALL(mMockClock, alarmAt(_, 500)).Times(1);
Kevin DuBois305bef12019-10-09 13:23:27 -0700831 EXPECT_CALL(mMockClock, alarmCancel()).Times(1);
832
833 VSyncCallbackRegistration cb(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700834 mDispatch, [](auto, auto, auto) {}, "");
Kevin DuBois305bef12019-10-09 13:23:27 -0700835 VSyncCallbackRegistration cb1(std::move(cb));
Ady Abraham9c53ee72020-07-22 21:16:18 -0700836 cb.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700837 cb.cancel();
838
Ady Abraham9c53ee72020-07-22 21:16:18 -0700839 cb1.schedule({.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700840 cb1.cancel();
841}
842
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800843TEST_F(VSyncDispatchTimerQueueTest, helperMoveAssign) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700844 EXPECT_CALL(mMockClock, alarmAt(_, 500)).Times(1);
Kevin DuBois305bef12019-10-09 13:23:27 -0700845 EXPECT_CALL(mMockClock, alarmCancel()).Times(1);
846
847 VSyncCallbackRegistration cb(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700848 mDispatch, [](auto, auto, auto) {}, "");
Kevin DuBois305bef12019-10-09 13:23:27 -0700849 VSyncCallbackRegistration cb1(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700850 mDispatch, [](auto, auto, auto) {}, "");
Kevin DuBois305bef12019-10-09 13:23:27 -0700851 cb1 = std::move(cb);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700852 cb.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700853 cb.cancel();
854
Ady Abraham9c53ee72020-07-22 21:16:18 -0700855 cb1.schedule({.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700856 cb1.cancel();
857}
858
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700859// b/154303580
860TEST_F(VSyncDispatchTimerQueueTest, skipsSchedulingIfTimerReschedulingIsImminent) {
861 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700862 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
863 EXPECT_CALL(mMockClock, alarmAt(_, 1900)).InSequence(seq);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700864 CountingCallback cb1(mDispatch);
865 CountingCallback cb2(mDispatch);
866
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700867 auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500868 mDispatch.schedule(cb1,
869 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700870 EXPECT_TRUE(result.has_value());
871 EXPECT_EQ(600, *result);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700872
873 mMockClock.setLag(100);
874 mMockClock.advanceBy(620);
875
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500876 result = mDispatch.schedule(cb2,
877 {.workDuration = 100, .readyDuration = 0, .earliestVsync = 2000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700878 EXPECT_TRUE(result.has_value());
879 EXPECT_EQ(1900, *result);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700880 mMockClock.advanceBy(80);
881
882 EXPECT_THAT(cb1.mCalls.size(), Eq(1));
883 EXPECT_THAT(cb2.mCalls.size(), Eq(0));
884}
885
886// b/154303580.
887// If the same callback tries to reschedule itself after it's too late, timer opts to apply the
888// update later, as opposed to blocking the calling thread.
889TEST_F(VSyncDispatchTimerQueueTest, skipsSchedulingIfTimerReschedulingIsImminentSameCallback) {
890 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700891 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
892 EXPECT_CALL(mMockClock, alarmAt(_, 1630)).InSequence(seq);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700893 CountingCallback cb(mDispatch);
894
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700895 auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500896 mDispatch.schedule(cb,
897 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700898 EXPECT_TRUE(result.has_value());
899 EXPECT_EQ(600, *result);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700900
901 mMockClock.setLag(100);
902 mMockClock.advanceBy(620);
903
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500904 result = mDispatch.schedule(cb,
905 {.workDuration = 370, .readyDuration = 0, .earliestVsync = 2000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700906 EXPECT_TRUE(result.has_value());
907 EXPECT_EQ(1630, *result);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700908 mMockClock.advanceBy(80);
909
910 EXPECT_THAT(cb.mCalls.size(), Eq(1));
911}
912
Kevin DuBoisb340b732020-06-16 09:07:35 -0700913// b/154303580.
914TEST_F(VSyncDispatchTimerQueueTest, skipsRearmingWhenNotNextScheduled) {
915 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700916 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
Kevin DuBoisb340b732020-06-16 09:07:35 -0700917 EXPECT_CALL(mMockClock, alarmCancel()).InSequence(seq);
918 CountingCallback cb1(mDispatch);
919 CountingCallback cb2(mDispatch);
920
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700921 auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500922 mDispatch.schedule(cb1,
923 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700924 EXPECT_TRUE(result.has_value());
925 EXPECT_EQ(600, *result);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500926 result = mDispatch.schedule(cb2,
927 {.workDuration = 100, .readyDuration = 0, .earliestVsync = 2000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700928 EXPECT_TRUE(result.has_value());
929 EXPECT_EQ(1900, *result);
Kevin DuBoisb340b732020-06-16 09:07:35 -0700930
931 mMockClock.setLag(100);
932 mMockClock.advanceBy(620);
933
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500934 EXPECT_EQ(mDispatch.cancel(cb2), CancelResult::Cancelled);
Kevin DuBoisb340b732020-06-16 09:07:35 -0700935
936 mMockClock.advanceBy(80);
937
938 EXPECT_THAT(cb1.mCalls.size(), Eq(1));
939 EXPECT_THAT(cb2.mCalls.size(), Eq(0));
940}
941
942TEST_F(VSyncDispatchTimerQueueTest, rearmsWhenCancelledAndIsNextScheduled) {
943 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700944 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
945 EXPECT_CALL(mMockClock, alarmAt(_, 1900)).InSequence(seq);
Kevin DuBoisb340b732020-06-16 09:07:35 -0700946 EXPECT_CALL(mMockClock, alarmCancel()).InSequence(seq);
947 CountingCallback cb1(mDispatch);
948 CountingCallback cb2(mDispatch);
949
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700950 auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500951 mDispatch.schedule(cb1,
952 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700953 EXPECT_TRUE(result.has_value());
954 EXPECT_EQ(600, *result);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500955 result = mDispatch.schedule(cb2,
956 {.workDuration = 100, .readyDuration = 0, .earliestVsync = 2000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700957 EXPECT_TRUE(result.has_value());
958 EXPECT_EQ(1900, *result);
Kevin DuBoisb340b732020-06-16 09:07:35 -0700959
960 mMockClock.setLag(100);
961 mMockClock.advanceBy(620);
962
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500963 EXPECT_EQ(mDispatch.cancel(cb1), CancelResult::Cancelled);
Kevin DuBoisb340b732020-06-16 09:07:35 -0700964
965 EXPECT_THAT(cb1.mCalls.size(), Eq(0));
966 EXPECT_THAT(cb2.mCalls.size(), Eq(0));
967 mMockClock.advanceToNextCallback();
968
969 EXPECT_THAT(cb1.mCalls.size(), Eq(0));
970 EXPECT_THAT(cb2.mCalls.size(), Eq(1));
971}
972
Kevin DuBoisf9477832020-07-16 10:21:36 -0700973TEST_F(VSyncDispatchTimerQueueTest, laggedTimerGroupsCallbacksWithinLag) {
974 CountingCallback cb1(mDispatch);
975 CountingCallback cb2(mDispatch);
976
977 Sequence seq;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500978 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(1000))
Kevin DuBoisf9477832020-07-16 10:21:36 -0700979 .InSequence(seq)
980 .WillOnce(Return(1000));
Ady Abrahamb491c902020-08-15 15:47:56 -0700981 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500982 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(1000))
Kevin DuBoisf9477832020-07-16 10:21:36 -0700983 .InSequence(seq)
984 .WillOnce(Return(1000));
985
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700986 auto result =
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500987 mDispatch.schedule(cb1,
988 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700989 EXPECT_TRUE(result.has_value());
990 EXPECT_EQ(600, *result);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500991 result = mDispatch.schedule(cb2,
992 {.workDuration = 390, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700993 EXPECT_TRUE(result.has_value());
994 EXPECT_EQ(610, *result);
Kevin DuBoisf9477832020-07-16 10:21:36 -0700995
996 mMockClock.setLag(100);
997 mMockClock.advanceBy(700);
998
999 ASSERT_THAT(cb1.mWakeupTime.size(), Eq(1));
1000 EXPECT_THAT(cb1.mWakeupTime[0], Eq(600));
Ady Abraham9c53ee72020-07-22 21:16:18 -07001001 ASSERT_THAT(cb1.mReadyTime.size(), Eq(1));
1002 EXPECT_THAT(cb1.mReadyTime[0], Eq(1000));
Kevin DuBoisf9477832020-07-16 10:21:36 -07001003 ASSERT_THAT(cb2.mWakeupTime.size(), Eq(1));
1004 EXPECT_THAT(cb2.mWakeupTime[0], Eq(610));
Ady Abraham9c53ee72020-07-22 21:16:18 -07001005 ASSERT_THAT(cb2.mReadyTime.size(), Eq(1));
1006 EXPECT_THAT(cb2.mReadyTime[0], Eq(1000));
1007}
1008
1009TEST_F(VSyncDispatchTimerQueueTest, basicAlarmSettingFutureWithReadyDuration) {
1010 auto intended = mPeriod - 230;
1011 EXPECT_CALL(mMockClock, alarmAt(_, 900));
1012
1013 CountingCallback cb(mDispatch);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001014 const auto result = mDispatch.schedule(cb,
1015 {.workDuration = 70,
1016 .readyDuration = 30,
1017 .earliestVsync = intended});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001018 EXPECT_TRUE(result.has_value());
1019 EXPECT_EQ(900, *result);
Ady Abraham9c53ee72020-07-22 21:16:18 -07001020 advanceToNextCallback();
1021
1022 ASSERT_THAT(cb.mCalls.size(), Eq(1));
1023 EXPECT_THAT(cb.mCalls[0], Eq(mPeriod));
1024 ASSERT_THAT(cb.mWakeupTime.size(), Eq(1));
1025 EXPECT_THAT(cb.mWakeupTime[0], 900);
1026 ASSERT_THAT(cb.mReadyTime.size(), Eq(1));
1027 EXPECT_THAT(cb.mReadyTime[0], 970);
Kevin DuBoisf9477832020-07-16 10:21:36 -07001028}
1029
Ady Abraham69b9e622021-07-19 12:24:31 -07001030TEST_F(VSyncDispatchTimerQueueTest, updatesVsyncTimeForCloseWakeupTime) {
1031 Sequence seq;
1032 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
1033
1034 CountingCallback cb(mDispatch);
1035
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001036 mDispatch.schedule(cb, {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
1037 mDispatch.schedule(cb, {.workDuration = 1400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abraham69b9e622021-07-19 12:24:31 -07001038
1039 advanceToNextCallback();
1040
1041 advanceToNextCallback();
1042
1043 ASSERT_THAT(cb.mCalls.size(), Eq(1));
1044 EXPECT_THAT(cb.mCalls[0], Eq(2000));
1045 ASSERT_THAT(cb.mWakeupTime.size(), Eq(1));
1046 EXPECT_THAT(cb.mWakeupTime[0], Eq(600));
1047 ASSERT_THAT(cb.mReadyTime.size(), Eq(1));
1048 EXPECT_THAT(cb.mReadyTime[0], Eq(2000));
1049}
1050
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001051class VSyncDispatchTimerQueueEntryTest : public testing::Test {
Kevin DuBois305bef12019-10-09 13:23:27 -07001052protected:
1053 nsecs_t const mPeriod = 1000;
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001054 nsecs_t const mVsyncMoveThreshold = 200;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001055 NiceMock<MockVSyncTracker> mStubTracker{mPeriod};
Kevin DuBois305bef12019-10-09 13:23:27 -07001056};
1057
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001058TEST_F(VSyncDispatchTimerQueueEntryTest, stateAfterInitialization) {
Kevin DuBois305bef12019-10-09 13:23:27 -07001059 std::string name("basicname");
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001060 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001061 name, [](auto, auto, auto) {}, mVsyncMoveThreshold);
Kevin DuBois305bef12019-10-09 13:23:27 -07001062 EXPECT_THAT(entry.name(), Eq(name));
1063 EXPECT_FALSE(entry.lastExecutedVsyncTarget());
1064 EXPECT_FALSE(entry.wakeupTime());
1065}
1066
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001067TEST_F(VSyncDispatchTimerQueueEntryTest, stateScheduling) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001068 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001069 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Kevin DuBois305bef12019-10-09 13:23:27 -07001070
1071 EXPECT_FALSE(entry.wakeupTime());
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001072 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001073 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001074 .has_value());
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001075 auto const wakeup = entry.wakeupTime();
1076 ASSERT_TRUE(wakeup);
1077 EXPECT_THAT(*wakeup, Eq(900));
Kevin DuBois305bef12019-10-09 13:23:27 -07001078
1079 entry.disarm();
1080 EXPECT_FALSE(entry.wakeupTime());
1081}
1082
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001083TEST_F(VSyncDispatchTimerQueueEntryTest, stateSchedulingReallyLongWakeupLatency) {
Kevin DuBois305bef12019-10-09 13:23:27 -07001084 auto const duration = 500;
1085 auto const now = 8750;
1086
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001087 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(now + duration))
Kevin DuBois305bef12019-10-09 13:23:27 -07001088 .Times(1)
1089 .WillOnce(Return(10000));
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001090 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001091 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Kevin DuBois305bef12019-10-09 13:23:27 -07001092
1093 EXPECT_FALSE(entry.wakeupTime());
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001094 EXPECT_TRUE(entry.schedule({.workDuration = 500, .readyDuration = 0, .earliestVsync = 994},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001095 mStubTracker, now)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001096 .has_value());
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001097 auto const wakeup = entry.wakeupTime();
1098 ASSERT_TRUE(wakeup);
1099 EXPECT_THAT(*wakeup, Eq(9500));
Kevin DuBois305bef12019-10-09 13:23:27 -07001100}
1101
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001102TEST_F(VSyncDispatchTimerQueueEntryTest, runCallback) {
Kevin DuBois305bef12019-10-09 13:23:27 -07001103 auto callCount = 0;
Kevin DuBois2968afc2020-01-14 09:48:50 -08001104 auto vsyncCalledTime = 0;
1105 auto wakeupCalledTime = 0;
Ady Abraham9c53ee72020-07-22 21:16:18 -07001106 auto readyCalledTime = 0;
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001107 VSyncDispatchTimerQueueEntry entry(
1108 "test",
Ady Abraham9c53ee72020-07-22 21:16:18 -07001109 [&](auto vsyncTime, auto wakeupTime, auto readyTime) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001110 callCount++;
Kevin DuBois2968afc2020-01-14 09:48:50 -08001111 vsyncCalledTime = vsyncTime;
1112 wakeupCalledTime = wakeupTime;
Ady Abraham9c53ee72020-07-22 21:16:18 -07001113 readyCalledTime = readyTime;
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001114 },
1115 mVsyncMoveThreshold);
Kevin DuBois305bef12019-10-09 13:23:27 -07001116
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001117 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001118 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001119 .has_value());
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001120 auto const wakeup = entry.wakeupTime();
1121 ASSERT_TRUE(wakeup);
1122 EXPECT_THAT(*wakeup, Eq(900));
Kevin DuBois305bef12019-10-09 13:23:27 -07001123
Ady Abraham9c53ee72020-07-22 21:16:18 -07001124 auto const ready = entry.readyTime();
1125 ASSERT_TRUE(ready);
1126 EXPECT_THAT(*ready, Eq(1000));
1127
1128 entry.callback(entry.executing(), *wakeup, *ready);
Kevin DuBois305bef12019-10-09 13:23:27 -07001129
1130 EXPECT_THAT(callCount, Eq(1));
Kevin DuBois2968afc2020-01-14 09:48:50 -08001131 EXPECT_THAT(vsyncCalledTime, Eq(mPeriod));
1132 EXPECT_THAT(wakeupCalledTime, Eq(*wakeup));
Kevin DuBois305bef12019-10-09 13:23:27 -07001133 EXPECT_FALSE(entry.wakeupTime());
1134 auto lastCalledTarget = entry.lastExecutedVsyncTarget();
1135 ASSERT_TRUE(lastCalledTarget);
1136 EXPECT_THAT(*lastCalledTarget, Eq(mPeriod));
1137}
1138
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001139TEST_F(VSyncDispatchTimerQueueEntryTest, updateCallback) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001140 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(_))
Kevin DuBois305bef12019-10-09 13:23:27 -07001141 .Times(2)
1142 .WillOnce(Return(1000))
1143 .WillOnce(Return(1020));
1144
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001145 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001146 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Kevin DuBois305bef12019-10-09 13:23:27 -07001147
1148 EXPECT_FALSE(entry.wakeupTime());
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001149 entry.update(mStubTracker, 0);
Kevin DuBois305bef12019-10-09 13:23:27 -07001150 EXPECT_FALSE(entry.wakeupTime());
1151
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001152 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001153 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001154 .has_value());
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001155 auto wakeup = entry.wakeupTime();
1156 ASSERT_TRUE(wakeup);
Kevin DuBois305bef12019-10-09 13:23:27 -07001157 EXPECT_THAT(wakeup, Eq(900));
1158
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001159 entry.update(mStubTracker, 0);
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001160 wakeup = entry.wakeupTime();
1161 ASSERT_TRUE(wakeup);
1162 EXPECT_THAT(*wakeup, Eq(920));
Kevin DuBois305bef12019-10-09 13:23:27 -07001163}
1164
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001165TEST_F(VSyncDispatchTimerQueueEntryTest, skipsUpdateIfJustScheduled) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001166 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001167 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001168 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001169 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001170 .has_value());
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001171 entry.update(mStubTracker, 0);
Kevin DuBois305bef12019-10-09 13:23:27 -07001172
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001173 auto const wakeup = entry.wakeupTime();
1174 ASSERT_TRUE(wakeup);
1175 EXPECT_THAT(*wakeup, Eq(wakeup));
1176}
1177
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001178TEST_F(VSyncDispatchTimerQueueEntryTest, willSnapToNextTargettableVSync) {
1179 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001180 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001181 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001182 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001183 .has_value());
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001184 entry.executing(); // 1000 is executing
1185 // had 1000 not been executing, this could have been scheduled for time 800.
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001186 EXPECT_TRUE(entry.schedule({.workDuration = 200, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001187 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001188 .has_value());
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001189 EXPECT_THAT(*entry.wakeupTime(), Eq(1800));
Ady Abraham9c53ee72020-07-22 21:16:18 -07001190 EXPECT_THAT(*entry.readyTime(), Eq(2000));
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001191
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001192 EXPECT_TRUE(entry.schedule({.workDuration = 50, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001193 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001194 .has_value());
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001195 EXPECT_THAT(*entry.wakeupTime(), Eq(1950));
Ady Abraham9c53ee72020-07-22 21:16:18 -07001196 EXPECT_THAT(*entry.readyTime(), Eq(2000));
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001197
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001198 EXPECT_TRUE(entry.schedule({.workDuration = 200, .readyDuration = 0, .earliestVsync = 1001},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001199 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001200 .has_value());
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001201 EXPECT_THAT(*entry.wakeupTime(), Eq(1800));
Ady Abraham9c53ee72020-07-22 21:16:18 -07001202 EXPECT_THAT(*entry.readyTime(), Eq(2000));
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001203}
1204
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001205TEST_F(VSyncDispatchTimerQueueEntryTest,
1206 willRequestNextEstimateWhenSnappingToNextTargettableVSync) {
1207 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001208 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001209
1210 Sequence seq;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001211 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(500))
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001212 .InSequence(seq)
1213 .WillOnce(Return(1000));
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001214 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(500))
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001215 .InSequence(seq)
1216 .WillOnce(Return(1000));
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001217 EXPECT_CALL(mStubTracker, nextAnticipatedVSyncTimeFrom(1000 + mVsyncMoveThreshold))
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001218 .InSequence(seq)
1219 .WillOnce(Return(2000));
1220
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001221 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001222 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001223 .has_value());
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001224
1225 entry.executing(); // 1000 is executing
1226
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001227 EXPECT_TRUE(entry.schedule({.workDuration = 200, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001228 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001229 .has_value());
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001230}
1231
1232TEST_F(VSyncDispatchTimerQueueEntryTest, reportsScheduledIfStillTime) {
1233 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001234 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001235 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001236 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001237 .has_value());
1238 EXPECT_TRUE(entry.schedule({.workDuration = 200, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001239 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001240 .has_value());
1241 EXPECT_TRUE(entry.schedule({.workDuration = 50, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001242 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001243 .has_value());
1244 EXPECT_TRUE(entry.schedule({.workDuration = 1200, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001245 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001246 .has_value());
Kevin DuBois305bef12019-10-09 13:23:27 -07001247}
1248
Kevin DuBois5c18c1c2020-05-27 15:50:50 -07001249TEST_F(VSyncDispatchTimerQueueEntryTest, storesPendingUpdatesUntilUpdate) {
1250 static constexpr auto effectualOffset = 200;
1251 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001252 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -07001253 EXPECT_FALSE(entry.hasPendingWorkloadUpdate());
Ady Abraham9c53ee72020-07-22 21:16:18 -07001254 entry.addPendingWorkloadUpdate({.workDuration = 100, .readyDuration = 0, .earliestVsync = 400});
1255 entry.addPendingWorkloadUpdate(
1256 {.workDuration = effectualOffset, .readyDuration = 0, .earliestVsync = 400});
Kevin DuBois5c18c1c2020-05-27 15:50:50 -07001257 EXPECT_TRUE(entry.hasPendingWorkloadUpdate());
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001258 entry.update(mStubTracker, 0);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -07001259 EXPECT_FALSE(entry.hasPendingWorkloadUpdate());
1260 EXPECT_THAT(*entry.wakeupTime(), Eq(mPeriod - effectualOffset));
1261}
1262
Ady Abraham9c53ee72020-07-22 21:16:18 -07001263TEST_F(VSyncDispatchTimerQueueEntryTest, runCallbackWithReadyDuration) {
1264 auto callCount = 0;
1265 auto vsyncCalledTime = 0;
1266 auto wakeupCalledTime = 0;
1267 auto readyCalledTime = 0;
1268 VSyncDispatchTimerQueueEntry entry(
1269 "test",
1270 [&](auto vsyncTime, auto wakeupTime, auto readyTime) {
1271 callCount++;
1272 vsyncCalledTime = vsyncTime;
1273 wakeupCalledTime = wakeupTime;
1274 readyCalledTime = readyTime;
1275 },
1276 mVsyncMoveThreshold);
1277
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001278 EXPECT_TRUE(entry.schedule({.workDuration = 70, .readyDuration = 30, .earliestVsync = 500},
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -05001279 mStubTracker, 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001280 .has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -07001281 auto const wakeup = entry.wakeupTime();
1282 ASSERT_TRUE(wakeup);
1283 EXPECT_THAT(*wakeup, Eq(900));
1284
1285 auto const ready = entry.readyTime();
1286 ASSERT_TRUE(ready);
1287 EXPECT_THAT(*ready, Eq(970));
1288
1289 entry.callback(entry.executing(), *wakeup, *ready);
1290
1291 EXPECT_THAT(callCount, Eq(1));
1292 EXPECT_THAT(vsyncCalledTime, Eq(mPeriod));
1293 EXPECT_THAT(wakeupCalledTime, Eq(*wakeup));
1294 EXPECT_FALSE(entry.wakeupTime());
1295 auto lastCalledTarget = entry.lastExecutedVsyncTarget();
1296 ASSERT_TRUE(lastCalledTarget);
1297 EXPECT_THAT(*lastCalledTarget, Eq(mPeriod));
1298}
1299
Kevin DuBois305bef12019-10-09 13:23:27 -07001300} // namespace android::scheduler
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001301
1302// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +01001303#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"