Mojo: MultiprocessTestBase -> MultiprocessTestHelper.

Because inheritance sucks.

R=sky@chromium.org

Review URL: https://codereview.chromium.org/190943003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255798 0039d316-1c4b-4281-b951-d872f2087c98
parent 3e9d30ee
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/common/test/multiprocess_test_base.h"
#include "mojo/common/test/multiprocess_test_helper.h"
#include "base/command_line.h"
#include "base/logging.h"
......@@ -14,43 +14,29 @@
namespace mojo {
namespace test {
MultiprocessTestBase::MultiprocessTestBase()
MultiprocessTestHelper::MultiprocessTestHelper()
: test_child_handle_(base::kNullProcessHandle) {
}
MultiprocessTestBase::~MultiprocessTestBase() {
CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
}
void MultiprocessTestBase::SetUp() {
CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
MultiProcessTest::SetUp();
platform_channel_pair_.reset(new embedder::PlatformChannelPair());
server_platform_handle = platform_channel_pair_->PassServerHandle();
}
void MultiprocessTestBase::TearDown() {
MultiprocessTestHelper::~MultiprocessTestHelper() {
CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
server_platform_handle.reset();
platform_channel_pair_.reset();
MultiProcessTest::TearDown();
}
void MultiprocessTestBase::StartChild(const std::string& test_child_name) {
void MultiprocessTestHelper::StartChild(const std::string& test_child_name) {
CHECK(platform_channel_pair_.get());
CHECK(!test_child_name.empty());
CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
std::string test_child_main = test_child_name + "TestChildMain";
CommandLine unused(CommandLine::NO_PROGRAM);
CommandLine command_line(base::GetMultiProcessTestChildBaseCommandLine());
embedder::HandlePassingInformation handle_passing_info;
platform_channel_pair_->PrepareToPassClientHandleToChildProcess(
&unused, &handle_passing_info);
&command_line, &handle_passing_info);
base::LaunchOptions options;
#if defined(OS_POSIX)
......@@ -62,39 +48,26 @@ void MultiprocessTestBase::StartChild(const std::string& test_child_name) {
#error "Not supported yet."
#endif
test_child_handle_ = SpawnChildWithOptions(test_child_main, options, false);
test_child_handle_ = base::SpawnMultiProcessTestChild(
test_child_main, command_line, options, false);
platform_channel_pair_->ChildProcessLaunched();
CHECK_NE(test_child_handle_, base::kNullProcessHandle);
}
int MultiprocessTestBase::WaitForChildShutdown() {
int MultiprocessTestHelper::WaitForChildShutdown() {
CHECK_NE(test_child_handle_, base::kNullProcessHandle);
static const int kTimeoutSeconds = 5;
int rv = -1;
CHECK(base::WaitForExitCodeWithTimeout(
test_child_handle_, &rv, base::TimeDelta::FromSeconds(kTimeoutSeconds)));
test_child_handle_, &rv, TestTimeouts::action_timeout()));
base::CloseProcessHandle(test_child_handle_);
test_child_handle_ = base::kNullProcessHandle;
return rv;
}
CommandLine MultiprocessTestBase::MakeCmdLine(const std::string& procname,
bool debug_on_start) {
CHECK(platform_channel_pair_.get());
CommandLine command_line =
base::MultiProcessTest::MakeCmdLine(procname, debug_on_start);
embedder::HandlePassingInformation unused;
platform_channel_pair_->PrepareToPassClientHandleToChildProcess(&command_line,
&unused);
return command_line;
}
// static
void MultiprocessTestBase::ChildSetup() {
void MultiprocessTestHelper::ChildSetup() {
CHECK(CommandLine::InitializedForCurrentProcess());
client_platform_handle =
embedder::PlatformChannelPair::PassClientHandleFromParentProcess(
......@@ -102,7 +75,7 @@ void MultiprocessTestBase::ChildSetup() {
}
// static
embedder::ScopedPlatformHandle MultiprocessTestBase::client_platform_handle;
embedder::ScopedPlatformHandle MultiprocessTestHelper::client_platform_handle;
} // namespace test
} // namespace mojo
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MOJO_COMMON_TEST_MULTIPROCESS_TEST_BASE_H_
#define MOJO_COMMON_TEST_MULTIPROCESS_TEST_BASE_H_
#ifndef MOJO_COMMON_TEST_MULTIPROCESS_TEST_HELPER_H_
#define MOJO_COMMON_TEST_MULTIPROCESS_TEST_HELPER_H_
#include <string>
......@@ -11,6 +11,7 @@
#include "base/compiler_specific.h"
#include "base/process/process_handle.h"
#include "base/test/multiprocess_test.h"
#include "base/test/test_timeouts.h"
#include "mojo/system/embedder/scoped_platform_handle.h"
#include "testing/multiprocess_func_list.h"
......@@ -22,13 +23,10 @@ class PlatformChannelPair;
namespace test {
class MultiprocessTestBase : public base::MultiProcessTest {
class MultiprocessTestHelper {
public:
MultiprocessTestBase();
virtual ~MultiprocessTestBase();
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
MultiprocessTestHelper();
~MultiprocessTestHelper();
// Start a child process and run the "main" function "named" |test_child_name|
// declared using |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| (below).
......@@ -51,26 +49,23 @@ class MultiprocessTestBase : public base::MultiProcessTest {
static embedder::ScopedPlatformHandle client_platform_handle;
private:
virtual CommandLine MakeCmdLine(const std::string& procname,
bool debug_on_start) OVERRIDE;
scoped_ptr<embedder::PlatformChannelPair> platform_channel_pair_;
// Valid after |StartChild()| and before |WaitForChildShutdown()|.
base::ProcessHandle test_child_handle_;
DISALLOW_COPY_AND_ASSIGN(MultiprocessTestBase);
DISALLOW_COPY_AND_ASSIGN(MultiprocessTestHelper);
};
// Use this to declare the child process's "main()" function for tests using
// |MultiprocessTestBase|. It returns an |int|, which will be the process's exit
// code (but see the comment about |WaitForChildShutdown()|).
// |MultiprocessTestHelper|. It returns an |int|, which will be the process's
// exit code (but see the comment about |WaitForChildShutdown()|).
#define MOJO_MULTIPROCESS_TEST_CHILD_MAIN(test_child_name) \
MULTIPROCESS_TEST_MAIN_WITH_SETUP( \
test_child_name ## TestChildMain, \
::mojo::test::MultiprocessTestBase::ChildSetup)
::mojo::test::MultiprocessTestHelper::ChildSetup)
} // namespace test
} // namespace mojo
#endif // MOJO_COMMON_TEST_MULTIPROCESS_TEST_BASE_H_
#endif // MOJO_COMMON_TEST_MULTIPROCESS_TEST_HELPER_H_
......@@ -2,12 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/common/test/multiprocess_test_base.h"
#include "mojo/common/test/multiprocess_test_helper.h"
#include "base/logging.h"
#include "build/build_config.h"
#include "mojo/common/test/test_utils.h"
#include "mojo/system/embedder/scoped_platform_handle.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_POSIX)
#include <fcntl.h>
......@@ -55,41 +56,44 @@ bool ReadByte(const embedder::PlatformHandle& handle, char* c) {
return bytes_read == 1;
}
typedef MultiprocessTestBase MultiprocessTestBaseTest;
typedef testing::Test MultiprocessTestHelperTest;
TEST_F(MultiprocessTestBaseTest, RunChild) {
TEST_F(MultiprocessTestHelperTest, RunChild) {
if (SkipTest())
return;
EXPECT_TRUE(server_platform_handle.is_valid());
MultiprocessTestHelper helper;
EXPECT_TRUE(helper.server_platform_handle.is_valid());
StartChild("RunChild");
EXPECT_EQ(123, WaitForChildShutdown());
helper.StartChild("RunChild");
EXPECT_EQ(123, helper.WaitForChildShutdown());
}
MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild) {
CHECK(MultiprocessTestBaseTest::client_platform_handle.is_valid());
CHECK(MultiprocessTestHelper::client_platform_handle.is_valid());
return 123;
}
TEST_F(MultiprocessTestBaseTest, TestChildMainNotFound) {
TEST_F(MultiprocessTestHelperTest, TestChildMainNotFound) {
if (SkipTest())
return;
StartChild("NoSuchTestChildMain");
int result = WaitForChildShutdown();
MultiprocessTestHelper helper;
helper.StartChild("NoSuchTestChildMain");
int result = helper.WaitForChildShutdown();
EXPECT_FALSE(result >= 0 && result <= 127);
}
TEST_F(MultiprocessTestBaseTest, PassedChannel) {
TEST_F(MultiprocessTestHelperTest, PassedChannel) {
if (SkipTest())
return;
EXPECT_TRUE(server_platform_handle.is_valid());
StartChild("PassedChannel");
MultiprocessTestHelper helper;
EXPECT_TRUE(helper.server_platform_handle.is_valid());
helper.StartChild("PassedChannel");
// Take ownership of the handle.
embedder::ScopedPlatformHandle handle = server_platform_handle.Pass();
embedder::ScopedPlatformHandle handle = helper.server_platform_handle.Pass();
// The handle should be non-blocking.
EXPECT_TRUE(IsNonBlocking(handle.get()));
......@@ -104,15 +108,15 @@ TEST_F(MultiprocessTestBaseTest, PassedChannel) {
EXPECT_EQ(c + 1, d);
// And return it, incremented again.
EXPECT_EQ(c + 2, WaitForChildShutdown());
EXPECT_EQ(c + 2, helper.WaitForChildShutdown());
}
MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PassedChannel) {
CHECK(MultiprocessTestBaseTest::client_platform_handle.is_valid());
CHECK(MultiprocessTestHelper::client_platform_handle.is_valid());
// Take ownership of the handle.
embedder::ScopedPlatformHandle handle =
MultiprocessTestBaseTest::client_platform_handle.Pass();
MultiprocessTestHelper::client_platform_handle.Pass();
// The handle should be non-blocking.
EXPECT_TRUE(IsNonBlocking(handle.get()));
......
......@@ -262,8 +262,8 @@
'mojo_system_impl',
],
'sources': [
'common/test/multiprocess_test_base.cc',
'common/test/multiprocess_test_base.h',
'common/test/multiprocess_test_helper.cc',
'common/test/multiprocess_test_helper.h',
'common/test/test_utils.h',
'common/test/test_utils_posix.cc',
'common/test/test_utils_win.cc',
......@@ -289,7 +289,7 @@
'common/common_type_converters_unittest.cc',
'common/handle_watcher_unittest.cc',
'common/message_pump_mojo_unittest.cc',
'common/test/multiprocess_test_base_unittest.cc',
'common/test/multiprocess_test_helper_unittest.cc',
],
},
{
......
......@@ -17,7 +17,7 @@
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/thread.h"
#include "mojo/common/test/multiprocess_test_base.h"
#include "mojo/common/test/multiprocess_test_helper.h"
#include "mojo/system/channel.h"
#include "mojo/system/embedder/scoped_platform_handle.h"
#include "mojo/system/local_message_pipe_endpoint.h"
......@@ -25,11 +25,13 @@
#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 {
// TODO(vtl): Replace this with a |TestIOThread|.
class IOThreadWrapper {
public:
IOThreadWrapper() : io_thread_("io_thread") {}
......@@ -110,7 +112,7 @@ class IOThreadWrapper {
DISALLOW_COPY_AND_ASSIGN(IOThreadWrapper);
};
class MultiprocessMessagePipeTest : public mojo::test::MultiprocessTestBase {
class MultiprocessMessagePipeTest : public testing::Test {
public:
MultiprocessMessagePipeTest() {}
virtual ~MultiprocessMessagePipeTest() {}
......@@ -118,15 +120,18 @@ class MultiprocessMessagePipeTest : public mojo::test::MultiprocessTestBase {
virtual void TearDown() OVERRIDE {
if (io_thread_wrapper_.is_initialized())
io_thread_wrapper_.Shutdown();
mojo::test::MultiprocessTestBase::TearDown();
}
protected:
void Init(scoped_refptr<MessagePipe> mp) {
io_thread_wrapper_.Init(server_platform_handle.Pass(), mp);
io_thread_wrapper_.Init(helper_.server_platform_handle.Pass(), mp);
}
mojo::test::MultiprocessTestHelper* helper() { return &helper_; }
private:
IOThreadWrapper io_thread_wrapper_;
mojo::test::MultiprocessTestHelper helper_;
DISALLOW_COPY_AND_ASSIGN(MultiprocessMessagePipeTest);
};
......@@ -153,7 +158,7 @@ MojoResult WaitIfNecessary(scoped_refptr<MessagePipe> mp, MojoWaitFlags flags) {
MOJO_MULTIPROCESS_TEST_CHILD_MAIN(EchoEcho) {
IOThreadWrapper io_thread_wrapper;
embedder::ScopedPlatformHandle client_platform_handle =
MultiprocessMessagePipeTest::client_platform_handle.Pass();
mojo::test::MultiprocessTestHelper::client_platform_handle.Pass();
CHECK(client_platform_handle.is_valid());
scoped_refptr<MessagePipe> mp(new MessagePipe(
scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()),
......@@ -203,7 +208,7 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(EchoEcho) {
// Sends "hello" to child, and expects "hellohello" back.
TEST_F(MultiprocessMessagePipeTest, Basic) {
StartChild("EchoEcho");
helper()->StartChild("EchoEcho");
scoped_refptr<MessagePipe> mp(new MessagePipe(
scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()),
......@@ -233,13 +238,13 @@ TEST_F(MultiprocessMessagePipeTest, Basic) {
mp->Close(0);
// We sent one message.
EXPECT_EQ(1 % 100, WaitForChildShutdown());
EXPECT_EQ(1 % 100, helper()->WaitForChildShutdown());
}
// Sends a bunch of messages to the child. Expects them "repeated" back. Waits
// for the child to close its end before quitting.
TEST_F(MultiprocessMessagePipeTest, QueueMessages) {
StartChild("EchoEcho");
helper()->StartChild("EchoEcho");
scoped_refptr<MessagePipe> mp(new MessagePipe(
scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()),
......@@ -287,7 +292,8 @@ TEST_F(MultiprocessMessagePipeTest, QueueMessages) {
mp->Close(0);
EXPECT_EQ(static_cast<int>(kNumMessages % 100), WaitForChildShutdown());
EXPECT_EQ(static_cast<int>(kNumMessages % 100),
helper()->WaitForChildShutdown());
}
} // namespace
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment