This source file includes following definitions.
- IsNonBlocking
- WriteByte
- ReadByte
- TEST_F
- MOJO_MULTIPROCESS_TEST_CHILD_MAIN
- TEST_F
- TEST_F
- MOJO_MULTIPROCESS_TEST_CHILD_MAIN
- TEST_F
- MOJO_MULTIPROCESS_TEST_CHILD_TEST
- TEST_F
- MOJO_MULTIPROCESS_TEST_CHILD_TEST
- TEST_F
- MOJO_MULTIPROCESS_TEST_CHILD_TEST
#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/embedder/scoped_platform_handle.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_POSIX)
#include <fcntl.h>
#endif
namespace mojo {
namespace test {
namespace {
bool IsNonBlocking(const embedder::PlatformHandle& handle) {
#if defined(OS_WIN)
return true;
#else
return fcntl(handle.fd, F_GETFL) & O_NONBLOCK;
#endif
}
bool WriteByte(const embedder::PlatformHandle& handle, char c) {
size_t bytes_written = 0;
BlockingWrite(handle, &c, 1, &bytes_written);
return bytes_written == 1;
}
bool ReadByte(const embedder::PlatformHandle& handle, char* c) {
size_t bytes_read = 0;
BlockingRead(handle, c, 1, &bytes_read);
return bytes_read == 1;
}
typedef testing::Test MultiprocessTestHelperTest;
TEST_F(MultiprocessTestHelperTest, RunChild) {
MultiprocessTestHelper helper;
EXPECT_TRUE(helper.server_platform_handle.is_valid());
helper.StartChild("RunChild");
EXPECT_EQ(123, helper.WaitForChildShutdown());
}
MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild) {
CHECK(MultiprocessTestHelper::client_platform_handle.is_valid());
return 123;
}
TEST_F(MultiprocessTestHelperTest, TestChildMainNotFound) {
MultiprocessTestHelper helper;
helper.StartChild("NoSuchTestChildMain");
int result = helper.WaitForChildShutdown();
EXPECT_FALSE(result >= 0 && result <= 127);
}
TEST_F(MultiprocessTestHelperTest, PassedChannel) {
MultiprocessTestHelper helper;
EXPECT_TRUE(helper.server_platform_handle.is_valid());
helper.StartChild("PassedChannel");
embedder::ScopedPlatformHandle handle = helper.server_platform_handle.Pass();
EXPECT_TRUE(IsNonBlocking(handle.get()));
const char c = 'X';
EXPECT_TRUE(WriteByte(handle.get(), c));
char d = 0;
EXPECT_TRUE(ReadByte(handle.get(), &d));
EXPECT_EQ(c + 1, d);
EXPECT_EQ(c + 2, helper.WaitForChildShutdown());
}
MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PassedChannel) {
CHECK(MultiprocessTestHelper::client_platform_handle.is_valid());
embedder::ScopedPlatformHandle handle =
MultiprocessTestHelper::client_platform_handle.Pass();
EXPECT_TRUE(IsNonBlocking(handle.get()));
char c = 0;
EXPECT_TRUE(ReadByte(handle.get(), &c));
c++;
EXPECT_TRUE(WriteByte(handle.get(), c));
c++;
return static_cast<int>(c);
}
TEST_F(MultiprocessTestHelperTest, ChildTestPasses) {
MultiprocessTestHelper helper;
EXPECT_TRUE(helper.server_platform_handle.is_valid());
helper.StartChild("ChildTestPasses");
EXPECT_TRUE(helper.WaitForChildTestShutdown());
}
MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestPasses) {
ASSERT_TRUE(MultiprocessTestHelper::client_platform_handle.is_valid());
EXPECT_TRUE(IsNonBlocking(
MultiprocessTestHelper::client_platform_handle.get()));
}
TEST_F(MultiprocessTestHelperTest, ChildTestFailsAssert) {
MultiprocessTestHelper helper;
EXPECT_TRUE(helper.server_platform_handle.is_valid());
helper.StartChild("ChildTestFailsAssert");
EXPECT_FALSE(helper.WaitForChildTestShutdown());
}
MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestFailsAssert) {
ASSERT_FALSE(MultiprocessTestHelper::client_platform_handle.is_valid())
<< "DISREGARD: Expected failure in child process";
ASSERT_FALSE(IsNonBlocking(
MultiprocessTestHelper::client_platform_handle.get())) << "Not reached";
CHECK(false) << "Not reached";
}
TEST_F(MultiprocessTestHelperTest, ChildTestFailsExpect) {
MultiprocessTestHelper helper;
EXPECT_TRUE(helper.server_platform_handle.is_valid());
helper.StartChild("ChildTestFailsExpect");
EXPECT_FALSE(helper.WaitForChildTestShutdown());
}
MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestFailsExpect) {
EXPECT_FALSE(MultiprocessTestHelper::client_platform_handle.is_valid())
<< "DISREGARD: Expected failure #1 in child process";
EXPECT_FALSE(IsNonBlocking(
MultiprocessTestHelper::client_platform_handle.get()))
<< "DISREGARD: Expected failure #2 in child process";
}
}
}
}