Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(435)

Side by Side Diff: base/test/test_simple_task_runner.cc

Issue 2331423002: Replace TestSimpleTaskRunner::GetPendingTasks with TakePendingTasks (Closed)
Patch Set: make a Location result base::Optional Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 20 matching lines...) Expand all
31 pending_tasks_.push_back( 31 pending_tasks_.push_back(
32 TestPendingTask(from_here, task, TimeTicks(), delay, 32 TestPendingTask(from_here, task, TimeTicks(), delay,
33 TestPendingTask::NON_NESTABLE)); 33 TestPendingTask::NON_NESTABLE));
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::GetPendingTasks() const { 41 std::deque<TestPendingTask> TestSimpleTaskRunner::TakePendingTasks() {
42 AutoLock auto_lock(lock_); 42 AutoLock auto_lock(lock_);
43 return 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 return pending_tasks_.front().GetTimeToRun() - base::TimeTicks();
59 } 59 }
60 60
61 base::TimeDelta TestSimpleTaskRunner::FinalPendingTaskDelay() const {
62 AutoLock auto_lock(lock_);
63 return pending_tasks_.back().GetTimeToRun() - base::TimeTicks();
64 }
65
66 base::Optional<tracked_objects::Location>
67 TestSimpleTaskRunner::GetPendingTaskLocationAt(size_t index) const {
68 AutoLock auto_lock(lock_);
69 if (index >= pending_tasks_.size())
70 return base::nullopt;
71 return pending_tasks_[index].location;
72 }
73
61 void TestSimpleTaskRunner::ClearPendingTasks() { 74 void TestSimpleTaskRunner::ClearPendingTasks() {
62 AutoLock auto_lock(lock_); 75 AutoLock auto_lock(lock_);
63 pending_tasks_.clear(); 76 pending_tasks_.clear();
64 } 77 }
65 78
66 void TestSimpleTaskRunner::RunPendingTasks() { 79 void TestSimpleTaskRunner::RunPendingTasks() {
67 DCHECK(RunsTasksOnCurrentThread()); 80 DCHECK(RunsTasksOnCurrentThread());
68 81
69 // Swap with a local variable to avoid re-entrancy problems. 82 // Swap with a local variable to avoid re-entrancy problems.
70 std::deque<TestPendingTask> tasks_to_run; 83 std::deque<TestPendingTask> tasks_to_run;
71 { 84 {
72 AutoLock auto_lock(lock_); 85 AutoLock auto_lock(lock_);
73 tasks_to_run.swap(pending_tasks_); 86 tasks_to_run.swap(pending_tasks_);
74 } 87 }
75 88
76 for (const auto& task : tasks_to_run) 89 for (const auto& task : tasks_to_run)
77 task.task.Run(); 90 task.task.Run();
78 } 91 }
79 92
80 void TestSimpleTaskRunner::RunUntilIdle() { 93 void TestSimpleTaskRunner::RunUntilIdle() {
81 while (!pending_tasks_.empty()) { 94 while (!pending_tasks_.empty()) {
82 RunPendingTasks(); 95 RunPendingTasks();
83 } 96 }
84 } 97 }
85 98
86 } // namespace base 99 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698