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

Side by Side Diff: base/deferred_sequenced_task_runner_unittest.cc

Issue 2791243002: Rewrite base::Bind into base::BindOnce on trivial cases in base (Closed)
Patch Set: rebase Created 3 years, 8 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
« no previous file with comments | « base/debug/task_annotator_unittest.cc ('k') | base/files/file_descriptor_watcher_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/deferred_sequenced_task_runner.h" 5 #include "base/deferred_sequenced_task_runner.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
(...skipping 26 matching lines...) Expand all
37 DeferredSequencedTaskRunnerTest* executor_; 37 DeferredSequencedTaskRunnerTest* executor_;
38 int task_id_; 38 int task_id_;
39 }; 39 };
40 40
41 void ExecuteTask(int task_id) { 41 void ExecuteTask(int task_id) {
42 base::AutoLock lock(lock_); 42 base::AutoLock lock(lock_);
43 executed_task_ids_.push_back(task_id); 43 executed_task_ids_.push_back(task_id);
44 } 44 }
45 45
46 void PostExecuteTask(int task_id) { 46 void PostExecuteTask(int task_id) {
47 runner_->PostTask(FROM_HERE, 47 runner_->PostTask(
48 base::Bind(&DeferredSequencedTaskRunnerTest::ExecuteTask, 48 FROM_HERE, base::BindOnce(&DeferredSequencedTaskRunnerTest::ExecuteTask,
49 base::Unretained(this), 49 base::Unretained(this), task_id));
50 task_id));
51 } 50 }
52 51
53 void StartRunner() { 52 void StartRunner() {
54 runner_->Start(); 53 runner_->Start();
55 } 54 }
56 55
57 void DoNothing(ExecuteTaskOnDestructor* object) { 56 void DoNothing(ExecuteTaskOnDestructor* object) {
58 } 57 }
59 58
60 protected: 59 protected:
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 118
120 TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleThreads) { 119 TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleThreads) {
121 { 120 {
122 base::Thread thread1("DeferredSequencedTaskRunnerTestThread1"); 121 base::Thread thread1("DeferredSequencedTaskRunnerTestThread1");
123 base::Thread thread2("DeferredSequencedTaskRunnerTestThread2"); 122 base::Thread thread2("DeferredSequencedTaskRunnerTestThread2");
124 thread1.Start(); 123 thread1.Start();
125 thread2.Start(); 124 thread2.Start();
126 for (int i = 0; i < 5; ++i) { 125 for (int i = 0; i < 5; ++i) {
127 thread1.task_runner()->PostTask( 126 thread1.task_runner()->PostTask(
128 FROM_HERE, 127 FROM_HERE,
129 base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask, 128 base::BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
130 base::Unretained(this), 2 * i)); 129 base::Unretained(this), 2 * i));
131 thread2.task_runner()->PostTask( 130 thread2.task_runner()->PostTask(
132 FROM_HERE, 131 FROM_HERE,
133 base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask, 132 base::BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
134 base::Unretained(this), 2 * i + 1)); 133 base::Unretained(this), 2 * i + 1));
135 if (i == 2) { 134 if (i == 2) {
136 thread1.task_runner()->PostTask( 135 thread1.task_runner()->PostTask(
137 FROM_HERE, base::Bind(&DeferredSequencedTaskRunnerTest::StartRunner, 136 FROM_HERE,
138 base::Unretained(this))); 137 base::BindOnce(&DeferredSequencedTaskRunnerTest::StartRunner,
138 base::Unretained(this)));
139 } 139 }
140 } 140 }
141 } 141 }
142 142
143 base::RunLoop().RunUntilIdle(); 143 base::RunLoop().RunUntilIdle();
144 EXPECT_THAT(executed_task_ids_, 144 EXPECT_THAT(executed_task_ids_,
145 testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))); 145 testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
146 } 146 }
147 147
148 TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) { 148 TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) {
149 { 149 {
150 base::Thread thread("DeferredSequencedTaskRunnerTestThread"); 150 base::Thread thread("DeferredSequencedTaskRunnerTestThread");
151 thread.Start(); 151 thread.Start();
152 runner_ = new base::DeferredSequencedTaskRunner(thread.task_runner()); 152 runner_ = new base::DeferredSequencedTaskRunner(thread.task_runner());
153 for (int i = 0; i < 5; ++i) { 153 for (int i = 0; i < 5; ++i) {
154 { 154 {
155 // Use a block to ensure that no reference to |short_lived_object| 155 // Use a block to ensure that no reference to |short_lived_object|
156 // is kept on the main thread after it is posted to |runner_|. 156 // is kept on the main thread after it is posted to |runner_|.
157 scoped_refptr<ExecuteTaskOnDestructor> short_lived_object = 157 scoped_refptr<ExecuteTaskOnDestructor> short_lived_object =
158 new ExecuteTaskOnDestructor(this, 2 * i); 158 new ExecuteTaskOnDestructor(this, 2 * i);
159 runner_->PostTask( 159 runner_->PostTask(
160 FROM_HERE, base::Bind(&DeferredSequencedTaskRunnerTest::DoNothing, 160 FROM_HERE,
161 base::Unretained(this), 161 base::BindOnce(&DeferredSequencedTaskRunnerTest::DoNothing,
162 base::RetainedRef(short_lived_object))); 162 base::Unretained(this),
163 base::RetainedRef(short_lived_object)));
163 } 164 }
164 // |short_lived_object| with id |2 * i| should be destroyed before the 165 // |short_lived_object| with id |2 * i| should be destroyed before the
165 // task |2 * i + 1| is executed. 166 // task |2 * i + 1| is executed.
166 PostExecuteTask(2 * i + 1); 167 PostExecuteTask(2 * i + 1);
167 } 168 }
168 StartRunner(); 169 StartRunner();
169 } 170 }
170 171
171 // All |short_lived_object| with id |2 * i| are destroyed before the task 172 // All |short_lived_object| with id |2 * i| are destroyed before the task
172 // |2 * i + 1| is executed. 173 // |2 * i + 1| is executed.
173 EXPECT_THAT(executed_task_ids_, 174 EXPECT_THAT(executed_task_ids_,
174 testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); 175 testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
175 } 176 }
176 177
177 } // namespace 178 } // namespace
OLDNEW
« no previous file with comments | « base/debug/task_annotator_unittest.cc ('k') | base/files/file_descriptor_watcher_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698