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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 | 113 |
114 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() { | 114 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() { |
115 } | 115 } |
116 | 116 |
117 TestMockTimeTaskRunner::TestOrderedPendingTask& | 117 TestMockTimeTaskRunner::TestOrderedPendingTask& |
118 TestMockTimeTaskRunner::TestOrderedPendingTask::operator=( | 118 TestMockTimeTaskRunner::TestOrderedPendingTask::operator=( |
119 TestOrderedPendingTask&&) = default; | 119 TestOrderedPendingTask&&) = default; |
120 | 120 |
121 // TestMockTimeTaskRunner ----------------------------------------------------- | 121 // TestMockTimeTaskRunner ----------------------------------------------------- |
122 | 122 |
123 // TODO(gab): This should also set the SequenceToken for the current thread. | |
124 // Ref. TestMockTimeTaskRunner::RunsTasksOnCurrentThread(). | |
125 TestMockTimeTaskRunner::ScopedContext::ScopedContext( | |
126 scoped_refptr<TestMockTimeTaskRunner> scope) | |
127 : task_runner_handle_(scope) { | |
danakj
2017/02/16 17:57:24
move()
gab
2017/02/16 21:03:36
Can't because... (next comment)
| |
128 scope->RunUntilIdle(); | |
danakj
2017/02/16 17:57:24
then task_runner_handle_->
gab
2017/02/16 21:03:36
Wanted to do that too (and move above) but can't b
| |
129 } | |
130 | |
131 TestMockTimeTaskRunner::ScopedContext::~ScopedContext() = default; | |
132 | |
123 bool TestMockTimeTaskRunner::TemporalOrder::operator()( | 133 bool TestMockTimeTaskRunner::TemporalOrder::operator()( |
124 const TestOrderedPendingTask& first_task, | 134 const TestOrderedPendingTask& first_task, |
125 const TestOrderedPendingTask& second_task) const { | 135 const TestOrderedPendingTask& second_task) const { |
126 if (first_task.GetTimeToRun() == second_task.GetTimeToRun()) | 136 if (first_task.GetTimeToRun() == second_task.GetTimeToRun()) |
127 return first_task.ordinal > second_task.ordinal; | 137 return first_task.ordinal > second_task.ordinal; |
128 return first_task.GetTimeToRun() > second_task.GetTimeToRun(); | 138 return first_task.GetTimeToRun() > second_task.GetTimeToRun(); |
129 } | 139 } |
130 | 140 |
131 TestMockTimeTaskRunner::TestMockTimeTaskRunner() | 141 TestMockTimeTaskRunner::TestMockTimeTaskRunner() |
132 : now_(Time::UnixEpoch()), next_task_ordinal_(0) { | 142 : now_(Time::UnixEpoch()), next_task_ordinal_(0) { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
206 DCHECK(thread_checker_.CalledOnValidThread()); | 216 DCHECK(thread_checker_.CalledOnValidThread()); |
207 return tasks_.size(); | 217 return tasks_.size(); |
208 } | 218 } |
209 | 219 |
210 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { | 220 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { |
211 DCHECK(thread_checker_.CalledOnValidThread()); | 221 DCHECK(thread_checker_.CalledOnValidThread()); |
212 return tasks_.empty() ? TimeDelta::Max() | 222 return tasks_.empty() ? TimeDelta::Max() |
213 : tasks_.top().GetTimeToRun() - now_ticks_; | 223 : tasks_.top().GetTimeToRun() - now_ticks_; |
214 } | 224 } |
215 | 225 |
226 // TODO(gab): Combine |thread_checker_| with a SequenceToken to differentiate | |
227 // between tasks running in the scope of this TestMockTimeTaskRunner and other | |
228 // task runners sharing this thread. http://crbug.com/631186 | |
216 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { | 229 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { |
217 return thread_checker_.CalledOnValidThread(); | 230 return thread_checker_.CalledOnValidThread(); |
218 } | 231 } |
219 | 232 |
220 bool TestMockTimeTaskRunner::PostDelayedTask( | 233 bool TestMockTimeTaskRunner::PostDelayedTask( |
221 const tracked_objects::Location& from_here, | 234 const tracked_objects::Location& from_here, |
222 const Closure& task, | 235 const Closure& task, |
223 TimeDelta delay) { | 236 TimeDelta delay) { |
224 AutoLock scoped_lock(tasks_lock_); | 237 AutoLock scoped_lock(tasks_lock_); |
225 tasks_.push(TestOrderedPendingTask(from_here, task, now_ticks_, delay, | 238 tasks_.push(TestOrderedPendingTask(from_here, task, now_ticks_, delay, |
(...skipping 20 matching lines...) Expand all Loading... | |
246 void TestMockTimeTaskRunner::OnAfterTimePassed() { | 259 void TestMockTimeTaskRunner::OnAfterTimePassed() { |
247 // Empty default implementation. | 260 // Empty default implementation. |
248 } | 261 } |
249 | 262 |
250 void TestMockTimeTaskRunner::OnAfterTaskRun() { | 263 void TestMockTimeTaskRunner::OnAfterTaskRun() { |
251 // Empty default implementation. | 264 // Empty default implementation. |
252 } | 265 } |
253 | 266 |
254 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) { | 267 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) { |
255 DCHECK_GE(max_delta, TimeDelta()); | 268 DCHECK_GE(max_delta, TimeDelta()); |
269 | |
270 // Multiple test task runners can share the same thread for determinism in | |
271 // unit tests. Make sure this TestMockTimeTaskRunner's tasks run in its scope. | |
272 std::unique_ptr<ThreadTaskRunnerHandle::NestedForTesting> handle_override; | |
273 if (!ThreadTaskRunnerHandle::IsSet() || | |
274 ThreadTaskRunnerHandle::Get() != this) { | |
275 handle_override = | |
276 MakeUnique<ThreadTaskRunnerHandle::NestedForTesting>(this); | |
277 } | |
278 | |
256 const TimeTicks original_now_ticks = now_ticks_; | 279 const TimeTicks original_now_ticks = now_ticks_; |
257 while (!IsElapsingStopped()) { | 280 while (!IsElapsingStopped()) { |
258 OnBeforeSelectingTask(); | 281 OnBeforeSelectingTask(); |
259 TestPendingTask task_info; | 282 TestPendingTask task_info; |
260 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info)) | 283 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info)) |
261 break; | 284 break; |
262 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will | 285 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will |
263 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not | 286 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not |
264 // moving the clock backwards in this case. | 287 // moving the clock backwards in this case. |
265 ForwardClocksUntilTickTime(task_info.GetTimeToRun()); | 288 ForwardClocksUntilTickTime(task_info.GetTimeToRun()); |
(...skipping 20 matching lines...) Expand all Loading... | |
286 // It's safe to remove const and consume |task| here, since |task| is not | 309 // It's safe to remove const and consume |task| here, since |task| is not |
287 // used for ordering the item. | 310 // used for ordering the item. |
288 *next_task = std::move(const_cast<TestOrderedPendingTask&>(tasks_.top())); | 311 *next_task = std::move(const_cast<TestOrderedPendingTask&>(tasks_.top())); |
289 tasks_.pop(); | 312 tasks_.pop(); |
290 return true; | 313 return true; |
291 } | 314 } |
292 return false; | 315 return false; |
293 } | 316 } |
294 | 317 |
295 } // namespace base | 318 } // namespace base |
OLD | NEW |