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" |
| 11 #include "base/threading/thread_task_runner_handle.h" |
11 #include "base/time/clock.h" | 12 #include "base/time/clock.h" |
12 #include "base/time/tick_clock.h" | 13 #include "base/time/tick_clock.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 // MockTickClock -------------------------------------------------------------- | 19 // MockTickClock -------------------------------------------------------------- |
19 | 20 |
20 // TickClock that always returns the then-current mock time ticks of | 21 // TickClock that always returns the then-current mock time ticks of |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 | 114 |
114 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() { | 115 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() { |
115 } | 116 } |
116 | 117 |
117 TestMockTimeTaskRunner::TestOrderedPendingTask& | 118 TestMockTimeTaskRunner::TestOrderedPendingTask& |
118 TestMockTimeTaskRunner::TestOrderedPendingTask::operator=( | 119 TestMockTimeTaskRunner::TestOrderedPendingTask::operator=( |
119 TestOrderedPendingTask&&) = default; | 120 TestOrderedPendingTask&&) = default; |
120 | 121 |
121 // TestMockTimeTaskRunner ----------------------------------------------------- | 122 // TestMockTimeTaskRunner ----------------------------------------------------- |
122 | 123 |
| 124 // TODO(gab): This should also set the SequenceToken for the current thread. |
| 125 // Ref. TestMockTimeTaskRunner::RunsTasksOnCurrentThread(). |
| 126 TestMockTimeTaskRunner::ScopedContext::ScopedContext( |
| 127 scoped_refptr<TestMockTimeTaskRunner> scope) |
| 128 : on_destroy_(ThreadTaskRunnerHandle::OverrideForTesting(scope)) { |
| 129 scope->RunUntilIdle(); |
| 130 } |
| 131 |
| 132 TestMockTimeTaskRunner::ScopedContext::~ScopedContext() = default; |
| 133 |
123 bool TestMockTimeTaskRunner::TemporalOrder::operator()( | 134 bool TestMockTimeTaskRunner::TemporalOrder::operator()( |
124 const TestOrderedPendingTask& first_task, | 135 const TestOrderedPendingTask& first_task, |
125 const TestOrderedPendingTask& second_task) const { | 136 const TestOrderedPendingTask& second_task) const { |
126 if (first_task.GetTimeToRun() == second_task.GetTimeToRun()) | 137 if (first_task.GetTimeToRun() == second_task.GetTimeToRun()) |
127 return first_task.ordinal > second_task.ordinal; | 138 return first_task.ordinal > second_task.ordinal; |
128 return first_task.GetTimeToRun() > second_task.GetTimeToRun(); | 139 return first_task.GetTimeToRun() > second_task.GetTimeToRun(); |
129 } | 140 } |
130 | 141 |
131 TestMockTimeTaskRunner::TestMockTimeTaskRunner() | 142 TestMockTimeTaskRunner::TestMockTimeTaskRunner() |
132 : now_(Time::UnixEpoch()), next_task_ordinal_(0) { | 143 : 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()); | 217 DCHECK(thread_checker_.CalledOnValidThread()); |
207 return tasks_.size(); | 218 return tasks_.size(); |
208 } | 219 } |
209 | 220 |
210 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { | 221 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { |
211 DCHECK(thread_checker_.CalledOnValidThread()); | 222 DCHECK(thread_checker_.CalledOnValidThread()); |
212 return tasks_.empty() ? TimeDelta::Max() | 223 return tasks_.empty() ? TimeDelta::Max() |
213 : tasks_.top().GetTimeToRun() - now_ticks_; | 224 : tasks_.top().GetTimeToRun() - now_ticks_; |
214 } | 225 } |
215 | 226 |
| 227 // TODO(gab): Combine |thread_checker_| with a SequenceToken to differentiate |
| 228 // between tasks running in the scope of this TestMockTimeTaskRunner and other |
| 229 // task runners sharing this thread. http://crbug.com/631186 |
216 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { | 230 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { |
217 return thread_checker_.CalledOnValidThread(); | 231 return thread_checker_.CalledOnValidThread(); |
218 } | 232 } |
219 | 233 |
220 bool TestMockTimeTaskRunner::PostDelayedTask( | 234 bool TestMockTimeTaskRunner::PostDelayedTask( |
221 const tracked_objects::Location& from_here, | 235 const tracked_objects::Location& from_here, |
222 const Closure& task, | 236 const Closure& task, |
223 TimeDelta delay) { | 237 TimeDelta delay) { |
224 AutoLock scoped_lock(tasks_lock_); | 238 AutoLock scoped_lock(tasks_lock_); |
225 tasks_.push(TestOrderedPendingTask(from_here, task, now_ticks_, delay, | 239 tasks_.push(TestOrderedPendingTask(from_here, task, now_ticks_, delay, |
(...skipping 20 matching lines...) Expand all Loading... |
246 void TestMockTimeTaskRunner::OnAfterTimePassed() { | 260 void TestMockTimeTaskRunner::OnAfterTimePassed() { |
247 // Empty default implementation. | 261 // Empty default implementation. |
248 } | 262 } |
249 | 263 |
250 void TestMockTimeTaskRunner::OnAfterTaskRun() { | 264 void TestMockTimeTaskRunner::OnAfterTaskRun() { |
251 // Empty default implementation. | 265 // Empty default implementation. |
252 } | 266 } |
253 | 267 |
254 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) { | 268 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) { |
255 DCHECK_GE(max_delta, TimeDelta()); | 269 DCHECK_GE(max_delta, TimeDelta()); |
| 270 |
| 271 // Multiple test task runners can share the same thread for determinism in |
| 272 // unit tests. Make sure this TestMockTimeTaskRunner's tasks run in its scope. |
| 273 ScopedClosureRunner undo_override; |
| 274 if (!ThreadTaskRunnerHandle::IsSet() || |
| 275 ThreadTaskRunnerHandle::Get() != this) { |
| 276 undo_override = ThreadTaskRunnerHandle::OverrideForTesting(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 |