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

Side by Side Diff: base/message_loop/message_loop_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/memory/weak_ptr_unittest.cc ('k') | base/message_loop/message_loop_test.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) 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/message_loop/message_loop_task_runner.h" 5 #include "base/message_loop/message_loop_task_runner.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/atomic_sequence_num.h" 9 #include "base/atomic_sequence_num.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 20 matching lines...) Expand all
31 void DeleteCurrentMessageLoop() { current_loop_.reset(); } 31 void DeleteCurrentMessageLoop() { current_loop_.reset(); }
32 32
33 protected: 33 protected:
34 void SetUp() override { 34 void SetUp() override {
35 // Use SetUp() instead of the constructor to avoid posting a task to a 35 // Use SetUp() instead of the constructor to avoid posting a task to a
36 // partially constructed object. 36 // partially constructed object.
37 task_thread_.Start(); 37 task_thread_.Start();
38 38
39 // Allow us to pause the |task_thread_|'s MessageLoop. 39 // Allow us to pause the |task_thread_|'s MessageLoop.
40 task_thread_.task_runner()->PostTask( 40 task_thread_.task_runner()->PostTask(
41 FROM_HERE, Bind(&MessageLoopTaskRunnerTest::BlockTaskThreadHelper, 41 FROM_HERE, BindOnce(&MessageLoopTaskRunnerTest::BlockTaskThreadHelper,
42 Unretained(this))); 42 Unretained(this)));
43 } 43 }
44 44
45 void TearDown() override { 45 void TearDown() override {
46 // Make sure the |task_thread_| is not blocked, and stop the thread 46 // Make sure the |task_thread_| is not blocked, and stop the thread
47 // fully before destruction because its tasks may still depend on the 47 // fully before destruction because its tasks may still depend on the
48 // |thread_sync_| event. 48 // |thread_sync_| event.
49 thread_sync_.Signal(); 49 thread_sync_.Signal();
50 task_thread_.Stop(); 50 task_thread_.Stop();
51 DeleteCurrentMessageLoop(); 51 DeleteCurrentMessageLoop();
52 } 52 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 MessageLoop* reply_run_on = NULL; 107 MessageLoop* reply_run_on = NULL;
108 MessageLoop* reply_deleted_on = NULL; 108 MessageLoop* reply_deleted_on = NULL;
109 int reply_delete_order = -1; 109 int reply_delete_order = -1;
110 110
111 scoped_refptr<LoopRecorder> task_recorder = 111 scoped_refptr<LoopRecorder> task_recorder =
112 new LoopRecorder(&task_run_on, &task_deleted_on, &task_delete_order); 112 new LoopRecorder(&task_run_on, &task_deleted_on, &task_delete_order);
113 scoped_refptr<LoopRecorder> reply_recorder = 113 scoped_refptr<LoopRecorder> reply_recorder =
114 new LoopRecorder(&reply_run_on, &reply_deleted_on, &reply_delete_order); 114 new LoopRecorder(&reply_run_on, &reply_deleted_on, &reply_delete_order);
115 115
116 ASSERT_TRUE(task_thread_.task_runner()->PostTaskAndReply( 116 ASSERT_TRUE(task_thread_.task_runner()->PostTaskAndReply(
117 FROM_HERE, Bind(&RecordLoop, task_recorder), 117 FROM_HERE, BindOnce(&RecordLoop, task_recorder),
118 Bind(&RecordLoopAndQuit, reply_recorder))); 118 BindOnce(&RecordLoopAndQuit, reply_recorder)));
119 119
120 // Die if base::Bind doesn't retain a reference to the recorders. 120 // Die if base::Bind doesn't retain a reference to the recorders.
121 task_recorder = NULL; 121 task_recorder = NULL;
122 reply_recorder = NULL; 122 reply_recorder = NULL;
123 ASSERT_FALSE(task_deleted_on); 123 ASSERT_FALSE(task_deleted_on);
124 ASSERT_FALSE(reply_deleted_on); 124 ASSERT_FALSE(reply_deleted_on);
125 125
126 UnblockTaskThread(); 126 UnblockTaskThread();
127 RunLoop().Run(); 127 RunLoop().Run();
128 128
(...skipping 16 matching lines...) Expand all
145 new LoopRecorder(&task_run_on, &task_deleted_on, &task_delete_order); 145 new LoopRecorder(&task_run_on, &task_deleted_on, &task_delete_order);
146 scoped_refptr<LoopRecorder> reply_recorder = 146 scoped_refptr<LoopRecorder> reply_recorder =
147 new LoopRecorder(&reply_run_on, &reply_deleted_on, &reply_delete_order); 147 new LoopRecorder(&reply_run_on, &reply_deleted_on, &reply_delete_order);
148 148
149 // Grab a task runner to a dead MessageLoop. 149 // Grab a task runner to a dead MessageLoop.
150 scoped_refptr<SingleThreadTaskRunner> task_runner = 150 scoped_refptr<SingleThreadTaskRunner> task_runner =
151 task_thread_.task_runner(); 151 task_thread_.task_runner();
152 UnblockTaskThread(); 152 UnblockTaskThread();
153 task_thread_.Stop(); 153 task_thread_.Stop();
154 154
155 ASSERT_FALSE( 155 ASSERT_FALSE(task_runner->PostTaskAndReply(
156 task_runner->PostTaskAndReply(FROM_HERE, Bind(&RecordLoop, task_recorder), 156 FROM_HERE, BindOnce(&RecordLoop, task_recorder),
157 Bind(&RecordLoopAndQuit, reply_recorder))); 157 BindOnce(&RecordLoopAndQuit, reply_recorder)));
158 158
159 // The relay should have properly deleted its resources leaving us as the only 159 // The relay should have properly deleted its resources leaving us as the only
160 // reference. 160 // reference.
161 EXPECT_EQ(task_delete_order, reply_delete_order); 161 EXPECT_EQ(task_delete_order, reply_delete_order);
162 ASSERT_TRUE(task_recorder->HasOneRef()); 162 ASSERT_TRUE(task_recorder->HasOneRef());
163 ASSERT_TRUE(reply_recorder->HasOneRef()); 163 ASSERT_TRUE(reply_recorder->HasOneRef());
164 164
165 // Nothing should have run though. 165 // Nothing should have run though.
166 EXPECT_FALSE(task_run_on); 166 EXPECT_FALSE(task_run_on);
167 EXPECT_FALSE(reply_run_on); 167 EXPECT_FALSE(reply_run_on);
168 } 168 }
169 169
170 TEST_F(MessageLoopTaskRunnerTest, PostTaskAndReply_SameLoop) { 170 TEST_F(MessageLoopTaskRunnerTest, PostTaskAndReply_SameLoop) {
171 MessageLoop* task_run_on = NULL; 171 MessageLoop* task_run_on = NULL;
172 MessageLoop* task_deleted_on = NULL; 172 MessageLoop* task_deleted_on = NULL;
173 int task_delete_order = -1; 173 int task_delete_order = -1;
174 MessageLoop* reply_run_on = NULL; 174 MessageLoop* reply_run_on = NULL;
175 MessageLoop* reply_deleted_on = NULL; 175 MessageLoop* reply_deleted_on = NULL;
176 int reply_delete_order = -1; 176 int reply_delete_order = -1;
177 177
178 scoped_refptr<LoopRecorder> task_recorder = 178 scoped_refptr<LoopRecorder> task_recorder =
179 new LoopRecorder(&task_run_on, &task_deleted_on, &task_delete_order); 179 new LoopRecorder(&task_run_on, &task_deleted_on, &task_delete_order);
180 scoped_refptr<LoopRecorder> reply_recorder = 180 scoped_refptr<LoopRecorder> reply_recorder =
181 new LoopRecorder(&reply_run_on, &reply_deleted_on, &reply_delete_order); 181 new LoopRecorder(&reply_run_on, &reply_deleted_on, &reply_delete_order);
182 182
183 // Enqueue the relay. 183 // Enqueue the relay.
184 ASSERT_TRUE(current_loop_->task_runner()->PostTaskAndReply( 184 ASSERT_TRUE(current_loop_->task_runner()->PostTaskAndReply(
185 FROM_HERE, Bind(&RecordLoop, task_recorder), 185 FROM_HERE, BindOnce(&RecordLoop, task_recorder),
186 Bind(&RecordLoopAndQuit, reply_recorder))); 186 BindOnce(&RecordLoopAndQuit, reply_recorder)));
187 187
188 // Die if base::Bind doesn't retain a reference to the recorders. 188 // Die if base::Bind doesn't retain a reference to the recorders.
189 task_recorder = NULL; 189 task_recorder = NULL;
190 reply_recorder = NULL; 190 reply_recorder = NULL;
191 ASSERT_FALSE(task_deleted_on); 191 ASSERT_FALSE(task_deleted_on);
192 ASSERT_FALSE(reply_deleted_on); 192 ASSERT_FALSE(reply_deleted_on);
193 193
194 RunLoop().Run(); 194 RunLoop().Run();
195 195
196 EXPECT_EQ(current_loop_.get(), task_run_on); 196 EXPECT_EQ(current_loop_.get(), task_run_on);
(...skipping 14 matching lines...) Expand all
211 MessageLoop* reply_deleted_on = NULL; 211 MessageLoop* reply_deleted_on = NULL;
212 int reply_delete_order = -1; 212 int reply_delete_order = -1;
213 213
214 scoped_refptr<LoopRecorder> task_recorder = 214 scoped_refptr<LoopRecorder> task_recorder =
215 new LoopRecorder(&task_run_on, &task_deleted_on, &task_delete_order); 215 new LoopRecorder(&task_run_on, &task_deleted_on, &task_delete_order);
216 scoped_refptr<LoopRecorder> reply_recorder = 216 scoped_refptr<LoopRecorder> reply_recorder =
217 new LoopRecorder(&reply_run_on, &reply_deleted_on, &reply_delete_order); 217 new LoopRecorder(&reply_run_on, &reply_deleted_on, &reply_delete_order);
218 218
219 // Enqueue the relay. 219 // Enqueue the relay.
220 task_thread_.task_runner()->PostTaskAndReply( 220 task_thread_.task_runner()->PostTaskAndReply(
221 FROM_HERE, Bind(&RecordLoop, task_recorder), 221 FROM_HERE, BindOnce(&RecordLoop, task_recorder),
222 Bind(&RecordLoopAndQuit, reply_recorder)); 222 BindOnce(&RecordLoopAndQuit, reply_recorder));
223 223
224 // Die if base::Bind doesn't retain a reference to the recorders. 224 // Die if base::Bind doesn't retain a reference to the recorders.
225 task_recorder = NULL; 225 task_recorder = NULL;
226 reply_recorder = NULL; 226 reply_recorder = NULL;
227 ASSERT_FALSE(task_deleted_on); 227 ASSERT_FALSE(task_deleted_on);
228 ASSERT_FALSE(reply_deleted_on); 228 ASSERT_FALSE(reply_deleted_on);
229 229
230 UnblockTaskThread(); 230 UnblockTaskThread();
231 231
232 // Mercilessly whack the current loop before |reply| gets to run. 232 // Mercilessly whack the current loop before |reply| gets to run.
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 324
325 TEST_F(MessageLoopTaskRunnerThreadingTest, Delete) { 325 TEST_F(MessageLoopTaskRunnerThreadingTest, Delete) {
326 DeletedOnFile* deleted_on_file = new DeletedOnFile(this); 326 DeletedOnFile* deleted_on_file = new DeletedOnFile(this);
327 EXPECT_TRUE( 327 EXPECT_TRUE(
328 file_thread_->task_runner()->DeleteSoon(FROM_HERE, deleted_on_file)); 328 file_thread_->task_runner()->DeleteSoon(FROM_HERE, deleted_on_file));
329 RunLoop().Run(); 329 RunLoop().Run();
330 } 330 }
331 331
332 TEST_F(MessageLoopTaskRunnerThreadingTest, PostTask) { 332 TEST_F(MessageLoopTaskRunnerThreadingTest, PostTask) {
333 EXPECT_TRUE(file_thread_->task_runner()->PostTask( 333 EXPECT_TRUE(file_thread_->task_runner()->PostTask(
334 FROM_HERE, Bind(&MessageLoopTaskRunnerThreadingTest::BasicFunction, 334 FROM_HERE, BindOnce(&MessageLoopTaskRunnerThreadingTest::BasicFunction,
335 Unretained(this)))); 335 Unretained(this))));
336 RunLoop().Run(); 336 RunLoop().Run();
337 } 337 }
338 338
339 TEST_F(MessageLoopTaskRunnerThreadingTest, PostTaskAfterThreadExits) { 339 TEST_F(MessageLoopTaskRunnerThreadingTest, PostTaskAfterThreadExits) {
340 std::unique_ptr<Thread> test_thread( 340 std::unique_ptr<Thread> test_thread(
341 new Thread("MessageLoopTaskRunnerThreadingTest_Dummy")); 341 new Thread("MessageLoopTaskRunnerThreadingTest_Dummy"));
342 test_thread->Start(); 342 test_thread->Start();
343 scoped_refptr<SingleThreadTaskRunner> task_runner = 343 scoped_refptr<SingleThreadTaskRunner> task_runner =
344 test_thread->task_runner(); 344 test_thread->task_runner();
345 test_thread->Stop(); 345 test_thread->Stop();
346 346
347 bool ret = task_runner->PostTask( 347 bool ret = task_runner->PostTask(
348 FROM_HERE, Bind(&MessageLoopTaskRunnerThreadingTest::AssertNotRun)); 348 FROM_HERE, BindOnce(&MessageLoopTaskRunnerThreadingTest::AssertNotRun));
349 EXPECT_FALSE(ret); 349 EXPECT_FALSE(ret);
350 } 350 }
351 351
352 TEST_F(MessageLoopTaskRunnerThreadingTest, PostTaskAfterThreadIsDeleted) { 352 TEST_F(MessageLoopTaskRunnerThreadingTest, PostTaskAfterThreadIsDeleted) {
353 scoped_refptr<SingleThreadTaskRunner> task_runner; 353 scoped_refptr<SingleThreadTaskRunner> task_runner;
354 { 354 {
355 std::unique_ptr<Thread> test_thread( 355 std::unique_ptr<Thread> test_thread(
356 new Thread("MessageLoopTaskRunnerThreadingTest_Dummy")); 356 new Thread("MessageLoopTaskRunnerThreadingTest_Dummy"));
357 test_thread->Start(); 357 test_thread->Start();
358 task_runner = test_thread->task_runner(); 358 task_runner = test_thread->task_runner();
359 } 359 }
360 bool ret = task_runner->PostTask( 360 bool ret = task_runner->PostTask(
361 FROM_HERE, Bind(&MessageLoopTaskRunnerThreadingTest::AssertNotRun)); 361 FROM_HERE, BindOnce(&MessageLoopTaskRunnerThreadingTest::AssertNotRun));
362 EXPECT_FALSE(ret); 362 EXPECT_FALSE(ret);
363 } 363 }
364 364
365 } // namespace base 365 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/weak_ptr_unittest.cc ('k') | base/message_loop/message_loop_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698