diff options
author | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-29 03:31:34 +0000 |
---|---|---|
committer | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-29 03:31:34 +0000 |
commit | 2cbac9eb5be45ba99a37b28eb1dbf306ee3e67cd (patch) | |
tree | 9bae18a794cacca3aa2164deca4e6460815f20b9 /chrome/browser/chrome_thread_unittest.cc | |
parent | ad3d7273dff93e5107a947c05c6a47c35ea7eea7 (diff) | |
download | chromium_src-2cbac9eb5be45ba99a37b28eb1dbf306ee3e67cd.zip chromium_src-2cbac9eb5be45ba99a37b28eb1dbf306ee3e67cd.tar.gz chromium_src-2cbac9eb5be45ba99a37b28eb1dbf306ee3e67cd.tar.bz2 |
Created a MessageLoopProxy interface. This provides a thread-safe refcounted interface to the Post* methods
of a message loop. This class can outlive the target message loop. Changed ChromeThread to vend an implementation of this proxy.
BUG=None
TEST=ChromeThread unit-tests modified.
Review URL: http://codereview.chromium.org/1800008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@45907 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chrome_thread_unittest.cc')
-rw-r--r-- | chrome/browser/chrome_thread_unittest.cc | 81 |
1 files changed, 67 insertions, 14 deletions
diff --git a/chrome/browser/chrome_thread_unittest.cc b/chrome/browser/chrome_thread_unittest.cc index 58a3dc3..a6ae766 100644 --- a/chrome/browser/chrome_thread_unittest.cc +++ b/chrome/browser/chrome_thread_unittest.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "base/message_loop.h" +#include "base/message_loop_proxy.h" #include "chrome/browser/chrome_thread.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" @@ -10,25 +11,25 @@ class ChromeThreadTest : public testing::Test { public: void Release() { - CHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); + CHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); } protected: virtual void SetUp() { + ui_thread_.reset(new ChromeThread(ChromeThread::UI)); file_thread_.reset(new ChromeThread(ChromeThread::FILE)); - io_thread_.reset(new ChromeThread(ChromeThread::IO)); + ui_thread_->Start(); file_thread_->Start(); - io_thread_->Start(); } virtual void TearDown() { + ui_thread_->Stop(); file_thread_->Stop(); - io_thread_->Stop(); } static void BasicFunction(MessageLoop* message_loop) { - CHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); + CHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask); } @@ -47,15 +48,15 @@ class ChromeThreadTest : public testing::Test { bool* deleted_; }; - class DeletedOnIO + class DeletedOnFile : public base::RefCountedThreadSafe< - DeletedOnIO, ChromeThread::DeleteOnIOThread> { + DeletedOnFile, ChromeThread::DeleteOnFileThread> { public: - explicit DeletedOnIO(MessageLoop* message_loop) + explicit DeletedOnFile(MessageLoop* message_loop) : message_loop_(message_loop) { } - ~DeletedOnIO() { - CHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); + ~DeletedOnFile() { + CHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); } @@ -73,20 +74,20 @@ class ChromeThreadTest : public testing::Test { }; private: + scoped_ptr<ChromeThread> ui_thread_; scoped_ptr<ChromeThread> file_thread_; - scoped_ptr<ChromeThread> io_thread_; MessageLoop loop_; }; TEST_F(ChromeThreadTest, PostTask) { ChromeThread::PostTask( - ChromeThread::IO, FROM_HERE, + ChromeThread::FILE, FROM_HERE, NewRunnableFunction(&BasicFunction, MessageLoop::current())); MessageLoop::current()->Run(); } TEST_F(ChromeThreadTest, Release) { - ChromeThread::ReleaseSoon(ChromeThread::FILE, FROM_HERE, this); + ChromeThread::ReleaseSoon(ChromeThread::UI, FROM_HERE, this); MessageLoop::current()->Run(); } @@ -100,7 +101,8 @@ TEST_F(ChromeThreadTest, TaskToNonExistentThreadIsDeleted) { TEST_F(ChromeThreadTest, ReleasedOnCorrectThread) { { - scoped_refptr<DeletedOnIO> test(new DeletedOnIO(MessageLoop::current())); + scoped_refptr<DeletedOnFile> test( + new DeletedOnFile(MessageLoop::current())); } MessageLoop::current()->Run(); } @@ -108,3 +110,54 @@ TEST_F(ChromeThreadTest, ReleasedOnCorrectThread) { TEST_F(ChromeThreadTest, NotReleasedIfTargetThreadNonExistent) { scoped_refptr<NeverDeleted> test(new NeverDeleted()); } + +TEST_F(ChromeThreadTest, PostTaskViaMessageLoopProxy) { + scoped_refptr<MessageLoopProxy> message_loop_proxy = + ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE); + message_loop_proxy->PostTask(FROM_HERE, + NewRunnableFunction(&BasicFunction, + MessageLoop::current())); + MessageLoop::current()->Run(); +} + +TEST_F(ChromeThreadTest, ReleaseViaMessageLoopProxy) { + scoped_refptr<MessageLoopProxy> message_loop_proxy = + ChromeThread::GetMessageLoopProxyForThread(ChromeThread::UI); + message_loop_proxy->ReleaseSoon(FROM_HERE, this); + MessageLoop::current()->Run(); +} + +TEST_F(ChromeThreadTest, TaskToNonExistentThreadIsDeletedViaMessageLoopProxy) { + bool deleted = false; + scoped_refptr<MessageLoopProxy> message_loop_proxy = + ChromeThread::GetMessageLoopProxyForThread(ChromeThread::WEBKIT); + message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); + EXPECT_TRUE(deleted); +} + +TEST_F(ChromeThreadTest, PostTaskViaMessageLoopProxyAfterThreadExits) { + scoped_ptr<ChromeThread> io_thread(new ChromeThread(ChromeThread::IO)); + io_thread->Start(); + io_thread->Stop(); + + bool deleted = false; + scoped_refptr<MessageLoopProxy> message_loop_proxy = + ChromeThread::GetMessageLoopProxyForThread(ChromeThread::IO); + bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); + EXPECT_FALSE(ret); + EXPECT_TRUE(deleted); +} + +TEST_F(ChromeThreadTest, PostTaskViaMessageLoopProxyAfterThreadIsDeleted) { + { + scoped_ptr<ChromeThread> io_thread(new ChromeThread(ChromeThread::IO)); + io_thread->Start(); + } + bool deleted = false; + scoped_refptr<MessageLoopProxy> message_loop_proxy = + ChromeThread::GetMessageLoopProxyForThread(ChromeThread::IO); + bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); + EXPECT_FALSE(ret); + EXPECT_TRUE(deleted); +} + |