summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-06 01:14:38 +0000
committerviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-06 01:14:38 +0000
commit28b1aa8b24633ae6983f91540cae2f526746331d (patch)
tree9f8e2a261dc281f5eb1131290a452eedf351b367
parent5a8a8063c387f18f9f090d31cf385d65b048254b (diff)
downloadchromium_src-28b1aa8b24633ae6983f91540cae2f526746331d.zip
chromium_src-28b1aa8b24633ae6983f91540cae2f526746331d.tar.gz
chromium_src-28b1aa8b24633ae6983f91540cae2f526746331d.tar.bz2
Mojo: Replace TestWithIOThreadBase with just a helper TestIOThread class.
This makes "adding" an I/O thread easier (and usable when you already have a test base class). R=sky@chromium.org Review URL: https://codereview.chromium.org/187383007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255196 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--mojo/system/embedder/embedder_unittest.cc27
-rw-r--r--mojo/system/raw_channel_unittest.cc53
-rw-r--r--mojo/system/remote_message_pipe_posix_unittest.cc35
-rw-r--r--mojo/system/test_utils.cc12
-rw-r--r--mojo/system/test_utils.h22
5 files changed, 78 insertions, 71 deletions
diff --git a/mojo/system/embedder/embedder_unittest.cc b/mojo/system/embedder/embedder_unittest.cc
index 13e7c8f..0f2634a 100644
--- a/mojo/system/embedder/embedder_unittest.cc
+++ b/mojo/system/embedder/embedder_unittest.cc
@@ -14,12 +14,25 @@
#include "mojo/system/embedder/platform_channel_pair.h"
#include "mojo/system/embedder/test_embedder.h"
#include "mojo/system/test_utils.h"
+#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace embedder {
namespace {
-typedef system::test::TestWithIOThreadBase EmbedderTest;
+class EmbedderTest : public testing::Test {
+ public:
+ EmbedderTest() {}
+ virtual ~EmbedderTest() {}
+
+ protected:
+ system::test::TestIOThread* io_thread() { return &io_thread_; }
+
+ private:
+ system::test::TestIOThread io_thread_;
+
+ DISALLOW_COPY_AND_ASSIGN(EmbedderTest);
+};
void StoreChannelInfo(ChannelInfo** store_channel_info_here,
ChannelInfo* channel_info) {
@@ -37,14 +50,14 @@ TEST_F(EmbedderTest, ChannelsBasic) {
ChannelInfo* server_channel_info = NULL;
MojoHandle server_mp = CreateChannel(server_handle.Pass(),
- io_thread_task_runner(),
+ io_thread()->task_runner(),
base::Bind(&StoreChannelInfo,
&server_channel_info));
EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
ChannelInfo* client_channel_info = NULL;
MojoHandle client_mp = CreateChannel(client_handle.Pass(),
- io_thread_task_runner(),
+ io_thread()->task_runner(),
base::Bind(&StoreChannelInfo,
&client_channel_info));
EXPECT_NE(client_mp, MOJO_HANDLE_INVALID);
@@ -73,13 +86,13 @@ TEST_F(EmbedderTest, ChannelsBasic) {
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
EXPECT_TRUE(server_channel_info != NULL);
- system::test::PostTaskAndWait(io_thread_task_runner(),
+ system::test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&DestroyChannelOnIOThread,
server_channel_info));
EXPECT_TRUE(client_channel_info != NULL);
- system::test::PostTaskAndWait(io_thread_task_runner(),
+ system::test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&DestroyChannelOnIOThread,
client_channel_info));
@@ -96,14 +109,14 @@ TEST_F(EmbedderTest, ChannelsHandlePassing) {
ChannelInfo* server_channel_info = NULL;
MojoHandle server_mp = CreateChannel(server_handle.Pass(),
- io_thread_task_runner(),
+ io_thread()->task_runner(),
base::Bind(&StoreChannelInfo,
&server_channel_info));
EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
ChannelInfo* client_channel_info = NULL;
MojoHandle client_mp = CreateChannel(client_handle.Pass(),
- io_thread_task_runner(),
+ io_thread()->task_runner(),
base::Bind(&StoreChannelInfo,
&client_channel_info));
EXPECT_NE(client_mp, MOJO_HANDLE_INVALID);
diff --git a/mojo/system/raw_channel_unittest.cc b/mojo/system/raw_channel_unittest.cc
index 5723f1c..4f37401 100644
--- a/mojo/system/raw_channel_unittest.cc
+++ b/mojo/system/raw_channel_unittest.cc
@@ -27,6 +27,7 @@
#include "mojo/system/embedder/scoped_platform_handle.h"
#include "mojo/system/message_in_transit.h"
#include "mojo/system/test_utils.h"
+#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_POSIX)
#include <sys/socket.h>
@@ -71,14 +72,12 @@ bool WriteTestMessageToHandle(const embedder::PlatformHandle& handle,
// -----------------------------------------------------------------------------
-class RawChannelTest : public test::TestWithIOThreadBase {
+class RawChannelTest : public testing::Test {
public:
RawChannelTest() {}
virtual ~RawChannelTest() {}
virtual void SetUp() OVERRIDE {
- test::TestWithIOThreadBase::SetUp();
-
embedder::PlatformChannelPair channel_pair;
handles[0] = channel_pair.PassServerHandle();
handles[1] = channel_pair.PassClientHandle();
@@ -87,14 +86,16 @@ class RawChannelTest : public test::TestWithIOThreadBase {
virtual void TearDown() OVERRIDE {
handles[0].reset();
handles[1].reset();
-
- test::TestWithIOThreadBase::TearDown();
}
protected:
+ test::TestIOThread* io_thread() { return &io_thread_; }
+
embedder::ScopedPlatformHandle handles[2];
private:
+ test::TestIOThread io_thread_;
+
DISALLOW_COPY_AND_ASSIGN(RawChannelTest);
};
@@ -191,11 +192,11 @@ TEST_F(RawChannelTest, WriteMessage) {
WriteOnlyRawChannelDelegate delegate;
scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass(),
&delegate,
- io_thread_message_loop()));
+ io_thread()->message_loop()));
TestMessageReaderAndChecker checker(handles[1].get());
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&InitOnIOThread, rc.get()));
@@ -211,7 +212,7 @@ TEST_F(RawChannelTest, WriteMessage) {
for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1)
EXPECT_TRUE(checker.ReadAndCheckNextMessage(size)) << size;
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&RawChannel::Shutdown,
base::Unretained(rc.get())));
@@ -282,9 +283,9 @@ TEST_F(RawChannelTest, OnReadMessage) {
ReadCheckerRawChannelDelegate delegate;
scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass(),
&delegate,
- io_thread_message_loop()));
+ io_thread()->message_loop()));
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&InitOnIOThread, rc.get()));
@@ -307,7 +308,7 @@ TEST_F(RawChannelTest, OnReadMessage) {
EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), size));
delegate.Wait();
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&RawChannel::Shutdown,
base::Unretained(rc.get())));
@@ -388,9 +389,9 @@ TEST_F(RawChannelTest, WriteMessageAndOnReadMessage) {
scoped_ptr<RawChannel> writer_rc(
RawChannel::Create(handles[0].Pass(),
&writer_delegate,
- io_thread_message_loop()));
+ io_thread()->message_loop()));
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&InitOnIOThread, writer_rc.get()));
@@ -399,9 +400,9 @@ TEST_F(RawChannelTest, WriteMessageAndOnReadMessage) {
scoped_ptr<RawChannel> reader_rc(
RawChannel::Create(handles[1].Pass(),
&reader_delegate,
- io_thread_message_loop()));
+ io_thread()->message_loop()));
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&InitOnIOThread, reader_rc.get()));
@@ -422,12 +423,12 @@ TEST_F(RawChannelTest, WriteMessageAndOnReadMessage) {
// Wait for reading to finish.
reader_delegate.Wait();
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&RawChannel::Shutdown,
base::Unretained(reader_rc.get())));
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&RawChannel::Shutdown,
base::Unretained(writer_rc.get())));
@@ -485,9 +486,9 @@ TEST_F(RawChannelTest, OnFatalError) {
scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass(),
&delegate,
- io_thread_message_loop()));
+ io_thread()->message_loop()));
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&InitOnIOThread, rc.get()));
@@ -506,7 +507,7 @@ TEST_F(RawChannelTest, OnFatalError) {
// notification. (If we actually get another one, |OnFatalError()| crashes.)
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&RawChannel::Shutdown,
base::Unretained(rc.get())));
@@ -524,9 +525,9 @@ TEST_F(RawChannelTest, ReadUnaffectedByWriteFatalError) {
true);
scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass(),
&delegate,
- io_thread_message_loop()));
+ io_thread()->message_loop()));
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&InitOnIOThread, rc.get()));
@@ -558,7 +559,7 @@ TEST_F(RawChannelTest, ReadUnaffectedByWriteFatalError) {
// Wait for reading to finish. A writing failure shouldn't affect reading.
delegate.Wait();
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&RawChannel::Shutdown,
base::Unretained(rc.get())));
@@ -573,12 +574,12 @@ TEST_F(RawChannelTest, WriteMessageAfterShutdown) {
WriteOnlyRawChannelDelegate delegate;
scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass(),
&delegate,
- io_thread_message_loop()));
+ io_thread()->message_loop()));
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&InitOnIOThread, rc.get()));
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&RawChannel::Shutdown,
base::Unretained(rc.get())));
diff --git a/mojo/system/remote_message_pipe_posix_unittest.cc b/mojo/system/remote_message_pipe_posix_unittest.cc
index bd279e8..789c1f9 100644
--- a/mojo/system/remote_message_pipe_posix_unittest.cc
+++ b/mojo/system/remote_message_pipe_posix_unittest.cc
@@ -25,39 +25,39 @@
#include "mojo/system/proxy_message_pipe_endpoint.h"
#include "mojo/system/test_utils.h"
#include "mojo/system/waiter.h"
+#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace system {
namespace {
-class RemoteMessagePipeTest : public test::TestWithIOThreadBase {
+class RemoteMessagePipeTest : public testing::Test {
public:
RemoteMessagePipeTest() {}
virtual ~RemoteMessagePipeTest() {}
virtual void SetUp() OVERRIDE {
- test::TestWithIOThreadBase::SetUp();
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&RemoteMessagePipeTest::SetUpOnIOThread,
base::Unretained(this)));
}
virtual void TearDown() OVERRIDE {
- test::PostTaskAndWait(io_thread_task_runner(),
+ test::PostTaskAndWait(io_thread()->task_runner(),
FROM_HERE,
base::Bind(&RemoteMessagePipeTest::TearDownOnIOThread,
base::Unretained(this)));
- test::TestWithIOThreadBase::TearDown();
}
+ protected:
// This connects MP 0, port 1 and MP 1, port 0 (leaving MP 0, port 0 and MP 1,
// port 1 as the user-visible endpoints) to channel 0 and 1, respectively. MP
// 0, port 1 and MP 1, port 0 must have |ProxyMessagePipeEndpoint|s.
void ConnectMessagePipes(scoped_refptr<MessagePipe> mp0,
scoped_refptr<MessagePipe> mp1) {
test::PostTaskAndWait(
- io_thread_task_runner(),
+ io_thread()->task_runner(),
FROM_HERE,
base::Bind(&RemoteMessagePipeTest::ConnectMessagePipesOnIOThread,
base::Unretained(this), mp0, mp1));
@@ -69,7 +69,7 @@ class RemoteMessagePipeTest : public test::TestWithIOThreadBase {
// returns *without* waiting for it to finish connecting.
void BootstrapMessagePipeNoWait(unsigned channel_index,
scoped_refptr<MessagePipe> mp) {
- io_thread_task_runner()->PostTask(
+ io_thread()->task_runner()->PostTask(
FROM_HERE,
base::Bind(&RemoteMessagePipeTest::BootstrapMessagePipeOnIOThread,
base::Unretained(this), channel_index, mp));
@@ -77,15 +77,17 @@ class RemoteMessagePipeTest : public test::TestWithIOThreadBase {
void RestoreInitialState() {
test::PostTaskAndWait(
- io_thread_task_runner(),
+ io_thread()->task_runner(),
FROM_HERE,
base::Bind(&RemoteMessagePipeTest::RestoreInitialStateOnIOThread,
base::Unretained(this)));
}
+ test::TestIOThread* io_thread() { return &io_thread_; }
+
private:
void SetUpOnIOThread() {
- CHECK_EQ(base::MessageLoop::current(), io_thread_message_loop());
+ CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop());
embedder::PlatformChannelPair channel_pair;
platform_handles_[0] = channel_pair.PassServerHandle();
@@ -93,7 +95,7 @@ class RemoteMessagePipeTest : public test::TestWithIOThreadBase {
}
void TearDownOnIOThread() {
- CHECK_EQ(base::MessageLoop::current(), io_thread_message_loop());
+ CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop());
if (channels_[0].get()) {
channels_[0]->Shutdown();
@@ -106,7 +108,7 @@ class RemoteMessagePipeTest : public test::TestWithIOThreadBase {
}
void CreateAndInitChannel(unsigned channel_index) {
- CHECK_EQ(base::MessageLoop::current(), io_thread_message_loop());
+ CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop());
CHECK(channel_index == 0 || channel_index == 1);
CHECK(!channels_[channel_index].get());
@@ -117,7 +119,7 @@ class RemoteMessagePipeTest : public test::TestWithIOThreadBase {
void ConnectMessagePipesOnIOThread(scoped_refptr<MessagePipe> mp0,
scoped_refptr<MessagePipe> mp1) {
- CHECK_EQ(base::MessageLoop::current(), io_thread_message_loop());
+ CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop());
if (!channels_[0].get())
CreateAndInitChannel(0);
@@ -135,7 +137,7 @@ class RemoteMessagePipeTest : public test::TestWithIOThreadBase {
void BootstrapMessagePipeOnIOThread(unsigned channel_index,
scoped_refptr<MessagePipe> mp) {
- CHECK_EQ(base::MessageLoop::current(), io_thread_message_loop());
+ CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop());
CHECK(channel_index == 0 || channel_index == 1);
unsigned port = channel_index ^ 1u;
@@ -149,12 +151,13 @@ class RemoteMessagePipeTest : public test::TestWithIOThreadBase {
}
void RestoreInitialStateOnIOThread() {
- CHECK_EQ(base::MessageLoop::current(), io_thread_message_loop());
+ CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop());
TearDownOnIOThread();
SetUpOnIOThread();
}
+ test::TestIOThread io_thread_;
embedder::ScopedPlatformHandle platform_handles_[2];
scoped_refptr<Channel> channels_[2];
@@ -569,7 +572,7 @@ TEST_F(RemoteMessagePipeTest, RacingClosesStress) {
BootstrapMessagePipeNoWait(1, mp1);
if (i & 1u) {
- io_thread_task_runner()->PostTask(
+ io_thread()->task_runner()->PostTask(
FROM_HERE, base::Bind(&base::PlatformThread::Sleep, delay));
}
if (i & 2u)
@@ -578,7 +581,7 @@ TEST_F(RemoteMessagePipeTest, RacingClosesStress) {
mp0->Close(0);
if (i & 4u) {
- io_thread_task_runner()->PostTask(
+ io_thread()->task_runner()->PostTask(
FROM_HERE, base::Bind(&base::PlatformThread::Sleep, delay));
}
if (i & 8u)
diff --git a/mojo/system/test_utils.cc b/mojo/system/test_utils.cc
index 673b36e..8e9cbb8 100644
--- a/mojo/system/test_utils.cc
+++ b/mojo/system/test_utils.cc
@@ -31,20 +31,14 @@ void PostTaskAndWait(scoped_refptr<base::TaskRunner> task_runner,
event.Wait();
}
-// TestWithIOThreadBase --------------------------------------------------------
+// TestIOThread ----------------------------------------------------------------
-TestWithIOThreadBase::TestWithIOThreadBase() : io_thread_("io_thread") {
-}
-
-TestWithIOThreadBase::~TestWithIOThreadBase() {
-}
-
-void TestWithIOThreadBase::SetUp() {
+TestIOThread::TestIOThread() : io_thread_("test_io_thread") {
io_thread_.StartWithOptions(
base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
}
-void TestWithIOThreadBase::TearDown() {
+TestIOThread::~TestIOThread() {
io_thread_.Stop();
}
diff --git a/mojo/system/test_utils.h b/mojo/system/test_utils.h
index 9310bd9..23b704b 100644
--- a/mojo/system/test_utils.h
+++ b/mojo/system/test_utils.h
@@ -14,7 +14,6 @@
#include "base/task_runner.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
-#include "testing/gtest/include/gtest/gtest.h"
namespace tracked_objects {
class Location;
@@ -50,29 +49,26 @@ void PostTaskAndWait(scoped_refptr<base::TaskRunner> task_runner,
const tracked_objects::Location& from_here,
const base::Closure& task);
-// TestWithIOThreadBase --------------------------------------------------------
+// TestIOThread ----------------------------------------------------------------
-class TestWithIOThreadBase : public testing::Test {
+class TestIOThread {
public:
- TestWithIOThreadBase();
- virtual ~TestWithIOThreadBase();
+ // Note: The I/O thread is started on construction and stopped on destruction.
+ TestIOThread();
+ ~TestIOThread();
- virtual void SetUp() OVERRIDE;
- virtual void TearDown() OVERRIDE;
-
- protected:
- base::MessageLoopForIO* io_thread_message_loop() {
+ base::MessageLoopForIO* message_loop() {
return static_cast<base::MessageLoopForIO*>(io_thread_.message_loop());
}
- scoped_refptr<base::TaskRunner> io_thread_task_runner() {
- return io_thread_message_loop()->message_loop_proxy();
+ scoped_refptr<base::TaskRunner> task_runner() {
+ return message_loop()->message_loop_proxy();
}
private:
base::Thread io_thread_;
- DISALLOW_COPY_AND_ASSIGN(TestWithIOThreadBase);
+ DISALLOW_COPY_AND_ASSIGN(TestIOThread);
};
} // namespace test