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

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

Issue 1373503002: Fix the drift in repeating timers (try #2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Try to fix compile Created 5 years, 2 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 "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
34 { 34 {
35 m_task->run(); 35 m_task->run();
36 } 36 }
37 37
38 private: 38 private:
39 OwnPtr<WebTaskRunner::Task> m_task; 39 OwnPtr<WebTaskRunner::Task> m_task;
40 }; 40 };
41 41
42 class DelayedTask { 42 class DelayedTask {
43 public: 43 public:
44 DelayedTask(WebTaskRunner::Task* task, long long delayMs) 44 DelayedTask(WebTaskRunner::Task* task, double delaySecs)
esprehn 2015/09/28 23:34:43 delaySeconds
alex clarke (OOO till 29th) 2015/09/29 10:58:36 Done.
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) { }
esprehn 2015/09/28 23:34:43 m_delaySeconds
alex clarke (OOO till 29th) 2015/09/29 10:58:36 Done.
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
esprehn 2015/09/28 23:34:43 ditto, don't abbreviate
alex clarke (OOO till 29th) 2015/09/29 10:58:36 Done.
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;
esprehn 2015/09/28 23:34:43 This one should be fixed too
alex clarke (OOO till 29th) 2015/09/29 10:58:36 Done.
72 long long m_delayMs; 72 double m_delaySecs;
73 }; 73 };
74 74
75 class MockWebTaskRunner : public WebTaskRunner { 75 class MockWebTaskRunner : public WebTaskRunner {
76 public: 76 public:
77 explicit MockWebTaskRunner(std::priority_queue<DelayedTask>* timerTasks) : m _timerTasks(timerTasks) { } 77 explicit MockWebTaskRunner(std::priority_queue<DelayedTask>* timerTasks) : m _timerTasks(timerTasks) { }
78 ~MockWebTaskRunner() override { } 78 ~MockWebTaskRunner() override { }
79 79
80 virtual void postTask(const WebTraceLocation&, Task* task) 80 virtual void postTask(const WebTraceLocation&, Task* task)
81 { 81 {
82 m_timerTasks->push(DelayedTask(task, 0)); 82 m_timerTasks->push(DelayedTask(task, 0));
83 } 83 }
84 84
85 void postDelayedTask(const WebTraceLocation&, Task* task, long long delayMs) override 85 void postDelayedTask(const WebTraceLocation&, Task* task, double delayMs) ov erride
86 { 86 {
87 m_timerTasks->push(DelayedTask(task, delayMs)); 87 m_timerTasks->push(DelayedTask(task, delayMs * 0.001));
88 } 88 }
89 89
90 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED 90 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED
91 }; 91 };
92 92
93 class MockWebScheduler : public WebScheduler { 93 class MockWebScheduler : public WebScheduler {
94 public: 94 public:
95 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { } 95 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { }
96 ~MockWebScheduler() override { } 96 ~MockWebScheduler() override { }
97 97
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 if (m_timerTasks.top().runTimeSecs() > deadline) { 148 if (m_timerTasks.top().runTimeSecs() > deadline) {
149 gCurrentTimeSecs = deadline; 149 gCurrentTimeSecs = deadline;
150 break; 150 break;
151 } 151 }
152 gCurrentTimeSecs = m_timerTasks.top().runTimeSecs(); 152 gCurrentTimeSecs = m_timerTasks.top().runTimeSecs();
153 m_timerTasks.top().run(); 153 m_timerTasks.top().run();
154 m_timerTasks.pop(); 154 m_timerTasks.pop();
155 } 155 }
156 } 156 }
157 157
158 void runPendingTasks()
159 {
160 while (!m_timerTasks.empty() && m_timerTasks.top().runTimeSecs() <= gCur rentTimeSecs) {
161 m_timerTasks.top().run();
162 m_timerTasks.pop();
163 }
164 }
165
158 bool hasOneTimerTask() const 166 bool hasOneTimerTask() const
159 { 167 {
160 return m_timerTasks.size() == 1; 168 return m_timerTasks.size() == 1;
161 } 169 }
162 170
163 long nextTimerTaskDelayMillis() const 171 double nextTimerTaskDelaySecs() const
164 { 172 {
165 ASSERT(hasOneTimerTask()); 173 ASSERT(hasOneTimerTask());
166 return m_timerTasks.top().delayMs(); 174 return m_timerTasks.top().delaySecs();
167 } 175 }
168 176
169 private: 177 private:
170 std::priority_queue<DelayedTask> m_timerTasks; 178 std::priority_queue<DelayedTask> m_timerTasks;
171 MockWebTaskRunner m_timerWebTaskRunner; 179 MockWebTaskRunner m_timerWebTaskRunner;
172 }; 180 };
173 181
174 class FakeWebThread : public WebThread { 182 class FakeWebThread : public WebThread {
175 public: 183 public:
176 FakeWebThread() : m_webScheduler(adoptPtr(new MockWebScheduler())) { } 184 FakeWebThread() : m_webScheduler(adoptPtr(new MockWebScheduler())) { }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 { 241 {
234 static const unsigned char enabled[] = {0}; 242 static const unsigned char enabled[] = {0};
235 return enabled; 243 return enabled;
236 } 244 }
237 245
238 void runUntilIdle() 246 void runUntilIdle()
239 { 247 {
240 mockScheduler()->runUntilIdle(); 248 mockScheduler()->runUntilIdle();
241 } 249 }
242 250
251 void runPendingTasks()
252 {
253 mockScheduler()->runPendingTasks();
254 }
255
243 void runUntilIdleOrDeadlinePassed(double deadline) 256 void runUntilIdleOrDeadlinePassed(double deadline)
244 { 257 {
245 mockScheduler()->runUntilIdleOrDeadlinePassed(deadline); 258 mockScheduler()->runUntilIdleOrDeadlinePassed(deadline);
246 } 259 }
247 260
248 bool hasOneTimerTask() const 261 bool hasOneTimerTask() const
249 { 262 {
250 return mockScheduler()->hasOneTimerTask(); 263 return mockScheduler()->hasOneTimerTask();
251 } 264 }
252 265
253 long nextTimerTaskDelayMillis() const 266 double nextTimerTaskDelaySecs() const
254 { 267 {
255 return mockScheduler()->nextTimerTaskDelayMillis(); 268 return mockScheduler()->nextTimerTaskDelaySecs();
256 } 269 }
257 270
258 private: 271 private:
259 MockWebScheduler* mockScheduler() const 272 MockWebScheduler* mockScheduler() const
260 { 273 {
261 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); 274 return static_cast<MockWebScheduler*>(m_webThread->scheduler());
262 } 275 }
263 276
264 OwnPtr<FakeWebThread> m_webThread; 277 OwnPtr<FakeWebThread> m_webThread;
265 }; 278 };
(...skipping 15 matching lines...) Expand all
281 void TearDown() override 294 void TearDown() override
282 { 295 {
283 Platform::initialize(m_oldPlatform); 296 Platform::initialize(m_oldPlatform);
284 } 297 }
285 298
286 void countingTask(Timer<TimerTest>*) 299 void countingTask(Timer<TimerTest>*)
287 { 300 {
288 m_runTimes.push_back(monotonicallyIncreasingTime()); 301 m_runTimes.push_back(monotonicallyIncreasingTime());
289 } 302 }
290 303
304 void recordNextFireTimeTask(Timer<TimerTest>* timer)
305 {
306 m_nextFireTimes.push_back(monotonicallyIncreasingTime() + timer->nextFir eInterval());
307 }
308
291 void advanceTimeBy(double timeSecs) 309 void advanceTimeBy(double timeSecs)
292 { 310 {
293 gCurrentTimeSecs += timeSecs; 311 gCurrentTimeSecs += timeSecs;
294 } 312 }
295 313
296 void runUntilIdle() 314 void runUntilIdle()
297 { 315 {
298 m_platform->runUntilIdle(); 316 m_platform->runUntilIdle();
299 } 317 }
300 318
319 void runPendingTasks()
320 {
321 m_platform->runPendingTasks();
322 }
323
301 void runUntilIdleOrDeadlinePassed(double deadline) 324 void runUntilIdleOrDeadlinePassed(double deadline)
302 { 325 {
303 m_platform->runUntilIdleOrDeadlinePassed(deadline); 326 m_platform->runUntilIdleOrDeadlinePassed(deadline);
304 } 327 }
305 328
306 bool hasOneTimerTask() const 329 bool hasOneTimerTask() const
307 { 330 {
308 return m_platform->hasOneTimerTask(); 331 return m_platform->hasOneTimerTask();
309 } 332 }
310 333
311 long nextTimerTaskDelayMillis() const 334 double nextTimerTaskDelaySecs() const
312 { 335 {
313 return m_platform->nextTimerTaskDelayMillis(); 336 return m_platform->nextTimerTaskDelaySecs();
314 } 337 }
315 338
316 protected: 339 protected:
317 double m_startTime; 340 double m_startTime;
318 std::vector<double> m_runTimes; 341 std::vector<double> m_runTimes;
342 std::vector<double> m_nextFireTimes;
esprehn 2015/09/28 23:34:43 These should be WTF::Vector, we don't use std::vec
alex clarke (OOO till 29th) 2015/09/29 10:58:36 I've added a todo.
319 343
320 private: 344 private:
321 OwnPtr<TimerTestPlatform> m_platform; 345 OwnPtr<TimerTestPlatform> m_platform;
322 Platform* m_oldPlatform; 346 Platform* m_oldPlatform;
323 }; 347 };
324 348
325 TEST_F(TimerTest, StartOneShot_Zero) 349 TEST_F(TimerTest, StartOneShot_Zero)
326 { 350 {
327 Timer<TimerTest> timer(this, &TimerTest::countingTask); 351 Timer<TimerTest> timer(this, &TimerTest::countingTask);
328 timer.startOneShot(0, FROM_HERE); 352 timer.startOneShot(0, FROM_HERE);
329 353
330 ASSERT(hasOneTimerTask()); 354 ASSERT(hasOneTimerTask());
331 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); 355 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs());
332 356
333 runUntilIdle(); 357 runUntilIdle();
334 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 358 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
335 } 359 }
336 360
337 TEST_F(TimerTest, StartOneShot_ZeroAndCancel) 361 TEST_F(TimerTest, StartOneShot_ZeroAndCancel)
338 { 362 {
339 Timer<TimerTest> timer(this, &TimerTest::countingTask); 363 Timer<TimerTest> timer(this, &TimerTest::countingTask);
340 timer.startOneShot(0, FROM_HERE); 364 timer.startOneShot(0, FROM_HERE);
341 365
342 ASSERT(hasOneTimerTask()); 366 ASSERT(hasOneTimerTask());
343 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); 367 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs());
344 368
345 timer.stop(); 369 timer.stop();
346 370
347 runUntilIdle(); 371 runUntilIdle();
348 EXPECT_TRUE(m_runTimes.empty()); 372 EXPECT_TRUE(m_runTimes.empty());
349 } 373 }
350 374
351 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost) 375 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost)
352 { 376 {
353 Timer<TimerTest> timer(this, &TimerTest::countingTask); 377 Timer<TimerTest> timer(this, &TimerTest::countingTask);
354 timer.startOneShot(0, FROM_HERE); 378 timer.startOneShot(0, FROM_HERE);
355 379
356 ASSERT(hasOneTimerTask()); 380 ASSERT(hasOneTimerTask());
357 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); 381 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs());
358 382
359 timer.stop(); 383 timer.stop();
360 384
361 runUntilIdle(); 385 runUntilIdle();
362 EXPECT_TRUE(m_runTimes.empty()); 386 EXPECT_TRUE(m_runTimes.empty());
363 387
364 timer.startOneShot(0, FROM_HERE); 388 timer.startOneShot(0, FROM_HERE);
365 389
366 ASSERT(hasOneTimerTask()); 390 ASSERT(hasOneTimerTask());
367 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); 391 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs());
368 392
369 runUntilIdle(); 393 runUntilIdle();
370 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 394 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
371 } 395 }
372 396
373 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning) 397 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning)
374 { 398 {
375 Timer<TimerTest> timer(this, &TimerTest::countingTask); 399 Timer<TimerTest> timer(this, &TimerTest::countingTask);
376 timer.startOneShot(0, FROM_HERE); 400 timer.startOneShot(0, FROM_HERE);
377 401
378 ASSERT(hasOneTimerTask()); 402 ASSERT(hasOneTimerTask());
379 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); 403 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs());
380 404
381 runUntilIdle(); 405 runUntilIdle();
382 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 406 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
383 407
384 timer.startOneShot(0, FROM_HERE); 408 timer.startOneShot(0, FROM_HERE);
385 409
386 ASSERT(hasOneTimerTask()); 410 ASSERT(hasOneTimerTask());
387 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); 411 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs());
388 412
389 runUntilIdle(); 413 runUntilIdle();
390 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime)); 414 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime));
391 } 415 }
392 416
393 TEST_F(TimerTest, StartOneShot_NonZero) 417 TEST_F(TimerTest, StartOneShot_NonZero)
394 { 418 {
395 Timer<TimerTest> timer(this, &TimerTest::countingTask); 419 Timer<TimerTest> timer(this, &TimerTest::countingTask);
396 timer.startOneShot(10.0, FROM_HERE); 420 timer.startOneShot(10.0, FROM_HERE);
397 421
398 ASSERT(hasOneTimerTask()); 422 ASSERT(hasOneTimerTask());
399 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); 423 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs());
400 424
401 runUntilIdle(); 425 runUntilIdle();
402 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 426 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
403 } 427 }
404 428
405 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel) 429 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel)
406 { 430 {
407 Timer<TimerTest> timer(this, &TimerTest::countingTask); 431 Timer<TimerTest> timer(this, &TimerTest::countingTask);
408 timer.startOneShot(10, FROM_HERE); 432 timer.startOneShot(10, FROM_HERE);
409 433
410 ASSERT(hasOneTimerTask()); 434 ASSERT(hasOneTimerTask());
411 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); 435 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs());
412 436
413 timer.stop(); 437 timer.stop();
414 438
415 runUntilIdle(); 439 runUntilIdle();
416 EXPECT_TRUE(m_runTimes.empty()); 440 EXPECT_TRUE(m_runTimes.empty());
417 } 441 }
418 442
419 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost) 443 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost)
420 { 444 {
421 Timer<TimerTest> timer(this, &TimerTest::countingTask); 445 Timer<TimerTest> timer(this, &TimerTest::countingTask);
422 timer.startOneShot(10, FROM_HERE); 446 timer.startOneShot(10, FROM_HERE);
423 447
424 ASSERT(hasOneTimerTask()); 448 ASSERT(hasOneTimerTask());
425 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); 449 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs());
426 450
427 timer.stop(); 451 timer.stop();
428 452
429 runUntilIdle(); 453 runUntilIdle();
430 EXPECT_TRUE(m_runTimes.empty()); 454 EXPECT_TRUE(m_runTimes.empty());
431 455
432 double secondPostTime = monotonicallyIncreasingTime(); 456 double secondPostTime = monotonicallyIncreasingTime();
433 timer.startOneShot(10, FROM_HERE); 457 timer.startOneShot(10, FROM_HERE);
434 458
435 ASSERT(hasOneTimerTask()); 459 ASSERT(hasOneTimerTask());
436 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); 460 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs());
437 461
438 runUntilIdle(); 462 runUntilIdle();
439 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); 463 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0));
440 } 464 }
441 465
442 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) 466 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning)
443 { 467 {
444 Timer<TimerTest> timer(this, &TimerTest::countingTask); 468 Timer<TimerTest> timer(this, &TimerTest::countingTask);
445 timer.startOneShot(10, FROM_HERE); 469 timer.startOneShot(10, FROM_HERE);
446 470
447 ASSERT(hasOneTimerTask()); 471 ASSERT(hasOneTimerTask());
448 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); 472 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs());
449 473
450 runUntilIdle(); 474 runUntilIdle();
451 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 475 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
452 476
453 timer.startOneShot(20, FROM_HERE); 477 timer.startOneShot(20, FROM_HERE);
454 478
455 ASSERT(hasOneTimerTask()); 479 ASSERT(hasOneTimerTask());
456 EXPECT_EQ(20000ll, nextTimerTaskDelayMillis()); 480 EXPECT_FLOAT_EQ(20.0, nextTimerTaskDelaySecs());
457 481
458 runUntilIdle(); 482 runUntilIdle();
459 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0)) ; 483 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0)) ;
460 } 484 }
461 485
462 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing) 486 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing)
463 { 487 {
464 Timer<TimerTest> timer(this, &TimerTest::countingTask); 488 Timer<TimerTest> timer(this, &TimerTest::countingTask);
465 timer.startOneShot(10, FROM_HERE); 489 timer.startOneShot(10, FROM_HERE);
466 timer.startOneShot(10, FROM_HERE); 490 timer.startOneShot(10, FROM_HERE);
467 491
468 ASSERT(hasOneTimerTask()); 492 ASSERT(hasOneTimerTask());
469 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); 493 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs());
470 494
471 runUntilIdle(); 495 runUntilIdle();
472 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 496 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
473 } 497 }
474 498
475 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOriginalTask) 499 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOriginalTask)
476 { 500 {
477 Timer<TimerTest> timer(this, &TimerTest::countingTask); 501 Timer<TimerTest> timer(this, &TimerTest::countingTask);
478 timer.startOneShot(10, FROM_HERE); 502 timer.startOneShot(10, FROM_HERE);
479 timer.startOneShot(0, FROM_HERE); 503 timer.startOneShot(0, FROM_HERE);
(...skipping 11 matching lines...) Expand all
491 runUntilIdle(); 515 runUntilIdle();
492 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 516 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
493 } 517 }
494 518
495 TEST_F(TimerTest, StartRepeatingTask) 519 TEST_F(TimerTest, StartRepeatingTask)
496 { 520 {
497 Timer<TimerTest> timer(this, &TimerTest::countingTask); 521 Timer<TimerTest> timer(this, &TimerTest::countingTask);
498 timer.startRepeating(1.0, FROM_HERE); 522 timer.startRepeating(1.0, FROM_HERE);
499 523
500 ASSERT(hasOneTimerTask()); 524 ASSERT(hasOneTimerTask());
501 EXPECT_EQ(1000ll, nextTimerTaskDelayMillis()); 525 EXPECT_FLOAT_EQ(1.0, nextTimerTaskDelaySecs());
502 526
503 runUntilIdleOrDeadlinePassed(m_startTime + 5.5); 527 runUntilIdleOrDeadlinePassed(m_startTime + 5.5);
504 EXPECT_THAT(m_runTimes, ElementsAre( 528 EXPECT_THAT(m_runTimes, ElementsAre(
505 m_startTime + 1.0, m_startTime + 2.0, m_startTime + 3.0, m_startTime + 4 .0, m_startTime + 5.0)); 529 m_startTime + 1.0, m_startTime + 2.0, m_startTime + 3.0, m_startTime + 4 .0, m_startTime + 5.0));
506 } 530 }
507 531
508 TEST_F(TimerTest, StartRepeatingTask_ThenCancel) 532 TEST_F(TimerTest, StartRepeatingTask_ThenCancel)
509 { 533 {
510 Timer<TimerTest> timer(this, &TimerTest::countingTask); 534 Timer<TimerTest> timer(this, &TimerTest::countingTask);
511 timer.startRepeating(1.0, FROM_HERE); 535 timer.startRepeating(1.0, FROM_HERE);
512 536
513 ASSERT(hasOneTimerTask()); 537 ASSERT(hasOneTimerTask());
514 EXPECT_EQ(1000ll, nextTimerTaskDelayMillis()); 538 EXPECT_FLOAT_EQ(1.0, nextTimerTaskDelaySecs());
515 539
516 runUntilIdleOrDeadlinePassed(m_startTime + 2.5); 540 runUntilIdleOrDeadlinePassed(m_startTime + 2.5);
517 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 541 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
518 542
519 timer.stop(); 543 timer.stop();
520 runUntilIdle(); 544 runUntilIdle();
521 545
522 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 546 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
523 } 547 }
524 548
525 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot) 549 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot)
526 { 550 {
527 Timer<TimerTest> timer(this, &TimerTest::countingTask); 551 Timer<TimerTest> timer(this, &TimerTest::countingTask);
528 timer.startRepeating(1.0, FROM_HERE); 552 timer.startRepeating(1.0, FROM_HERE);
529 553
530 ASSERT(hasOneTimerTask()); 554 ASSERT(hasOneTimerTask());
531 EXPECT_EQ(1000ll, nextTimerTaskDelayMillis()); 555 EXPECT_FLOAT_EQ(1.0, nextTimerTaskDelaySecs());
532 556
533 runUntilIdleOrDeadlinePassed(m_startTime + 2.5); 557 runUntilIdleOrDeadlinePassed(m_startTime + 2.5);
534 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 558 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
535 559
536 timer.startOneShot(0, FROM_HERE); 560 timer.startOneShot(0, FROM_HERE);
537 runUntilIdle(); 561 runUntilIdle();
538 562
539 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, m_ startTime + 2.5)); 563 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, m_ startTime + 2.5));
540 } 564 }
541 565
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
745 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); 769 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime());
746 770
747 timer.setAlignedFireTime(m_startTime); 771 timer.setAlignedFireTime(m_startTime);
748 timer.didChangeAlignmentInterval(monotonicallyIncreasingTime()); 772 timer.didChangeAlignmentInterval(monotonicallyIncreasingTime());
749 773
750 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); 774 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval());
751 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); 775 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval());
752 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); 776 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime());
753 } 777 }
754 778
779 TEST_F(TimerTest, RepeatingTimerDoesNotDrift)
780 {
781 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask);
782 timer.startRepeating(2.0, FROM_HERE);
783
784 ASSERT(hasOneTimerTask());
785 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime + 2.0
786
787 // Simulate timer firing early. Next scheduled task to run at m_startTime + 4.0
788 advanceTimeBy(1.9);
789 runUntilIdleOrDeadlinePassed(gCurrentTimeSecs + 0.2);
790
791 advanceTimeBy(2.0);
792 runPendingTasks(); // Next scheduled task to run at m_startTime + 6.0
793
794 advanceTimeBy(2.1);
795 runPendingTasks(); // Next scheduled task to run at m_startTime + 8.0
796
797 advanceTimeBy(2.9);
798 runPendingTasks(); // Next scheduled task to run at m_startTime + 10.0
799
800 advanceTimeBy(3.1);
801 runPendingTasks(); // Next scheduled task to run at m_startTime + 14.0 (skip s a beat)
802
803 advanceTimeBy(4.0);
804 runPendingTasks(); // Next scheduled task to run at m_startTime + 18.0 (skip s a beat)
805
806 advanceTimeBy(10.0); // Next scheduled task to run at m_startTime + 28.0 (sk ips 5 beats)
807 runPendingTasks();
808
809 runUntilIdleOrDeadlinePassed(m_startTime + 5.5);
810 EXPECT_THAT(m_nextFireTimes, ElementsAre(
811 m_startTime + 2.0,
812 m_startTime + 4.0,
813 m_startTime + 6.0,
814 m_startTime + 8.0,
815 m_startTime + 10.0,
816 m_startTime + 14.0,
817 m_startTime + 18.0,
818 m_startTime + 28.0));
819 }
755 820
756 } // namespace 821 } // namespace
757 } // namespace blink 822 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698