Commit 98561e7b authored by alexclarke's avatar alexclarke Committed by Commit bot

Fix TestingPlatformSupportWithMockScheduler::runForPeriodSeconds

When one calls runForPeriodSeconds(x) you naturally expect the
testing clock to alwats advance by that many seconds.

Unfortunately cc::OrderedSimpleTaskRunner is a strange beast and
the actual time advances to the last delayed task before the
deadline if any.

This patch fixes that and makes the operation of timer fast
forwarding explicit.  It also fixes a bug in
TimerTest.RepeatingTimerDoesNotDrift which was relying on
strange behavior of cc::OrderedSimpleTaskRunner when
setAutoAdvanceNowToPendingTasks is false.

BUG=657517

Review-Url: https://chromiumcodereview.appspot.com/2433323003
Cr-Commit-Position: refs/heads/master@{#426545}
parent 5f65e96e
......@@ -46,10 +46,6 @@ class TimerTest : public testing::Test {
double period = deadline - monotonicallyIncreasingTime();
EXPECT_GE(period, 0.0);
m_platform.runForPeriodSeconds(period);
// We may have stopped before the clock advanced to |deadline|.
double timeToAdvance = deadline - monotonicallyIncreasingTime();
m_platform.advanceClockSeconds(timeToAdvance);
}
// Returns false if there are no pending delayed tasks, otherwise sets |time|
......@@ -457,11 +453,14 @@ TEST_F(TimerTest, RepeatingTimerDoesNotDrift) {
// Next scheduled task to run at m_startTime + 10.0
m_platform.runForPeriodSeconds(2.9);
// Next scheduled task to run at m_startTime + 14.0 (skips a beat)
m_platform.runForPeriodSeconds(3.1);
m_platform.advanceClockSeconds(3.1);
m_platform.runUntilIdle();
// Next scheduled task to run at m_startTime + 18.0 (skips a beat)
m_platform.runForPeriodSeconds(4.0);
m_platform.advanceClockSeconds(4.0);
m_platform.runUntilIdle();
// Next scheduled task to run at m_startTime + 28.0 (skips 5 beats)
m_platform.runForPeriodSeconds(10.0);
m_platform.advanceClockSeconds(10.0);
m_platform.runUntilIdle();
EXPECT_THAT(
m_nextFireTimes,
......
......@@ -491,5 +491,9 @@ void TaskQueueManager::OnTriedToSelectBlockedWorkQueue(
}
}
bool TaskQueueManager::HasImmediateWorkForTesting() const {
return !selector_.EnabledWorkQueuesEmpty();
}
} // namespace scheduler
} // namespace blink
......@@ -140,6 +140,9 @@ class BLINK_PLATFORM_EXPORT TaskQueueManager
// Return number of pending tasks in task queues.
size_t GetNumberOfPendingTasks() const;
// Returns true if there is a task that could be executed immediately.
bool HasImmediateWorkForTesting() const;
private:
friend class LazyNow;
friend class internal::TaskQueueImpl;
......
......@@ -40,6 +40,8 @@
#include "cc/test/ordered_simple_task_runner.h"
#include "platform/HTTPNames.h"
#include "platform/heap/Heap.h"
#include "platform/scheduler/base/real_time_domain.h"
#include "platform/scheduler/base/task_queue_manager.h"
#include "platform/scheduler/base/test_time_source.h"
#include "platform/scheduler/child/scheduler_tqm_delegate_for_test.h"
#include "platform/scheduler/renderer/renderer_scheduler_impl.h"
......@@ -220,7 +222,34 @@ void TestingPlatformSupportWithMockScheduler::runUntilIdle() {
void TestingPlatformSupportWithMockScheduler::runForPeriodSeconds(
double seconds) {
m_mockTaskRunner->RunForPeriod(base::TimeDelta::FromSecondsD(seconds));
const base::TimeTicks deadline =
m_clock->NowTicks() + base::TimeDelta::FromSecondsD(seconds);
scheduler::TaskQueueManager* taskQueueManager =
m_scheduler->GetSchedulerHelperForTesting()
->GetTaskQueueManagerForTesting();
for (;;) {
// If we've run out of immediate work then fast forward to the next delayed
// task, but don't pass |deadline|.
if (!taskQueueManager->HasImmediateWorkForTesting()) {
base::TimeTicks nextDelayedTask;
if (!taskQueueManager->real_time_domain()->NextScheduledRunTime(
&nextDelayedTask) ||
nextDelayedTask > deadline) {
break;
}
m_clock->SetNowTicks(nextDelayedTask);
}
if (m_clock->NowTicks() > deadline)
break;
m_mockTaskRunner->RunPendingTasks();
}
m_clock->SetNowTicks(deadline);
}
void TestingPlatformSupportWithMockScheduler::advanceClockSeconds(
......
......@@ -171,8 +171,9 @@ class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport {
// instead.
void runUntilIdle();
// Runs for |seconds|. Note we use a testing clock rather than the wall clock
// here.
// Runs for |seconds| the testing clock is advanced by |seconds|. Note real
// time elapsed will typically much less than |seconds| because delays between
// timers are fast forwarded.
void runForPeriodSeconds(double seconds);
// Advances |m_clock| by |seconds|.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment