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

Side by Side Diff: components/scheduler/child/worker_scheduler_impl_unittest.cc

Issue 1132753008: Replaced TestNowSource with SimpleTestTickClock. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Incorporated review comments: scoped leaking clocks, refactored num_now_calls_ functionality 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 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 WakeUpTask(std::vector<std::string>* timeline, cc::TestNowSource* clock) { 29 void WakeUpTask(std::vector<std::string>* timeline,
30 base::SimpleTestTickClock* clock) {
30 if (timeline) { 31 if (timeline) {
31 timeline->push_back(base::StringPrintf("run WakeUpTask @ %d", 32 timeline->push_back(base::StringPrintf(
32 TimeTicksToIntMs(clock->Now()))); 33 "run WakeUpTask @ %d", TimeTicksToIntMs(clock->NowTicks())));
33 } 34 }
34 } 35 }
35 36
36 void RecordTimelineTask(std::vector<std::string>* timeline, 37 void RecordTimelineTask(std::vector<std::string>* timeline,
37 cc::TestNowSource* clock) { 38 base::SimpleTestTickClock* clock) {
38 timeline->push_back(base::StringPrintf("run RecordTimelineTask @ %d", 39 timeline->push_back(base::StringPrintf("run RecordTimelineTask @ %d",
39 TimeTicksToIntMs(clock->Now()))); 40 TimeTicksToIntMs(clock->NowTicks())));
40 } 41 }
41 42
42 void AppendToVectorTestTask(std::vector<std::string>* vector, 43 void AppendToVectorTestTask(std::vector<std::string>* vector,
43 std::string value) { 44 std::string value) {
44 vector->push_back(value); 45 vector->push_back(value);
45 } 46 }
46 47
47 void AppendToVectorIdleTestTask(std::vector<std::string>* vector, 48 void AppendToVectorIdleTestTask(std::vector<std::string>* vector,
48 std::string value, 49 std::string value,
49 base::TimeTicks deadline) { 50 base::TimeTicks deadline) {
50 AppendToVectorTestTask(vector, value); 51 AppendToVectorTestTask(vector, value);
51 } 52 }
52 53
53 void TimelineIdleTestTask(std::vector<std::string>* timeline, 54 void TimelineIdleTestTask(std::vector<std::string>* timeline,
54 base::TimeTicks deadline) { 55 base::TimeTicks deadline) {
55 timeline->push_back(base::StringPrintf("run TimelineIdleTestTask deadline %d", 56 timeline->push_back(base::StringPrintf("run TimelineIdleTestTask deadline %d",
56 TimeTicksToIntMs(deadline))); 57 TimeTicksToIntMs(deadline)));
57 } 58 }
58 59
59 }; // namespace 60 }; // namespace
60 61
61 class WorkerSchedulerImplForTest : public WorkerSchedulerImpl { 62 class WorkerSchedulerImplForTest : public WorkerSchedulerImpl {
62 public: 63 public:
63 WorkerSchedulerImplForTest( 64 WorkerSchedulerImplForTest(
64 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner, 65 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner,
65 scoped_refptr<cc::TestNowSource> clock_) 66 base::SimpleTestTickClock* clock_)
66 : WorkerSchedulerImpl(main_task_runner), 67 : WorkerSchedulerImpl(main_task_runner),
67 clock_(clock_), 68 clock_(clock_),
68 timeline_(nullptr) {} 69 timeline_(nullptr) {}
69 70
70 void RecordTimelineEvents(std::vector<std::string>* timeline) { 71 void RecordTimelineEvents(std::vector<std::string>* timeline) {
71 timeline_ = timeline; 72 timeline_ = timeline;
72 } 73 }
73 74
74 private: 75 private:
75 bool CanEnterLongIdlePeriod( 76 bool CanEnterLongIdlePeriod(
76 base::TimeTicks now, 77 base::TimeTicks now,
77 base::TimeDelta* next_long_idle_period_delay_out) override { 78 base::TimeDelta* next_long_idle_period_delay_out) override {
78 if (timeline_) { 79 if (timeline_) {
79 timeline_->push_back(base::StringPrintf("CanEnterLongIdlePeriod @ %d", 80 timeline_->push_back(base::StringPrintf("CanEnterLongIdlePeriod @ %d",
80 TimeTicksToIntMs(now))); 81 TimeTicksToIntMs(now)));
81 } 82 }
82 return WorkerSchedulerImpl::CanEnterLongIdlePeriod( 83 return WorkerSchedulerImpl::CanEnterLongIdlePeriod(
83 now, next_long_idle_period_delay_out); 84 now, next_long_idle_period_delay_out);
84 } 85 }
85 86
86 void IsNotQuiescent() override { 87 void IsNotQuiescent() override {
87 if (timeline_) { 88 if (timeline_) {
88 timeline_->push_back(base::StringPrintf("IsNotQuiescent @ %d", 89 timeline_->push_back(base::StringPrintf(
89 TimeTicksToIntMs(clock_->Now()))); 90 "IsNotQuiescent @ %d", TimeTicksToIntMs(clock_->NowTicks())));
90 } 91 }
91 WorkerSchedulerImpl::IsNotQuiescent(); 92 WorkerSchedulerImpl::IsNotQuiescent();
92 } 93 }
93 94
94 scoped_refptr<cc::TestNowSource> clock_; 95 base::SimpleTestTickClock* clock_;
95 std::vector<std::string>* timeline_; // NOT OWNED 96 std::vector<std::string>* timeline_; // NOT OWNED
96 }; 97 };
97 98
98 class WorkerSchedulerImplTest : public testing::Test { 99 class WorkerSchedulerImplTest : public testing::Test {
99 public: 100 public:
100 WorkerSchedulerImplTest() 101 WorkerSchedulerImplTest()
101 : clock_(cc::TestNowSource::Create(5000)), 102 : clock_(new base::SimpleTestTickClock()), timeline_(nullptr) {
102 mock_task_runner_(new cc::OrderedSimpleTaskRunner(clock_, true)), 103 clock_->Advance(base::TimeDelta::FromInternalValue(5000));
103 nestable_task_runner_( 104 mock_task_runner_ = new cc::OrderedSimpleTaskRunner(clock_.get(), true);
104 NestableTaskRunnerForTest::Create(mock_task_runner_)), 105 nestable_task_runner_ =
105 scheduler_( 106 NestableTaskRunnerForTest::Create(mock_task_runner_);
106 new WorkerSchedulerImplForTest(nestable_task_runner_, clock_)), 107 scheduler_ = make_scoped_ptr(
107 timeline_(nullptr) { 108 new WorkerSchedulerImplForTest(nestable_task_runner_, clock_.get()));
108 scheduler_->GetSchedulerHelperForTesting()->SetTimeSourceForTesting( 109 scheduler_->GetSchedulerHelperForTesting()->SetTimeSourceForTesting(
109 make_scoped_ptr(new TestTimeSource(clock_))); 110 make_scoped_ptr(new TestTimeSource(clock_.get())));
110 scheduler_->GetSchedulerHelperForTesting() 111 scheduler_->GetSchedulerHelperForTesting()
111 ->GetTaskQueueManagerForTesting() 112 ->GetTaskQueueManagerForTesting()
112 ->SetTimeSourceForTesting(make_scoped_ptr(new TestTimeSource(clock_))); 113 ->SetTimeSourceForTesting(
114 make_scoped_ptr(new TestTimeSource(clock_.get())));
113 } 115 }
114 116
115 ~WorkerSchedulerImplTest() override {} 117 ~WorkerSchedulerImplTest() override {}
116 118
117 void TearDown() override { 119 void TearDown() override {
118 // Check that all tests stop posting tasks. 120 // Check that all tests stop posting tasks.
119 while (mock_task_runner_->RunUntilIdle()) { 121 while (mock_task_runner_->RunUntilIdle()) {
120 } 122 }
121 } 123 }
122 124
123 void Init() { 125 void Init() {
124 scheduler_->Init(); 126 scheduler_->Init();
125 default_task_runner_ = scheduler_->DefaultTaskRunner(); 127 default_task_runner_ = scheduler_->DefaultTaskRunner();
126 idle_task_runner_ = scheduler_->IdleTaskRunner(); 128 idle_task_runner_ = scheduler_->IdleTaskRunner();
127 timeline_ = nullptr; 129 timeline_ = nullptr;
128 } 130 }
129 131
130 void RecordTimelineEvents(std::vector<std::string>* timeline) { 132 void RecordTimelineEvents(std::vector<std::string>* timeline) {
131 timeline_ = timeline; 133 timeline_ = timeline;
132 scheduler_->RecordTimelineEvents(timeline); 134 scheduler_->RecordTimelineEvents(timeline);
133 } 135 }
134 136
135 void RunUntilIdle() { 137 void RunUntilIdle() {
136 if (timeline_) { 138 if (timeline_) {
137 timeline_->push_back(base::StringPrintf("RunUntilIdle begin @ %d", 139 timeline_->push_back(base::StringPrintf(
138 TimeTicksToIntMs(clock_->Now()))); 140 "RunUntilIdle begin @ %d", TimeTicksToIntMs(clock_->NowTicks())));
139 } 141 }
140 mock_task_runner_->RunUntilIdle(); 142 mock_task_runner_->RunUntilIdle();
141 if (timeline_) { 143 if (timeline_) {
142 timeline_->push_back(base::StringPrintf("RunUntilIdle end @ %d", 144 timeline_->push_back(base::StringPrintf(
143 TimeTicksToIntMs(clock_->Now()))); 145 "RunUntilIdle end @ %d", TimeTicksToIntMs(clock_->NowTicks())));
144 } 146 }
145 } 147 }
146 148
147 void InitAndPostDelayedWakeupTask() { 149 void InitAndPostDelayedWakeupTask() {
148 Init(); 150 Init();
149 // WorkerSchedulerImpl::Init causes a delayed task to be posted on the 151 // WorkerSchedulerImpl::Init causes a delayed task to be posted on the
150 // after wakeup control runner. We need a task to wake the system up 152 // after wakeup control runner. We need a task to wake the system up
151 // AFTER the delay for this has expired. 153 // AFTER the delay for this has expired.
152 default_task_runner_->PostDelayedTask( 154 default_task_runner_->PostDelayedTask(
153 FROM_HERE, base::Bind(&WakeUpTask, base::Unretained(timeline_), 155 FROM_HERE, base::Bind(&WakeUpTask, base::Unretained(timeline_),
(...skipping 27 matching lines...) Expand all
181 } 183 }
182 } 184 }
183 } 185 }
184 186
185 static base::TimeDelta maximum_idle_period_duration() { 187 static base::TimeDelta maximum_idle_period_duration() {
186 return base::TimeDelta::FromMilliseconds( 188 return base::TimeDelta::FromMilliseconds(
187 IdleHelper::kMaximumIdlePeriodMillis); 189 IdleHelper::kMaximumIdlePeriodMillis);
188 } 190 }
189 191
190 protected: 192 protected:
191 scoped_refptr<cc::TestNowSource> clock_; 193 scoped_ptr<base::SimpleTestTickClock> clock_;
192 // Only one of mock_task_runner_ or message_loop_ will be set. 194 // Only one of mock_task_runner_ or message_loop_ will be set.
193 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; 195 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_;
194 196
195 scoped_refptr<NestableSingleThreadTaskRunner> nestable_task_runner_; 197 scoped_refptr<NestableSingleThreadTaskRunner> nestable_task_runner_;
196 scoped_ptr<WorkerSchedulerImplForTest> scheduler_; 198 scoped_ptr<WorkerSchedulerImplForTest> scheduler_;
197 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_; 199 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
198 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_; 200 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
199 std::vector<std::string>* timeline_; // NOT OWNED 201 std::vector<std::string>* timeline_; // NOT OWNED
200 202
201 DISALLOW_COPY_AND_ASSIGN(WorkerSchedulerImplTest); 203 DISALLOW_COPY_AND_ASSIGN(WorkerSchedulerImplTest);
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 377
376 std::vector<std::string> timeline; 378 std::vector<std::string> timeline;
377 RecordTimelineEvents(&timeline); 379 RecordTimelineEvents(&timeline);
378 380
379 // The scheduler should not run the initiate_next_long_idle_period task if 381 // The scheduler should not run the initiate_next_long_idle_period task if
380 // there are no idle tasks and no other task woke up the scheduler, thus 382 // there are no idle tasks and no other task woke up the scheduler, thus
381 // the idle period deadline shouldn't update at the end of the current long 383 // the idle period deadline shouldn't update at the end of the current long
382 // idle period. 384 // idle period.
383 base::TimeTicks idle_period_deadline = 385 base::TimeTicks idle_period_deadline =
384 scheduler_->CurrentIdleTaskDeadlineForTesting(); 386 scheduler_->CurrentIdleTaskDeadlineForTesting();
385 clock_->AdvanceNow(maximum_idle_period_duration()); 387 clock_->Advance(maximum_idle_period_duration());
386 RunUntilIdle(); 388 RunUntilIdle();
387 389
388 base::TimeTicks new_idle_period_deadline = 390 base::TimeTicks new_idle_period_deadline =
389 scheduler_->CurrentIdleTaskDeadlineForTesting(); 391 scheduler_->CurrentIdleTaskDeadlineForTesting();
390 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); 392 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline);
391 393
392 // Posting a after-wakeup idle task also shouldn't wake the scheduler or 394 // Posting a after-wakeup idle task also shouldn't wake the scheduler or
393 // initiate the next long idle period. 395 // initiate the next long idle period.
394 timeline.push_back("PostIdleTaskAfterWakeup"); 396 timeline.push_back("PostIdleTaskAfterWakeup");
395 idle_task_runner_->PostIdleTaskAfterWakeup( 397 idle_task_runner_->PostIdleTaskAfterWakeup(
(...skipping 21 matching lines...) Expand all
417 "IsNotQuiescent @ 55", // NOTE we have to wait for quiescence. 419 "IsNotQuiescent @ 55", // NOTE we have to wait for quiescence.
418 "CanEnterLongIdlePeriod @ 355", 420 "CanEnterLongIdlePeriod @ 355",
419 "run TimelineIdleTestTask deadline 405", 421 "run TimelineIdleTestTask deadline 405",
420 "CanEnterLongIdlePeriod @ 405", 422 "CanEnterLongIdlePeriod @ 405",
421 "RunUntilIdle end @ 455"}; 423 "RunUntilIdle end @ 455"};
422 424
423 EXPECT_THAT(timeline, ElementsAreArray(expected_timeline)); 425 EXPECT_THAT(timeline, ElementsAreArray(expected_timeline));
424 } 426 }
425 427
426 } // namespace scheduler 428 } // namespace scheduler
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698