| 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/scheduler/child/idle_helper.h" | 5 #include "platform/scheduler/child/idle_helper.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 | 389 |
| 390 idle_helper_->EndIdlePeriod(); | 390 idle_helper_->EndIdlePeriod(); |
| 391 idle_helper_->StartIdlePeriod( | 391 idle_helper_->StartIdlePeriod( |
| 392 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(), | 392 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(), |
| 393 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10)); | 393 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10)); |
| 394 RunUntilIdle(); | 394 RunUntilIdle(); |
| 395 // Second task should be run on the next idle period. | 395 // Second task should be run on the next idle period. |
| 396 EXPECT_EQ(2, run_count); | 396 EXPECT_EQ(2, run_count); |
| 397 } | 397 } |
| 398 | 398 |
| 399 TEST_F(IdleHelperTest, TestPostIdleTaskAfterWakeup) { | |
| 400 base::TimeTicks deadline_in_task; | |
| 401 int run_count = 0; | |
| 402 | |
| 403 idle_task_runner_->PostIdleTaskAfterWakeup( | |
| 404 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | |
| 405 | |
| 406 idle_helper_->StartIdlePeriod( | |
| 407 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(), | |
| 408 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10)); | |
| 409 RunUntilIdle(); | |
| 410 // Shouldn't run yet as no other task woke up the scheduler. | |
| 411 EXPECT_EQ(0, run_count); | |
| 412 | |
| 413 // Must start a new idle period before idle task runs. | |
| 414 idle_task_runner_->PostIdleTaskAfterWakeup( | |
| 415 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | |
| 416 | |
| 417 idle_helper_->StartIdlePeriod( | |
| 418 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(), | |
| 419 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10)); | |
| 420 RunUntilIdle(); | |
| 421 // Another after wakeup idle task shouldn't wake the scheduler. | |
| 422 EXPECT_EQ(0, run_count); | |
| 423 | |
| 424 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | |
| 425 | |
| 426 RunUntilIdle(); | |
| 427 idle_helper_->StartIdlePeriod( | |
| 428 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(), | |
| 429 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10)); | |
| 430 RunUntilIdle(); | |
| 431 // Execution of default task queue task should trigger execution of idle task. | |
| 432 EXPECT_EQ(2, run_count); | |
| 433 } | |
| 434 | |
| 435 TEST_F(IdleHelperTest, TestPostIdleTaskAfterWakeupWhileAwake) { | |
| 436 base::TimeTicks deadline_in_task; | |
| 437 int run_count = 0; | |
| 438 | |
| 439 idle_task_runner_->PostIdleTaskAfterWakeup( | |
| 440 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | |
| 441 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | |
| 442 | |
| 443 RunUntilIdle(); | |
| 444 // Must start a new idle period before idle task runs. | |
| 445 idle_helper_->StartIdlePeriod( | |
| 446 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(), | |
| 447 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10)); | |
| 448 RunUntilIdle(); | |
| 449 // Should run as the scheduler was already awakened by the normal task. | |
| 450 EXPECT_EQ(1, run_count); | |
| 451 } | |
| 452 | |
| 453 TEST_F(IdleHelperTest, TestPostIdleTaskWakesAfterWakeupIdleTask) { | |
| 454 base::TimeTicks deadline_in_task; | |
| 455 int run_count = 0; | |
| 456 | |
| 457 idle_task_runner_->PostIdleTaskAfterWakeup( | |
| 458 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | |
| 459 idle_task_runner_->PostIdleTask( | |
| 460 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | |
| 461 | |
| 462 idle_helper_->StartIdlePeriod( | |
| 463 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(), | |
| 464 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10)); | |
| 465 RunUntilIdle(); | |
| 466 // Must start a new idle period before after-wakeup idle task runs. | |
| 467 idle_helper_->StartIdlePeriod( | |
| 468 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(), | |
| 469 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10)); | |
| 470 RunUntilIdle(); | |
| 471 // Normal idle task should wake up after-wakeup idle task. | |
| 472 EXPECT_EQ(2, run_count); | |
| 473 } | |
| 474 | |
| 475 class IdleHelperTestWithIdlePeriodObserver : public BaseIdleHelperTest { | 399 class IdleHelperTestWithIdlePeriodObserver : public BaseIdleHelperTest { |
| 476 public: | 400 public: |
| 477 IdleHelperTestWithIdlePeriodObserver() | 401 IdleHelperTestWithIdlePeriodObserver() |
| 478 : BaseIdleHelperTest(nullptr, base::TimeDelta()) {} | 402 : BaseIdleHelperTest(nullptr, base::TimeDelta()) {} |
| 479 | 403 |
| 480 ~IdleHelperTestWithIdlePeriodObserver() override {} | 404 ~IdleHelperTestWithIdlePeriodObserver() override {} |
| 481 | 405 |
| 482 void SetUp() override { | 406 void SetUp() override { |
| 483 // Don't set expectations on IdleHelper::Delegate. | 407 // Don't set expectations on IdleHelper::Delegate. |
| 484 } | 408 } |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 idle_task_runtime, &actual_deadlines)); | 622 idle_task_runtime, &actual_deadlines)); |
| 699 idle_task_runner_->PostIdleTask( | 623 idle_task_runner_->PostIdleTask( |
| 700 FROM_HERE, | 624 FROM_HERE, |
| 701 base::Bind(&EndIdlePeriodIdleTask, base::Unretained(idle_helper_.get()))); | 625 base::Bind(&EndIdlePeriodIdleTask, base::Unretained(idle_helper_.get()))); |
| 702 | 626 |
| 703 // Ensure that reposting tasks stop after EndIdlePeriod is called. | 627 // Ensure that reposting tasks stop after EndIdlePeriod is called. |
| 704 RunUntilIdle(); | 628 RunUntilIdle(); |
| 705 EXPECT_EQ(4, run_count); | 629 EXPECT_EQ(4, run_count); |
| 706 } | 630 } |
| 707 | 631 |
| 708 TEST_F(IdleHelperTest, TestLongIdlePeriodDoesNotWakeScheduler) { | |
| 709 base::TimeTicks deadline_in_task; | |
| 710 int run_count = 0; | |
| 711 | |
| 712 // Start a long idle period and get the time it should end. | |
| 713 idle_helper_->EnableLongIdlePeriod(); | |
| 714 // The scheduler should not run the enable_next_long_idle_period task if | |
| 715 // there are no idle tasks and no other task woke up the scheduler, thus | |
| 716 // the idle period deadline shouldn't update at the end of the current long | |
| 717 // idle period. | |
| 718 base::TimeTicks idle_period_deadline = CurrentIdleTaskDeadline(); | |
| 719 clock_->Advance(maximum_idle_period_duration()); | |
| 720 RunUntilIdle(); | |
| 721 | |
| 722 base::TimeTicks new_idle_period_deadline = CurrentIdleTaskDeadline(); | |
| 723 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); | |
| 724 | |
| 725 // Posting a after-wakeup idle task also shouldn't wake the scheduler or | |
| 726 // initiate the next long idle period. | |
| 727 idle_task_runner_->PostIdleTaskAfterWakeup( | |
| 728 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | |
| 729 RunUntilIdle(); | |
| 730 new_idle_period_deadline = CurrentIdleTaskDeadline(); | |
| 731 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); | |
| 732 EXPECT_EQ(0, run_count); | |
| 733 | |
| 734 // Running a normal task should initiate a new long idle period though. | |
| 735 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | |
| 736 RunUntilIdle(); | |
| 737 new_idle_period_deadline = CurrentIdleTaskDeadline(); | |
| 738 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(), | |
| 739 new_idle_period_deadline); | |
| 740 | |
| 741 EXPECT_EQ(1, run_count); | |
| 742 } | |
| 743 | |
| 744 TEST_F(IdleHelperTestWithIdlePeriodObserver, | 632 TEST_F(IdleHelperTestWithIdlePeriodObserver, |
| 745 TestLongIdlePeriodWhenNotCanEnterLongIdlePeriod) { | 633 TestLongIdlePeriodWhenNotCanEnterLongIdlePeriod) { |
| 746 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(1000); | 634 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(1000); |
| 747 base::TimeDelta halfDelay = base::TimeDelta::FromMilliseconds(500); | 635 base::TimeDelta halfDelay = base::TimeDelta::FromMilliseconds(500); |
| 748 base::TimeTicks delayOver = clock_->NowTicks() + delay; | 636 base::TimeTicks delayOver = clock_->NowTicks() + delay; |
| 749 base::TimeTicks deadline_in_task; | 637 base::TimeTicks deadline_in_task; |
| 750 int run_count = 0; | 638 int run_count = 0; |
| 751 | 639 |
| 752 ON_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | 640 ON_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 753 .WillByDefault(Invoke( | 641 .WillByDefault(Invoke( |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1150 | 1038 |
| 1151 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), | 1039 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), |
| 1152 more_than_min_deadline_duration); | 1040 more_than_min_deadline_duration); |
| 1153 idle_helper_->EnableLongIdlePeriod(); | 1041 idle_helper_->EnableLongIdlePeriod(); |
| 1154 RunUntilIdle(); | 1042 RunUntilIdle(); |
| 1155 EXPECT_EQ(1, run_count); | 1043 EXPECT_EQ(1, run_count); |
| 1156 } | 1044 } |
| 1157 | 1045 |
| 1158 } // namespace scheduler | 1046 } // namespace scheduler |
| 1159 } // namespace blink | 1047 } // namespace blink |
| OLD | NEW |