| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_simple_task_runner.h" | 5 #include "base/test/test_simple_task_runner.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 | 10 |
| 11 TestSimpleTaskRunner::TestSimpleTaskRunner() = default; | 11 TestSimpleTaskRunner::TestSimpleTaskRunner() = default; |
| 12 | 12 |
| 13 TestSimpleTaskRunner::~TestSimpleTaskRunner() = default; | 13 TestSimpleTaskRunner::~TestSimpleTaskRunner() = default; |
| 14 | 14 |
| 15 bool TestSimpleTaskRunner::PostDelayedTask( | 15 bool TestSimpleTaskRunner::PostDelayedTask( |
| 16 const tracked_objects::Location& from_here, | 16 const tracked_objects::Location& from_here, |
| 17 const Closure& task, | 17 const Closure& task, |
| 18 TimeDelta delay) { | 18 TimeDelta delay) { |
| 19 AutoLock auto_lock(lock_); | 19 AutoLock auto_lock(lock_); |
| 20 pending_tasks_.push_back( | 20 TestPendingTaskInfo task_info(from_here, TimeTicks(), delay, |
| 21 TestPendingTask(from_here, task, TimeTicks(), delay, | 21 TestPendingTaskInfo::NESTABLE); |
| 22 TestPendingTask::NESTABLE)); | 22 pending_tasks_.push_back(std::make_pair(task_info, task)); |
| 23 return true; | 23 return true; |
| 24 } | 24 } |
| 25 | 25 |
| 26 bool TestSimpleTaskRunner::PostNonNestableDelayedTask( | 26 bool TestSimpleTaskRunner::PostNonNestableDelayedTask( |
| 27 const tracked_objects::Location& from_here, | 27 const tracked_objects::Location& from_here, |
| 28 const Closure& task, | 28 const Closure& task, |
| 29 TimeDelta delay) { | 29 TimeDelta delay) { |
| 30 AutoLock auto_lock(lock_); | 30 AutoLock auto_lock(lock_); |
| 31 pending_tasks_.push_back( | 31 TestPendingTaskInfo task_info(from_here, TimeTicks(), delay, |
| 32 TestPendingTask(from_here, task, TimeTicks(), delay, | 32 TestPendingTaskInfo::NON_NESTABLE); |
| 33 TestPendingTask::NON_NESTABLE)); | 33 pending_tasks_.push_back(std::make_pair(task_info, task)); |
| 34 return true; | 34 return true; |
| 35 } | 35 } |
| 36 | 36 |
| 37 bool TestSimpleTaskRunner::RunsTasksOnCurrentThread() const { | 37 bool TestSimpleTaskRunner::RunsTasksOnCurrentThread() const { |
| 38 return thread_ref_ == PlatformThread::CurrentRef(); | 38 return thread_ref_ == PlatformThread::CurrentRef(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 std::deque<TestPendingTask> TestSimpleTaskRunner::TakePendingTasks() { | 41 TestPendingTaskQueue TestSimpleTaskRunner::TakePendingTasks() { |
| 42 AutoLock auto_lock(lock_); | 42 AutoLock auto_lock(lock_); |
| 43 return std::move(pending_tasks_); | 43 return std::move(pending_tasks_); |
| 44 } | 44 } |
| 45 | 45 |
| 46 size_t TestSimpleTaskRunner::NumPendingTasks() const { | 46 size_t TestSimpleTaskRunner::NumPendingTasks() const { |
| 47 AutoLock auto_lock(lock_); | 47 AutoLock auto_lock(lock_); |
| 48 return pending_tasks_.size(); | 48 return pending_tasks_.size(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool TestSimpleTaskRunner::HasPendingTask() const { | 51 bool TestSimpleTaskRunner::HasPendingTask() const { |
| 52 AutoLock auto_lock(lock_); | 52 AutoLock auto_lock(lock_); |
| 53 return !pending_tasks_.empty(); | 53 return !pending_tasks_.empty(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 base::TimeDelta TestSimpleTaskRunner::NextPendingTaskDelay() const { | 56 base::TimeDelta TestSimpleTaskRunner::NextPendingTaskDelay() const { |
| 57 AutoLock auto_lock(lock_); | 57 AutoLock auto_lock(lock_); |
| 58 return pending_tasks_.front().GetTimeToRun() - base::TimeTicks(); | 58 const TestPendingTaskInfo& task_info = pending_tasks_.front().first; |
| 59 return task_info.GetTimeToRun() - base::TimeTicks(); |
| 59 } | 60 } |
| 60 | 61 |
| 61 base::TimeDelta TestSimpleTaskRunner::FinalPendingTaskDelay() const { | 62 base::TimeDelta TestSimpleTaskRunner::FinalPendingTaskDelay() const { |
| 62 AutoLock auto_lock(lock_); | 63 AutoLock auto_lock(lock_); |
| 63 return pending_tasks_.back().GetTimeToRun() - base::TimeTicks(); | 64 const TestPendingTaskInfo& task_info = pending_tasks_.back().first; |
| 65 return task_info.GetTimeToRun() - base::TimeTicks(); |
| 64 } | 66 } |
| 65 | 67 |
| 66 void TestSimpleTaskRunner::ClearPendingTasks() { | 68 void TestSimpleTaskRunner::ClearPendingTasks() { |
| 67 AutoLock auto_lock(lock_); | 69 AutoLock auto_lock(lock_); |
| 68 pending_tasks_.clear(); | 70 pending_tasks_.clear(); |
| 69 } | 71 } |
| 70 | 72 |
| 71 void TestSimpleTaskRunner::RunPendingTasks() { | 73 void TestSimpleTaskRunner::RunPendingTasks() { |
| 72 DCHECK(RunsTasksOnCurrentThread()); | 74 DCHECK(RunsTasksOnCurrentThread()); |
| 73 | 75 |
| 74 // Swap with a local variable to avoid re-entrancy problems. | 76 // Swap with a local variable to avoid re-entrancy problems. |
| 75 std::deque<TestPendingTask> tasks_to_run; | 77 TestPendingTaskQueue tasks_to_run; |
| 76 { | 78 { |
| 77 AutoLock auto_lock(lock_); | 79 AutoLock auto_lock(lock_); |
| 78 tasks_to_run.swap(pending_tasks_); | 80 tasks_to_run.swap(pending_tasks_); |
| 79 } | 81 } |
| 80 | 82 |
| 81 for (const auto& task : tasks_to_run) | 83 for (auto& task : tasks_to_run) |
| 82 task.task.Run(); | 84 std::move(task.second).Run(); |
| 83 } | 85 } |
| 84 | 86 |
| 85 void TestSimpleTaskRunner::RunUntilIdle() { | 87 void TestSimpleTaskRunner::RunUntilIdle() { |
| 86 while (!pending_tasks_.empty()) { | 88 while (!pending_tasks_.empty()) { |
| 87 RunPendingTasks(); | 89 RunPendingTasks(); |
| 88 } | 90 } |
| 89 } | 91 } |
| 90 | 92 |
| 91 } // namespace base | 93 } // namespace base |
| OLD | NEW |