Index: third_party/mojo/src/mojo/edk/embedder/embedder_unittest.cc |
diff --git a/third_party/mojo/src/mojo/edk/embedder/embedder_unittest.cc b/third_party/mojo/src/mojo/edk/embedder/embedder_unittest.cc |
index e0bcd8842c0ab90ff6d026cbf2e1a0d7438340b4..27192dee3d300a30a4f18e721152703e626d3ba4 100644 |
--- a/third_party/mojo/src/mojo/edk/embedder/embedder_unittest.cc |
+++ b/third_party/mojo/src/mojo/edk/embedder/embedder_unittest.cc |
@@ -20,8 +20,9 @@ |
#include "mojo/edk/system/test_utils.h" |
#include "mojo/edk/test/multiprocess_test_helper.h" |
#include "mojo/edk/test/scoped_ipc_support.h" |
-#include "mojo/edk/test/test_utils.h" |
#include "mojo/public/c/system/core.h" |
+#include "mojo/public/cpp/system/handle.h" |
+#include "mojo/public/cpp/system/message_pipe.h" |
#include "testing/gtest/include/gtest/gtest.h" |
namespace mojo { |
@@ -39,20 +40,18 @@ const char kConnectionIdFlag[] = "test-connection-id"; |
class ScopedTestChannel { |
public: |
- // Creates a channel that lives on a given I/O thread (determined by the given |
- // |TaskRunner|) attached to the given |platform_handle|. After construction, |
- // |bootstrap_message_pipe()| gives the Mojo handle for the bootstrap message |
- // pipe on this channel; it is up to the caller to close this handle. |
- // Note: The I/O thread must outlive this object (and its message loop must |
- // continue pumping messages while this object is alive). |
- ScopedTestChannel(scoped_refptr<base::TaskRunner> io_thread_task_runner, |
- ScopedPlatformHandle platform_handle) |
- : io_thread_task_runner_(io_thread_task_runner), |
- bootstrap_message_pipe_(MOJO_HANDLE_INVALID), |
+ // Creates a channel, which lives on the I/O thread given to |
+ // |InitIPCSupport()|. After construction, |bootstrap_message_pipe()| gives |
+ // the Mojo handle for the bootstrap message pipe on this channel; it is up to |
+ // the caller to close this handle. Note: The I/O thread must outlive this |
+ // object (and its message loop must continue pumping messages while this |
+ // object is alive). |
+ explicit ScopedTestChannel(ScopedPlatformHandle platform_handle) |
+ : bootstrap_message_pipe_(MOJO_HANDLE_INVALID), |
event_(true, false), // Manual reset. |
channel_info_(nullptr) { |
bootstrap_message_pipe_ = |
- CreateChannel(platform_handle.Pass(), io_thread_task_runner_, |
+ CreateChannel(platform_handle.Pass(), |
base::Bind(&ScopedTestChannel::DidCreateChannel, |
base::Unretained(this)), |
nullptr) |
@@ -93,8 +92,6 @@ class ScopedTestChannel { |
void DidDestroyChannel() { event_.Signal(); } |
- scoped_refptr<base::TaskRunner> io_thread_task_runner_; |
- |
// Valid from creation until whenever it gets closed (by the "owner" of this |
// object). |
// Note: We don't want use the C++ wrappers here, since we want to test the |
@@ -118,6 +115,7 @@ class EmbedderTest : public testing::Test { |
~EmbedderTest() override {} |
protected: |
+ base::TestIOThread& test_io_thread() { return test_io_thread_; } |
scoped_refptr<base::TaskRunner> test_io_task_runner() { |
return test_io_thread_.task_runner(); |
} |
@@ -136,12 +134,10 @@ TEST_F(EmbedderTest, ChannelsBasic) { |
mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); |
PlatformChannelPair channel_pair; |
- ScopedTestChannel server_channel(test_io_task_runner(), |
- channel_pair.PassServerHandle()); |
+ ScopedTestChannel server_channel(channel_pair.PassServerHandle()); |
MojoHandle server_mp = server_channel.bootstrap_message_pipe(); |
EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); |
- ScopedTestChannel client_channel(test_io_task_runner(), |
- channel_pair.PassClientHandle()); |
+ ScopedTestChannel client_channel(channel_pair.PassClientHandle()); |
MojoHandle client_mp = client_channel.bootstrap_message_pipe(); |
EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); |
@@ -264,12 +260,10 @@ TEST_F(EmbedderTest, ChannelsHandlePassing) { |
mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); |
PlatformChannelPair channel_pair; |
- ScopedTestChannel server_channel(test_io_task_runner(), |
- channel_pair.PassServerHandle()); |
+ ScopedTestChannel server_channel(channel_pair.PassServerHandle()); |
MojoHandle server_mp = server_channel.bootstrap_message_pipe(); |
EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); |
- ScopedTestChannel client_channel(test_io_task_runner(), |
- channel_pair.PassClientHandle()); |
+ ScopedTestChannel client_channel(channel_pair.PassClientHandle()); |
MojoHandle client_mp = client_channel.bootstrap_message_pipe(); |
EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); |
@@ -394,31 +388,46 @@ TEST_F(EmbedderTest, MAYBE_MultiprocessMasterSlave) { |
mojo::test::ScopedMasterIPCSupport ipc_support(test_io_task_runner()); |
mojo::test::MultiprocessTestHelper multiprocess_test_helper; |
- ScopedPlatformHandle second_platform_handle; |
std::string connection_id; |
- ConnectToSlave(nullptr, |
- multiprocess_test_helper.server_platform_handle.Pass(), |
- &second_platform_handle, &connection_id); |
- ASSERT_TRUE(second_platform_handle.is_valid()); |
+ base::WaitableEvent event(true, false); |
+ ChannelInfo* channel_info = nullptr; |
+ ScopedMessagePipeHandle mp = ConnectToSlave( |
+ nullptr, multiprocess_test_helper.server_platform_handle.Pass(), |
+ base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)), |
+ nullptr, &connection_id, &channel_info); |
+ ASSERT_TRUE(mp.is_valid()); |
+ EXPECT_TRUE(channel_info); |
ASSERT_FALSE(connection_id.empty()); |
multiprocess_test_helper.StartChildWithExtraSwitch( |
"MultiprocessMasterSlave", kConnectionIdFlag, connection_id); |
- // We write a '?'. The slave should write a '!' in response. |
- size_t n = 0; |
- EXPECT_TRUE( |
- mojo::test::BlockingWrite(second_platform_handle.get(), "?", 1, &n)); |
- EXPECT_EQ(1u, n); |
+ // Send a message saying "hello". |
+ EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(mp.get(), "hello", 5, nullptr, 0, |
+ MOJO_WRITE_MESSAGE_FLAG_NONE)); |
+ |
+ // Wait for a response. |
+ EXPECT_EQ(MOJO_RESULT_OK, |
+ Wait(mp.get(), MOJO_HANDLE_SIGNAL_READABLE, |
+ mojo::system::test::ActionDeadline(), nullptr)); |
+ |
+ // The response message should say "world". |
+ char buffer[100]; |
+ uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
+ EXPECT_EQ(MOJO_RESULT_OK, |
+ ReadMessageRaw(mp.get(), buffer, &num_bytes, nullptr, nullptr, |
+ MOJO_READ_MESSAGE_FLAG_NONE)); |
+ EXPECT_EQ(5u, num_bytes); |
+ EXPECT_EQ(0, memcmp(buffer, "world", 5)); |
- char c = '\0'; |
- n = 0; |
- EXPECT_TRUE( |
- mojo::test::BlockingRead(second_platform_handle.get(), &c, 1, &n)); |
- EXPECT_EQ(1u, n); |
- EXPECT_EQ('!', c); |
+ mp.reset(); |
EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown()); |
+ |
+ EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout())); |
+ test_io_thread().PostTaskAndWait( |
+ FROM_HERE, |
+ base::Bind(&DestroyChannelOnIOThread, base::Unretained(channel_info))); |
} |
MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessMasterSlave) { |
@@ -439,22 +448,39 @@ MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessMasterSlave) { |
std::string connection_id = |
command_line.GetSwitchValueASCII(kConnectionIdFlag); |
ASSERT_FALSE(connection_id.empty()); |
- ScopedPlatformHandle second_platform_handle; |
- ConnectToMaster(connection_id, &second_platform_handle); |
- ASSERT_TRUE(second_platform_handle.is_valid()); |
- |
- // The master should write a '?'. We'll write a '!' in response. |
- char c = '\0'; |
- size_t n = 0; |
- EXPECT_TRUE( |
- mojo::test::BlockingRead(second_platform_handle.get(), &c, 1, &n)); |
- EXPECT_EQ(1u, n); |
- EXPECT_EQ('?', c); |
- |
- n = 0; |
- EXPECT_TRUE( |
- mojo::test::BlockingWrite(second_platform_handle.get(), "!", 1, &n)); |
- EXPECT_EQ(1u, n); |
+ base::WaitableEvent event(true, false); |
+ ChannelInfo* channel_info = nullptr; |
+ ScopedMessagePipeHandle mp = ConnectToMaster( |
+ connection_id, |
+ base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)), |
+ nullptr, &channel_info); |
+ ASSERT_TRUE(mp.is_valid()); |
+ EXPECT_TRUE(channel_info); |
+ |
+ // Wait for the master to send us a message. |
+ EXPECT_EQ(MOJO_RESULT_OK, |
+ Wait(mp.get(), MOJO_HANDLE_SIGNAL_READABLE, |
+ mojo::system::test::ActionDeadline(), nullptr)); |
+ |
+ // It should say "hello". |
+ char buffer[100]; |
+ uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
+ EXPECT_EQ(MOJO_RESULT_OK, |
+ ReadMessageRaw(mp.get(), buffer, &num_bytes, nullptr, nullptr, |
+ MOJO_READ_MESSAGE_FLAG_NONE)); |
+ EXPECT_EQ(5u, num_bytes); |
+ EXPECT_EQ(0, memcmp(buffer, "hello", 5)); |
+ |
+ // In response send a message saying "world". |
+ EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(mp.get(), "world", 5, nullptr, 0, |
+ MOJO_WRITE_MESSAGE_FLAG_NONE)); |
+ |
+ mp.reset(); |
+ |
+ EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout())); |
+ test_io_thread.PostTaskAndWait( |
+ FROM_HERE, |
+ base::Bind(&DestroyChannelOnIOThread, base::Unretained(channel_info))); |
} |
EXPECT_TRUE(test::Shutdown()); |
@@ -491,7 +517,6 @@ TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) { |
{ |
ScopedTestChannel server_channel( |
- test_io_task_runner(), |
multiprocess_test_helper.server_platform_handle.Pass()); |
MojoHandle server_mp = server_channel.bootstrap_message_pipe(); |
EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); |
@@ -613,8 +638,7 @@ MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessChannelsClient) { |
// probably. |
mojo::test::ScopedIPCSupport ipc_support(test_io_thread.task_runner()); |
- ScopedTestChannel client_channel(test_io_thread.task_runner(), |
- client_platform_handle.Pass()); |
+ ScopedTestChannel client_channel(client_platform_handle.Pass()); |
MojoHandle client_mp = client_channel.bootstrap_message_pipe(); |
EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); |
client_channel.WaitForChannelCreationCompletion(); |