| 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 <memory> | 5 #include <memory> |
| 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/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/sequenced_task_runner_helpers.h" | 12 #include "base/sequenced_task_runner_helpers.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "content/browser/browser_thread_impl.h" | 15 #include "content/browser/browser_thread_impl.h" |
| 16 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "testing/platform_test.h" | 18 #include "testing/platform_test.h" |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 | 21 |
| 22 class BrowserThreadTest : public testing::Test { | 22 class BrowserThreadTest : public testing::Test { |
| 23 public: | 23 public: |
| 24 void Release() const { | 24 void Release() const { |
| 25 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 25 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 26 loop_.task_runner()->PostTask(FROM_HERE, | 26 loop_.task_runner()->PostTask(FROM_HERE, |
| 27 base::MessageLoop::QuitWhenIdleClosure()); | 27 base::MessageLoop::QuitWhenIdleClosure()); |
| 28 } | 28 } |
| 29 | 29 |
| 30 void StopFileThread() { | 30 void StopUIThread() { ui_thread_->Stop(); } |
| 31 file_thread_->Stop(); | |
| 32 } | |
| 33 | 31 |
| 34 protected: | 32 protected: |
| 35 void SetUp() override { | 33 void SetUp() override { |
| 36 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI)); | 34 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI)); |
| 37 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); | 35 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); |
| 38 ui_thread_->Start(); | 36 ui_thread_->Start(); |
| 39 file_thread_->Start(); | 37 file_thread_->Start(); |
| 40 } | 38 } |
| 41 | 39 |
| 42 void TearDown() override { | 40 void TearDown() override { |
| 43 ui_thread_->Stop(); | 41 StopUIThread(); |
| 44 StopFileThread(); | 42 file_thread_->Stop(); |
| 45 } | 43 } |
| 46 | 44 |
| 47 static void BasicFunction(base::MessageLoop* message_loop) { | 45 static void BasicFunction(base::MessageLoop* message_loop) { |
| 48 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 46 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 49 message_loop->task_runner()->PostTask( | 47 message_loop->task_runner()->PostTask( |
| 50 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 48 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 51 } | 49 } |
| 52 | 50 |
| 53 class DeletedOnFile | 51 class DeletedOnFile |
| 54 : public base::RefCountedThreadSafe< | 52 : public base::RefCountedThreadSafe< |
| (...skipping 16 matching lines...) Expand all Loading... |
| 71 }; | 69 }; |
| 72 | 70 |
| 73 private: | 71 private: |
| 74 std::unique_ptr<BrowserThreadImpl> ui_thread_; | 72 std::unique_ptr<BrowserThreadImpl> ui_thread_; |
| 75 std::unique_ptr<BrowserThreadImpl> file_thread_; | 73 std::unique_ptr<BrowserThreadImpl> file_thread_; |
| 76 // It's kind of ugly to make this mutable - solely so we can post the Quit | 74 // It's kind of ugly to make this mutable - solely so we can post the Quit |
| 77 // Task from Release(). This should be fixed. | 75 // Task from Release(). This should be fixed. |
| 78 mutable base::MessageLoop loop_; | 76 mutable base::MessageLoop loop_; |
| 79 }; | 77 }; |
| 80 | 78 |
| 81 class FileThreadDestructionObserver | 79 class UIThreadDestructionObserver |
| 82 : public base::MessageLoop::DestructionObserver { | 80 : public base::MessageLoop::DestructionObserver { |
| 83 public: | 81 public: |
| 84 explicit FileThreadDestructionObserver(bool* did_shutdown, | 82 explicit UIThreadDestructionObserver(bool* did_shutdown, |
| 85 const base::Closure& callback) | 83 const base::Closure& callback) |
| 86 : callback_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 84 : callback_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 87 callback_(callback), | 85 callback_(callback), |
| 88 file_task_runner_( | 86 ui_task_runner_( |
| 89 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE)), | 87 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI)), |
| 90 did_shutdown_(did_shutdown) { | 88 did_shutdown_(did_shutdown) { |
| 91 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE)->PostTask( | 89 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI) |
| 92 FROM_HERE, base::Bind(&Watch, this)); | 90 ->PostTask(FROM_HERE, base::Bind(&Watch, this)); |
| 93 } | 91 } |
| 94 | 92 |
| 95 private: | 93 private: |
| 96 static void Watch(FileThreadDestructionObserver* observer) { | 94 static void Watch(UIThreadDestructionObserver* observer) { |
| 97 base::MessageLoop::current()->AddDestructionObserver(observer); | 95 base::MessageLoop::current()->AddDestructionObserver(observer); |
| 98 } | 96 } |
| 99 | 97 |
| 100 // base::MessageLoop::DestructionObserver: | 98 // base::MessageLoop::DestructionObserver: |
| 101 void WillDestroyCurrentMessageLoop() override { | 99 void WillDestroyCurrentMessageLoop() override { |
| 102 // Ensure that even during MessageLoop teardown the BrowserThread ID is | 100 // Ensure that even during MessageLoop teardown the BrowserThread ID is |
| 103 // correctly associated with this thread and the BrowserThreadTaskRunner | 101 // correctly associated with this thread and the BrowserThreadTaskRunner |
| 104 // knows it's on the right thread. | 102 // knows it's on the right thread. |
| 105 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 103 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 106 EXPECT_TRUE(file_task_runner_->BelongsToCurrentThread()); | 104 EXPECT_TRUE(ui_task_runner_->BelongsToCurrentThread()); |
| 107 | 105 |
| 108 base::MessageLoop::current()->RemoveDestructionObserver(this); | 106 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 109 *did_shutdown_ = true; | 107 *did_shutdown_ = true; |
| 110 callback_task_runner_->PostTask(FROM_HERE, callback_); | 108 callback_task_runner_->PostTask(FROM_HERE, callback_); |
| 111 } | 109 } |
| 112 | 110 |
| 113 const scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_; | 111 const scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_; |
| 114 const base::Closure callback_; | 112 const base::Closure callback_; |
| 115 const scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 113 const scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 116 bool* did_shutdown_; | 114 bool* did_shutdown_; |
| 117 }; | 115 }; |
| 118 | 116 |
| 119 TEST_F(BrowserThreadTest, PostTask) { | 117 TEST_F(BrowserThreadTest, PostTask) { |
| 120 BrowserThread::PostTask( | 118 BrowserThread::PostTask( |
| 121 BrowserThread::FILE, | 119 BrowserThread::FILE, |
| 122 FROM_HERE, | 120 FROM_HERE, |
| 123 base::Bind(&BasicFunction, base::MessageLoop::current())); | 121 base::Bind(&BasicFunction, base::MessageLoop::current())); |
| 124 base::RunLoop().Run(); | 122 base::RunLoop().Run(); |
| 125 } | 123 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 ASSERT_TRUE(BrowserThread::PostTaskAndReply( | 156 ASSERT_TRUE(BrowserThread::PostTaskAndReply( |
| 159 BrowserThread::FILE, FROM_HERE, base::Bind(&base::DoNothing), | 157 BrowserThread::FILE, FROM_HERE, base::Bind(&base::DoNothing), |
| 160 base::Bind(&base::MessageLoop::QuitWhenIdle, | 158 base::Bind(&base::MessageLoop::QuitWhenIdle, |
| 161 base::Unretained(base::MessageLoop::current()->current())))); | 159 base::Unretained(base::MessageLoop::current()->current())))); |
| 162 base::RunLoop().Run(); | 160 base::RunLoop().Run(); |
| 163 } | 161 } |
| 164 | 162 |
| 165 TEST_F(BrowserThreadTest, RunsTasksOnCurrentThreadDuringShutdown) { | 163 TEST_F(BrowserThreadTest, RunsTasksOnCurrentThreadDuringShutdown) { |
| 166 bool did_shutdown = false; | 164 bool did_shutdown = false; |
| 167 base::RunLoop loop; | 165 base::RunLoop loop; |
| 168 FileThreadDestructionObserver observer(&did_shutdown, loop.QuitClosure()); | 166 UIThreadDestructionObserver observer(&did_shutdown, loop.QuitClosure()); |
| 169 base::ThreadTaskRunnerHandle::Get()->PostTask( | 167 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 170 FROM_HERE, | 168 FROM_HERE, |
| 171 base::Bind(&BrowserThreadTest::StopFileThread, base::Unretained(this))); | 169 base::Bind(&BrowserThreadTest::StopUIThread, base::Unretained(this))); |
| 172 loop.Run(); | 170 loop.Run(); |
| 173 | 171 |
| 174 EXPECT_TRUE(did_shutdown); | 172 EXPECT_TRUE(did_shutdown); |
| 175 } | 173 } |
| 176 | 174 |
| 177 } // namespace content | 175 } // namespace content |
| OLD | NEW |