| 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 "components/scheduler/child/idle_helper.h" | 5 #include "components/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 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 RunUntilIdle(); | 341 RunUntilIdle(); |
| 342 EXPECT_EQ(0, run_count); | 342 EXPECT_EQ(0, run_count); |
| 343 } | 343 } |
| 344 | 344 |
| 345 TEST_F(IdleHelperTest, TestRepostingIdleTask) { | 345 TEST_F(IdleHelperTest, TestRepostingIdleTask) { |
| 346 base::TimeTicks actual_deadline; | 346 base::TimeTicks actual_deadline; |
| 347 int run_count = 0; | 347 int run_count = 0; |
| 348 | 348 |
| 349 max_idle_task_reposts = 2; | 349 max_idle_task_reposts = 2; |
| 350 idle_task_runner_->PostIdleTask( | 350 idle_task_runner_->PostIdleTask( |
| 351 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_, | 351 FROM_HERE, |
| 352 &run_count, &actual_deadline)); | 352 base::Bind(&RepostingIdleTestTask, base::RetainedRef(idle_task_runner_), |
| 353 &run_count, &actual_deadline)); |
| 353 idle_helper_->StartIdlePeriod( | 354 idle_helper_->StartIdlePeriod( |
| 354 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(), | 355 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(), |
| 355 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10)); | 356 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10)); |
| 356 RunUntilIdle(); | 357 RunUntilIdle(); |
| 357 EXPECT_EQ(1, run_count); | 358 EXPECT_EQ(1, run_count); |
| 358 | 359 |
| 359 // Reposted tasks shouldn't run until next idle period. | 360 // Reposted tasks shouldn't run until next idle period. |
| 360 RunUntilIdle(); | 361 RunUntilIdle(); |
| 361 EXPECT_EQ(1, run_count); | 362 EXPECT_EQ(1, run_count); |
| 362 | 363 |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 | 664 |
| 664 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | 665 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 665 .Times(4) | 666 .Times(4) |
| 666 .WillRepeatedly(Return(true)); | 667 .WillRepeatedly(Return(true)); |
| 667 ExpectIdlePeriodStartsAndEnds(AtLeast(2)); | 668 ExpectIdlePeriodStartsAndEnds(AtLeast(2)); |
| 668 | 669 |
| 669 max_idle_task_reposts = 3; | 670 max_idle_task_reposts = 3; |
| 670 base::TimeTicks clock_before(clock_->NowTicks()); | 671 base::TimeTicks clock_before(clock_->NowTicks()); |
| 671 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); | 672 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); |
| 672 idle_task_runner_->PostIdleTask( | 673 idle_task_runner_->PostIdleTask( |
| 673 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask, | 674 FROM_HERE, |
| 674 idle_task_runner_, &run_count, clock_.get(), | 675 base::Bind(&RepostingUpdateClockIdleTestTask, |
| 675 idle_task_runtime, &actual_deadlines)); | 676 base::RetainedRef(idle_task_runner_), &run_count, clock_.get(), |
| 677 idle_task_runtime, &actual_deadlines)); |
| 676 | 678 |
| 677 // Check each idle task runs in their own idle period. | 679 // Check each idle task runs in their own idle period. |
| 678 idle_helper_->EnableLongIdlePeriod(); | 680 idle_helper_->EnableLongIdlePeriod(); |
| 679 RunUntilIdle(); | 681 RunUntilIdle(); |
| 680 EXPECT_EQ(3, run_count); | 682 EXPECT_EQ(3, run_count); |
| 681 EXPECT_THAT( | 683 EXPECT_THAT( |
| 682 actual_deadlines, | 684 actual_deadlines, |
| 683 testing::ElementsAre( | 685 testing::ElementsAre( |
| 684 clock_before + maximum_idle_period_duration(), | 686 clock_before + maximum_idle_period_duration(), |
| 685 clock_before + idle_task_runtime + maximum_idle_period_duration(), | 687 clock_before + idle_task_runtime + maximum_idle_period_duration(), |
| 686 clock_before + (2 * idle_task_runtime) + | 688 clock_before + (2 * idle_task_runtime) + |
| 687 maximum_idle_period_duration())); | 689 maximum_idle_period_duration())); |
| 688 | 690 |
| 689 max_idle_task_reposts = 5; | 691 max_idle_task_reposts = 5; |
| 690 idle_task_runner_->PostIdleTask( | 692 idle_task_runner_->PostIdleTask( |
| 691 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask, | 693 FROM_HERE, |
| 692 idle_task_runner_, &run_count, clock_.get(), | 694 base::Bind(&RepostingUpdateClockIdleTestTask, |
| 693 idle_task_runtime, &actual_deadlines)); | 695 base::RetainedRef(idle_task_runner_), &run_count, clock_.get(), |
| 696 idle_task_runtime, &actual_deadlines)); |
| 694 idle_task_runner_->PostIdleTask( | 697 idle_task_runner_->PostIdleTask( |
| 695 FROM_HERE, | 698 FROM_HERE, |
| 696 base::Bind(&EndIdlePeriodIdleTask, base::Unretained(idle_helper_.get()))); | 699 base::Bind(&EndIdlePeriodIdleTask, base::Unretained(idle_helper_.get()))); |
| 697 | 700 |
| 698 // Ensure that reposting tasks stop after EndIdlePeriod is called. | 701 // Ensure that reposting tasks stop after EndIdlePeriod is called. |
| 699 RunUntilIdle(); | 702 RunUntilIdle(); |
| 700 EXPECT_EQ(4, run_count); | 703 EXPECT_EQ(4, run_count); |
| 701 } | 704 } |
| 702 | 705 |
| 703 TEST_F(IdleHelperTest, TestLongIdlePeriodDoesNotWakeScheduler) { | 706 TEST_F(IdleHelperTest, TestLongIdlePeriodDoesNotWakeScheduler) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 779 std::vector<base::TimeTicks> actual_deadlines; | 782 std::vector<base::TimeTicks> actual_deadlines; |
| 780 int run_count = 0; | 783 int run_count = 0; |
| 781 | 784 |
| 782 base::TimeTicks clock_before(clock_->NowTicks()); | 785 base::TimeTicks clock_before(clock_->NowTicks()); |
| 783 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); | 786 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); |
| 784 | 787 |
| 785 // The second idle period should happen immediately after the first the | 788 // The second idle period should happen immediately after the first the |
| 786 // they have max deadlines. | 789 // they have max deadlines. |
| 787 max_idle_task_reposts = 2; | 790 max_idle_task_reposts = 2; |
| 788 idle_task_runner_->PostIdleTask( | 791 idle_task_runner_->PostIdleTask( |
| 789 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask, | 792 FROM_HERE, |
| 790 idle_task_runner_, &run_count, clock_.get(), | 793 base::Bind(&RepostingUpdateClockIdleTestTask, |
| 791 idle_task_runtime, &actual_deadlines)); | 794 base::RetainedRef(idle_task_runner_), &run_count, clock_.get(), |
| 795 idle_task_runtime, &actual_deadlines)); |
| 792 | 796 |
| 793 idle_helper_->EnableLongIdlePeriod(); | 797 idle_helper_->EnableLongIdlePeriod(); |
| 794 RunUntilIdle(); | 798 RunUntilIdle(); |
| 795 EXPECT_EQ(2, run_count); | 799 EXPECT_EQ(2, run_count); |
| 796 EXPECT_THAT( | 800 EXPECT_THAT( |
| 797 actual_deadlines, | 801 actual_deadlines, |
| 798 testing::ElementsAre( | 802 testing::ElementsAre( |
| 799 clock_before + maximum_idle_period_duration(), | 803 clock_before + maximum_idle_period_duration(), |
| 800 clock_before + idle_task_runtime + maximum_idle_period_duration())); | 804 clock_before + idle_task_runtime + maximum_idle_period_duration())); |
| 801 } | 805 } |
| 802 | 806 |
| 803 TEST_F(IdleHelperTest, TestLongIdlePeriodRestartWaitsIfNotMaxDeadline) { | 807 TEST_F(IdleHelperTest, TestLongIdlePeriodRestartWaitsIfNotMaxDeadline) { |
| 804 base::TimeTicks actual_deadline; | 808 base::TimeTicks actual_deadline; |
| 805 int run_count = 0; | 809 int run_count = 0; |
| 806 | 810 |
| 807 base::TimeDelta pending_task_delay(base::TimeDelta::FromMilliseconds(20)); | 811 base::TimeDelta pending_task_delay(base::TimeDelta::FromMilliseconds(20)); |
| 808 base::TimeDelta idle_task_duration(base::TimeDelta::FromMilliseconds(10)); | 812 base::TimeDelta idle_task_duration(base::TimeDelta::FromMilliseconds(10)); |
| 809 base::TimeTicks expected_deadline(clock_->NowTicks() + pending_task_delay + | 813 base::TimeTicks expected_deadline(clock_->NowTicks() + pending_task_delay + |
| 810 maximum_idle_period_duration() + | 814 maximum_idle_period_duration() + |
| 811 retry_enable_long_idle_period_delay()); | 815 retry_enable_long_idle_period_delay()); |
| 812 | 816 |
| 813 // Post delayed task to ensure idle period doesn't have a max deadline. | 817 // Post delayed task to ensure idle period doesn't have a max deadline. |
| 814 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), | 818 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), |
| 815 pending_task_delay); | 819 pending_task_delay); |
| 816 | 820 |
| 817 max_idle_task_reposts = 2; | 821 max_idle_task_reposts = 2; |
| 818 idle_task_runner_->PostIdleTask( | 822 idle_task_runner_->PostIdleTask( |
| 819 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_, | 823 FROM_HERE, |
| 820 &run_count, &actual_deadline)); | 824 base::Bind(&RepostingIdleTestTask, base::RetainedRef(idle_task_runner_), |
| 825 &run_count, &actual_deadline)); |
| 821 idle_helper_->EnableLongIdlePeriod(); | 826 idle_helper_->EnableLongIdlePeriod(); |
| 822 RunUntilIdle(); | 827 RunUntilIdle(); |
| 823 EXPECT_EQ(1, run_count); | 828 EXPECT_EQ(1, run_count); |
| 824 clock_->Advance(idle_task_duration); | 829 clock_->Advance(idle_task_duration); |
| 825 | 830 |
| 826 // Next idle period shouldn't happen until the pending task has been run. | 831 // Next idle period shouldn't happen until the pending task has been run. |
| 827 RunUntilIdle(); | 832 RunUntilIdle(); |
| 828 EXPECT_EQ(1, run_count); | 833 EXPECT_EQ(1, run_count); |
| 829 | 834 |
| 830 // Once the pending task is run the new idle period should start. | 835 // Once the pending task is run the new idle period should start. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 850 // There shouldn't be any delayed tasks posted by the idle helper when paused. | 855 // There shouldn't be any delayed tasks posted by the idle helper when paused. |
| 851 base::TimeTicks next_pending_delayed_task; | 856 base::TimeTicks next_pending_delayed_task; |
| 852 EXPECT_FALSE(scheduler_helper_->real_time_domain()->NextScheduledRunTime( | 857 EXPECT_FALSE(scheduler_helper_->real_time_domain()->NextScheduledRunTime( |
| 853 &next_pending_delayed_task)); | 858 &next_pending_delayed_task)); |
| 854 | 859 |
| 855 // Posting a task should transition us to the an active state. | 860 // Posting a task should transition us to the an active state. |
| 856 max_idle_task_reposts = 2; | 861 max_idle_task_reposts = 2; |
| 857 base::TimeTicks clock_before(clock_->NowTicks()); | 862 base::TimeTicks clock_before(clock_->NowTicks()); |
| 858 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); | 863 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); |
| 859 idle_task_runner_->PostIdleTask( | 864 idle_task_runner_->PostIdleTask( |
| 860 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask, | 865 FROM_HERE, |
| 861 idle_task_runner_, &run_count, clock_.get(), | 866 base::Bind(&RepostingUpdateClockIdleTestTask, |
| 862 idle_task_runtime, &actual_deadlines)); | 867 base::RetainedRef(idle_task_runner_), &run_count, clock_.get(), |
| 868 idle_task_runtime, &actual_deadlines)); |
| 863 RunUntilIdle(); | 869 RunUntilIdle(); |
| 864 EXPECT_EQ(2, run_count); | 870 EXPECT_EQ(2, run_count); |
| 865 EXPECT_THAT( | 871 EXPECT_THAT( |
| 866 actual_deadlines, | 872 actual_deadlines, |
| 867 testing::ElementsAre( | 873 testing::ElementsAre( |
| 868 clock_before + maximum_idle_period_duration(), | 874 clock_before + maximum_idle_period_duration(), |
| 869 clock_before + idle_task_runtime + maximum_idle_period_duration())); | 875 clock_before + idle_task_runtime + maximum_idle_period_duration())); |
| 870 | 876 |
| 871 // Once all task have been run we should go back to the paused state. | 877 // Once all task have been run we should go back to the paused state. |
| 872 CheckIdlePeriodStateIs("in_long_idle_period_paused"); | 878 CheckIdlePeriodStateIs("in_long_idle_period_paused"); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 996 IdleHelperWithQuiescencePeriodTestWithIdlePeriodObserver); | 1002 IdleHelperWithQuiescencePeriodTestWithIdlePeriodObserver); |
| 997 }; | 1003 }; |
| 998 | 1004 |
| 999 | 1005 |
| 1000 TEST_F(IdleHelperWithQuiescencePeriodTest, | 1006 TEST_F(IdleHelperWithQuiescencePeriodTest, |
| 1001 LongIdlePeriodStartsImmediatelyIfQuiescent) { | 1007 LongIdlePeriodStartsImmediatelyIfQuiescent) { |
| 1002 base::TimeTicks actual_deadline; | 1008 base::TimeTicks actual_deadline; |
| 1003 int run_count = 0; | 1009 int run_count = 0; |
| 1004 max_idle_task_reposts = 1; | 1010 max_idle_task_reposts = 1; |
| 1005 idle_task_runner_->PostIdleTask( | 1011 idle_task_runner_->PostIdleTask( |
| 1006 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_, | 1012 FROM_HERE, |
| 1007 &run_count, &actual_deadline)); | 1013 base::Bind(&RepostingIdleTestTask, base::RetainedRef(idle_task_runner_), |
| 1014 &run_count, &actual_deadline)); |
| 1008 | 1015 |
| 1009 idle_helper_->EnableLongIdlePeriod(); | 1016 idle_helper_->EnableLongIdlePeriod(); |
| 1010 RunUntilIdle(); | 1017 RunUntilIdle(); |
| 1011 | 1018 |
| 1012 EXPECT_EQ(1, run_count); | 1019 EXPECT_EQ(1, run_count); |
| 1013 } | 1020 } |
| 1014 | 1021 |
| 1015 TEST_F(IdleHelperWithQuiescencePeriodTestWithIdlePeriodObserver, | 1022 TEST_F(IdleHelperWithQuiescencePeriodTestWithIdlePeriodObserver, |
| 1016 LongIdlePeriodDoesNotStartsImmediatelyIfBusy) { | 1023 LongIdlePeriodDoesNotStartsImmediatelyIfBusy) { |
| 1017 MakeNonQuiescent(); | 1024 MakeNonQuiescent(); |
| 1018 EXPECT_CALL(*idle_helper_, OnIdlePeriodStarted()).Times(0); | 1025 EXPECT_CALL(*idle_helper_, OnIdlePeriodStarted()).Times(0); |
| 1019 EXPECT_CALL(*idle_helper_, OnIdlePeriodEnded()).Times(0); | 1026 EXPECT_CALL(*idle_helper_, OnIdlePeriodEnded()).Times(0); |
| 1020 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)).Times(0); | 1027 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)).Times(0); |
| 1021 EXPECT_CALL(*idle_helper_, IsNotQuiescent()).Times(AtLeast(1)); | 1028 EXPECT_CALL(*idle_helper_, IsNotQuiescent()).Times(AtLeast(1)); |
| 1022 | 1029 |
| 1023 base::TimeTicks actual_deadline; | 1030 base::TimeTicks actual_deadline; |
| 1024 int run_count = 0; | 1031 int run_count = 0; |
| 1025 max_idle_task_reposts = 1; | 1032 max_idle_task_reposts = 1; |
| 1026 idle_task_runner_->PostIdleTask( | 1033 idle_task_runner_->PostIdleTask( |
| 1027 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_, | 1034 FROM_HERE, |
| 1028 &run_count, &actual_deadline)); | 1035 base::Bind(&RepostingIdleTestTask, base::RetainedRef(idle_task_runner_), |
| 1036 &run_count, &actual_deadline)); |
| 1029 | 1037 |
| 1030 idle_helper_->EnableLongIdlePeriod(); | 1038 idle_helper_->EnableLongIdlePeriod(); |
| 1031 RunUntilIdle(); | 1039 RunUntilIdle(); |
| 1032 | 1040 |
| 1033 EXPECT_EQ(0, run_count); | 1041 EXPECT_EQ(0, run_count); |
| 1034 | 1042 |
| 1035 scheduler_helper_->Shutdown(); | 1043 scheduler_helper_->Shutdown(); |
| 1036 } | 1044 } |
| 1037 | 1045 |
| 1038 TEST_F(IdleHelperWithQuiescencePeriodTest, | 1046 TEST_F(IdleHelperWithQuiescencePeriodTest, |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1141 EXPECT_EQ(0, run_count); | 1149 EXPECT_EQ(0, run_count); |
| 1142 | 1150 |
| 1143 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), | 1151 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), |
| 1144 more_than_min_deadline_duration); | 1152 more_than_min_deadline_duration); |
| 1145 idle_helper_->EnableLongIdlePeriod(); | 1153 idle_helper_->EnableLongIdlePeriod(); |
| 1146 RunUntilIdle(); | 1154 RunUntilIdle(); |
| 1147 EXPECT_EQ(1, run_count); | 1155 EXPECT_EQ(1, run_count); |
| 1148 } | 1156 } |
| 1149 | 1157 |
| 1150 } // namespace scheduler | 1158 } // namespace scheduler |
| OLD | NEW |