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/message_loop_proxy.h" | 5 #include "base/message_loop_proxy.h" |
6 | 6 |
7 #include "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/debug/leak_annotations.h" | 9 #include "base/debug/leak_annotations.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 EXPECT_EQ(task_delete_order, reply_delete_order); | 254 EXPECT_EQ(task_delete_order, reply_delete_order); |
255 | 255 |
256 // The PostTaskAndReplyRelay is leaked here. Even if we had a reference to | 256 // The PostTaskAndReplyRelay is leaked here. Even if we had a reference to |
257 // it, we cannot just delete it because PostTaskAndReplyRelay's destructor | 257 // it, we cannot just delete it because PostTaskAndReplyRelay's destructor |
258 // checks that MessageLoop::current() is the the same as when the | 258 // checks that MessageLoop::current() is the the same as when the |
259 // PostTaskAndReplyRelay object was constructed. However, this loop must have | 259 // PostTaskAndReplyRelay object was constructed. However, this loop must have |
260 // aleady been deleted in order to perform this test. See | 260 // aleady been deleted in order to perform this test. See |
261 // http://crbug.com/86301. | 261 // http://crbug.com/86301. |
262 } | 262 } |
263 | 263 |
| 264 class MessageLoopProxyTest2 : public testing::Test { |
| 265 public: |
| 266 void Release() const { |
| 267 AssertOnIOThread(); |
| 268 Quit(); |
| 269 } |
| 270 |
| 271 void Quit() const { |
| 272 loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 273 } |
| 274 |
| 275 void AssertOnIOThread() const { |
| 276 ASSERT_TRUE(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); |
| 277 ASSERT_EQ(io_thread_->message_loop_proxy(), |
| 278 base::MessageLoopProxy::current()); |
| 279 } |
| 280 |
| 281 void AssertOnFileThread() const { |
| 282 ASSERT_TRUE(file_thread_->message_loop_proxy()->BelongsToCurrentThread()); |
| 283 ASSERT_EQ(file_thread_->message_loop_proxy(), |
| 284 base::MessageLoopProxy::current()); |
| 285 } |
| 286 |
| 287 protected: |
| 288 virtual void SetUp() OVERRIDE { |
| 289 io_thread_.reset(new base::Thread("MessageLoopProxyTest_IO")); |
| 290 file_thread_.reset(new base::Thread("MessageLoopProxyTest_File")); |
| 291 io_thread_->Start(); |
| 292 file_thread_->Start(); |
| 293 } |
| 294 |
| 295 virtual void TearDown() OVERRIDE { |
| 296 io_thread_->Stop(); |
| 297 file_thread_->Stop(); |
| 298 } |
| 299 |
| 300 static void BasicFunction(MessageLoopProxyTest2* test) { |
| 301 test->AssertOnFileThread(); |
| 302 test->Quit(); |
| 303 } |
| 304 |
| 305 static void AssertNotRun() { |
| 306 FAIL() << "Callback Should not get executed."; |
| 307 } |
| 308 |
| 309 class DeletedOnFile { |
| 310 public: |
| 311 explicit DeletedOnFile(MessageLoopProxyTest2* test) : test_(test) {} |
| 312 |
| 313 ~DeletedOnFile() { |
| 314 test_->AssertOnFileThread(); |
| 315 test_->Quit(); |
| 316 } |
| 317 |
| 318 private: |
| 319 MessageLoopProxyTest2* test_; |
| 320 }; |
| 321 |
| 322 scoped_ptr<base::Thread> io_thread_; |
| 323 scoped_ptr<base::Thread> file_thread_; |
| 324 |
| 325 private: |
| 326 mutable MessageLoop loop_; |
| 327 }; |
| 328 |
| 329 TEST_F(MessageLoopProxyTest2, Release) { |
| 330 EXPECT_TRUE(io_thread_->message_loop_proxy()->ReleaseSoon(FROM_HERE, this)); |
| 331 MessageLoop::current()->Run(); |
| 332 } |
| 333 |
| 334 TEST_F(MessageLoopProxyTest2, Delete) { |
| 335 DeletedOnFile* deleted_on_file = new DeletedOnFile(this); |
| 336 EXPECT_TRUE(file_thread_->message_loop_proxy()->DeleteSoon( |
| 337 FROM_HERE, deleted_on_file)); |
| 338 MessageLoop::current()->Run(); |
| 339 } |
| 340 |
| 341 TEST_F(MessageLoopProxyTest2, PostTask) { |
| 342 EXPECT_TRUE(file_thread_->message_loop_proxy()->PostTask( |
| 343 FROM_HERE, base::Bind(&MessageLoopProxyTest2::BasicFunction, |
| 344 base::Unretained(this)))); |
| 345 MessageLoop::current()->Run(); |
| 346 } |
| 347 |
| 348 TEST_F(MessageLoopProxyTest2, PostTaskAfterThreadExits) { |
| 349 scoped_ptr<base::Thread> test_thread( |
| 350 new base::Thread("MessageLoopProxyTest2_Dummy")); |
| 351 test_thread->Start(); |
| 352 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
| 353 test_thread->message_loop_proxy(); |
| 354 test_thread->Stop(); |
| 355 |
| 356 bool ret = message_loop_proxy->PostTask( |
| 357 FROM_HERE, |
| 358 base::Bind(&MessageLoopProxyTest2::AssertNotRun)); |
| 359 EXPECT_FALSE(ret); |
| 360 } |
| 361 |
| 362 TEST_F(MessageLoopProxyTest2, PostTaskAfterThreadIsDeleted) { |
| 363 scoped_refptr<base::MessageLoopProxy> message_loop_proxy; |
| 364 { |
| 365 scoped_ptr<base::Thread> test_thread( |
| 366 new base::Thread("MessageLoopProxyTest2_Dummy")); |
| 367 test_thread->Start(); |
| 368 message_loop_proxy = test_thread->message_loop_proxy(); |
| 369 } |
| 370 bool ret = message_loop_proxy->PostTask( |
| 371 FROM_HERE, |
| 372 base::Bind(&MessageLoopProxyTest2::AssertNotRun)); |
| 373 EXPECT_FALSE(ret); |
| 374 } |
264 } // namespace | 375 } // namespace |
265 | 376 |
266 } // namespace base | 377 } // namespace base |
OLD | NEW |