| 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 "platform/Timer.h" | 5 #include "platform/Timer.h" |
| 6 | 6 |
| 7 #include "platform/scheduler/base/task_queue_impl.h" | 7 #include "platform/scheduler/base/task_queue_impl.h" |
| 8 #include "platform/scheduler/child/web_task_runner_impl.h" | 8 #include "platform/scheduler/child/web_task_runner_impl.h" |
| 9 #include "platform/scheduler/renderer/renderer_scheduler_impl.h" | 9 #include "platform/scheduler/renderer/renderer_scheduler_impl.h" |
| 10 #include "platform/testing/TestingPlatformSupport.h" | 10 #include "platform/testing/TestingPlatformSupport.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 void runUntilDeadline(double deadline) { | 45 void runUntilDeadline(double deadline) { |
| 46 double period = deadline - monotonicallyIncreasingTime(); | 46 double period = deadline - monotonicallyIncreasingTime(); |
| 47 EXPECT_GE(period, 0.0); | 47 EXPECT_GE(period, 0.0); |
| 48 m_platform.runForPeriodSeconds(period); | 48 m_platform.runForPeriodSeconds(period); |
| 49 | 49 |
| 50 // We may have stopped before the clock advanced to |deadline|. | 50 // We may have stopped before the clock advanced to |deadline|. |
| 51 double timeToAdvance = deadline - monotonicallyIncreasingTime(); | 51 double timeToAdvance = deadline - monotonicallyIncreasingTime(); |
| 52 m_platform.advanceClockSeconds(timeToAdvance); | 52 m_platform.advanceClockSeconds(timeToAdvance); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // Returns false if there are no pending delayed tasks, otherwise sets |time|
to | 55 // Returns false if there are no pending delayed tasks, otherwise sets |time| |
| 56 // the delay in seconds till the next pending delayed task is scheduled to fir
e. | 56 // to the delay in seconds till the next pending delayed task is scheduled to |
| 57 // fire. |
| 57 bool timeTillNextDelayedTask(double* time) const { | 58 bool timeTillNextDelayedTask(double* time) const { |
| 58 base::TimeTicks nextRunTime; | 59 base::TimeTicks nextRunTime; |
| 59 if (!m_platform.rendererScheduler() | 60 if (!m_platform.rendererScheduler() |
| 60 ->TimerTaskRunner() | 61 ->TimerTaskRunner() |
| 61 ->GetTimeDomain() | 62 ->GetTimeDomain() |
| 62 ->NextScheduledRunTime(&nextRunTime)) | 63 ->NextScheduledRunTime(&nextRunTime)) |
| 63 return false; | 64 return false; |
| 64 *time = (nextRunTime - | 65 *time = (nextRunTime - |
| 65 m_platform.rendererScheduler() | 66 m_platform.rendererScheduler() |
| 66 ->TimerTaskRunner() | 67 ->TimerTaskRunner() |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 timer.startRepeating(10, BLINK_FROM_HERE); | 405 timer.startRepeating(10, BLINK_FROM_HERE); |
| 405 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); | 406 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); |
| 406 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); | 407 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); |
| 407 | 408 |
| 408 m_platform.advanceClockSeconds(2.0); | 409 m_platform.advanceClockSeconds(2.0); |
| 409 timer.augmentRepeatInterval(10); | 410 timer.augmentRepeatInterval(10); |
| 410 | 411 |
| 411 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); | 412 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); |
| 412 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); | 413 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); |
| 413 | 414 |
| 414 // NOTE setAutoAdvanceNowToPendingTasks(true) (which uses cc::OrderedSimpleTas
kRunner) | 415 // NOTE setAutoAdvanceNowToPendingTasks(true) (which uses |
| 415 // results in somewhat strange behavior of the test clock which breaks this te
st. | 416 // cc::OrderedSimpleTaskRunner) results in somewhat strange behavior of the |
| 416 // Specifically the test clock advancing logic ignores newly posted delayed ta
sks and | 417 // test clock which breaks this test. Specifically the test clock advancing |
| 417 // advances too far. | 418 // logic ignores newly posted delayed tasks and advances too far. |
| 418 runUntilDeadline(m_startTime + 50.0); | 419 runUntilDeadline(m_startTime + 50.0); |
| 419 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0)); | 420 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0)); |
| 420 } | 421 } |
| 421 | 422 |
| 422 TEST_F(TimerTest, AugmentRepeatInterval_TimerFireDelayed) { | 423 TEST_F(TimerTest, AugmentRepeatInterval_TimerFireDelayed) { |
| 423 m_platform.setAutoAdvanceNowToPendingTasks(false); | 424 m_platform.setAutoAdvanceNowToPendingTasks(false); |
| 424 | 425 |
| 425 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 426 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
| 426 timer.startRepeating(10, BLINK_FROM_HERE); | 427 timer.startRepeating(10, BLINK_FROM_HERE); |
| 427 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); | 428 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); |
| 428 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); | 429 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); |
| 429 | 430 |
| 430 m_platform.advanceClockSeconds(123.0); // Make the timer long overdue. | 431 m_platform.advanceClockSeconds(123.0); // Make the timer long overdue. |
| 431 timer.augmentRepeatInterval(10); | 432 timer.augmentRepeatInterval(10); |
| 432 | 433 |
| 433 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); | 434 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); |
| 434 // The timer is overdue so it should be scheduled to fire immediatly. | 435 // The timer is overdue so it should be scheduled to fire immediatly. |
| 435 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); | 436 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); |
| 436 } | 437 } |
| 437 | 438 |
| 438 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) { | 439 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) { |
| 439 m_platform.setAutoAdvanceNowToPendingTasks(false); | 440 m_platform.setAutoAdvanceNowToPendingTasks(false); |
| 440 | 441 |
| 441 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); | 442 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); |
| 442 timer.startRepeating(2.0, BLINK_FROM_HERE); | 443 timer.startRepeating(2.0, BLINK_FROM_HERE); |
| 443 | 444 |
| 444 recordNextFireTimeTask( | 445 recordNextFireTimeTask( |
| 445 &timer); // Next scheduled task to run at m_startTime + 2.0 | 446 &timer); // Next scheduled task to run at m_startTime + 2.0 |
| 446 | 447 |
| 447 // Simulate timer firing early. Next scheduled task to run at m_startTime + 4.
0 | 448 // Simulate timer firing early. Next scheduled task to run at |
| 449 // m_startTime + 4.0 |
| 448 m_platform.advanceClockSeconds(1.9); | 450 m_platform.advanceClockSeconds(1.9); |
| 449 runUntilDeadline(monotonicallyIncreasingTime() + 0.2); | 451 runUntilDeadline(monotonicallyIncreasingTime() + 0.2); |
| 450 | 452 |
| 451 m_platform.runForPeriodSeconds( | 453 // Next scheduled task to run at m_startTime + 6.0 |
| 452 2.0); // Next scheduled task to run at m_startTime + 6.0 | 454 m_platform.runForPeriodSeconds(2.0); |
| 453 m_platform.runForPeriodSeconds( | 455 // Next scheduled task to run at m_startTime + 8.0 |
| 454 2.1); // Next scheduled task to run at m_startTime + 8.0 | 456 m_platform.runForPeriodSeconds(2.1); |
| 455 m_platform.runForPeriodSeconds( | 457 // Next scheduled task to run at m_startTime + 10.0 |
| 456 2.9); // Next scheduled task to run at m_startTime + 10.0 | 458 m_platform.runForPeriodSeconds(2.9); |
| 457 m_platform.runForPeriodSeconds( | 459 // Next scheduled task to run at m_startTime + 14.0 (skips a beat) |
| 458 3.1); // Next scheduled task to run at m_startTime + 14.0 (skips a beat) | 460 m_platform.runForPeriodSeconds(3.1); |
| 459 m_platform.runForPeriodSeconds( | 461 // Next scheduled task to run at m_startTime + 18.0 (skips a beat) |
| 460 4.0); // Next scheduled task to run at m_startTime + 18.0 (skips a beat) | 462 m_platform.runForPeriodSeconds(4.0); |
| 461 m_platform.runForPeriodSeconds( | 463 // Next scheduled task to run at m_startTime + 28.0 (skips 5 beats) |
| 462 10.0); // Next scheduled task to run at m_startTime + 28.0 (skips 5 beats
) | 464 m_platform.runForPeriodSeconds(10.0); |
| 463 | 465 |
| 464 EXPECT_THAT( | 466 EXPECT_THAT( |
| 465 m_nextFireTimes, | 467 m_nextFireTimes, |
| 466 ElementsAre(m_startTime + 2.0, m_startTime + 4.0, m_startTime + 6.0, | 468 ElementsAre(m_startTime + 2.0, m_startTime + 4.0, m_startTime + 6.0, |
| 467 m_startTime + 8.0, m_startTime + 10.0, m_startTime + 14.0, | 469 m_startTime + 8.0, m_startTime + 10.0, m_startTime + 14.0, |
| 468 m_startTime + 18.0, m_startTime + 28.0)); | 470 m_startTime + 18.0, m_startTime + 28.0)); |
| 469 } | 471 } |
| 470 | 472 |
| 471 template <typename TimerFiredClass> | 473 template <typename TimerFiredClass> |
| 472 class TimerForTest : public TaskRunnerTimer<TimerFiredClass> { | 474 class TimerForTest : public TaskRunnerTimer<TimerFiredClass> { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 490 scheduler::WebTaskRunnerImpl webTaskRunner(taskRunner); | 492 scheduler::WebTaskRunnerImpl webTaskRunner(taskRunner); |
| 491 TimerForTest<TimerTest> timer(&webTaskRunner, this, &TimerTest::countingTask); | 493 TimerForTest<TimerTest> timer(&webTaskRunner, this, &TimerTest::countingTask); |
| 492 timer.startOneShot(0, BLINK_FROM_HERE); | 494 timer.startOneShot(0, BLINK_FROM_HERE); |
| 493 | 495 |
| 494 // Make sure the task was posted on taskRunner. | 496 // Make sure the task was posted on taskRunner. |
| 495 EXPECT_FALSE(taskRunner->IsEmpty()); | 497 EXPECT_FALSE(taskRunner->IsEmpty()); |
| 496 } | 498 } |
| 497 | 499 |
| 498 } // namespace | 500 } // namespace |
| 499 } // namespace blink | 501 } // namespace blink |
| OLD | NEW |