| 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/worker_scheduler_impl.h" | 5 #include "components/scheduler/child/worker_scheduler_impl.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/test/simple_test_tick_clock.h" |
| 9 #include "cc/test/ordered_simple_task_runner.h" | 10 #include "cc/test/ordered_simple_task_runner.h" |
| 10 #include "cc/test/test_now_source.h" | |
| 11 #include "components/scheduler/child/nestable_task_runner_for_test.h" | 11 #include "components/scheduler/child/nestable_task_runner_for_test.h" |
| 12 #include "components/scheduler/child/scheduler_message_loop_delegate.h" | 12 #include "components/scheduler/child/scheduler_message_loop_delegate.h" |
| 13 #include "components/scheduler/child/test_time_source.h" | 13 #include "components/scheduler/child/test_time_source.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using testing::ElementsAreArray; | 17 using testing::ElementsAreArray; |
| 18 | 18 |
| 19 namespace scheduler { | 19 namespace scheduler { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 void NopTask() { | 22 void NopTask() { |
| 23 } | 23 } |
| 24 | 24 |
| 25 int TimeTicksToIntMs(const base::TimeTicks& time) { | 25 int TimeTicksToIntMs(const base::TimeTicks& time) { |
| 26 return static_cast<int>((time - base::TimeTicks()).InMilliseconds()); | 26 return static_cast<int>((time - base::TimeTicks()).InMilliseconds()); |
| 27 } | 27 } |
| 28 | 28 |
| 29 void RecordTimelineTask(std::vector<std::string>* timeline, | 29 void RecordTimelineTask(std::vector<std::string>* timeline, |
| 30 cc::TestNowSource* clock) { | 30 base::SimpleTestTickClock* clock) { |
| 31 timeline->push_back(base::StringPrintf("run RecordTimelineTask @ %d", | 31 timeline->push_back(base::StringPrintf("run RecordTimelineTask @ %d", |
| 32 TimeTicksToIntMs(clock->Now()))); | 32 TimeTicksToIntMs(clock->NowTicks()))); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void AppendToVectorTestTask(std::vector<std::string>* vector, | 35 void AppendToVectorTestTask(std::vector<std::string>* vector, |
| 36 std::string value) { | 36 std::string value) { |
| 37 vector->push_back(value); | 37 vector->push_back(value); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void AppendToVectorIdleTestTask(std::vector<std::string>* vector, | 40 void AppendToVectorIdleTestTask(std::vector<std::string>* vector, |
| 41 std::string value, | 41 std::string value, |
| 42 base::TimeTicks deadline) { | 42 base::TimeTicks deadline) { |
| 43 AppendToVectorTestTask(vector, value); | 43 AppendToVectorTestTask(vector, value); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void TimelineIdleTestTask(std::vector<std::string>* timeline, | 46 void TimelineIdleTestTask(std::vector<std::string>* timeline, |
| 47 base::TimeTicks deadline) { | 47 base::TimeTicks deadline) { |
| 48 timeline->push_back(base::StringPrintf("run TimelineIdleTestTask deadline %d", | 48 timeline->push_back(base::StringPrintf("run TimelineIdleTestTask deadline %d", |
| 49 TimeTicksToIntMs(deadline))); | 49 TimeTicksToIntMs(deadline))); |
| 50 } | 50 } |
| 51 | 51 |
| 52 }; // namespace | 52 }; // namespace |
| 53 | 53 |
| 54 class WorkerSchedulerImplForTest : public WorkerSchedulerImpl { | 54 class WorkerSchedulerImplForTest : public WorkerSchedulerImpl { |
| 55 public: | 55 public: |
| 56 WorkerSchedulerImplForTest( | 56 WorkerSchedulerImplForTest( |
| 57 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner, | 57 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner, |
| 58 scoped_refptr<cc::TestNowSource> clock_) | 58 base::SimpleTestTickClock* clock_) |
| 59 : WorkerSchedulerImpl(main_task_runner), | 59 : WorkerSchedulerImpl(main_task_runner), |
| 60 clock_(clock_), | 60 clock_(clock_), |
| 61 timeline_(nullptr) {} | 61 timeline_(nullptr) {} |
| 62 | 62 |
| 63 void RecordTimelineEvents(std::vector<std::string>* timeline) { | 63 void RecordTimelineEvents(std::vector<std::string>* timeline) { |
| 64 timeline_ = timeline; | 64 timeline_ = timeline; |
| 65 } | 65 } |
| 66 | 66 |
| 67 private: | 67 private: |
| 68 bool CanEnterLongIdlePeriod( | 68 bool CanEnterLongIdlePeriod( |
| 69 base::TimeTicks now, | 69 base::TimeTicks now, |
| 70 base::TimeDelta* next_long_idle_period_delay_out) override { | 70 base::TimeDelta* next_long_idle_period_delay_out) override { |
| 71 if (timeline_) { | 71 if (timeline_) { |
| 72 timeline_->push_back(base::StringPrintf("CanEnterLongIdlePeriod @ %d", | 72 timeline_->push_back(base::StringPrintf("CanEnterLongIdlePeriod @ %d", |
| 73 TimeTicksToIntMs(now))); | 73 TimeTicksToIntMs(now))); |
| 74 } | 74 } |
| 75 return WorkerSchedulerImpl::CanEnterLongIdlePeriod( | 75 return WorkerSchedulerImpl::CanEnterLongIdlePeriod( |
| 76 now, next_long_idle_period_delay_out); | 76 now, next_long_idle_period_delay_out); |
| 77 } | 77 } |
| 78 | 78 |
| 79 void IsNotQuiescent() override { | 79 void IsNotQuiescent() override { |
| 80 if (timeline_) { | 80 if (timeline_) { |
| 81 timeline_->push_back(base::StringPrintf("IsNotQuiescent @ %d", | 81 timeline_->push_back(base::StringPrintf( |
| 82 TimeTicksToIntMs(clock_->Now()))); | 82 "IsNotQuiescent @ %d", TimeTicksToIntMs(clock_->NowTicks()))); |
| 83 } | 83 } |
| 84 WorkerSchedulerImpl::IsNotQuiescent(); | 84 WorkerSchedulerImpl::IsNotQuiescent(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 scoped_refptr<cc::TestNowSource> clock_; | 87 base::SimpleTestTickClock* clock_; // NOT OWNED |
| 88 std::vector<std::string>* timeline_; // NOT OWNED | 88 std::vector<std::string>* timeline_; // NOT OWNED |
| 89 }; | 89 }; |
| 90 | 90 |
| 91 class WorkerSchedulerImplTest : public testing::Test { | 91 class WorkerSchedulerImplTest : public testing::Test { |
| 92 public: | 92 public: |
| 93 WorkerSchedulerImplTest() | 93 WorkerSchedulerImplTest() |
| 94 : clock_(cc::TestNowSource::Create(5000)), | 94 : clock_(new base::SimpleTestTickClock()), |
| 95 mock_task_runner_(new cc::OrderedSimpleTaskRunner(clock_, true)), | 95 mock_task_runner_(new cc::OrderedSimpleTaskRunner(clock_.get(), true)), |
| 96 nestable_task_runner_( | 96 nestable_task_runner_( |
| 97 NestableTaskRunnerForTest::Create(mock_task_runner_)), | 97 NestableTaskRunnerForTest::Create(mock_task_runner_)), |
| 98 scheduler_( | 98 scheduler_(new WorkerSchedulerImplForTest(nestable_task_runner_, |
| 99 new WorkerSchedulerImplForTest(nestable_task_runner_, clock_)), | 99 clock_.get())), |
| 100 timeline_(nullptr) { | 100 timeline_(nullptr) { |
| 101 clock_->Advance(base::TimeDelta::FromMicroseconds(5000)); |
| 101 scheduler_->GetSchedulerHelperForTesting()->SetTimeSourceForTesting( | 102 scheduler_->GetSchedulerHelperForTesting()->SetTimeSourceForTesting( |
| 102 make_scoped_ptr(new TestTimeSource(clock_))); | 103 make_scoped_ptr(new TestTimeSource(clock_.get()))); |
| 103 scheduler_->GetSchedulerHelperForTesting() | 104 scheduler_->GetSchedulerHelperForTesting() |
| 104 ->GetTaskQueueManagerForTesting() | 105 ->GetTaskQueueManagerForTesting() |
| 105 ->SetTimeSourceForTesting(make_scoped_ptr(new TestTimeSource(clock_))); | 106 ->SetTimeSourceForTesting( |
| 107 make_scoped_ptr(new TestTimeSource(clock_.get()))); |
| 106 } | 108 } |
| 107 | 109 |
| 108 ~WorkerSchedulerImplTest() override {} | 110 ~WorkerSchedulerImplTest() override {} |
| 109 | 111 |
| 110 void TearDown() override { | 112 void TearDown() override { |
| 111 // Check that all tests stop posting tasks. | 113 // Check that all tests stop posting tasks. |
| 112 while (mock_task_runner_->RunUntilIdle()) { | 114 while (mock_task_runner_->RunUntilIdle()) { |
| 113 } | 115 } |
| 114 } | 116 } |
| 115 | 117 |
| 116 void Init() { | 118 void Init() { |
| 117 scheduler_->Init(); | 119 scheduler_->Init(); |
| 118 default_task_runner_ = scheduler_->DefaultTaskRunner(); | 120 default_task_runner_ = scheduler_->DefaultTaskRunner(); |
| 119 idle_task_runner_ = scheduler_->IdleTaskRunner(); | 121 idle_task_runner_ = scheduler_->IdleTaskRunner(); |
| 120 timeline_ = nullptr; | 122 timeline_ = nullptr; |
| 121 } | 123 } |
| 122 | 124 |
| 123 void RecordTimelineEvents(std::vector<std::string>* timeline) { | 125 void RecordTimelineEvents(std::vector<std::string>* timeline) { |
| 124 timeline_ = timeline; | 126 timeline_ = timeline; |
| 125 scheduler_->RecordTimelineEvents(timeline); | 127 scheduler_->RecordTimelineEvents(timeline); |
| 126 } | 128 } |
| 127 | 129 |
| 128 void RunUntilIdle() { | 130 void RunUntilIdle() { |
| 129 if (timeline_) { | 131 if (timeline_) { |
| 130 timeline_->push_back(base::StringPrintf("RunUntilIdle begin @ %d", | 132 timeline_->push_back(base::StringPrintf( |
| 131 TimeTicksToIntMs(clock_->Now()))); | 133 "RunUntilIdle begin @ %d", TimeTicksToIntMs(clock_->NowTicks()))); |
| 132 } | 134 } |
| 133 mock_task_runner_->RunUntilIdle(); | 135 mock_task_runner_->RunUntilIdle(); |
| 134 if (timeline_) { | 136 if (timeline_) { |
| 135 timeline_->push_back(base::StringPrintf("RunUntilIdle end @ %d", | 137 timeline_->push_back(base::StringPrintf( |
| 136 TimeTicksToIntMs(clock_->Now()))); | 138 "RunUntilIdle end @ %d", TimeTicksToIntMs(clock_->NowTicks()))); |
| 137 } | 139 } |
| 138 } | 140 } |
| 139 | 141 |
| 140 // Helper for posting several tasks of specific types. |task_descriptor| is a | 142 // Helper for posting several tasks of specific types. |task_descriptor| is a |
| 141 // string with space delimited task identifiers. The first letter of each | 143 // string with space delimited task identifiers. The first letter of each |
| 142 // task identifier specifies the task type: | 144 // task identifier specifies the task type: |
| 143 // - 'D': Default task | 145 // - 'D': Default task |
| 144 // - 'I': Idle task | 146 // - 'I': Idle task |
| 145 void PostTestTasks(std::vector<std::string>* run_order, | 147 void PostTestTasks(std::vector<std::string>* run_order, |
| 146 const std::string& task_descriptor) { | 148 const std::string& task_descriptor) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 163 } | 165 } |
| 164 } | 166 } |
| 165 } | 167 } |
| 166 | 168 |
| 167 static base::TimeDelta maximum_idle_period_duration() { | 169 static base::TimeDelta maximum_idle_period_duration() { |
| 168 return base::TimeDelta::FromMilliseconds( | 170 return base::TimeDelta::FromMilliseconds( |
| 169 IdleHelper::kMaximumIdlePeriodMillis); | 171 IdleHelper::kMaximumIdlePeriodMillis); |
| 170 } | 172 } |
| 171 | 173 |
| 172 protected: | 174 protected: |
| 173 scoped_refptr<cc::TestNowSource> clock_; | 175 scoped_ptr<base::SimpleTestTickClock> clock_; |
| 174 // Only one of mock_task_runner_ or message_loop_ will be set. | 176 // Only one of mock_task_runner_ or message_loop_ will be set. |
| 175 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; | 177 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; |
| 176 | 178 |
| 177 scoped_refptr<NestableSingleThreadTaskRunner> nestable_task_runner_; | 179 scoped_refptr<NestableSingleThreadTaskRunner> nestable_task_runner_; |
| 178 scoped_ptr<WorkerSchedulerImplForTest> scheduler_; | 180 scoped_ptr<WorkerSchedulerImplForTest> scheduler_; |
| 179 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_; | 181 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_; |
| 180 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_; | 182 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_; |
| 181 std::vector<std::string>* timeline_; // NOT OWNED | 183 std::vector<std::string>* timeline_; // NOT OWNED |
| 182 | 184 |
| 183 DISALLOW_COPY_AND_ASSIGN(WorkerSchedulerImplTest); | 185 DISALLOW_COPY_AND_ASSIGN(WorkerSchedulerImplTest); |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 | 339 |
| 338 std::vector<std::string> timeline; | 340 std::vector<std::string> timeline; |
| 339 RecordTimelineEvents(&timeline); | 341 RecordTimelineEvents(&timeline); |
| 340 | 342 |
| 341 // The scheduler should not run the initiate_next_long_idle_period task if | 343 // The scheduler should not run the initiate_next_long_idle_period task if |
| 342 // there are no idle tasks and no other task woke up the scheduler, thus | 344 // there are no idle tasks and no other task woke up the scheduler, thus |
| 343 // the idle period deadline shouldn't update at the end of the current long | 345 // the idle period deadline shouldn't update at the end of the current long |
| 344 // idle period. | 346 // idle period. |
| 345 base::TimeTicks idle_period_deadline = | 347 base::TimeTicks idle_period_deadline = |
| 346 scheduler_->CurrentIdleTaskDeadlineForTesting(); | 348 scheduler_->CurrentIdleTaskDeadlineForTesting(); |
| 347 clock_->AdvanceNow(maximum_idle_period_duration()); | 349 clock_->Advance(maximum_idle_period_duration()); |
| 348 RunUntilIdle(); | 350 RunUntilIdle(); |
| 349 | 351 |
| 350 base::TimeTicks new_idle_period_deadline = | 352 base::TimeTicks new_idle_period_deadline = |
| 351 scheduler_->CurrentIdleTaskDeadlineForTesting(); | 353 scheduler_->CurrentIdleTaskDeadlineForTesting(); |
| 352 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); | 354 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); |
| 353 | 355 |
| 354 // Posting a after-wakeup idle task also shouldn't wake the scheduler or | 356 // Posting a after-wakeup idle task also shouldn't wake the scheduler or |
| 355 // initiate the next long idle period. | 357 // initiate the next long idle period. |
| 356 timeline.push_back("PostIdleTaskAfterWakeup"); | 358 timeline.push_back("PostIdleTaskAfterWakeup"); |
| 357 idle_task_runner_->PostIdleTaskAfterWakeup( | 359 idle_task_runner_->PostIdleTaskAfterWakeup( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 378 "run RecordTimelineTask @ 55", | 380 "run RecordTimelineTask @ 55", |
| 379 "IsNotQuiescent @ 55", // NOTE we have to wait for quiescence. | 381 "IsNotQuiescent @ 55", // NOTE we have to wait for quiescence. |
| 380 "CanEnterLongIdlePeriod @ 355", | 382 "CanEnterLongIdlePeriod @ 355", |
| 381 "run TimelineIdleTestTask deadline 405", | 383 "run TimelineIdleTestTask deadline 405", |
| 382 "RunUntilIdle end @ 355"}; | 384 "RunUntilIdle end @ 355"}; |
| 383 | 385 |
| 384 EXPECT_THAT(timeline, ElementsAreArray(expected_timeline)); | 386 EXPECT_THAT(timeline, ElementsAreArray(expected_timeline)); |
| 385 } | 387 } |
| 386 | 388 |
| 387 } // namespace scheduler | 389 } // namespace scheduler |
| OLD | NEW |