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

Side by Side Diff: cc/test/ordered_simple_task_runner_unittest.cc

Issue 1132753008: Replaced TestNowSource with SimpleTestTickClock. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased again. Replaces TimeSource with TickClock in TestAlwaysFailTimeSource Created 5 years, 6 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <string> 5 #include <string>
6 6
7 #include "base/cancelable_callback.h" 7 #include "base/cancelable_callback.h"
8 #include "base/format_macros.h" 8 #include "base/format_macros.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 TestOrderablePendingTask a2 = a; 49 TestOrderablePendingTask a2 = a;
50 EXPECT_EQ(a, a2); 50 EXPECT_EQ(a, a2);
51 EXPECT_LT(a2, b); 51 EXPECT_LT(a2, b);
52 EXPECT_LT(b, c); 52 EXPECT_LT(b, c);
53 EXPECT_LT(a2, c); 53 EXPECT_LT(a2, c);
54 } 54 }
55 55
56 class OrderedSimpleTaskRunnerTest : public testing::Test { 56 class OrderedSimpleTaskRunnerTest : public testing::Test {
57 public: 57 public:
58 OrderedSimpleTaskRunnerTest() 58 OrderedSimpleTaskRunnerTest()
59 : now_src_(TestNowSource::Create(base::TimeTicks())) { 59 : now_src_(new base::SimpleTestTickClock()),
60 task_runner_ = new OrderedSimpleTaskRunner(now_src_, true); 60 task_runner_(new OrderedSimpleTaskRunner(now_src_.get(), true)) {}
61 }
62 ~OrderedSimpleTaskRunnerTest() override {} 61 ~OrderedSimpleTaskRunnerTest() override {}
63 62
64 protected: 63 protected:
65 std::string executed_tasks_; 64 std::string executed_tasks_;
66 scoped_refptr<TestNowSource> now_src_; 65 scoped_ptr<base::SimpleTestTickClock> now_src_;
67 scoped_refptr<OrderedSimpleTaskRunner> task_runner_; 66 scoped_refptr<OrderedSimpleTaskRunner> task_runner_;
68 67
69 void PostTask(int task_num, base::TimeDelta delay) { 68 void PostTask(int task_num, base::TimeDelta delay) {
70 base::Closure test_task = base::Bind(&OrderedSimpleTaskRunnerTest::Task, 69 base::Closure test_task = base::Bind(&OrderedSimpleTaskRunnerTest::Task,
71 base::Unretained(this), 70 base::Unretained(this),
72 task_num); 71 task_num);
73 task_runner_->PostDelayedTask(FROM_HERE, test_task, delay); 72 task_runner_->PostDelayedTask(FROM_HERE, test_task, delay);
74 } 73 }
75 74
76 void PostTaskWhichPostsInstantTask(int task_num, base::TimeDelta delay) { 75 void PostTaskWhichPostsInstantTask(int task_num, base::TimeDelta delay) {
(...skipping 30 matching lines...) Expand all
107 task_num, 106 task_num,
108 delay); 107 delay);
109 task_runner_->PostDelayedTask(FROM_HERE, test_task, delay); 108 task_runner_->PostDelayedTask(FROM_HERE, test_task, delay);
110 } 109 }
111 110
112 private: 111 private:
113 void Task(int task_num) { 112 void Task(int task_num) {
114 if (!executed_tasks_.empty()) 113 if (!executed_tasks_.empty())
115 executed_tasks_ += " "; 114 executed_tasks_ += " ";
116 executed_tasks_ += 115 executed_tasks_ +=
117 base::StringPrintf("%d(%" PRId64 "ms)", 116 base::StringPrintf("%d(%" PRId64 "ms)", task_num,
118 task_num, 117 now_src_->NowTicks().ToInternalValue() /
119 now_src_->Now().ToInternalValue() /
120 base::Time::kMicrosecondsPerMillisecond); 118 base::Time::kMicrosecondsPerMillisecond);
121 } 119 }
122 120
123 void TaskWhichPostsInstantTask(int task_num) { 121 void TaskWhichPostsInstantTask(int task_num) {
124 Task(task_num); 122 Task(task_num);
125 PostTask(-task_num, base::TimeDelta()); 123 PostTask(-task_num, base::TimeDelta());
126 } 124 }
127 125
128 void TaskWhichPostsDelayedTask(int task_num, base::TimeDelta delay) { 126 void TaskWhichPostsDelayedTask(int task_num, base::TimeDelta delay) {
129 Task(task_num); 127 Task(task_num);
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 task_runner_->SetAutoAdvanceNowToPendingTasks(false); 267 task_runner_->SetAutoAdvanceNowToPendingTasks(false);
270 PostTask(1, base::TimeDelta()); 268 PostTask(1, base::TimeDelta());
271 PostTask(2, base::TimeDelta::FromMilliseconds(15)); 269 PostTask(2, base::TimeDelta::FromMilliseconds(15));
272 PostTask(3, base::TimeDelta()); 270 PostTask(3, base::TimeDelta());
273 PostTask(4, base::TimeDelta::FromMilliseconds(8)); 271 PostTask(4, base::TimeDelta::FromMilliseconds(8));
274 272
275 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "1(0ms) 3(0ms)"); 273 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "1(0ms) 3(0ms)");
276 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), ""); 274 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunPendingTasks(), "");
277 EXPECT_EQ(task_runner_->DelayToNextTaskTime(), 275 EXPECT_EQ(task_runner_->DelayToNextTaskTime(),
278 base::TimeDelta::FromMilliseconds(8)); 276 base::TimeDelta::FromMilliseconds(8));
279 now_src_->SetNow(base::TimeTicks::FromInternalValue(5000)); 277 now_src_->Advance(base::TimeDelta::FromInternalValue(5000));
280 EXPECT_EQ(task_runner_->DelayToNextTaskTime(), 278 EXPECT_EQ(task_runner_->DelayToNextTaskTime(),
281 base::TimeDelta::FromMilliseconds(3)); 279 base::TimeDelta::FromMilliseconds(3));
282 now_src_->SetNow(base::TimeTicks::FromInternalValue(25000)); 280 now_src_->Advance(base::TimeDelta::FromInternalValue(20000));
283 EXPECT_EQ(task_runner_->DelayToNextTaskTime(), base::TimeDelta()); 281 EXPECT_EQ(task_runner_->DelayToNextTaskTime(), base::TimeDelta());
284 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "4(25ms) 2(25ms)"); 282 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "4(25ms) 2(25ms)");
285 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), ""); 283 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
286 } 284 }
287 285
288 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilIdle) { 286 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilIdle) {
289 PostTaskWhichPostsInstantTask(1, base::TimeDelta()); 287 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
290 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilIdle(), "1(0ms) -1(0ms)"); 288 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilIdle(), "1(0ms) -1(0ms)");
291 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilIdle(), ""); 289 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilIdle(), "");
292 } 290 }
293 291
294 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilTimeAutoNow) { 292 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilTimeAutoNow) {
295 PostTaskWhichPostsInstantTask(1, base::TimeDelta()); 293 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
296 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(2)); 294 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(2));
297 PostTaskWhichPostsInstantTask(3, base::TimeDelta::FromMilliseconds(3)); 295 PostTaskWhichPostsInstantTask(3, base::TimeDelta::FromMilliseconds(3));
298 296
299 task_runner_->SetAutoAdvanceNowToPendingTasks(true); 297 task_runner_->SetAutoAdvanceNowToPendingTasks(true);
300 298
301 base::TimeTicks run_at = base::TimeTicks(); 299 base::TimeTicks run_at = base::TimeTicks();
302 300
303 run_at += base::TimeDelta::FromMilliseconds(2); 301 run_at += base::TimeDelta::FromMilliseconds(2);
304 RUN_AND_CHECK_RESULT( 302 RUN_AND_CHECK_RESULT(
305 EXPECT_TRUE, RunUntilTime(run_at), "1(0ms) -1(0ms) 2(2ms) -2(2ms)"); 303 EXPECT_TRUE, RunUntilTime(run_at), "1(0ms) -1(0ms) 2(2ms) -2(2ms)");
306 EXPECT_EQ(run_at, now_src_->Now()); 304 EXPECT_EQ(run_at, now_src_->NowTicks());
307 305
308 run_at += base::TimeDelta::FromMilliseconds(1); 306 run_at += base::TimeDelta::FromMilliseconds(1);
309 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_at), "3(3ms) -3(3ms)"); 307 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_at), "3(3ms) -3(3ms)");
310 EXPECT_EQ(run_at, now_src_->Now()); 308 EXPECT_EQ(run_at, now_src_->NowTicks());
311 309
312 run_at += base::TimeDelta::FromMilliseconds(1); 310 run_at += base::TimeDelta::FromMilliseconds(1);
313 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_at), ""); 311 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_at), "");
314 EXPECT_EQ(run_at, now_src_->Now()); 312 EXPECT_EQ(run_at, now_src_->NowTicks());
315 } 313 }
316 314
317 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilTimeManualNow) { 315 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilTimeManualNow) {
318 PostTaskWhichPostsInstantTask(1, base::TimeDelta()); 316 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
319 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(2)); 317 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(2));
320 PostTaskWhichPostsInstantTask(3, base::TimeDelta::FromMilliseconds(3)); 318 PostTaskWhichPostsInstantTask(3, base::TimeDelta::FromMilliseconds(3));
321 319
322 task_runner_->SetAutoAdvanceNowToPendingTasks(false); 320 task_runner_->SetAutoAdvanceNowToPendingTasks(false);
323 321
324 base::TimeTicks run_at = base::TimeTicks(); 322 base::TimeTicks run_at = base::TimeTicks();
325 323
326 run_at += base::TimeDelta::FromMilliseconds(2); 324 run_at += base::TimeDelta::FromMilliseconds(2);
327 RUN_AND_CHECK_RESULT( 325 RUN_AND_CHECK_RESULT(
328 EXPECT_TRUE, RunUntilTime(run_at), "1(2ms) 2(2ms) -1(2ms) -2(2ms)"); 326 EXPECT_TRUE, RunUntilTime(run_at), "1(2ms) 2(2ms) -1(2ms) -2(2ms)");
329 EXPECT_EQ(run_at, now_src_->Now()); 327 EXPECT_EQ(run_at, now_src_->NowTicks());
330 328
331 run_at += base::TimeDelta::FromMilliseconds(1); 329 run_at += base::TimeDelta::FromMilliseconds(1);
332 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_at), "3(3ms) -3(3ms)"); 330 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_at), "3(3ms) -3(3ms)");
333 EXPECT_EQ(run_at, now_src_->Now()); 331 EXPECT_EQ(run_at, now_src_->NowTicks());
334 332
335 run_at += base::TimeDelta::FromMilliseconds(1); 333 run_at += base::TimeDelta::FromMilliseconds(1);
336 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_at), ""); 334 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_at), "");
337 EXPECT_EQ(run_at, now_src_->Now()); 335 EXPECT_EQ(run_at, now_src_->NowTicks());
338 } 336 }
339 337
340 TEST_F(OrderedSimpleTaskRunnerTest, RunForPeriod) { 338 TEST_F(OrderedSimpleTaskRunnerTest, RunForPeriod) {
341 PostTaskWhichPostsInstantTask(1, base::TimeDelta()); 339 PostTaskWhichPostsInstantTask(1, base::TimeDelta());
342 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(2)); 340 PostTaskWhichPostsInstantTask(2, base::TimeDelta::FromMilliseconds(2));
343 PostTaskWhichPostsInstantTask(3, base::TimeDelta::FromMilliseconds(3)); 341 PostTaskWhichPostsInstantTask(3, base::TimeDelta::FromMilliseconds(3));
344 342
345 RUN_AND_CHECK_RESULT(EXPECT_TRUE, 343 RUN_AND_CHECK_RESULT(EXPECT_TRUE,
346 RunForPeriod(base::TimeDelta::FromMilliseconds(2)), 344 RunForPeriod(base::TimeDelta::FromMilliseconds(2)),
347 "1(0ms) -1(0ms) 2(2ms) -2(2ms)"); 345 "1(0ms) -1(0ms) 2(2ms) -2(2ms)");
348 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(2), 346 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(2),
349 now_src_->Now()); 347 now_src_->NowTicks());
350 348
351 RUN_AND_CHECK_RESULT(EXPECT_FALSE, 349 RUN_AND_CHECK_RESULT(EXPECT_FALSE,
352 RunForPeriod(base::TimeDelta::FromMilliseconds(1)), 350 RunForPeriod(base::TimeDelta::FromMilliseconds(1)),
353 "3(3ms) -3(3ms)"); 351 "3(3ms) -3(3ms)");
354 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(3), 352 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(3),
355 now_src_->Now()); 353 now_src_->NowTicks());
356 354
357 RUN_AND_CHECK_RESULT( 355 RUN_AND_CHECK_RESULT(
358 EXPECT_FALSE, RunForPeriod(base::TimeDelta::FromMilliseconds(1)), ""); 356 EXPECT_FALSE, RunForPeriod(base::TimeDelta::FromMilliseconds(1)), "");
359 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(4), 357 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(4),
360 now_src_->Now()); 358 now_src_->NowTicks());
361 } 359 }
362 360
363 TEST_F(OrderedSimpleTaskRunnerTest, RunTasksWhileWithCallback) { 361 TEST_F(OrderedSimpleTaskRunnerTest, RunTasksWhileWithCallback) {
364 base::Callback<bool(void)> return_true = base::Bind(&ReturnTrue); 362 base::Callback<bool(void)> return_true = base::Bind(&ReturnTrue);
365 base::Callback<bool(void)> return_false = base::Bind(&ReturnFalse); 363 base::Callback<bool(void)> return_false = base::Bind(&ReturnFalse);
366 364
367 PostTask(1, base::TimeDelta()); 365 PostTask(1, base::TimeDelta());
368 366
369 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunTasksWhile(return_false), ""); 367 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunTasksWhile(return_false), "");
370 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunTasksWhile(return_true), "1(0ms)"); 368 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunTasksWhile(return_true), "1(0ms)");
371 } 369 }
372 370
373 TEST_F(OrderedSimpleTaskRunnerTest, EmptyTaskList) { 371 TEST_F(OrderedSimpleTaskRunnerTest, EmptyTaskList) {
374 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), ""); 372 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunPendingTasks(), "");
375 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilIdle(), ""); 373 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilIdle(), "");
376 374
377 ASSERT_EQ(base::TimeTicks(), now_src_->Now()); 375 ASSERT_EQ(base::TimeTicks(), now_src_->NowTicks());
378 376
379 RUN_AND_CHECK_RESULT( 377 RUN_AND_CHECK_RESULT(
380 EXPECT_FALSE, RunUntilTime(base::TimeTicks::FromInternalValue(100)), ""); 378 EXPECT_FALSE, RunUntilTime(base::TimeTicks::FromInternalValue(100)), "");
381 EXPECT_EQ(base::TimeTicks::FromInternalValue(100), now_src_->Now()); 379 EXPECT_EQ(base::TimeTicks::FromInternalValue(100), now_src_->NowTicks());
382 380
383 RUN_AND_CHECK_RESULT( 381 RUN_AND_CHECK_RESULT(
384 EXPECT_FALSE, RunForPeriod(base::TimeDelta::FromInternalValue(100)), ""); 382 EXPECT_FALSE, RunForPeriod(base::TimeDelta::FromInternalValue(100)), "");
385 EXPECT_EQ(base::TimeTicks::FromInternalValue(200), now_src_->Now()); 383 EXPECT_EQ(base::TimeTicks::FromInternalValue(200), now_src_->NowTicks());
386 384
387 base::Callback<bool(void)> return_true = base::Bind(&ReturnTrue); 385 base::Callback<bool(void)> return_true = base::Bind(&ReturnTrue);
388 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunTasksWhile(return_true), ""); 386 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunTasksWhile(return_true), "");
389 } 387 }
390 388
391 TEST_F(OrderedSimpleTaskRunnerTest, RunPendingTasksTimeout) { 389 TEST_F(OrderedSimpleTaskRunnerTest, RunPendingTasksTimeout) {
392 PostTask(1, base::TimeDelta()); 390 PostTask(1, base::TimeDelta());
393 PostTask(2, base::TimeDelta()); 391 PostTask(2, base::TimeDelta());
394 PostTask(3, base::TimeDelta()); 392 PostTask(3, base::TimeDelta());
395 PostTask(4, base::TimeDelta()); 393 PostTask(4, base::TimeDelta());
(...skipping 25 matching lines...) Expand all
421 419
422 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilTimeout) { 420 TEST_F(OrderedSimpleTaskRunnerTest, RunUntilTimeout) {
423 base::TimeTicks run_to = base::TimeTicks() + base::TimeDelta::FromSeconds(1); 421 base::TimeTicks run_to = base::TimeTicks() + base::TimeDelta::FromSeconds(1);
424 422
425 PostTask(1, base::TimeDelta::FromMilliseconds(1)); 423 PostTask(1, base::TimeDelta::FromMilliseconds(1));
426 PostTask(2, base::TimeDelta::FromMilliseconds(2)); 424 PostTask(2, base::TimeDelta::FromMilliseconds(2));
427 PostTask(3, base::TimeDelta::FromMilliseconds(3)); 425 PostTask(3, base::TimeDelta::FromMilliseconds(3));
428 PostTask(4, base::TimeDelta::FromMilliseconds(4)); 426 PostTask(4, base::TimeDelta::FromMilliseconds(4));
429 PostTask(5, base::TimeDelta::FromMilliseconds(5)); 427 PostTask(5, base::TimeDelta::FromMilliseconds(5));
430 428
431 EXPECT_EQ(base::TimeTicks(), now_src_->Now()); 429 EXPECT_EQ(base::TimeTicks(), now_src_->NowTicks());
432 task_runner_->SetRunTaskLimit(3); 430 task_runner_->SetRunTaskLimit(3);
433 RUN_AND_CHECK_RESULT( 431 RUN_AND_CHECK_RESULT(
434 EXPECT_TRUE, RunUntilTime(run_to), "1(1ms) 2(2ms) 3(3ms)"); 432 EXPECT_TRUE, RunUntilTime(run_to), "1(1ms) 2(2ms) 3(3ms)");
435 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(3), 433 EXPECT_EQ(base::TimeTicks() + base::TimeDelta::FromMilliseconds(3),
436 now_src_->Now()); 434 now_src_->NowTicks());
437 435
438 task_runner_->SetRunTaskLimit(0); 436 task_runner_->SetRunTaskLimit(0);
439 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunUntilTime(run_to), ""); 437 RUN_AND_CHECK_RESULT(EXPECT_TRUE, RunUntilTime(run_to), "");
440 438
441 task_runner_->SetRunTaskLimit(100); 439 task_runner_->SetRunTaskLimit(100);
442 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_to), "4(4ms) 5(5ms)"); 440 RUN_AND_CHECK_RESULT(EXPECT_FALSE, RunUntilTime(run_to), "4(4ms) 5(5ms)");
443 EXPECT_EQ(run_to, now_src_->Now()); 441 EXPECT_EQ(run_to, now_src_->NowTicks());
444 } 442 }
445 443
446 } // namespace cc 444 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698