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 "base/test/test_mock_time_task_runner.h" | 5 #include "base/test/test_mock_time_task_runner.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 // in the order of being posted. | 76 // in the order of being posted. |
77 struct TestMockTimeTaskRunner::TestOrderedPendingTask | 77 struct TestMockTimeTaskRunner::TestOrderedPendingTask |
78 : public base::TestPendingTask { | 78 : public base::TestPendingTask { |
79 TestOrderedPendingTask(); | 79 TestOrderedPendingTask(); |
80 TestOrderedPendingTask(const tracked_objects::Location& location, | 80 TestOrderedPendingTask(const tracked_objects::Location& location, |
81 const Closure& task, | 81 const Closure& task, |
82 TimeTicks post_time, | 82 TimeTicks post_time, |
83 TimeDelta delay, | 83 TimeDelta delay, |
84 size_t ordinal, | 84 size_t ordinal, |
85 TestNestability nestability); | 85 TestNestability nestability); |
86 TestOrderedPendingTask(TestOrderedPendingTask&&); | |
86 ~TestOrderedPendingTask(); | 87 ~TestOrderedPendingTask(); |
87 | 88 |
89 TestOrderedPendingTask& operator=(TestOrderedPendingTask&&); | |
90 | |
88 size_t ordinal; | 91 size_t ordinal; |
92 | |
93 private: | |
94 DISALLOW_COPY_AND_ASSIGN(TestOrderedPendingTask); | |
89 }; | 95 }; |
90 | 96 |
91 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask() | 97 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask() |
92 : ordinal(0) { | 98 : ordinal(0) { |
93 } | 99 } |
94 | 100 |
95 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask( | 101 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask( |
102 TestOrderedPendingTask&&) = default; | |
103 | |
104 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask( | |
96 const tracked_objects::Location& location, | 105 const tracked_objects::Location& location, |
97 const Closure& task, | 106 const Closure& task, |
98 TimeTicks post_time, | 107 TimeTicks post_time, |
99 TimeDelta delay, | 108 TimeDelta delay, |
100 size_t ordinal, | 109 size_t ordinal, |
101 TestNestability nestability) | 110 TestNestability nestability) |
102 : base::TestPendingTask(location, task, post_time, delay, nestability), | 111 : base::TestPendingTask(location, task, post_time, delay, nestability), |
103 ordinal(ordinal) { | 112 ordinal(ordinal) {} |
104 } | |
105 | 113 |
106 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() { | 114 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() { |
107 } | 115 } |
108 | 116 |
117 TestMockTimeTaskRunner::TestOrderedPendingTask& | |
118 TestMockTimeTaskRunner::TestOrderedPendingTask::operator=( | |
119 TestOrderedPendingTask&&) = default; | |
120 | |
109 // TestMockTimeTaskRunner ----------------------------------------------------- | 121 // TestMockTimeTaskRunner ----------------------------------------------------- |
110 | 122 |
111 bool TestMockTimeTaskRunner::TemporalOrder::operator()( | 123 bool TestMockTimeTaskRunner::TemporalOrder::operator()( |
112 const TestOrderedPendingTask& first_task, | 124 const TestOrderedPendingTask& first_task, |
113 const TestOrderedPendingTask& second_task) const { | 125 const TestOrderedPendingTask& second_task) const { |
114 if (first_task.GetTimeToRun() == second_task.GetTimeToRun()) | 126 if (first_task.GetTimeToRun() == second_task.GetTimeToRun()) |
115 return first_task.ordinal > second_task.ordinal; | 127 return first_task.ordinal > second_task.ordinal; |
116 return first_task.GetTimeToRun() > second_task.GetTimeToRun(); | 128 return first_task.GetTimeToRun() > second_task.GetTimeToRun(); |
117 } | 129 } |
118 | 130 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 } | 181 } |
170 | 182 |
171 std::unique_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { | 183 std::unique_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { |
172 DCHECK(thread_checker_.CalledOnValidThread()); | 184 DCHECK(thread_checker_.CalledOnValidThread()); |
173 return MakeUnique<MockTickClock>(this); | 185 return MakeUnique<MockTickClock>(this); |
174 } | 186 } |
175 | 187 |
176 std::deque<TestPendingTask> TestMockTimeTaskRunner::TakePendingTasks() { | 188 std::deque<TestPendingTask> TestMockTimeTaskRunner::TakePendingTasks() { |
177 std::deque<TestPendingTask> tasks; | 189 std::deque<TestPendingTask> tasks; |
178 while (!tasks_.empty()) { | 190 while (!tasks_.empty()) { |
179 tasks.push_back(tasks_.top()); | 191 tasks.push_back( |
192 std::move(const_cast<TestOrderedPendingTask&>(tasks_.top()))); | |
dcheng
2017/01/24 09:15:52
Nit: comment here too.
tzik
2017/01/24 11:46:27
Done.
| |
180 tasks_.pop(); | 193 tasks_.pop(); |
181 } | 194 } |
182 return tasks; | 195 return tasks; |
183 } | 196 } |
184 | 197 |
185 bool TestMockTimeTaskRunner::HasPendingTask() const { | 198 bool TestMockTimeTaskRunner::HasPendingTask() const { |
186 DCHECK(thread_checker_.CalledOnValidThread()); | 199 DCHECK(thread_checker_.CalledOnValidThread()); |
187 return !tasks_.empty(); | 200 return !tasks_.empty(); |
188 } | 201 } |
189 | 202 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 const TimeTicks original_now_ticks = now_ticks_; | 254 const TimeTicks original_now_ticks = now_ticks_; |
242 while (!IsElapsingStopped()) { | 255 while (!IsElapsingStopped()) { |
243 OnBeforeSelectingTask(); | 256 OnBeforeSelectingTask(); |
244 TestPendingTask task_info; | 257 TestPendingTask task_info; |
245 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info)) | 258 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info)) |
246 break; | 259 break; |
247 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will | 260 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will |
248 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not | 261 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not |
249 // moving the clock backwards in this case. | 262 // moving the clock backwards in this case. |
250 ForwardClocksUntilTickTime(task_info.GetTimeToRun()); | 263 ForwardClocksUntilTickTime(task_info.GetTimeToRun()); |
251 task_info.task.Run(); | 264 std::move(task_info.task).Run(); |
252 OnAfterTaskRun(); | 265 OnAfterTaskRun(); |
253 } | 266 } |
254 } | 267 } |
255 | 268 |
256 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) { | 269 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) { |
257 if (later_ticks <= now_ticks_) | 270 if (later_ticks <= now_ticks_) |
258 return; | 271 return; |
259 | 272 |
260 now_ += later_ticks - now_ticks_; | 273 now_ += later_ticks - now_ticks_; |
261 now_ticks_ = later_ticks; | 274 now_ticks_ = later_ticks; |
262 OnAfterTimePassed(); | 275 OnAfterTimePassed(); |
263 } | 276 } |
264 | 277 |
265 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference, | 278 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference, |
266 const TimeDelta& max_delta, | 279 const TimeDelta& max_delta, |
267 TestPendingTask* next_task) { | 280 TestPendingTask* next_task) { |
268 AutoLock scoped_lock(tasks_lock_); | 281 AutoLock scoped_lock(tasks_lock_); |
269 if (!tasks_.empty() && | 282 if (!tasks_.empty() && |
270 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { | 283 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { |
271 *next_task = tasks_.top(); | 284 *next_task = std::move(const_cast<TestOrderedPendingTask&>(tasks_.top())); |
dcheng
2017/01/24 09:15:52
And here.
tzik
2017/01/24 11:46:27
Done.
| |
272 tasks_.pop(); | 285 tasks_.pop(); |
273 return true; | 286 return true; |
274 } | 287 } |
275 return false; | 288 return false; |
276 } | 289 } |
277 | 290 |
278 } // namespace base | 291 } // namespace base |
OLD | NEW |