Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: third_party/WebKit/Source/platform/TimerTest.cpp

Issue 2266443002: Optimize posting of WTF::Closure and improve scheduler test mocks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix FakeWebTaskRunner leak Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "platform/Timer.h" 5 #include "platform/Timer.h"
6 6
7 #include "platform/scheduler/base/task_queue_impl.h"
8 #include "platform/scheduler/child/web_task_runner_impl.h"
9 #include "platform/scheduler/renderer/renderer_scheduler_impl.h"
7 #include "platform/testing/TestingPlatformSupport.h" 10 #include "platform/testing/TestingPlatformSupport.h"
8 #include "public/platform/Platform.h" 11 #include "public/platform/Platform.h"
9 #include "public/platform/WebScheduler.h" 12 #include "public/platform/WebScheduler.h"
10 #include "public/platform/WebThread.h" 13 #include "public/platform/WebThread.h"
11 #include "public/platform/WebViewScheduler.h" 14 #include "public/platform/WebViewScheduler.h"
12 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "wtf/CurrentTime.h"
14 #include "wtf/PtrUtil.h" 18 #include "wtf/PtrUtil.h"
15 #include "wtf/RefCounted.h" 19 #include "wtf/RefCounted.h"
16 #include <memory> 20 #include <memory>
17 #include <queue> 21 #include <queue>
18 22
19 using testing::ElementsAre; 23 using testing::ElementsAre;
20 24
21 namespace blink { 25 namespace blink {
22 namespace { 26 namespace {
23 double gCurrentTimeSecs = 0.0;
24
25 // This class exists because gcc doesn't know how to move an std::unique_ptr.
26 class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> {
27 public:
28 explicit RefCountedTaskContainer(WebTaskRunner::Task* task) : m_task(wrapUni que(task)) { }
29
30 ~RefCountedTaskContainer() { }
31
32 void run()
33 {
34 m_task->run();
35 }
36
37 private:
38 std::unique_ptr<WebTaskRunner::Task> m_task;
39 };
40
41 class DelayedTask {
42 public:
43 DelayedTask(WebTaskRunner::Task* task, double delaySeconds)
44 : m_task(adoptRef(new RefCountedTaskContainer(task)))
45 , m_runTimeSeconds(gCurrentTimeSecs + delaySeconds)
46 , m_delaySeconds(delaySeconds)
47 {
48 }
49
50 bool operator<(const DelayedTask& other) const
51 {
52 return m_runTimeSeconds > other.m_runTimeSeconds;
53 }
54
55 void run() const
56 {
57 m_task->run();
58 }
59
60 double runTimeSeconds() const
61 {
62 return m_runTimeSeconds;
63 }
64
65 double delaySeconds() const
66 {
67 return m_delaySeconds;
68 }
69
70 private:
71 RefPtr<RefCountedTaskContainer> m_task;
72 double m_runTimeSeconds;
73 double m_delaySeconds;
74 };
75
76 class MockWebTaskRunner : public WebTaskRunner {
77 public:
78 explicit MockWebTaskRunner(std::priority_queue<DelayedTask>* timerTasks) : m _timerTasks(timerTasks) { }
79 ~MockWebTaskRunner() override { }
80
81 void postTask(const WebTraceLocation&, Task* task) override
82 {
83 m_timerTasks->push(DelayedTask(task, 0));
84 }
85
86 void postDelayedTask(const WebTraceLocation&, Task* task, double delayMs) ov erride
87 {
88 m_timerTasks->push(DelayedTask(task, delayMs * 0.001));
89 }
90
91 bool runsTasksOnCurrentThread() override
92 {
93 NOTREACHED();
94 return true;
95 }
96
97 std::unique_ptr<WebTaskRunner> clone() override
98 {
99 NOTREACHED();
100 return nullptr;
101 }
102
103 double virtualTimeSeconds() const override
104 {
105 NOTREACHED();
106 return 0.0;
107 }
108
109 double monotonicallyIncreasingVirtualTimeSeconds() const override
110 {
111 return gCurrentTimeSecs;
112 }
113
114 base::SingleThreadTaskRunner* taskRunner() override
115 {
116 ASSERT_NOT_REACHED();
117 return nullptr;
118 }
119
120 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED
121 };
122
123 class MockWebScheduler : public WebScheduler {
124 public:
125 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { }
126 ~MockWebScheduler() override { }
127
128 bool shouldYieldForHighPriorityWork() override
129 {
130 return false;
131 }
132
133 bool canExceedIdleDeadlineIfRequired() override
134 {
135 return false;
136 }
137
138 void postIdleTask(const WebTraceLocation&, WebThread::IdleTask*) override
139 {
140 }
141
142 void postNonNestableIdleTask(const WebTraceLocation&, WebThread::IdleTask*) override
143 {
144 }
145
146 void postIdleTaskAfterWakeup(const WebTraceLocation&, WebThread::IdleTask*) override
147 {
148 }
149
150 WebTaskRunner* timerTaskRunner() override
151 {
152 return &m_timerWebTaskRunner;
153 }
154
155 WebTaskRunner* loadingTaskRunner() override
156 {
157 NOTREACHED();
158 return nullptr;
159 }
160
161 void runUntilIdle()
162 {
163 while (m_timerTasks.size()) {
164 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds();
165 m_timerTasks.top().run();
166 m_timerTasks.pop();
167 }
168 }
169
170 void runUntilIdleOrDeadlinePassed(double deadline)
171 {
172 while (m_timerTasks.size()) {
173 if (m_timerTasks.top().runTimeSeconds() > deadline) {
174 gCurrentTimeSecs = deadline;
175 break;
176 }
177 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds();
178 m_timerTasks.top().run();
179 m_timerTasks.pop();
180 }
181 }
182
183 void runPendingTasks()
184 {
185 while (m_timerTasks.size() && m_timerTasks.top().runTimeSeconds() <= gCu rrentTimeSecs) {
186 m_timerTasks.top().run();
187 m_timerTasks.pop();
188 }
189 }
190
191 bool hasOneTimerTask() const
192 {
193 return m_timerTasks.size() == 1;
194 }
195
196 double nextTimerTaskDelaySecs() const
197 {
198 ASSERT(hasOneTimerTask());
199 return m_timerTasks.top().delaySeconds();
200 }
201
202 void shutdown() override {}
203 std::unique_ptr<WebViewScheduler> createWebViewScheduler(InterventionReporte r*) override { return nullptr; }
204 void suspendTimerQueue() override { }
205 void resumeTimerQueue() override { }
206 void addPendingNavigation(WebScheduler::NavigatingFrameType) override { }
207 void removePendingNavigation(WebScheduler::NavigatingFrameType) override { }
208 void onNavigationStarted() override { }
209
210 private:
211 std::priority_queue<DelayedTask> m_timerTasks;
212 MockWebTaskRunner m_timerWebTaskRunner;
213 };
214
215 class FakeWebThread : public WebThread {
216 public:
217 FakeWebThread() : m_webScheduler(wrapUnique(new MockWebScheduler())) { }
218 ~FakeWebThread() override { }
219
220 virtual bool isCurrentThread() const
221 {
222 NOTREACHED();
223 return true;
224 }
225
226 virtual PlatformThreadId threadId() const
227 {
228 NOTREACHED();
229 return 0;
230 }
231
232 WebTaskRunner* getWebTaskRunner() override
233 {
234 NOTREACHED();
235 return nullptr;
236 }
237
238 WebScheduler* scheduler() const override
239 {
240 return m_webScheduler.get();
241 }
242
243 virtual void enterRunLoop()
244 {
245 NOTREACHED();
246 }
247
248 virtual void exitRunLoop()
249 {
250 NOTREACHED();
251 }
252
253 private:
254 std::unique_ptr<MockWebScheduler> m_webScheduler;
255 };
256
257 class TimerTestPlatform : public TestingPlatformSupport {
258 public:
259 TimerTestPlatform()
260 : m_webThread(wrapUnique(new FakeWebThread())) { }
261 ~TimerTestPlatform() override { }
262
263 WebThread* currentThread() override
264 {
265 return m_webThread.get();
266 }
267
268 void runUntilIdle()
269 {
270 mockScheduler()->runUntilIdle();
271 }
272
273 void runPendingTasks()
274 {
275 mockScheduler()->runPendingTasks();
276 }
277
278 void runUntilIdleOrDeadlinePassed(double deadline)
279 {
280 mockScheduler()->runUntilIdleOrDeadlinePassed(deadline);
281 }
282
283 bool hasOneTimerTask() const
284 {
285 return mockScheduler()->hasOneTimerTask();
286 }
287
288 double nextTimerTaskDelaySecs() const
289 {
290 return mockScheduler()->nextTimerTaskDelaySecs();
291 }
292
293 private:
294 MockWebScheduler* mockScheduler() const
295 {
296 return static_cast<MockWebScheduler*>(m_webThread->scheduler());
297 }
298
299 std::unique_ptr<FakeWebThread> m_webThread;
300 };
301 27
302 class TimerTest : public testing::Test { 28 class TimerTest : public testing::Test {
303 public: 29 public:
304 void SetUp() override 30 void SetUp() override
305 { 31 {
306 m_runTimes.clear(); 32 m_runTimes.clear();
307 gCurrentTimeSecs = 10.0; 33 m_platform.advanceClockSeconds(10.0);
308 m_startTime = gCurrentTimeSecs; 34 m_startTime = monotonicallyIncreasingTime();
309 } 35 }
310 36
311 void countingTask(TimerBase*) 37 void countingTask(TimerBase*)
312 { 38 {
313 m_runTimes.append(gCurrentTimeSecs); 39 m_runTimes.append(monotonicallyIncreasingTime());
314 } 40 }
315 41
316 void recordNextFireTimeTask(TimerBase* timer) 42 void recordNextFireTimeTask(TimerBase* timer)
317 { 43 {
318 m_nextFireTimes.append(gCurrentTimeSecs + timer->nextFireInterval()); 44 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn terval());
319 } 45 }
320 46
321 void advanceTimeBy(double timeSecs) 47 void runUntilDeadline(double deadline)
322 { 48 {
323 gCurrentTimeSecs += timeSecs; 49 double period = deadline - monotonicallyIncreasingTime();
50 EXPECT_GE(period, 0.0);
51 m_platform.runForPeriodSeconds(period);
52
53 // We may have stopped before the clock advanced to |deadline|.
54 double timeToAdvance = deadline - monotonicallyIncreasingTime();
55 m_platform.advanceClockSeconds(timeToAdvance);
324 } 56 }
325 57
326 void runUntilIdle() 58 // Returns false if there are no pending delayed tasks, otherwise sets |time | to
59 // the delay in seconds till the next pending delayed task is scheduled to f ire.
60 bool timeTillNextDelayedTask(double* time) const
327 { 61 {
328 m_platform.runUntilIdle(); 62 base::TimeTicks nextRunTime;
329 } 63 if (!m_platform.rendererScheduler()->TimerTaskRunner()->GetTimeDomain()- >NextScheduledRunTime(&nextRunTime))
330 64 return false;
331 void runPendingTasks() 65 *time = (nextRunTime - m_platform.rendererScheduler()->TimerTaskRunner() ->GetTimeDomain()->Now()).InSecondsF();
332 { 66 return true;
333 m_platform.runPendingTasks();
334 }
335
336 void runUntilIdleOrDeadlinePassed(double deadline)
337 {
338 m_platform.runUntilIdleOrDeadlinePassed(deadline);
339 }
340
341 bool hasOneTimerTask() const
342 {
343 return m_platform.hasOneTimerTask();
344 }
345
346 double nextTimerTaskDelaySecs() const
347 {
348 return m_platform.nextTimerTaskDelaySecs();
349 } 67 }
350 68
351 protected: 69 protected:
352 double m_startTime; 70 double m_startTime;
353 WTF::Vector<double> m_runTimes; 71 WTF::Vector<double> m_runTimes;
354 WTF::Vector<double> m_nextFireTimes; 72 WTF::Vector<double> m_nextFireTimes;
355 73 TestingPlatformSupportWithMockScheduler m_platform;
356 private:
357 TimerTestPlatform m_platform;
358 }; 74 };
359 75
360 TEST_F(TimerTest, StartOneShot_Zero) 76 TEST_F(TimerTest, StartOneShot_Zero)
361 { 77 {
362 Timer<TimerTest> timer(this, &TimerTest::countingTask); 78 Timer<TimerTest> timer(this, &TimerTest::countingTask);
363 timer.startOneShot(0, BLINK_FROM_HERE); 79 timer.startOneShot(0, BLINK_FROM_HERE);
364 80
365 ASSERT(hasOneTimerTask()); 81 double runTime;
366 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); 82 EXPECT_FALSE(timeTillNextDelayedTask(&runTime));
367 83
368 runUntilIdle(); 84 m_platform.runUntilIdle();
369 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 85 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
370 } 86 }
371 87
372 TEST_F(TimerTest, StartOneShot_ZeroAndCancel) 88 TEST_F(TimerTest, StartOneShot_ZeroAndCancel)
373 { 89 {
374 Timer<TimerTest> timer(this, &TimerTest::countingTask); 90 Timer<TimerTest> timer(this, &TimerTest::countingTask);
375 timer.startOneShot(0, BLINK_FROM_HERE); 91 timer.startOneShot(0, BLINK_FROM_HERE);
376 92
377 ASSERT(hasOneTimerTask()); 93 double runTime;
378 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); 94 EXPECT_FALSE(timeTillNextDelayedTask(&runTime));
379 95
380 timer.stop(); 96 timer.stop();
381 97
382 runUntilIdle(); 98 m_platform.runUntilIdle();
383 EXPECT_FALSE(m_runTimes.size()); 99 EXPECT_FALSE(m_runTimes.size());
384 } 100 }
385 101
386 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost) 102 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost)
387 { 103 {
388 Timer<TimerTest> timer(this, &TimerTest::countingTask); 104 Timer<TimerTest> timer(this, &TimerTest::countingTask);
389 timer.startOneShot(0, BLINK_FROM_HERE); 105 timer.startOneShot(0, BLINK_FROM_HERE);
390 106
391 ASSERT(hasOneTimerTask()); 107 double runTime;
392 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); 108 EXPECT_FALSE(timeTillNextDelayedTask(&runTime));
393 109
394 timer.stop(); 110 timer.stop();
395 111
396 runUntilIdle(); 112 m_platform.runUntilIdle();
397 EXPECT_FALSE(m_runTimes.size()); 113 EXPECT_FALSE(m_runTimes.size());
398 114
399 timer.startOneShot(0, BLINK_FROM_HERE); 115 timer.startOneShot(0, BLINK_FROM_HERE);
400 116
401 ASSERT(hasOneTimerTask()); 117 EXPECT_FALSE(timeTillNextDelayedTask(&runTime));
402 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs());
403 118
404 runUntilIdle(); 119 m_platform.runUntilIdle();
405 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 120 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
406 } 121 }
407 122
408 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning) 123 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning)
409 { 124 {
410 Timer<TimerTest> timer(this, &TimerTest::countingTask); 125 Timer<TimerTest> timer(this, &TimerTest::countingTask);
411 timer.startOneShot(0, BLINK_FROM_HERE); 126 timer.startOneShot(0, BLINK_FROM_HERE);
412 127
413 ASSERT(hasOneTimerTask()); 128 double runTime;
414 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); 129 EXPECT_FALSE(timeTillNextDelayedTask(&runTime));
415 130
416 runUntilIdle(); 131 m_platform.runUntilIdle();
417 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 132 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
418 133
419 timer.startOneShot(0, BLINK_FROM_HERE); 134 timer.startOneShot(0, BLINK_FROM_HERE);
420 135
421 ASSERT(hasOneTimerTask()); 136 EXPECT_FALSE(timeTillNextDelayedTask(&runTime));
422 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs());
423 137
424 runUntilIdle(); 138 m_platform.runUntilIdle();
425 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime)); 139 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime));
426 } 140 }
427 141
428 TEST_F(TimerTest, StartOneShot_NonZero) 142 TEST_F(TimerTest, StartOneShot_NonZero)
429 { 143 {
430 Timer<TimerTest> timer(this, &TimerTest::countingTask); 144 Timer<TimerTest> timer(this, &TimerTest::countingTask);
431 timer.startOneShot(10.0, BLINK_FROM_HERE); 145 timer.startOneShot(10.0, BLINK_FROM_HERE);
432 146
433 ASSERT(hasOneTimerTask()); 147 double runTime;
434 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 148 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
149 EXPECT_FLOAT_EQ(10.0, runTime);
435 150
436 runUntilIdle(); 151 m_platform.runUntilIdle();
437 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 152 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
438 } 153 }
439 154
440 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel) 155 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel)
441 { 156 {
442 Timer<TimerTest> timer(this, &TimerTest::countingTask); 157 Timer<TimerTest> timer(this, &TimerTest::countingTask);
443 timer.startOneShot(10, BLINK_FROM_HERE); 158 timer.startOneShot(10, BLINK_FROM_HERE);
444 159
445 ASSERT(hasOneTimerTask()); 160 double runTime;
446 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 161 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
162 EXPECT_FLOAT_EQ(10.0, runTime);
447 163
448 timer.stop(); 164 timer.stop();
165 // TODO(alexclarke):cancel the underlying delayed task.
166 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
449 167
450 runUntilIdle(); 168 m_platform.runUntilIdle();
451 EXPECT_FALSE(m_runTimes.size()); 169 EXPECT_FALSE(m_runTimes.size());
452 } 170 }
453 171
454 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost) 172 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost)
455 { 173 {
456 Timer<TimerTest> timer(this, &TimerTest::countingTask); 174 Timer<TimerTest> timer(this, &TimerTest::countingTask);
457 timer.startOneShot(10, BLINK_FROM_HERE); 175 timer.startOneShot(10, BLINK_FROM_HERE);
458 176
459 ASSERT(hasOneTimerTask()); 177 double runTime;
460 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 178 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
179 EXPECT_FLOAT_EQ(10.0, runTime);
461 180
462 timer.stop(); 181 timer.stop();
182 // TODO(alexclarke):cancel the underlying delayed task.
183 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
463 184
464 runUntilIdle(); 185 m_platform.runUntilIdle();
465 EXPECT_FALSE(m_runTimes.size()); 186 EXPECT_FALSE(m_runTimes.size());
466 187
467 double secondPostTime = gCurrentTimeSecs; 188 double secondPostTime = monotonicallyIncreasingTime();
468 timer.startOneShot(10, BLINK_FROM_HERE); 189 timer.startOneShot(10, BLINK_FROM_HERE);
469 190
470 ASSERT(hasOneTimerTask()); 191 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
471 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 192 EXPECT_FLOAT_EQ(10.0, runTime);
472 193
473 runUntilIdle(); 194 m_platform.runUntilIdle();
474 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); 195 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0));
475 } 196 }
476 197
477 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) 198 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning)
478 { 199 {
479 Timer<TimerTest> timer(this, &TimerTest::countingTask); 200 Timer<TimerTest> timer(this, &TimerTest::countingTask);
480 timer.startOneShot(10, BLINK_FROM_HERE); 201 timer.startOneShot(10, BLINK_FROM_HERE);
481 202
482 ASSERT(hasOneTimerTask()); 203 double runTime;
483 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 204 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
205 EXPECT_FLOAT_EQ(10.0, runTime);
484 206
485 runUntilIdle(); 207 m_platform.runUntilIdle();
486 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 208 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
487 209
488 timer.startOneShot(20, BLINK_FROM_HERE); 210 timer.startOneShot(20, BLINK_FROM_HERE);
489 211
490 ASSERT(hasOneTimerTask()); 212 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
491 EXPECT_FLOAT_EQ(20.0, nextTimerTaskDelaySecs()); 213 EXPECT_FLOAT_EQ(20.0, runTime);
492 214
493 runUntilIdle(); 215 m_platform.runUntilIdle();
494 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0)) ; 216 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0)) ;
495 } 217 }
496 218
497 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing) 219 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing)
498 { 220 {
499 Timer<TimerTest> timer(this, &TimerTest::countingTask); 221 Timer<TimerTest> timer(this, &TimerTest::countingTask);
500 timer.startOneShot(10, BLINK_FROM_HERE); 222 timer.startOneShot(10, BLINK_FROM_HERE);
501 timer.startOneShot(10, BLINK_FROM_HERE); 223 timer.startOneShot(10, BLINK_FROM_HERE);
502 224
503 ASSERT(hasOneTimerTask()); 225 double runTime;
504 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 226 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
227 EXPECT_FLOAT_EQ(10.0, runTime);
505 228
506 runUntilIdle(); 229 m_platform.runUntilIdle();
507 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 230 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
508 } 231 }
509 232
510 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOriginalTask) 233 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOriginalTask)
511 { 234 {
512 Timer<TimerTest> timer(this, &TimerTest::countingTask); 235 Timer<TimerTest> timer(this, &TimerTest::countingTask);
513 timer.startOneShot(10, BLINK_FROM_HERE); 236 timer.startOneShot(10, BLINK_FROM_HERE);
514 timer.startOneShot(0, BLINK_FROM_HERE); 237 timer.startOneShot(0, BLINK_FROM_HERE);
515 238
516 runUntilIdle(); 239 m_platform.runUntilIdle();
517 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 0.0)); 240 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 0.0));
518 } 241 }
519 242
520 TEST_F(TimerTest, PostingTimerTwiceWithLaterRunTimeCancelsOriginalTask) 243 TEST_F(TimerTest, PostingTimerTwiceWithLaterRunTimeCancelsOriginalTask)
521 { 244 {
522 Timer<TimerTest> timer(this, &TimerTest::countingTask); 245 Timer<TimerTest> timer(this, &TimerTest::countingTask);
523 timer.startOneShot(0, BLINK_FROM_HERE); 246 timer.startOneShot(0, BLINK_FROM_HERE);
524 timer.startOneShot(10, BLINK_FROM_HERE); 247 timer.startOneShot(10, BLINK_FROM_HERE);
525 248
526 runUntilIdle(); 249 m_platform.runUntilIdle();
527 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 250 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
528 } 251 }
529 252
530 TEST_F(TimerTest, StartRepeatingTask) 253 TEST_F(TimerTest, StartRepeatingTask)
531 { 254 {
532 Timer<TimerTest> timer(this, &TimerTest::countingTask); 255 Timer<TimerTest> timer(this, &TimerTest::countingTask);
533 timer.startRepeating(1.0, BLINK_FROM_HERE); 256 timer.startRepeating(1.0, BLINK_FROM_HERE);
534 257
535 ASSERT(hasOneTimerTask()); 258 double runTime;
536 EXPECT_FLOAT_EQ(1.0, nextTimerTaskDelaySecs()); 259 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
260 EXPECT_FLOAT_EQ(1.0, runTime);
537 261
538 runUntilIdleOrDeadlinePassed(m_startTime + 5.5); 262 runUntilDeadline(m_startTime + 5.5);
539 EXPECT_THAT(m_runTimes, ElementsAre( 263 EXPECT_THAT(m_runTimes, ElementsAre(
540 m_startTime + 1.0, m_startTime + 2.0, m_startTime + 3.0, m_startTime + 4 .0, m_startTime + 5.0)); 264 m_startTime + 1.0, m_startTime + 2.0, m_startTime + 3.0, m_startTime + 4 .0, m_startTime + 5.0));
541 } 265 }
542 266
543 TEST_F(TimerTest, StartRepeatingTask_ThenCancel) 267 TEST_F(TimerTest, StartRepeatingTask_ThenCancel)
544 { 268 {
545 Timer<TimerTest> timer(this, &TimerTest::countingTask); 269 Timer<TimerTest> timer(this, &TimerTest::countingTask);
546 timer.startRepeating(1.0, BLINK_FROM_HERE); 270 timer.startRepeating(1.0, BLINK_FROM_HERE);
547 271
548 ASSERT(hasOneTimerTask()); 272 double runTime;
549 EXPECT_FLOAT_EQ(1.0, nextTimerTaskDelaySecs()); 273 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
274 EXPECT_FLOAT_EQ(1.0, runTime);
550 275
551 runUntilIdleOrDeadlinePassed(m_startTime + 2.5); 276 runUntilDeadline(m_startTime + 2.5);
552 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 277 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
553 278
554 timer.stop(); 279 timer.stop();
555 runUntilIdle(); 280 m_platform.runUntilIdle();
556 281
557 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 282 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
558 } 283 }
559 284
560 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot) 285 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot)
561 { 286 {
562 Timer<TimerTest> timer(this, &TimerTest::countingTask); 287 Timer<TimerTest> timer(this, &TimerTest::countingTask);
563 timer.startRepeating(1.0, BLINK_FROM_HERE); 288 timer.startRepeating(1.0, BLINK_FROM_HERE);
564 289
565 ASSERT(hasOneTimerTask()); 290 double runTime;
566 EXPECT_FLOAT_EQ(1.0, nextTimerTaskDelaySecs()); 291 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
292 EXPECT_FLOAT_EQ(1.0, runTime);
567 293
568 runUntilIdleOrDeadlinePassed(m_startTime + 2.5); 294 runUntilDeadline(m_startTime + 2.5);
569 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 295 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
570 296
571 timer.startOneShot(0, BLINK_FROM_HERE); 297 timer.startOneShot(0, BLINK_FROM_HERE);
572 runUntilIdle(); 298 m_platform.runUntilIdle();
573 299
574 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, m_ startTime + 2.5)); 300 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, m_ startTime + 2.5));
575 } 301 }
576 302
577 TEST_F(TimerTest, IsActive_NeverPosted) 303 TEST_F(TimerTest, IsActive_NeverPosted)
578 { 304 {
579 Timer<TimerTest> timer(this, &TimerTest::countingTask); 305 Timer<TimerTest> timer(this, &TimerTest::countingTask);
580 306
581 EXPECT_FALSE(timer.isActive()); 307 EXPECT_FALSE(timer.isActive());
582 } 308 }
(...skipping 20 matching lines...) Expand all
603 timer.startRepeating(1.0, BLINK_FROM_HERE); 329 timer.startRepeating(1.0, BLINK_FROM_HERE);
604 330
605 EXPECT_TRUE(timer.isActive()); 331 EXPECT_TRUE(timer.isActive());
606 } 332 }
607 333
608 TEST_F(TimerTest, IsActive_AfterRunning_OneShotZero) 334 TEST_F(TimerTest, IsActive_AfterRunning_OneShotZero)
609 { 335 {
610 Timer<TimerTest> timer(this, &TimerTest::countingTask); 336 Timer<TimerTest> timer(this, &TimerTest::countingTask);
611 timer.startOneShot(0, BLINK_FROM_HERE); 337 timer.startOneShot(0, BLINK_FROM_HERE);
612 338
613 runUntilIdle(); 339 m_platform.runUntilIdle();
614 EXPECT_FALSE(timer.isActive()); 340 EXPECT_FALSE(timer.isActive());
615 } 341 }
616 342
617 TEST_F(TimerTest, IsActive_AfterRunning_OneShotNonZero) 343 TEST_F(TimerTest, IsActive_AfterRunning_OneShotNonZero)
618 { 344 {
619 Timer<TimerTest> timer(this, &TimerTest::countingTask); 345 Timer<TimerTest> timer(this, &TimerTest::countingTask);
620 timer.startOneShot(10, BLINK_FROM_HERE); 346 timer.startOneShot(10, BLINK_FROM_HERE);
621 347
622 runUntilIdle(); 348 m_platform.runUntilIdle();
623 EXPECT_FALSE(timer.isActive()); 349 EXPECT_FALSE(timer.isActive());
624 } 350 }
625 351
626 TEST_F(TimerTest, IsActive_AfterRunning_Repeating) 352 TEST_F(TimerTest, IsActive_AfterRunning_Repeating)
627 { 353 {
628 Timer<TimerTest> timer(this, &TimerTest::countingTask); 354 Timer<TimerTest> timer(this, &TimerTest::countingTask);
629 timer.startRepeating(1.0, BLINK_FROM_HERE); 355 timer.startRepeating(1.0, BLINK_FROM_HERE);
630 356
631 runUntilIdleOrDeadlinePassed(m_startTime + 10); 357 runUntilDeadline(m_startTime + 10);
632 EXPECT_TRUE(timer.isActive()); // It should run until cancelled. 358 EXPECT_TRUE(timer.isActive()); // It should run until cancelled.
633 } 359 }
634 360
635 TEST_F(TimerTest, NextFireInterval_OneShotZero) 361 TEST_F(TimerTest, NextFireInterval_OneShotZero)
636 { 362 {
637 Timer<TimerTest> timer(this, &TimerTest::countingTask); 363 Timer<TimerTest> timer(this, &TimerTest::countingTask);
638 timer.startOneShot(0, BLINK_FROM_HERE); 364 timer.startOneShot(0, BLINK_FROM_HERE);
639 365
640 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); 366 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval());
641 } 367 }
642 368
643 TEST_F(TimerTest, NextFireInterval_OneShotNonZero) 369 TEST_F(TimerTest, NextFireInterval_OneShotNonZero)
644 { 370 {
645 Timer<TimerTest> timer(this, &TimerTest::countingTask); 371 Timer<TimerTest> timer(this, &TimerTest::countingTask);
646 timer.startOneShot(10, BLINK_FROM_HERE); 372 timer.startOneShot(10, BLINK_FROM_HERE);
647 373
648 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); 374 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval());
649 } 375 }
650 376
651 TEST_F(TimerTest, NextFireInterval_OneShotNonZero_AfterAFewSeconds) 377 TEST_F(TimerTest, NextFireInterval_OneShotNonZero_AfterAFewSeconds)
652 { 378 {
379 m_platform.setAutoAdvanceNowToPendingTasks(false);
380
653 Timer<TimerTest> timer(this, &TimerTest::countingTask); 381 Timer<TimerTest> timer(this, &TimerTest::countingTask);
654 timer.startOneShot(10, BLINK_FROM_HERE); 382 timer.startOneShot(10, BLINK_FROM_HERE);
655 383
656 advanceTimeBy(2.0); 384 m_platform.advanceClockSeconds(2.0);
657 EXPECT_FLOAT_EQ(8.0, timer.nextFireInterval()); 385 EXPECT_FLOAT_EQ(8.0, timer.nextFireInterval());
658 } 386 }
659 387
660 TEST_F(TimerTest, NextFireInterval_Repeating) 388 TEST_F(TimerTest, NextFireInterval_Repeating)
661 { 389 {
662 Timer<TimerTest> timer(this, &TimerTest::countingTask); 390 Timer<TimerTest> timer(this, &TimerTest::countingTask);
663 timer.startRepeating(20, BLINK_FROM_HERE); 391 timer.startRepeating(20, BLINK_FROM_HERE);
664 392
665 EXPECT_FLOAT_EQ(20.0, timer.nextFireInterval()); 393 EXPECT_FLOAT_EQ(20.0, timer.nextFireInterval());
666 } 394 }
(...skipping 29 matching lines...) Expand all
696 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); 424 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
697 } 425 }
698 426
699 TEST_F(TimerTest, AugmentRepeatInterval) 427 TEST_F(TimerTest, AugmentRepeatInterval)
700 { 428 {
701 Timer<TimerTest> timer(this, &TimerTest::countingTask); 429 Timer<TimerTest> timer(this, &TimerTest::countingTask);
702 timer.startRepeating(10, BLINK_FROM_HERE); 430 timer.startRepeating(10, BLINK_FROM_HERE);
703 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); 431 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval());
704 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); 432 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval());
705 433
706 advanceTimeBy(2.0); 434 m_platform.advanceClockSeconds(2.0);
707 timer.augmentRepeatInterval(10); 435 timer.augmentRepeatInterval(10);
708 436
709 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); 437 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
710 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); 438 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval());
711 439
712 runUntilIdleOrDeadlinePassed(m_startTime + 50.0); 440 // NOTE setAutoAdvanceNowToPendingTasks(true) (which uses cc::OrderedSimpleT askRunner)
441 // results in somewhat strange behavior of the test clock which breaks this test.
442 // Specifically the test clock advancing logic ignores newly posted delayed tasks and
443 // advances too far.
444 runUntilDeadline(m_startTime + 50.0);
713 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0)) ; 445 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0)) ;
714 } 446 }
715 447
716 TEST_F(TimerTest, AugmentRepeatInterval_TimerFireDelayed) 448 TEST_F(TimerTest, AugmentRepeatInterval_TimerFireDelayed)
717 { 449 {
450 m_platform.setAutoAdvanceNowToPendingTasks(false);
451
718 Timer<TimerTest> timer(this, &TimerTest::countingTask); 452 Timer<TimerTest> timer(this, &TimerTest::countingTask);
719 timer.startRepeating(10, BLINK_FROM_HERE); 453 timer.startRepeating(10, BLINK_FROM_HERE);
720 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); 454 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval());
721 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); 455 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval());
722 456
723 advanceTimeBy(123.0); // Make the timer long overdue. 457 m_platform.advanceClockSeconds(123.0); // Make the timer long overdue.
724 timer.augmentRepeatInterval(10); 458 timer.augmentRepeatInterval(10);
725 459
726 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); 460 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
727 // The timer is overdue so it should be scheduled to fire immediatly. 461 // The timer is overdue so it should be scheduled to fire immediatly.
728 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); 462 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval());
729 } 463 }
730 464
731 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) 465 TEST_F(TimerTest, RepeatingTimerDoesNotDrift)
732 { 466 {
467 m_platform.setAutoAdvanceNowToPendingTasks(false);
468
733 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); 469 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask);
734 timer.startRepeating(2.0, BLINK_FROM_HERE); 470 timer.startRepeating(2.0, BLINK_FROM_HERE);
735 471
736 ASSERT(hasOneTimerTask());
737 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime + 2.0 472 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime + 2.0
738 473
739 // Simulate timer firing early. Next scheduled task to run at m_startTime + 4.0 474 // Simulate timer firing early. Next scheduled task to run at m_startTime + 4.0
740 advanceTimeBy(1.9); 475 m_platform.advanceClockSeconds(1.9);
741 runUntilIdleOrDeadlinePassed(gCurrentTimeSecs + 0.2); 476 runUntilDeadline(monotonicallyIncreasingTime() + 0.2);
742 477
743 advanceTimeBy(2.0); 478 m_platform.advanceClockSeconds(2.0);
744 runPendingTasks(); // Next scheduled task to run at m_startTime + 6.0 479 m_platform.runPendingTasks(); // Next scheduled task to run at m_startTime + 6.0
745 480
746 advanceTimeBy(2.1); 481 m_platform.advanceClockSeconds(2.1);
747 runPendingTasks(); // Next scheduled task to run at m_startTime + 8.0 482 m_platform.runPendingTasks(); // Next scheduled task to run at m_startTime + 8.0
748 483
749 advanceTimeBy(2.9); 484 m_platform.advanceClockSeconds(2.9);
750 runPendingTasks(); // Next scheduled task to run at m_startTime + 10.0 485 m_platform.runPendingTasks(); // Next scheduled task to run at m_startTime + 10.0
751 486
752 advanceTimeBy(3.1); 487 m_platform.advanceClockSeconds(3.1);
753 runPendingTasks(); // Next scheduled task to run at m_startTime + 14.0 (skip s a beat) 488 m_platform.runPendingTasks(); // Next scheduled task to run at m_startTime + 14.0 (skips a beat)
754 489
755 advanceTimeBy(4.0); 490 m_platform.advanceClockSeconds(4.0);
756 runPendingTasks(); // Next scheduled task to run at m_startTime + 18.0 (skip s a beat) 491 m_platform.runPendingTasks(); // Next scheduled task to run at m_startTime + 18.0 (skips a beat)
757 492
758 advanceTimeBy(10.0); // Next scheduled task to run at m_startTime + 28.0 (sk ips 5 beats) 493 m_platform.advanceClockSeconds(10.0); // Next scheduled task to run at m_sta rtTime + 28.0 (skips 5 beats)
759 runPendingTasks(); 494 m_platform.runPendingTasks();
760 495
761 runUntilIdleOrDeadlinePassed(m_startTime + 5.5);
762 EXPECT_THAT(m_nextFireTimes, ElementsAre( 496 EXPECT_THAT(m_nextFireTimes, ElementsAre(
763 m_startTime + 2.0, 497 m_startTime + 2.0,
764 m_startTime + 4.0, 498 m_startTime + 4.0,
765 m_startTime + 6.0, 499 m_startTime + 6.0,
766 m_startTime + 8.0, 500 m_startTime + 8.0,
767 m_startTime + 10.0, 501 m_startTime + 10.0,
768 m_startTime + 14.0, 502 m_startTime + 14.0,
769 m_startTime + 18.0, 503 m_startTime + 18.0,
770 m_startTime + 28.0)); 504 m_startTime + 28.0));
771 } 505 }
772 506
773 template <typename TimerFiredClass> 507 template <typename TimerFiredClass>
774 class TimerForTest : public TaskRunnerTimer<TimerFiredClass> { 508 class TimerForTest : public TaskRunnerTimer<TimerFiredClass> {
775 public: 509 public:
776 using TimerFiredFunction = typename TaskRunnerTimer<TimerFiredClass>::TimerF iredFunction; 510 using TimerFiredFunction = typename TaskRunnerTimer<TimerFiredClass>::TimerF iredFunction;
777 511
778 ~TimerForTest() override { } 512 ~TimerForTest() override { }
779 513
780 TimerForTest(WebTaskRunner* webTaskRunner, TimerFiredClass* timerFiredClass, TimerFiredFunction timerFiredFunction) 514 TimerForTest(WebTaskRunner* webTaskRunner, TimerFiredClass* timerFiredClass, TimerFiredFunction timerFiredFunction)
781 : TaskRunnerTimer<TimerFiredClass>(webTaskRunner, timerFiredClass, timer FiredFunction) 515 : TaskRunnerTimer<TimerFiredClass>(webTaskRunner, timerFiredClass, timer FiredFunction)
782 { 516 {
783 } 517 }
784 }; 518 };
785 519
786 TEST_F(TimerTest, UserSuppliedWebTaskRunner) 520 TEST_F(TimerTest, UserSuppliedWebTaskRunner)
787 { 521 {
788 std::priority_queue<DelayedTask> timerTasks; 522 scoped_refptr<scheduler::TaskQueue> taskRunner(m_platform.rendererScheduler( )->NewTimerTaskRunner("test"));
789 MockWebTaskRunner taskRunner(&timerTasks); 523 scheduler::WebTaskRunnerImpl webTaskRunner(taskRunner);
790 TimerForTest<TimerTest> timer(&taskRunner, this, &TimerTest::countingTask); 524 TimerForTest<TimerTest> timer(&webTaskRunner, this, &TimerTest::countingTask );
791 timer.startOneShot(0, BLINK_FROM_HERE); 525 timer.startOneShot(0, BLINK_FROM_HERE);
792 526
793 // Make sure the task was posted on taskRunner. 527 // Make sure the task was posted on taskRunner.
794 EXPECT_FALSE(timerTasks.empty()); 528 EXPECT_FALSE(taskRunner->IsEmpty());
795 } 529 }
796 530
797 531
798 } // namespace 532 } // namespace
799 } // namespace blink 533 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698