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

Unified Diff: Source/platform/TimerTest.cpp

Issue 1261993006: Fix the drift in repeating timers. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: Source/platform/TimerTest.cpp
diff --git a/Source/platform/TimerTest.cpp b/Source/platform/TimerTest.cpp
index 912d1706c44439ecac46b1ede1d40506f63656c4..d5bc14d49e091e42961e54942d66bd128ca84ed9 100644
--- a/Source/platform/TimerTest.cpp
+++ b/Source/platform/TimerTest.cpp
@@ -135,6 +135,14 @@ public:
}
}
+ void runPendingTasks()
+ {
+ while (!m_timerTasks.empty() && m_timerTasks.top().runTimeSecs() <= gCurrentTimeSecs) {
+ m_timerTasks.top().run();
+ m_timerTasks.pop();
+ }
+ }
+
bool hasOneTimerTask() const
{
return m_timerTasks.size() == 1;
@@ -224,6 +232,11 @@ public:
mockScheduler()->runUntilIdle();
}
+ void runPendingTasks()
+ {
+ mockScheduler()->runPendingTasks();
+ }
+
void runUntilIdleOrDeadlinePassed(double deadline)
{
mockScheduler()->runUntilIdleOrDeadlinePassed(deadline);
@@ -272,6 +285,11 @@ public:
m_runTimes.push_back(monotonicallyIncreasingTime());
}
+ void recordNextFireTimeTask(Timer<TimerTest>* timer)
+ {
+ m_nextFireTimes.push_back(monotonicallyIncreasingTime() + timer->nextFireInterval());
+ }
+
void advanceTimeBy(double timeSecs)
{
gCurrentTimeSecs += timeSecs;
@@ -282,6 +300,11 @@ public:
m_platform->runUntilIdle();
}
+ void runPendingTasks()
+ {
+ m_platform->runPendingTasks();
+ }
+
void runUntilIdleOrDeadlinePassed(double deadline)
{
m_platform->runUntilIdleOrDeadlinePassed(deadline);
@@ -300,6 +323,7 @@ public:
protected:
double m_startTime;
std::vector<double> m_runTimes;
+ std::vector<double> m_nextFireTimes;
private:
OwnPtr<TimerTestPlatform> m_platform;
@@ -736,6 +760,42 @@ TEST_F(TimerTest, DidChangeAlignmentInterval)
EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime());
}
+TEST_F(TimerTest, RepeatingTimerDoesNotDrift)
+{
+ Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask);
+ timer.startRepeating(2.0, FROM_HERE);
+
+ ASSERT(hasOneTimerTask());
+ recordNextFireTimeTask(&timer); // Scheduled to run at m_startTime + 2.0
+
+ advanceTimeBy(2.0);
+ runPendingTasks(); // Scheduled to run at m_startTime + 4.0
+
+ advanceTimeBy(2.1);
+ runPendingTasks(); // Scheduled to run at m_startTime + 6.0
+
+ advanceTimeBy(2.9);
+ runPendingTasks(); // Scheduled to run at m_startTime + 8.0
+
+ advanceTimeBy(3.1);
+ runPendingTasks(); // Scheduled to run at m_startTime + 12.0 (skips a beat)
+
+ advanceTimeBy(4.0);
+ runPendingTasks(); // Scheduled to run at m_startTime + 16.0 (skips a beat)
+
+ advanceTimeBy(10.0); // Scheduled to run at m_startTime + 22.0 (skips a few beats)
+ runPendingTasks();
+
+ runUntilIdleOrDeadlinePassed(m_startTime + 5.5);
+ EXPECT_THAT(m_nextFireTimes, ElementsAre(
+ m_startTime + 2.0,
+ m_startTime + 4.0,
+ m_startTime + 6.0,
+ m_startTime + 8.0,
+ m_startTime + 12.0,
+ m_startTime + 16.0,
+ m_startTime + 26.0));
+}
} // namespace
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698