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

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

Issue 1290633002: Revert of Fix the drift in repeating timers. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 4 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
« no previous file with comments | « Source/platform/Timer.cpp ('k') | public/platform/WebScheduler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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<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, double delaySecs) 44 DelayedTask(WebThread::Task* task, long long delayMs)
45 : m_task(adoptRef(new RefCountedTaskContainer(task))) 45 : m_task(adoptRef(new RefCountedTaskContainer(task)))
46 , m_runTimeSecs(monotonicallyIncreasingTime() + delaySecs) 46 , m_runTimeSecs(monotonicallyIncreasingTime() + 0.001 * static_cast<doub le>(delayMs))
47 , m_delaySecs(delaySecs) { } 47 , m_delayMs(delayMs) { }
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 double delaySecs() const 64 long long delayMs() const
65 { 65 {
66 return m_delaySecs; 66 return m_delayMs;
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 double m_delaySecs; 72 long long m_delayMs;
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
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, double de laySecs) override 106 void postTimerTask(const WebTraceLocation&, WebThread::Task* task, long long delayMs) override
107 { 107 {
108 m_timerTasks.push(DelayedTask(task, delaySecs)); 108 m_timerTasks.push(DelayedTask(task, delayMs));
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
146 bool hasOneTimerTask() const 138 bool hasOneTimerTask() const
147 { 139 {
148 return m_timerTasks.size() == 1; 140 return m_timerTasks.size() == 1;
149 } 141 }
150 142
151 double nextTimerTaskDelaySecs() const 143 long nextTimerTaskDelayMillis() const
152 { 144 {
153 ASSERT(hasOneTimerTask()); 145 ASSERT(hasOneTimerTask());
154 return m_timerTasks.top().delaySecs(); 146 return m_timerTasks.top().delayMs();
155 } 147 }
156 148
157 private: 149 private:
158 std::priority_queue<DelayedTask> m_timerTasks; 150 std::priority_queue<DelayedTask> m_timerTasks;
159 }; 151 };
160 152
161 class FakeWebThread : public WebThread { 153 class FakeWebThread : public WebThread {
162 public: 154 public:
163 FakeWebThread() : m_webScheduler(adoptPtr(new MockWebScheduler())) { } 155 FakeWebThread() : m_webScheduler(adoptPtr(new MockWebScheduler())) { }
164 ~FakeWebThread() override { } 156 ~FakeWebThread() override { }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 { 217 {
226 static const unsigned char enabled[] = {0}; 218 static const unsigned char enabled[] = {0};
227 return enabled; 219 return enabled;
228 } 220 }
229 221
230 void runUntilIdle() 222 void runUntilIdle()
231 { 223 {
232 mockScheduler()->runUntilIdle(); 224 mockScheduler()->runUntilIdle();
233 } 225 }
234 226
235 void runPendingTasks()
236 {
237 mockScheduler()->runPendingTasks();
238 }
239
240 void runUntilIdleOrDeadlinePassed(double deadline) 227 void runUntilIdleOrDeadlinePassed(double deadline)
241 { 228 {
242 mockScheduler()->runUntilIdleOrDeadlinePassed(deadline); 229 mockScheduler()->runUntilIdleOrDeadlinePassed(deadline);
243 } 230 }
244 231
245 bool hasOneTimerTask() const 232 bool hasOneTimerTask() const
246 { 233 {
247 return mockScheduler()->hasOneTimerTask(); 234 return mockScheduler()->hasOneTimerTask();
248 } 235 }
249 236
250 double nextTimerTaskDelaySecs() const 237 long nextTimerTaskDelayMillis() const
251 { 238 {
252 return mockScheduler()->nextTimerTaskDelaySecs(); 239 return mockScheduler()->nextTimerTaskDelayMillis();
253 } 240 }
254 241
255 private: 242 private:
256 MockWebScheduler* mockScheduler() const 243 MockWebScheduler* mockScheduler() const
257 { 244 {
258 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); 245 return static_cast<MockWebScheduler*>(m_webThread->scheduler());
259 } 246 }
260 247
261 OwnPtr<FakeWebThread> m_webThread; 248 OwnPtr<FakeWebThread> m_webThread;
262 }; 249 };
(...skipping 15 matching lines...) Expand all
278 void TearDown() override 265 void TearDown() override
279 { 266 {
280 Platform::initialize(m_oldPlatform); 267 Platform::initialize(m_oldPlatform);
281 } 268 }
282 269
283 void countingTask(Timer<TimerTest>*) 270 void countingTask(Timer<TimerTest>*)
284 { 271 {
285 m_runTimes.push_back(monotonicallyIncreasingTime()); 272 m_runTimes.push_back(monotonicallyIncreasingTime());
286 } 273 }
287 274
288 void recordNextFireTimeTask(Timer<TimerTest>* timer)
289 {
290 m_nextFireTimes.push_back(monotonicallyIncreasingTime() + timer->nextFir eInterval());
291 }
292
293 void advanceTimeBy(double timeSecs) 275 void advanceTimeBy(double timeSecs)
294 { 276 {
295 gCurrentTimeSecs += timeSecs; 277 gCurrentTimeSecs += timeSecs;
296 } 278 }
297 279
298 void runUntilIdle() 280 void runUntilIdle()
299 { 281 {
300 m_platform->runUntilIdle(); 282 m_platform->runUntilIdle();
301 } 283 }
302 284
303 void runPendingTasks()
304 {
305 m_platform->runPendingTasks();
306 }
307
308 void runUntilIdleOrDeadlinePassed(double deadline) 285 void runUntilIdleOrDeadlinePassed(double deadline)
309 { 286 {
310 m_platform->runUntilIdleOrDeadlinePassed(deadline); 287 m_platform->runUntilIdleOrDeadlinePassed(deadline);
311 } 288 }
312 289
313 bool hasOneTimerTask() const 290 bool hasOneTimerTask() const
314 { 291 {
315 return m_platform->hasOneTimerTask(); 292 return m_platform->hasOneTimerTask();
316 } 293 }
317 294
318 double nextTimerTaskDelaySecs() const 295 long nextTimerTaskDelayMillis() const
319 { 296 {
320 return m_platform->nextTimerTaskDelaySecs(); 297 return m_platform->nextTimerTaskDelayMillis();
321 } 298 }
322 299
323 protected: 300 protected:
324 double m_startTime; 301 double m_startTime;
325 std::vector<double> m_runTimes; 302 std::vector<double> m_runTimes;
326 std::vector<double> m_nextFireTimes;
327 303
328 private: 304 private:
329 OwnPtr<TimerTestPlatform> m_platform; 305 OwnPtr<TimerTestPlatform> m_platform;
330 Platform* m_oldPlatform; 306 Platform* m_oldPlatform;
331 }; 307 };
332 308
333 TEST_F(TimerTest, StartOneShot_Zero) 309 TEST_F(TimerTest, StartOneShot_Zero)
334 { 310 {
335 Timer<TimerTest> timer(this, &TimerTest::countingTask); 311 Timer<TimerTest> timer(this, &TimerTest::countingTask);
336 timer.startOneShot(0, FROM_HERE); 312 timer.startOneShot(0, FROM_HERE);
337 313
338 ASSERT(hasOneTimerTask()); 314 ASSERT(hasOneTimerTask());
339 EXPECT_EQ(0.0, nextTimerTaskDelaySecs()); 315 EXPECT_EQ(0ll, nextTimerTaskDelayMillis());
340 316
341 runUntilIdle(); 317 runUntilIdle();
342 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 318 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
343 } 319 }
344 320
345 TEST_F(TimerTest, StartOneShot_ZeroAndCancel) 321 TEST_F(TimerTest, StartOneShot_ZeroAndCancel)
346 { 322 {
347 Timer<TimerTest> timer(this, &TimerTest::countingTask); 323 Timer<TimerTest> timer(this, &TimerTest::countingTask);
348 timer.startOneShot(0, FROM_HERE); 324 timer.startOneShot(0, FROM_HERE);
349 325
350 ASSERT(hasOneTimerTask()); 326 ASSERT(hasOneTimerTask());
351 EXPECT_EQ(0.0, nextTimerTaskDelaySecs()); 327 EXPECT_EQ(0ll, nextTimerTaskDelayMillis());
352 328
353 timer.stop(); 329 timer.stop();
354 330
355 runUntilIdle(); 331 runUntilIdle();
356 EXPECT_TRUE(m_runTimes.empty()); 332 EXPECT_TRUE(m_runTimes.empty());
357 } 333 }
358 334
359 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost) 335 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost)
360 { 336 {
361 Timer<TimerTest> timer(this, &TimerTest::countingTask); 337 Timer<TimerTest> timer(this, &TimerTest::countingTask);
362 timer.startOneShot(0, FROM_HERE); 338 timer.startOneShot(0, FROM_HERE);
363 339
364 ASSERT(hasOneTimerTask()); 340 ASSERT(hasOneTimerTask());
365 EXPECT_EQ(0.0, nextTimerTaskDelaySecs()); 341 EXPECT_EQ(0ll, nextTimerTaskDelayMillis());
366 342
367 timer.stop(); 343 timer.stop();
368 344
369 runUntilIdle(); 345 runUntilIdle();
370 EXPECT_TRUE(m_runTimes.empty()); 346 EXPECT_TRUE(m_runTimes.empty());
371 347
372 timer.startOneShot(0, FROM_HERE); 348 timer.startOneShot(0, FROM_HERE);
373 349
374 ASSERT(hasOneTimerTask()); 350 ASSERT(hasOneTimerTask());
375 EXPECT_EQ(0.0, nextTimerTaskDelaySecs()); 351 EXPECT_EQ(0ll, nextTimerTaskDelayMillis());
376 352
377 runUntilIdle(); 353 runUntilIdle();
378 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 354 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
379 } 355 }
380 356
381 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning) 357 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning)
382 { 358 {
383 Timer<TimerTest> timer(this, &TimerTest::countingTask); 359 Timer<TimerTest> timer(this, &TimerTest::countingTask);
384 timer.startOneShot(0, FROM_HERE); 360 timer.startOneShot(0, FROM_HERE);
385 361
386 ASSERT(hasOneTimerTask()); 362 ASSERT(hasOneTimerTask());
387 EXPECT_EQ(0.0, nextTimerTaskDelaySecs()); 363 EXPECT_EQ(0ll, nextTimerTaskDelayMillis());
388 364
389 runUntilIdle(); 365 runUntilIdle();
390 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 366 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
391 367
392 timer.startOneShot(0, FROM_HERE); 368 timer.startOneShot(0, FROM_HERE);
393 369
394 ASSERT(hasOneTimerTask()); 370 ASSERT(hasOneTimerTask());
395 EXPECT_EQ(0.0, nextTimerTaskDelaySecs()); 371 EXPECT_EQ(0ll, nextTimerTaskDelayMillis());
396 372
397 runUntilIdle(); 373 runUntilIdle();
398 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime)); 374 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime));
399 } 375 }
400 376
401 TEST_F(TimerTest, StartOneShot_NonZero) 377 TEST_F(TimerTest, StartOneShot_NonZero)
402 { 378 {
403 Timer<TimerTest> timer(this, &TimerTest::countingTask); 379 Timer<TimerTest> timer(this, &TimerTest::countingTask);
404 timer.startOneShot(10.0, FROM_HERE); 380 timer.startOneShot(10.0, FROM_HERE);
405 381
406 ASSERT(hasOneTimerTask()); 382 ASSERT(hasOneTimerTask());
407 EXPECT_EQ(10.0, nextTimerTaskDelaySecs()); 383 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis());
408 384
409 runUntilIdle(); 385 runUntilIdle();
410 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 386 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
411 } 387 }
412 388
413 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel) 389 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel)
414 { 390 {
415 Timer<TimerTest> timer(this, &TimerTest::countingTask); 391 Timer<TimerTest> timer(this, &TimerTest::countingTask);
416 timer.startOneShot(10, FROM_HERE); 392 timer.startOneShot(10, FROM_HERE);
417 393
418 ASSERT(hasOneTimerTask()); 394 ASSERT(hasOneTimerTask());
419 EXPECT_EQ(10.0, nextTimerTaskDelaySecs()); 395 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis());
420 396
421 timer.stop(); 397 timer.stop();
422 398
423 runUntilIdle(); 399 runUntilIdle();
424 EXPECT_TRUE(m_runTimes.empty()); 400 EXPECT_TRUE(m_runTimes.empty());
425 } 401 }
426 402
427 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost) 403 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost)
428 { 404 {
429 Timer<TimerTest> timer(this, &TimerTest::countingTask); 405 Timer<TimerTest> timer(this, &TimerTest::countingTask);
430 timer.startOneShot(10, FROM_HERE); 406 timer.startOneShot(10, FROM_HERE);
431 407
432 ASSERT(hasOneTimerTask()); 408 ASSERT(hasOneTimerTask());
433 EXPECT_EQ(10.0, nextTimerTaskDelaySecs()); 409 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis());
434 410
435 timer.stop(); 411 timer.stop();
436 412
437 runUntilIdle(); 413 runUntilIdle();
438 EXPECT_TRUE(m_runTimes.empty()); 414 EXPECT_TRUE(m_runTimes.empty());
439 415
440 double secondPostTime = monotonicallyIncreasingTime(); 416 double secondPostTime = monotonicallyIncreasingTime();
441 timer.startOneShot(10, FROM_HERE); 417 timer.startOneShot(10, FROM_HERE);
442 418
443 ASSERT(hasOneTimerTask()); 419 ASSERT(hasOneTimerTask());
444 EXPECT_EQ(10.0, nextTimerTaskDelaySecs()); 420 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis());
445 421
446 runUntilIdle(); 422 runUntilIdle();
447 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); 423 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0));
448 } 424 }
449 425
450 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) 426 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning)
451 { 427 {
452 Timer<TimerTest> timer(this, &TimerTest::countingTask); 428 Timer<TimerTest> timer(this, &TimerTest::countingTask);
453 timer.startOneShot(10, FROM_HERE); 429 timer.startOneShot(10, FROM_HERE);
454 430
455 ASSERT(hasOneTimerTask()); 431 ASSERT(hasOneTimerTask());
456 EXPECT_EQ(10.0, nextTimerTaskDelaySecs()); 432 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis());
457 433
458 runUntilIdle(); 434 runUntilIdle();
459 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 435 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
460 436
461 timer.startOneShot(20, FROM_HERE); 437 timer.startOneShot(20, FROM_HERE);
462 438
463 ASSERT(hasOneTimerTask()); 439 ASSERT(hasOneTimerTask());
464 EXPECT_EQ(20.0, nextTimerTaskDelaySecs()); 440 EXPECT_EQ(20000ll, nextTimerTaskDelayMillis());
465 441
466 runUntilIdle(); 442 runUntilIdle();
467 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0)) ; 443 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0)) ;
468 } 444 }
469 445
470 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing) 446 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing)
471 { 447 {
472 Timer<TimerTest> timer(this, &TimerTest::countingTask); 448 Timer<TimerTest> timer(this, &TimerTest::countingTask);
473 timer.startOneShot(10, FROM_HERE); 449 timer.startOneShot(10, FROM_HERE);
474 timer.startOneShot(10, FROM_HERE); 450 timer.startOneShot(10, FROM_HERE);
475 451
476 ASSERT(hasOneTimerTask()); 452 ASSERT(hasOneTimerTask());
477 EXPECT_EQ(10.0, nextTimerTaskDelaySecs()); 453 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis());
478 454
479 runUntilIdle(); 455 runUntilIdle();
480 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 456 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
481 } 457 }
482 458
483 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOriginalTask) 459 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOriginalTask)
484 { 460 {
485 Timer<TimerTest> timer(this, &TimerTest::countingTask); 461 Timer<TimerTest> timer(this, &TimerTest::countingTask);
486 timer.startOneShot(10, FROM_HERE); 462 timer.startOneShot(10, FROM_HERE);
487 timer.startOneShot(0, FROM_HERE); 463 timer.startOneShot(0, FROM_HERE);
(...skipping 11 matching lines...) Expand all
499 runUntilIdle(); 475 runUntilIdle();
500 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 476 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
501 } 477 }
502 478
503 TEST_F(TimerTest, StartRepeatingTask) 479 TEST_F(TimerTest, StartRepeatingTask)
504 { 480 {
505 Timer<TimerTest> timer(this, &TimerTest::countingTask); 481 Timer<TimerTest> timer(this, &TimerTest::countingTask);
506 timer.startRepeating(1.0, FROM_HERE); 482 timer.startRepeating(1.0, FROM_HERE);
507 483
508 ASSERT(hasOneTimerTask()); 484 ASSERT(hasOneTimerTask());
509 EXPECT_EQ(1.0, nextTimerTaskDelaySecs()); 485 EXPECT_EQ(1000ll, nextTimerTaskDelayMillis());
510 486
511 runUntilIdleOrDeadlinePassed(m_startTime + 5.5); 487 runUntilIdleOrDeadlinePassed(m_startTime + 5.5);
512 EXPECT_THAT(m_runTimes, ElementsAre( 488 EXPECT_THAT(m_runTimes, ElementsAre(
513 m_startTime + 1.0, m_startTime + 2.0, m_startTime + 3.0, m_startTime + 4 .0, m_startTime + 5.0)); 489 m_startTime + 1.0, m_startTime + 2.0, m_startTime + 3.0, m_startTime + 4 .0, m_startTime + 5.0));
514 } 490 }
515 491
516 TEST_F(TimerTest, StartRepeatingTask_ThenCancel) 492 TEST_F(TimerTest, StartRepeatingTask_ThenCancel)
517 { 493 {
518 Timer<TimerTest> timer(this, &TimerTest::countingTask); 494 Timer<TimerTest> timer(this, &TimerTest::countingTask);
519 timer.startRepeating(1.0, FROM_HERE); 495 timer.startRepeating(1.0, FROM_HERE);
520 496
521 ASSERT(hasOneTimerTask()); 497 ASSERT(hasOneTimerTask());
522 EXPECT_EQ(1.0, nextTimerTaskDelaySecs()); 498 EXPECT_EQ(1000ll, nextTimerTaskDelayMillis());
523 499
524 runUntilIdleOrDeadlinePassed(m_startTime + 2.5); 500 runUntilIdleOrDeadlinePassed(m_startTime + 2.5);
525 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 501 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
526 502
527 timer.stop(); 503 timer.stop();
528 runUntilIdle(); 504 runUntilIdle();
529 505
530 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 506 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
531 } 507 }
532 508
533 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot) 509 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot)
534 { 510 {
535 Timer<TimerTest> timer(this, &TimerTest::countingTask); 511 Timer<TimerTest> timer(this, &TimerTest::countingTask);
536 timer.startRepeating(1.0, FROM_HERE); 512 timer.startRepeating(1.0, FROM_HERE);
537 513
538 ASSERT(hasOneTimerTask()); 514 ASSERT(hasOneTimerTask());
539 EXPECT_EQ(1.0, nextTimerTaskDelaySecs()); 515 EXPECT_EQ(1000ll, nextTimerTaskDelayMillis());
540 516
541 runUntilIdleOrDeadlinePassed(m_startTime + 2.5); 517 runUntilIdleOrDeadlinePassed(m_startTime + 2.5);
542 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 518 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
543 519
544 timer.startOneShot(0, FROM_HERE); 520 timer.startOneShot(0, FROM_HERE);
545 runUntilIdle(); 521 runUntilIdle();
546 522
547 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, m_ startTime + 2.5)); 523 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, m_ startTime + 2.5));
548 } 524 }
549 525
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
753 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); 729 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime());
754 730
755 timer.setAlignedFireTime(m_startTime); 731 timer.setAlignedFireTime(m_startTime);
756 timer.didChangeAlignmentInterval(monotonicallyIncreasingTime()); 732 timer.didChangeAlignmentInterval(monotonicallyIncreasingTime());
757 733
758 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); 734 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval());
759 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); 735 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval());
760 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); 736 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime());
761 } 737 }
762 738
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 }
799 739
800 } // namespace 740 } // namespace
801 } // namespace blink 741 } // namespace blink
OLDNEW
« no previous file with comments | « Source/platform/Timer.cpp ('k') | public/platform/WebScheduler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698