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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 } // namespace | 70 } // namespace |
71 | 71 |
72 // TestMockTimeTaskRunner::TestOrderedPendingTask ----------------------------- | 72 // TestMockTimeTaskRunner::TestOrderedPendingTask ----------------------------- |
73 | 73 |
74 // Subclass of TestPendingTask which has a strictly monotonically increasing ID | 74 // Subclass of TestPendingTask which has a strictly monotonically increasing ID |
75 // for every task, so that tasks posted with the same 'time to run' can be run | 75 // for every task, so that tasks posted with the same 'time to run' can be run |
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(TestOrderedPendingTask&&) = default; |
80 TestOrderedPendingTask(const tracked_objects::Location& location, | 81 TestOrderedPendingTask(const tracked_objects::Location& location, |
81 const Closure& task, | 82 OnceClosure task, |
82 TimeTicks post_time, | 83 TimeTicks post_time, |
83 TimeDelta delay, | 84 TimeDelta delay, |
84 size_t ordinal, | 85 size_t ordinal, |
85 TestNestability nestability); | 86 TestNestability nestability); |
86 ~TestOrderedPendingTask(); | 87 ~TestOrderedPendingTask(); |
| 88 TestOrderedPendingTask& operator=(TestOrderedPendingTask&&) = default; |
87 | 89 |
88 size_t ordinal; | 90 size_t ordinal; |
89 }; | 91 }; |
90 | 92 |
91 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask() | 93 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask() |
92 : ordinal(0) { | 94 : ordinal(0) { |
93 } | 95 } |
94 | 96 |
95 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask( | 97 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask( |
96 const tracked_objects::Location& location, | 98 const tracked_objects::Location& location, |
97 const Closure& task, | 99 OnceClosure task, |
98 TimeTicks post_time, | 100 TimeTicks post_time, |
99 TimeDelta delay, | 101 TimeDelta delay, |
100 size_t ordinal, | 102 size_t ordinal, |
101 TestNestability nestability) | 103 TestNestability nestability) |
102 : base::TestPendingTask(location, task, post_time, delay, nestability), | 104 : base::TestPendingTask(location, |
103 ordinal(ordinal) { | 105 std::move(task), |
104 } | 106 post_time, |
| 107 delay, |
| 108 nestability), |
| 109 ordinal(ordinal) {} |
105 | 110 |
106 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() { | 111 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() { |
107 } | 112 } |
108 | 113 |
109 // TestMockTimeTaskRunner ----------------------------------------------------- | 114 // TestMockTimeTaskRunner ----------------------------------------------------- |
110 | 115 |
111 bool TestMockTimeTaskRunner::TemporalOrder::operator()( | 116 bool TestMockTimeTaskRunner::TemporalOrder::operator()( |
112 const TestOrderedPendingTask& first_task, | 117 const TestOrderedPendingTask& first_task, |
113 const TestOrderedPendingTask& second_task) const { | 118 const TestOrderedPendingTask& second_task) const { |
114 if (first_task.GetTimeToRun() == second_task.GetTimeToRun()) | 119 if (first_task.GetTimeToRun() == second_task.GetTimeToRun()) |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 return tasks_.empty() ? TimeDelta::Max() | 193 return tasks_.empty() ? TimeDelta::Max() |
189 : tasks_.top().GetTimeToRun() - now_ticks_; | 194 : tasks_.top().GetTimeToRun() - now_ticks_; |
190 } | 195 } |
191 | 196 |
192 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { | 197 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { |
193 return thread_checker_.CalledOnValidThread(); | 198 return thread_checker_.CalledOnValidThread(); |
194 } | 199 } |
195 | 200 |
196 bool TestMockTimeTaskRunner::PostDelayedTask( | 201 bool TestMockTimeTaskRunner::PostDelayedTask( |
197 const tracked_objects::Location& from_here, | 202 const tracked_objects::Location& from_here, |
198 const Closure& task, | 203 OnceClosure task, |
199 TimeDelta delay) { | 204 TimeDelta delay) { |
200 AutoLock scoped_lock(tasks_lock_); | 205 AutoLock scoped_lock(tasks_lock_); |
201 tasks_.push(TestOrderedPendingTask(from_here, task, now_ticks_, delay, | 206 tasks_.push(TestOrderedPendingTask(from_here, std::move(task), now_ticks_, |
202 next_task_ordinal_++, | 207 delay, next_task_ordinal_++, |
203 TestPendingTask::NESTABLE)); | 208 TestPendingTask::NESTABLE)); |
204 return true; | 209 return true; |
205 } | 210 } |
206 | 211 |
207 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( | 212 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( |
208 const tracked_objects::Location& from_here, | 213 const tracked_objects::Location& from_here, |
209 const Closure& task, | 214 OnceClosure task, |
210 TimeDelta delay) { | 215 TimeDelta delay) { |
211 return PostDelayedTask(from_here, task, delay); | 216 return PostDelayedTask(from_here, std::move(task), delay); |
212 } | 217 } |
213 | 218 |
214 bool TestMockTimeTaskRunner::IsElapsingStopped() { | 219 bool TestMockTimeTaskRunner::IsElapsingStopped() { |
215 return false; | 220 return false; |
216 } | 221 } |
217 | 222 |
218 void TestMockTimeTaskRunner::OnBeforeSelectingTask() { | 223 void TestMockTimeTaskRunner::OnBeforeSelectingTask() { |
219 // Empty default implementation. | 224 // Empty default implementation. |
220 } | 225 } |
221 | 226 |
(...skipping 10 matching lines...) Expand all Loading... |
232 const TimeTicks original_now_ticks = now_ticks_; | 237 const TimeTicks original_now_ticks = now_ticks_; |
233 while (!IsElapsingStopped()) { | 238 while (!IsElapsingStopped()) { |
234 OnBeforeSelectingTask(); | 239 OnBeforeSelectingTask(); |
235 TestPendingTask task_info; | 240 TestPendingTask task_info; |
236 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info)) | 241 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info)) |
237 break; | 242 break; |
238 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will | 243 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will |
239 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not | 244 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not |
240 // moving the clock backwards in this case. | 245 // moving the clock backwards in this case. |
241 ForwardClocksUntilTickTime(task_info.GetTimeToRun()); | 246 ForwardClocksUntilTickTime(task_info.GetTimeToRun()); |
242 task_info.task.Run(); | 247 std::move(task_info.task).Run(); |
243 OnAfterTaskRun(); | 248 OnAfterTaskRun(); |
244 } | 249 } |
245 } | 250 } |
246 | 251 |
247 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) { | 252 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) { |
248 if (later_ticks <= now_ticks_) | 253 if (later_ticks <= now_ticks_) |
249 return; | 254 return; |
250 | 255 |
251 now_ += later_ticks - now_ticks_; | 256 now_ += later_ticks - now_ticks_; |
252 now_ticks_ = later_ticks; | 257 now_ticks_ = later_ticks; |
253 OnAfterTimePassed(); | 258 OnAfterTimePassed(); |
254 } | 259 } |
255 | 260 |
256 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference, | 261 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference, |
257 const TimeDelta& max_delta, | 262 const TimeDelta& max_delta, |
258 TestPendingTask* next_task) { | 263 TestPendingTask* next_task) { |
259 AutoLock scoped_lock(tasks_lock_); | 264 AutoLock scoped_lock(tasks_lock_); |
260 if (!tasks_.empty() && | 265 if (!tasks_.empty() && |
261 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { | 266 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { |
262 *next_task = tasks_.top(); | 267 *next_task = std::move(const_cast<TestOrderedPendingTask&>(tasks_.top())); |
263 tasks_.pop(); | 268 tasks_.pop(); |
264 return true; | 269 return true; |
265 } | 270 } |
266 return false; | 271 return false; |
267 } | 272 } |
268 | 273 |
269 } // namespace base | 274 } // namespace base |
OLD | NEW |