| 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/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" |
| 11 #include "base/debug/leak_annotations.h" | 11 #include "base/debug/leak_annotations.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/message_loop/message_loop_task_runner.h" | 13 #include "base/message_loop/message_loop_task_runner.h" |
| 14 #include "base/run_loop.h" |
| 15 #include "base/single_thread_task_runner.h" |
| 14 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "testing/platform_test.h" | 20 #include "testing/platform_test.h" |
| 19 | 21 |
| 20 namespace base { | 22 namespace base { |
| 21 | 23 |
| 22 class MessageLoopTaskRunnerTest : public testing::Test { | 24 class MessageLoopTaskRunnerTest : public testing::Test { |
| 23 public: | 25 public: |
| 24 MessageLoopTaskRunnerTest() | 26 MessageLoopTaskRunnerTest() |
| 25 : current_loop_(new MessageLoop()), | 27 : current_loop_(new MessageLoop()), |
| 26 task_thread_("task_thread"), | 28 task_thread_("task_thread"), |
| 27 thread_sync_(WaitableEvent::ResetPolicy::MANUAL, | 29 thread_sync_(WaitableEvent::ResetPolicy::MANUAL, |
| 28 WaitableEvent::InitialState::NOT_SIGNALED) {} | 30 WaitableEvent::InitialState::NOT_SIGNALED) {} |
| 29 | 31 |
| 30 void DeleteCurrentMessageLoop() { current_loop_.reset(); } | 32 void DeleteCurrentMessageLoop() { current_loop_.reset(); } |
| 31 | 33 |
| 32 protected: | 34 protected: |
| 33 void SetUp() override { | 35 void SetUp() override { |
| 34 // Use SetUp() instead of the constructor to avoid posting a task to a | 36 // Use SetUp() instead of the constructor to avoid posting a task to a |
| 35 // partially constructed object. | 37 // partially constructed object. |
| 36 task_thread_.Start(); | 38 task_thread_.Start(); |
| 37 | 39 |
| 38 // Allow us to pause the |task_thread_|'s MessageLoop. | 40 // Allow us to pause the |task_thread_|'s MessageLoop. |
| 39 task_thread_.message_loop()->PostTask( | 41 task_thread_.message_loop()->task_runner()->PostTask( |
| 40 FROM_HERE, Bind(&MessageLoopTaskRunnerTest::BlockTaskThreadHelper, | 42 FROM_HERE, Bind(&MessageLoopTaskRunnerTest::BlockTaskThreadHelper, |
| 41 Unretained(this))); | 43 Unretained(this))); |
| 42 } | 44 } |
| 43 | 45 |
| 44 void TearDown() override { | 46 void TearDown() override { |
| 45 // Make sure the |task_thread_| is not blocked, and stop the thread | 47 // Make sure the |task_thread_| is not blocked, and stop the thread |
| 46 // fully before destruction because its tasks may still depend on the | 48 // fully before destruction because its tasks may still depend on the |
| 47 // |thread_sync_| event. | 49 // |thread_sync_| event. |
| 48 thread_sync_.Signal(); | 50 thread_sync_.Signal(); |
| 49 task_thread_.Stop(); | 51 task_thread_.Stop(); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 } | 253 } |
| 252 | 254 |
| 253 class MessageLoopTaskRunnerThreadingTest : public testing::Test { | 255 class MessageLoopTaskRunnerThreadingTest : public testing::Test { |
| 254 public: | 256 public: |
| 255 void Release() const { | 257 void Release() const { |
| 256 AssertOnIOThread(); | 258 AssertOnIOThread(); |
| 257 Quit(); | 259 Quit(); |
| 258 } | 260 } |
| 259 | 261 |
| 260 void Quit() const { | 262 void Quit() const { |
| 261 loop_.PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); | 263 loop_.task_runner()->PostTask(FROM_HERE, |
| 264 MessageLoop::QuitWhenIdleClosure()); |
| 262 } | 265 } |
| 263 | 266 |
| 264 void AssertOnIOThread() const { | 267 void AssertOnIOThread() const { |
| 265 ASSERT_TRUE(io_thread_->task_runner()->BelongsToCurrentThread()); | 268 ASSERT_TRUE(io_thread_->task_runner()->BelongsToCurrentThread()); |
| 266 ASSERT_EQ(io_thread_->task_runner(), ThreadTaskRunnerHandle::Get()); | 269 ASSERT_EQ(io_thread_->task_runner(), ThreadTaskRunnerHandle::Get()); |
| 267 } | 270 } |
| 268 | 271 |
| 269 void AssertOnFileThread() const { | 272 void AssertOnFileThread() const { |
| 270 ASSERT_TRUE(file_thread_->task_runner()->BelongsToCurrentThread()); | 273 ASSERT_TRUE(file_thread_->task_runner()->BelongsToCurrentThread()); |
| 271 ASSERT_EQ(file_thread_->task_runner(), ThreadTaskRunnerHandle::Get()); | 274 ASSERT_EQ(file_thread_->task_runner(), ThreadTaskRunnerHandle::Get()); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 | 310 |
| 308 std::unique_ptr<Thread> io_thread_; | 311 std::unique_ptr<Thread> io_thread_; |
| 309 std::unique_ptr<Thread> file_thread_; | 312 std::unique_ptr<Thread> file_thread_; |
| 310 | 313 |
| 311 private: | 314 private: |
| 312 mutable MessageLoop loop_; | 315 mutable MessageLoop loop_; |
| 313 }; | 316 }; |
| 314 | 317 |
| 315 TEST_F(MessageLoopTaskRunnerThreadingTest, Release) { | 318 TEST_F(MessageLoopTaskRunnerThreadingTest, Release) { |
| 316 EXPECT_TRUE(io_thread_->task_runner()->ReleaseSoon(FROM_HERE, this)); | 319 EXPECT_TRUE(io_thread_->task_runner()->ReleaseSoon(FROM_HERE, this)); |
| 317 MessageLoop::current()->Run(); | 320 RunLoop().Run(); |
| 318 } | 321 } |
| 319 | 322 |
| 320 TEST_F(MessageLoopTaskRunnerThreadingTest, Delete) { | 323 TEST_F(MessageLoopTaskRunnerThreadingTest, Delete) { |
| 321 DeletedOnFile* deleted_on_file = new DeletedOnFile(this); | 324 DeletedOnFile* deleted_on_file = new DeletedOnFile(this); |
| 322 EXPECT_TRUE( | 325 EXPECT_TRUE( |
| 323 file_thread_->task_runner()->DeleteSoon(FROM_HERE, deleted_on_file)); | 326 file_thread_->task_runner()->DeleteSoon(FROM_HERE, deleted_on_file)); |
| 324 MessageLoop::current()->Run(); | 327 RunLoop().Run(); |
| 325 } | 328 } |
| 326 | 329 |
| 327 TEST_F(MessageLoopTaskRunnerThreadingTest, PostTask) { | 330 TEST_F(MessageLoopTaskRunnerThreadingTest, PostTask) { |
| 328 EXPECT_TRUE(file_thread_->task_runner()->PostTask( | 331 EXPECT_TRUE(file_thread_->task_runner()->PostTask( |
| 329 FROM_HERE, Bind(&MessageLoopTaskRunnerThreadingTest::BasicFunction, | 332 FROM_HERE, Bind(&MessageLoopTaskRunnerThreadingTest::BasicFunction, |
| 330 Unretained(this)))); | 333 Unretained(this)))); |
| 331 MessageLoop::current()->Run(); | 334 RunLoop().Run(); |
| 332 } | 335 } |
| 333 | 336 |
| 334 TEST_F(MessageLoopTaskRunnerThreadingTest, PostTaskAfterThreadExits) { | 337 TEST_F(MessageLoopTaskRunnerThreadingTest, PostTaskAfterThreadExits) { |
| 335 std::unique_ptr<Thread> test_thread( | 338 std::unique_ptr<Thread> test_thread( |
| 336 new Thread("MessageLoopTaskRunnerThreadingTest_Dummy")); | 339 new Thread("MessageLoopTaskRunnerThreadingTest_Dummy")); |
| 337 test_thread->Start(); | 340 test_thread->Start(); |
| 338 scoped_refptr<SingleThreadTaskRunner> task_runner = | 341 scoped_refptr<SingleThreadTaskRunner> task_runner = |
| 339 test_thread->task_runner(); | 342 test_thread->task_runner(); |
| 340 test_thread->Stop(); | 343 test_thread->Stop(); |
| 341 | 344 |
| 342 bool ret = task_runner->PostTask( | 345 bool ret = task_runner->PostTask( |
| 343 FROM_HERE, Bind(&MessageLoopTaskRunnerThreadingTest::AssertNotRun)); | 346 FROM_HERE, Bind(&MessageLoopTaskRunnerThreadingTest::AssertNotRun)); |
| 344 EXPECT_FALSE(ret); | 347 EXPECT_FALSE(ret); |
| 345 } | 348 } |
| 346 | 349 |
| 347 TEST_F(MessageLoopTaskRunnerThreadingTest, PostTaskAfterThreadIsDeleted) { | 350 TEST_F(MessageLoopTaskRunnerThreadingTest, PostTaskAfterThreadIsDeleted) { |
| 348 scoped_refptr<SingleThreadTaskRunner> task_runner; | 351 scoped_refptr<SingleThreadTaskRunner> task_runner; |
| 349 { | 352 { |
| 350 std::unique_ptr<Thread> test_thread( | 353 std::unique_ptr<Thread> test_thread( |
| 351 new Thread("MessageLoopTaskRunnerThreadingTest_Dummy")); | 354 new Thread("MessageLoopTaskRunnerThreadingTest_Dummy")); |
| 352 test_thread->Start(); | 355 test_thread->Start(); |
| 353 task_runner = test_thread->task_runner(); | 356 task_runner = test_thread->task_runner(); |
| 354 } | 357 } |
| 355 bool ret = task_runner->PostTask( | 358 bool ret = task_runner->PostTask( |
| 356 FROM_HERE, Bind(&MessageLoopTaskRunnerThreadingTest::AssertNotRun)); | 359 FROM_HERE, Bind(&MessageLoopTaskRunnerThreadingTest::AssertNotRun)); |
| 357 EXPECT_FALSE(ret); | 360 EXPECT_FALSE(ret); |
| 358 } | 361 } |
| 359 | 362 |
| 360 } // namespace base | 363 } // namespace base |
| OLD | NEW |