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

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

Issue 2657013002: Introduce ThreadTaskRunnerHandle::OverrideForTesting and TestMockTimeTaskRunner::ScopedContext. (Closed)
Patch Set: fix RecentTabHelperTest crash? Created 3 years, 9 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 #include "base/memory/ptr_util.h"
9 #include "base/threading/thread_task_runner_handle.h"
8 10
9 namespace base { 11 namespace base {
10 12
11 TestSimpleTaskRunner::TestSimpleTaskRunner() = default; 13 TestSimpleTaskRunner::TestSimpleTaskRunner() = default;
12 14
13 TestSimpleTaskRunner::~TestSimpleTaskRunner() = default; 15 TestSimpleTaskRunner::~TestSimpleTaskRunner() = default;
14 16
15 bool TestSimpleTaskRunner::PostDelayedTask( 17 bool TestSimpleTaskRunner::PostDelayedTask(
16 const tracked_objects::Location& from_here, 18 const tracked_objects::Location& from_here,
17 const Closure& task, 19 const Closure& task,
18 TimeDelta delay) { 20 TimeDelta delay) {
19 AutoLock auto_lock(lock_); 21 AutoLock auto_lock(lock_);
20 pending_tasks_.push_back( 22 pending_tasks_.push_back(
21 TestPendingTask(from_here, task, TimeTicks(), delay, 23 TestPendingTask(from_here, task, TimeTicks(), delay,
22 TestPendingTask::NESTABLE)); 24 TestPendingTask::NESTABLE));
23 return true; 25 return true;
24 } 26 }
25 27
26 bool TestSimpleTaskRunner::PostNonNestableDelayedTask( 28 bool TestSimpleTaskRunner::PostNonNestableDelayedTask(
27 const tracked_objects::Location& from_here, 29 const tracked_objects::Location& from_here,
28 const Closure& task, 30 const Closure& task,
29 TimeDelta delay) { 31 TimeDelta delay) {
30 AutoLock auto_lock(lock_); 32 AutoLock auto_lock(lock_);
31 pending_tasks_.push_back( 33 pending_tasks_.push_back(
32 TestPendingTask(from_here, task, TimeTicks(), delay, 34 TestPendingTask(from_here, task, TimeTicks(), delay,
33 TestPendingTask::NON_NESTABLE)); 35 TestPendingTask::NON_NESTABLE));
34 return true; 36 return true;
35 } 37 }
36 38
39 // TODO(gab): Use SequenceToken here to differentiate between tasks running in
40 // the scope of this TestSimpleTaskRunner and other task runners sharing this
41 // thread. http://crbug.com/631186
37 bool TestSimpleTaskRunner::RunsTasksOnCurrentThread() const { 42 bool TestSimpleTaskRunner::RunsTasksOnCurrentThread() const {
38 return thread_ref_ == PlatformThread::CurrentRef(); 43 return thread_ref_ == PlatformThread::CurrentRef();
39 } 44 }
40 45
41 std::deque<TestPendingTask> TestSimpleTaskRunner::TakePendingTasks() { 46 std::deque<TestPendingTask> TestSimpleTaskRunner::TakePendingTasks() {
42 AutoLock auto_lock(lock_); 47 AutoLock auto_lock(lock_);
43 return std::move(pending_tasks_); 48 return std::move(pending_tasks_);
44 } 49 }
45 50
46 size_t TestSimpleTaskRunner::NumPendingTasks() const { 51 size_t TestSimpleTaskRunner::NumPendingTasks() const {
(...skipping 24 matching lines...) Expand all
71 void TestSimpleTaskRunner::RunPendingTasks() { 76 void TestSimpleTaskRunner::RunPendingTasks() {
72 DCHECK(RunsTasksOnCurrentThread()); 77 DCHECK(RunsTasksOnCurrentThread());
73 78
74 // Swap with a local variable to avoid re-entrancy problems. 79 // Swap with a local variable to avoid re-entrancy problems.
75 std::deque<TestPendingTask> tasks_to_run; 80 std::deque<TestPendingTask> tasks_to_run;
76 { 81 {
77 AutoLock auto_lock(lock_); 82 AutoLock auto_lock(lock_);
78 tasks_to_run.swap(pending_tasks_); 83 tasks_to_run.swap(pending_tasks_);
79 } 84 }
80 85
86 // Multiple test task runners can share the same thread for determinism in
87 // unit tests. Make sure this TestSimpleTaskRunner's tasks run in its scope.
88 ScopedClosureRunner undo_override;
89 if (!ThreadTaskRunnerHandle::IsSet() ||
90 ThreadTaskRunnerHandle::Get() != this) {
91 undo_override = ThreadTaskRunnerHandle::OverrideForTesting(this);
92 }
93
81 for (auto& task : tasks_to_run) 94 for (auto& task : tasks_to_run)
82 std::move(task.task).Run(); 95 std::move(task.task).Run();
83 } 96 }
84 97
85 void TestSimpleTaskRunner::RunUntilIdle() { 98 void TestSimpleTaskRunner::RunUntilIdle() {
86 while (!pending_tasks_.empty()) { 99 while (!pending_tasks_.empty()) {
87 RunPendingTasks(); 100 RunPendingTasks();
88 } 101 }
89 } 102 }
90 103
91 } // namespace base 104 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698