OLD | NEW |
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 "config.h" | 5 #include "config.h" |
6 #include "platform/Timer.h" | 6 #include "platform/Timer.h" |
7 | 7 |
8 #include "public/platform/Platform.h" | 8 #include "public/platform/Platform.h" |
9 #include "public/platform/WebScheduler.h" | 9 #include "public/platform/WebScheduler.h" |
10 #include "public/platform/WebThread.h" | 10 #include "public/platform/WebThread.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 { | 34 { |
35 m_task->run(); | 35 m_task->run(); |
36 } | 36 } |
37 | 37 |
38 private: | 38 private: |
39 OwnPtr<WebThread::Task> m_task; | 39 OwnPtr<WebThread::Task> m_task; |
40 }; | 40 }; |
41 | 41 |
42 class DelayedTask { | 42 class DelayedTask { |
43 public: | 43 public: |
44 DelayedTask(WebThread::Task* task, long long delayMs) | 44 DelayedTask(WebThread::Task* task, double delaySecs) |
45 : m_task(adoptRef(new RefCountedTaskContainer(task))) | 45 : m_task(adoptRef(new RefCountedTaskContainer(task))) |
46 , m_runTimeSecs(monotonicallyIncreasingTime() + 0.001 * static_cast<doub
le>(delayMs)) | 46 , m_runTimeSecs(monotonicallyIncreasingTime() + delaySecs) |
47 , m_delayMs(delayMs) { } | 47 , m_delaySecs(delaySecs) { } |
48 | 48 |
49 bool operator<(const DelayedTask& other) const | 49 bool operator<(const DelayedTask& other) const |
50 { | 50 { |
51 return m_runTimeSecs > other.m_runTimeSecs; | 51 return m_runTimeSecs > other.m_runTimeSecs; |
52 } | 52 } |
53 | 53 |
54 void run() const | 54 void run() const |
55 { | 55 { |
56 m_task->run(); | 56 m_task->run(); |
57 } | 57 } |
58 | 58 |
59 double runTimeSecs() const | 59 double runTimeSecs() const |
60 { | 60 { |
61 return m_runTimeSecs; | 61 return m_runTimeSecs; |
62 } | 62 } |
63 | 63 |
64 long long delayMs() const | 64 double delaySecs() const |
65 { | 65 { |
66 return m_delayMs; | 66 return m_delaySecs; |
67 } | 67 } |
68 | 68 |
69 private: | 69 private: |
70 RefPtr<RefCountedTaskContainer> m_task; | 70 RefPtr<RefCountedTaskContainer> m_task; |
71 double m_runTimeSecs; | 71 double m_runTimeSecs; |
72 long long m_delayMs; | 72 double m_delaySecs; |
73 }; | 73 }; |
74 | 74 |
75 class MockWebScheduler : public WebScheduler { | 75 class MockWebScheduler : public WebScheduler { |
76 public: | 76 public: |
77 MockWebScheduler() { } | 77 MockWebScheduler() { } |
78 ~MockWebScheduler() override { } | 78 ~MockWebScheduler() override { } |
79 | 79 |
80 bool shouldYieldForHighPriorityWork() override | 80 bool shouldYieldForHighPriorityWork() override |
81 { | 81 { |
82 return false; | 82 return false; |
(...skipping 13 matching lines...) Expand all Loading... |
96 } | 96 } |
97 | 97 |
98 void postIdleTaskAfterWakeup(const WebTraceLocation&, WebThread::IdleTask*)
override | 98 void postIdleTaskAfterWakeup(const WebTraceLocation&, WebThread::IdleTask*)
override |
99 { | 99 { |
100 } | 100 } |
101 | 101 |
102 void postLoadingTask(const WebTraceLocation&, WebThread::Task*) override | 102 void postLoadingTask(const WebTraceLocation&, WebThread::Task*) override |
103 { | 103 { |
104 } | 104 } |
105 | 105 |
106 void postTimerTask(const WebTraceLocation&, WebThread::Task* task, long long
delayMs) override | 106 void postTimerTask(const WebTraceLocation&, WebThread::Task* task, double de
laySecs) override |
107 { | 107 { |
108 m_timerTasks.push(DelayedTask(task, delayMs)); | 108 m_timerTasks.push(DelayedTask(task, delaySecs)); |
109 } | 109 } |
110 | 110 |
111 void postTimerTaskAt(const WebTraceLocation&, WebThread::Task* task, double
monotonicTime) override | 111 void postTimerTaskAt(const WebTraceLocation&, WebThread::Task* task, double
monotonicTime) override |
112 { | 112 { |
113 m_timerTasks.push(DelayedTask(task, (monotonicTime - monotonicallyIncrea
singTime()) * 1000)); | 113 m_timerTasks.push(DelayedTask(task, (monotonicTime - monotonicallyIncrea
singTime()) * 1000)); |
114 } | 114 } |
115 | 115 |
116 void runUntilIdle() | 116 void runUntilIdle() |
117 { | 117 { |
118 while (!m_timerTasks.empty()) { | 118 while (!m_timerTasks.empty()) { |
119 gCurrentTimeSecs = m_timerTasks.top().runTimeSecs(); | 119 gCurrentTimeSecs = m_timerTasks.top().runTimeSecs(); |
120 m_timerTasks.top().run(); | 120 m_timerTasks.top().run(); |
121 m_timerTasks.pop(); | 121 m_timerTasks.pop(); |
122 } | 122 } |
123 } | 123 } |
124 | 124 |
125 void runUntilIdleOrDeadlinePassed(double deadline) | 125 void runUntilIdleOrDeadlinePassed(double deadline) |
126 { | 126 { |
127 while (!m_timerTasks.empty()) { | 127 while (!m_timerTasks.empty()) { |
128 if (m_timerTasks.top().runTimeSecs() > deadline) { | 128 if (m_timerTasks.top().runTimeSecs() > deadline) { |
129 gCurrentTimeSecs = deadline; | 129 gCurrentTimeSecs = deadline; |
130 break; | 130 break; |
131 } | 131 } |
132 gCurrentTimeSecs = m_timerTasks.top().runTimeSecs(); | 132 gCurrentTimeSecs = m_timerTasks.top().runTimeSecs(); |
133 m_timerTasks.top().run(); | 133 m_timerTasks.top().run(); |
134 m_timerTasks.pop(); | 134 m_timerTasks.pop(); |
135 } | 135 } |
136 } | 136 } |
137 | 137 |
| 138 void runPendingTasks() |
| 139 { |
| 140 while (!m_timerTasks.empty() && m_timerTasks.top().runTimeSecs() <= gCur
rentTimeSecs) { |
| 141 m_timerTasks.top().run(); |
| 142 m_timerTasks.pop(); |
| 143 } |
| 144 } |
| 145 |
138 bool hasOneTimerTask() const | 146 bool hasOneTimerTask() const |
139 { | 147 { |
140 return m_timerTasks.size() == 1; | 148 return m_timerTasks.size() == 1; |
141 } | 149 } |
142 | 150 |
143 long nextTimerTaskDelayMillis() const | 151 double nextTimerTaskDelaySecs() const |
144 { | 152 { |
145 ASSERT(hasOneTimerTask()); | 153 ASSERT(hasOneTimerTask()); |
146 return m_timerTasks.top().delayMs(); | 154 return m_timerTasks.top().delaySecs(); |
147 } | 155 } |
148 | 156 |
149 private: | 157 private: |
150 std::priority_queue<DelayedTask> m_timerTasks; | 158 std::priority_queue<DelayedTask> m_timerTasks; |
151 }; | 159 }; |
152 | 160 |
153 class FakeWebThread : public WebThread { | 161 class FakeWebThread : public WebThread { |
154 public: | 162 public: |
155 FakeWebThread() : m_webScheduler(adoptPtr(new MockWebScheduler())) { } | 163 FakeWebThread() : m_webScheduler(adoptPtr(new MockWebScheduler())) { } |
156 ~FakeWebThread() override { } | 164 ~FakeWebThread() override { } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 { | 225 { |
218 static const unsigned char enabled[] = {0}; | 226 static const unsigned char enabled[] = {0}; |
219 return enabled; | 227 return enabled; |
220 } | 228 } |
221 | 229 |
222 void runUntilIdle() | 230 void runUntilIdle() |
223 { | 231 { |
224 mockScheduler()->runUntilIdle(); | 232 mockScheduler()->runUntilIdle(); |
225 } | 233 } |
226 | 234 |
| 235 void runPendingTasks() |
| 236 { |
| 237 mockScheduler()->runPendingTasks(); |
| 238 } |
| 239 |
227 void runUntilIdleOrDeadlinePassed(double deadline) | 240 void runUntilIdleOrDeadlinePassed(double deadline) |
228 { | 241 { |
229 mockScheduler()->runUntilIdleOrDeadlinePassed(deadline); | 242 mockScheduler()->runUntilIdleOrDeadlinePassed(deadline); |
230 } | 243 } |
231 | 244 |
232 bool hasOneTimerTask() const | 245 bool hasOneTimerTask() const |
233 { | 246 { |
234 return mockScheduler()->hasOneTimerTask(); | 247 return mockScheduler()->hasOneTimerTask(); |
235 } | 248 } |
236 | 249 |
237 long nextTimerTaskDelayMillis() const | 250 double nextTimerTaskDelaySecs() const |
238 { | 251 { |
239 return mockScheduler()->nextTimerTaskDelayMillis(); | 252 return mockScheduler()->nextTimerTaskDelaySecs(); |
240 } | 253 } |
241 | 254 |
242 private: | 255 private: |
243 MockWebScheduler* mockScheduler() const | 256 MockWebScheduler* mockScheduler() const |
244 { | 257 { |
245 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); | 258 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); |
246 } | 259 } |
247 | 260 |
248 OwnPtr<FakeWebThread> m_webThread; | 261 OwnPtr<FakeWebThread> m_webThread; |
249 }; | 262 }; |
(...skipping 15 matching lines...) Expand all Loading... |
265 void TearDown() override | 278 void TearDown() override |
266 { | 279 { |
267 Platform::initialize(m_oldPlatform); | 280 Platform::initialize(m_oldPlatform); |
268 } | 281 } |
269 | 282 |
270 void countingTask(Timer<TimerTest>*) | 283 void countingTask(Timer<TimerTest>*) |
271 { | 284 { |
272 m_runTimes.push_back(monotonicallyIncreasingTime()); | 285 m_runTimes.push_back(monotonicallyIncreasingTime()); |
273 } | 286 } |
274 | 287 |
| 288 void recordNextFireTimeTask(Timer<TimerTest>* timer) |
| 289 { |
| 290 m_nextFireTimes.push_back(monotonicallyIncreasingTime() + timer->nextFir
eInterval()); |
| 291 } |
| 292 |
275 void advanceTimeBy(double timeSecs) | 293 void advanceTimeBy(double timeSecs) |
276 { | 294 { |
277 gCurrentTimeSecs += timeSecs; | 295 gCurrentTimeSecs += timeSecs; |
278 } | 296 } |
279 | 297 |
280 void runUntilIdle() | 298 void runUntilIdle() |
281 { | 299 { |
282 m_platform->runUntilIdle(); | 300 m_platform->runUntilIdle(); |
283 } | 301 } |
284 | 302 |
| 303 void runPendingTasks() |
| 304 { |
| 305 m_platform->runPendingTasks(); |
| 306 } |
| 307 |
285 void runUntilIdleOrDeadlinePassed(double deadline) | 308 void runUntilIdleOrDeadlinePassed(double deadline) |
286 { | 309 { |
287 m_platform->runUntilIdleOrDeadlinePassed(deadline); | 310 m_platform->runUntilIdleOrDeadlinePassed(deadline); |
288 } | 311 } |
289 | 312 |
290 bool hasOneTimerTask() const | 313 bool hasOneTimerTask() const |
291 { | 314 { |
292 return m_platform->hasOneTimerTask(); | 315 return m_platform->hasOneTimerTask(); |
293 } | 316 } |
294 | 317 |
295 long nextTimerTaskDelayMillis() const | 318 double nextTimerTaskDelaySecs() const |
296 { | 319 { |
297 return m_platform->nextTimerTaskDelayMillis(); | 320 return m_platform->nextTimerTaskDelaySecs(); |
298 } | 321 } |
299 | 322 |
300 protected: | 323 protected: |
301 double m_startTime; | 324 double m_startTime; |
302 std::vector<double> m_runTimes; | 325 std::vector<double> m_runTimes; |
| 326 std::vector<double> m_nextFireTimes; |
303 | 327 |
304 private: | 328 private: |
305 OwnPtr<TimerTestPlatform> m_platform; | 329 OwnPtr<TimerTestPlatform> m_platform; |
306 Platform* m_oldPlatform; | 330 Platform* m_oldPlatform; |
307 }; | 331 }; |
308 | 332 |
309 TEST_F(TimerTest, StartOneShot_Zero) | 333 TEST_F(TimerTest, StartOneShot_Zero) |
310 { | 334 { |
311 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 335 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
312 timer.startOneShot(0, FROM_HERE); | 336 timer.startOneShot(0, FROM_HERE); |
313 | 337 |
314 ASSERT(hasOneTimerTask()); | 338 ASSERT(hasOneTimerTask()); |
315 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); | 339 EXPECT_EQ(0.0, nextTimerTaskDelaySecs()); |
316 | 340 |
317 runUntilIdle(); | 341 runUntilIdle(); |
318 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); | 342 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); |
319 } | 343 } |
320 | 344 |
321 TEST_F(TimerTest, StartOneShot_ZeroAndCancel) | 345 TEST_F(TimerTest, StartOneShot_ZeroAndCancel) |
322 { | 346 { |
323 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 347 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
324 timer.startOneShot(0, FROM_HERE); | 348 timer.startOneShot(0, FROM_HERE); |
325 | 349 |
326 ASSERT(hasOneTimerTask()); | 350 ASSERT(hasOneTimerTask()); |
327 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); | 351 EXPECT_EQ(0.0, nextTimerTaskDelaySecs()); |
328 | 352 |
329 timer.stop(); | 353 timer.stop(); |
330 | 354 |
331 runUntilIdle(); | 355 runUntilIdle(); |
332 EXPECT_TRUE(m_runTimes.empty()); | 356 EXPECT_TRUE(m_runTimes.empty()); |
333 } | 357 } |
334 | 358 |
335 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost) | 359 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost) |
336 { | 360 { |
337 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 361 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
338 timer.startOneShot(0, FROM_HERE); | 362 timer.startOneShot(0, FROM_HERE); |
339 | 363 |
340 ASSERT(hasOneTimerTask()); | 364 ASSERT(hasOneTimerTask()); |
341 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); | 365 EXPECT_EQ(0.0, nextTimerTaskDelaySecs()); |
342 | 366 |
343 timer.stop(); | 367 timer.stop(); |
344 | 368 |
345 runUntilIdle(); | 369 runUntilIdle(); |
346 EXPECT_TRUE(m_runTimes.empty()); | 370 EXPECT_TRUE(m_runTimes.empty()); |
347 | 371 |
348 timer.startOneShot(0, FROM_HERE); | 372 timer.startOneShot(0, FROM_HERE); |
349 | 373 |
350 ASSERT(hasOneTimerTask()); | 374 ASSERT(hasOneTimerTask()); |
351 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); | 375 EXPECT_EQ(0.0, nextTimerTaskDelaySecs()); |
352 | 376 |
353 runUntilIdle(); | 377 runUntilIdle(); |
354 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); | 378 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); |
355 } | 379 } |
356 | 380 |
357 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning) | 381 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning) |
358 { | 382 { |
359 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 383 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
360 timer.startOneShot(0, FROM_HERE); | 384 timer.startOneShot(0, FROM_HERE); |
361 | 385 |
362 ASSERT(hasOneTimerTask()); | 386 ASSERT(hasOneTimerTask()); |
363 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); | 387 EXPECT_EQ(0.0, nextTimerTaskDelaySecs()); |
364 | 388 |
365 runUntilIdle(); | 389 runUntilIdle(); |
366 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); | 390 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); |
367 | 391 |
368 timer.startOneShot(0, FROM_HERE); | 392 timer.startOneShot(0, FROM_HERE); |
369 | 393 |
370 ASSERT(hasOneTimerTask()); | 394 ASSERT(hasOneTimerTask()); |
371 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); | 395 EXPECT_EQ(0.0, nextTimerTaskDelaySecs()); |
372 | 396 |
373 runUntilIdle(); | 397 runUntilIdle(); |
374 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime)); | 398 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime)); |
375 } | 399 } |
376 | 400 |
377 TEST_F(TimerTest, StartOneShot_NonZero) | 401 TEST_F(TimerTest, StartOneShot_NonZero) |
378 { | 402 { |
379 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 403 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
380 timer.startOneShot(10.0, FROM_HERE); | 404 timer.startOneShot(10.0, FROM_HERE); |
381 | 405 |
382 ASSERT(hasOneTimerTask()); | 406 ASSERT(hasOneTimerTask()); |
383 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); | 407 EXPECT_EQ(10.0, nextTimerTaskDelaySecs()); |
384 | 408 |
385 runUntilIdle(); | 409 runUntilIdle(); |
386 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); | 410 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); |
387 } | 411 } |
388 | 412 |
389 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel) | 413 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel) |
390 { | 414 { |
391 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 415 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
392 timer.startOneShot(10, FROM_HERE); | 416 timer.startOneShot(10, FROM_HERE); |
393 | 417 |
394 ASSERT(hasOneTimerTask()); | 418 ASSERT(hasOneTimerTask()); |
395 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); | 419 EXPECT_EQ(10.0, nextTimerTaskDelaySecs()); |
396 | 420 |
397 timer.stop(); | 421 timer.stop(); |
398 | 422 |
399 runUntilIdle(); | 423 runUntilIdle(); |
400 EXPECT_TRUE(m_runTimes.empty()); | 424 EXPECT_TRUE(m_runTimes.empty()); |
401 } | 425 } |
402 | 426 |
403 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost) | 427 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost) |
404 { | 428 { |
405 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 429 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
406 timer.startOneShot(10, FROM_HERE); | 430 timer.startOneShot(10, FROM_HERE); |
407 | 431 |
408 ASSERT(hasOneTimerTask()); | 432 ASSERT(hasOneTimerTask()); |
409 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); | 433 EXPECT_EQ(10.0, nextTimerTaskDelaySecs()); |
410 | 434 |
411 timer.stop(); | 435 timer.stop(); |
412 | 436 |
413 runUntilIdle(); | 437 runUntilIdle(); |
414 EXPECT_TRUE(m_runTimes.empty()); | 438 EXPECT_TRUE(m_runTimes.empty()); |
415 | 439 |
416 double secondPostTime = monotonicallyIncreasingTime(); | 440 double secondPostTime = monotonicallyIncreasingTime(); |
417 timer.startOneShot(10, FROM_HERE); | 441 timer.startOneShot(10, FROM_HERE); |
418 | 442 |
419 ASSERT(hasOneTimerTask()); | 443 ASSERT(hasOneTimerTask()); |
420 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); | 444 EXPECT_EQ(10.0, nextTimerTaskDelaySecs()); |
421 | 445 |
422 runUntilIdle(); | 446 runUntilIdle(); |
423 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); | 447 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); |
424 } | 448 } |
425 | 449 |
426 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) | 450 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) |
427 { | 451 { |
428 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 452 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
429 timer.startOneShot(10, FROM_HERE); | 453 timer.startOneShot(10, FROM_HERE); |
430 | 454 |
431 ASSERT(hasOneTimerTask()); | 455 ASSERT(hasOneTimerTask()); |
432 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); | 456 EXPECT_EQ(10.0, nextTimerTaskDelaySecs()); |
433 | 457 |
434 runUntilIdle(); | 458 runUntilIdle(); |
435 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); | 459 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); |
436 | 460 |
437 timer.startOneShot(20, FROM_HERE); | 461 timer.startOneShot(20, FROM_HERE); |
438 | 462 |
439 ASSERT(hasOneTimerTask()); | 463 ASSERT(hasOneTimerTask()); |
440 EXPECT_EQ(20000ll, nextTimerTaskDelayMillis()); | 464 EXPECT_EQ(20.0, nextTimerTaskDelaySecs()); |
441 | 465 |
442 runUntilIdle(); | 466 runUntilIdle(); |
443 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0))
; | 467 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0))
; |
444 } | 468 } |
445 | 469 |
446 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing) | 470 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing) |
447 { | 471 { |
448 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 472 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
449 timer.startOneShot(10, FROM_HERE); | 473 timer.startOneShot(10, FROM_HERE); |
450 timer.startOneShot(10, FROM_HERE); | 474 timer.startOneShot(10, FROM_HERE); |
451 | 475 |
452 ASSERT(hasOneTimerTask()); | 476 ASSERT(hasOneTimerTask()); |
453 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); | 477 EXPECT_EQ(10.0, nextTimerTaskDelaySecs()); |
454 | 478 |
455 runUntilIdle(); | 479 runUntilIdle(); |
456 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); | 480 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); |
457 } | 481 } |
458 | 482 |
459 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOriginalTask) | 483 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOriginalTask) |
460 { | 484 { |
461 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 485 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
462 timer.startOneShot(10, FROM_HERE); | 486 timer.startOneShot(10, FROM_HERE); |
463 timer.startOneShot(0, FROM_HERE); | 487 timer.startOneShot(0, FROM_HERE); |
(...skipping 11 matching lines...) Expand all Loading... |
475 runUntilIdle(); | 499 runUntilIdle(); |
476 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); | 500 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); |
477 } | 501 } |
478 | 502 |
479 TEST_F(TimerTest, StartRepeatingTask) | 503 TEST_F(TimerTest, StartRepeatingTask) |
480 { | 504 { |
481 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 505 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
482 timer.startRepeating(1.0, FROM_HERE); | 506 timer.startRepeating(1.0, FROM_HERE); |
483 | 507 |
484 ASSERT(hasOneTimerTask()); | 508 ASSERT(hasOneTimerTask()); |
485 EXPECT_EQ(1000ll, nextTimerTaskDelayMillis()); | 509 EXPECT_EQ(1.0, nextTimerTaskDelaySecs()); |
486 | 510 |
487 runUntilIdleOrDeadlinePassed(m_startTime + 5.5); | 511 runUntilIdleOrDeadlinePassed(m_startTime + 5.5); |
488 EXPECT_THAT(m_runTimes, ElementsAre( | 512 EXPECT_THAT(m_runTimes, ElementsAre( |
489 m_startTime + 1.0, m_startTime + 2.0, m_startTime + 3.0, m_startTime + 4
.0, m_startTime + 5.0)); | 513 m_startTime + 1.0, m_startTime + 2.0, m_startTime + 3.0, m_startTime + 4
.0, m_startTime + 5.0)); |
490 } | 514 } |
491 | 515 |
492 TEST_F(TimerTest, StartRepeatingTask_ThenCancel) | 516 TEST_F(TimerTest, StartRepeatingTask_ThenCancel) |
493 { | 517 { |
494 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 518 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
495 timer.startRepeating(1.0, FROM_HERE); | 519 timer.startRepeating(1.0, FROM_HERE); |
496 | 520 |
497 ASSERT(hasOneTimerTask()); | 521 ASSERT(hasOneTimerTask()); |
498 EXPECT_EQ(1000ll, nextTimerTaskDelayMillis()); | 522 EXPECT_EQ(1.0, nextTimerTaskDelaySecs()); |
499 | 523 |
500 runUntilIdleOrDeadlinePassed(m_startTime + 2.5); | 524 runUntilIdleOrDeadlinePassed(m_startTime + 2.5); |
501 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); | 525 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); |
502 | 526 |
503 timer.stop(); | 527 timer.stop(); |
504 runUntilIdle(); | 528 runUntilIdle(); |
505 | 529 |
506 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); | 530 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); |
507 } | 531 } |
508 | 532 |
509 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot) | 533 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot) |
510 { | 534 { |
511 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 535 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
512 timer.startRepeating(1.0, FROM_HERE); | 536 timer.startRepeating(1.0, FROM_HERE); |
513 | 537 |
514 ASSERT(hasOneTimerTask()); | 538 ASSERT(hasOneTimerTask()); |
515 EXPECT_EQ(1000ll, nextTimerTaskDelayMillis()); | 539 EXPECT_EQ(1.0, nextTimerTaskDelaySecs()); |
516 | 540 |
517 runUntilIdleOrDeadlinePassed(m_startTime + 2.5); | 541 runUntilIdleOrDeadlinePassed(m_startTime + 2.5); |
518 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); | 542 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); |
519 | 543 |
520 timer.startOneShot(0, FROM_HERE); | 544 timer.startOneShot(0, FROM_HERE); |
521 runUntilIdle(); | 545 runUntilIdle(); |
522 | 546 |
523 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, m_
startTime + 2.5)); | 547 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, m_
startTime + 2.5)); |
524 } | 548 } |
525 | 549 |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); | 753 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); |
730 | 754 |
731 timer.setAlignedFireTime(m_startTime); | 755 timer.setAlignedFireTime(m_startTime); |
732 timer.didChangeAlignmentInterval(monotonicallyIncreasingTime()); | 756 timer.didChangeAlignmentInterval(monotonicallyIncreasingTime()); |
733 | 757 |
734 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); | 758 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); |
735 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); | 759 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); |
736 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); | 760 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); |
737 } | 761 } |
738 | 762 |
| 763 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) |
| 764 { |
| 765 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); |
| 766 timer.startRepeating(2.0, FROM_HERE); |
| 767 |
| 768 ASSERT(hasOneTimerTask()); |
| 769 recordNextFireTimeTask(&timer); // Scheduled to run at m_startTime + 2.0 |
| 770 |
| 771 advanceTimeBy(2.0); |
| 772 runPendingTasks(); // Scheduled to run at m_startTime + 4.0 |
| 773 |
| 774 advanceTimeBy(2.1); |
| 775 runPendingTasks(); // Scheduled to run at m_startTime + 6.0 |
| 776 |
| 777 advanceTimeBy(2.9); |
| 778 runPendingTasks(); // Scheduled to run at m_startTime + 8.0 |
| 779 |
| 780 advanceTimeBy(3.1); |
| 781 runPendingTasks(); // Scheduled to run at m_startTime + 12.0 (skips a beat) |
| 782 |
| 783 advanceTimeBy(4.0); |
| 784 runPendingTasks(); // Scheduled to run at m_startTime + 16.0 (skips a beat) |
| 785 |
| 786 advanceTimeBy(10.0); // Scheduled to run at m_startTime + 22.0 (skips a few
beats) |
| 787 runPendingTasks(); |
| 788 |
| 789 runUntilIdleOrDeadlinePassed(m_startTime + 5.5); |
| 790 EXPECT_THAT(m_nextFireTimes, ElementsAre( |
| 791 m_startTime + 2.0, |
| 792 m_startTime + 4.0, |
| 793 m_startTime + 6.0, |
| 794 m_startTime + 8.0, |
| 795 m_startTime + 12.0, |
| 796 m_startTime + 16.0, |
| 797 m_startTime + 26.0)); |
| 798 } |
739 | 799 |
740 } // namespace | 800 } // namespace |
741 } // namespace blink | 801 } // namespace blink |
OLD | NEW |