summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chrome_thread_unittest.cc
diff options
context:
space:
mode:
authorjam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-02 06:10:30 +0000
committerjam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-02 06:10:30 +0000
commitf671062f8c572d6729173c4ac7f058a1a89b5a1d (patch)
tree3eebe393f6f20310ba812662d092c3ca3c3c4d9e /chrome/browser/chrome_thread_unittest.cc
parent6fad26338ed6119903826156f307e20fe6657c31 (diff)
downloadchromium_src-f671062f8c572d6729173c4ac7f058a1a89b5a1d.zip
chromium_src-f671062f8c572d6729173c4ac7f058a1a89b5a1d.tar.gz
chromium_src-f671062f8c572d6729173c4ac7f058a1a89b5a1d.tar.bz2
Add the ability for objects which derive from RefCountedThreadSafe to specify a destructor trait. This allows browser objects to specify which thread they're terminated on. The benefit is we avoid the need to do manual ref counting when an object posts tasks to itself on different threads, if an object must be destructed on a specific thread.
This patch adds initial support and only shows one example with ResourceMessageFilter. I will do the rest in a follow-up patch to keep things small. BUG=25354 TEST=added unit tests Review URL: http://codereview.chromium.org/338065 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30688 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chrome_thread_unittest.cc')
-rw-r--r--chrome/browser/chrome_thread_unittest.cc156
1 files changed, 96 insertions, 60 deletions
diff --git a/chrome/browser/chrome_thread_unittest.cc b/chrome/browser/chrome_thread_unittest.cc
index a6c9981..62f8097 100644
--- a/chrome/browser/chrome_thread_unittest.cc
+++ b/chrome/browser/chrome_thread_unittest.cc
@@ -1,72 +1,108 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include <vector>
-
#include "chrome/browser/chrome_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
-typedef PlatformTest ChromeThreadTest;
-
-TEST_F(ChromeThreadTest, Get) {
- /*
- // TODO(jabdelmalek): rewrite this test when the change to delete objects on
- // a specific thread lands.
- scoped_ptr<ChromeThread> io_thread;
- scoped_ptr<ChromeThread> file_thread;
- scoped_ptr<ChromeThread> db_thread;
-
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::IO) == NULL);
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::FILE) == NULL);
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::DB) == NULL);
-
- // Phase 1: Create threads.
-
- io_thread.reset(new ChromeThread(ChromeThread::IO));
- file_thread.reset(new ChromeThread(ChromeThread::FILE));
- db_thread.reset(new ChromeThread(ChromeThread::DB));
-
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::IO) == NULL);
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::FILE) == NULL);
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::DB) == NULL);
-
- // Phase 2: Start the threads.
-
- io_thread->Start();
- file_thread->Start();
- db_thread->Start();
-
- EXPECT_TRUE(io_thread->message_loop() != NULL);
- EXPECT_TRUE(file_thread->message_loop() != NULL);
- EXPECT_TRUE(db_thread->message_loop() != NULL);
-
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::IO) ==
- io_thread->message_loop());
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::FILE) ==
- file_thread->message_loop());
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::DB) ==
- db_thread->message_loop());
-
- // Phase 3: Stop the threads.
-
- io_thread->Stop();
- file_thread->Stop();
- db_thread->Stop();
+class ChromeThreadTest : public testing::Test {
+ public:
+ void Release() {
+ CHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
+ loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask);
+ }
+
+ protected:
+ virtual void SetUp() {
+ file_thread_.reset(new ChromeThread(ChromeThread::FILE));
+ io_thread_.reset(new ChromeThread(ChromeThread::IO));
+ file_thread_->Start();
+ io_thread_->Start();
+ }
+
+ virtual void TearDown() {
+ file_thread_->Stop();
+ io_thread_->Stop();
+ }
+
+ static void BasicFunction(MessageLoop* message_loop) {
+ CHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
+ message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask);
+ }
+
+ class DummyTask : public Task {
+ public:
+ DummyTask(bool* deleted) : deleted_(deleted) { }
+ ~DummyTask() {
+ *deleted_ = true;
+ }
+
+ void Run() {
+ CHECK(false);
+ }
+
+ private:
+ bool* deleted_;
+ };
+
+ class DeletedOnIO
+ : public base::RefCountedThreadSafe<
+ DeletedOnIO, ChromeThread::DeleteOnIOThread> {
+ public:
+ DeletedOnIO(MessageLoop* message_loop) : message_loop_(message_loop) { }
+
+ ~DeletedOnIO() {
+ CHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
+ message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
+ }
+
+ private:
+ MessageLoop* message_loop_;
+ };
+
+ class NeverDeleted
+ : public base::RefCountedThreadSafe<
+ NeverDeleted, ChromeThread::DeleteOnWebKitThread> {
+ public:
+ ~NeverDeleted() {
+ CHECK(false);
+ }
+ };
+
+ private:
+ scoped_ptr<ChromeThread> file_thread_;
+ scoped_ptr<ChromeThread> io_thread_;
+ MessageLoop loop_;
+};
+
+TEST_F(ChromeThreadTest, PostTask) {
+ ChromeThread::PostTask(
+ ChromeThread::IO, FROM_HERE,
+ NewRunnableFunction(&BasicFunction, MessageLoop::current()));
+ MessageLoop::current()->Run();
+}
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::IO) == NULL);
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::FILE) == NULL);
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::DB) == NULL);
+TEST_F(ChromeThreadTest, Release) {
+ ChromeThread::ReleaseSoon(ChromeThread::FILE, FROM_HERE, this);
+ MessageLoop::current()->Run();
+}
- // Phase 4: Destroy the threads.
+TEST_F(ChromeThreadTest, TaskToNonExistentThreadIsDeleted) {
+ bool deleted = false;
+ ChromeThread::PostTask(
+ ChromeThread::WEBKIT, FROM_HERE,
+ new DummyTask(&deleted));
+ EXPECT_TRUE(deleted);
+}
- io_thread.reset();
- file_thread.reset();
- db_thread.reset();
+TEST_F(ChromeThreadTest, ReleasedOnCorrectThread) {
+ {
+ scoped_refptr<DeletedOnIO> test(new DeletedOnIO(MessageLoop::current()));
+ }
+ MessageLoop::current()->Run();
+}
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::IO) == NULL);
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::FILE) == NULL);
- EXPECT_TRUE(ChromeThread::GetMessageLoop(ChromeThread::DB) == NULL);
- */
+TEST_F(ChromeThreadTest, NotReleasedIfTargetThreadNonExistent) {
+ scoped_refptr<NeverDeleted> test(new NeverDeleted());
}