| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/edk/embedder/embedder.h" | 5 #include "mojo/edk/embedder/embedder.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "mojo/edk/embedder/platform_channel_pair.h" | 14 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 15 #include "mojo/edk/embedder/test_embedder.h" | 15 #include "mojo/edk/embedder/test_embedder.h" |
| 16 #include "mojo/edk/system/test/test_command_line.h" | 16 #include "mojo/edk/system/test/test_command_line.h" |
| 17 #include "mojo/edk/system/test/test_io_thread.h" | 17 #include "mojo/edk/system/test/test_io_thread.h" |
| 18 #include "mojo/edk/system/test/timeouts.h" | 18 #include "mojo/edk/system/test/timeouts.h" |
| 19 #include "mojo/edk/system/waitable_event.h" | |
| 20 #include "mojo/edk/test/multiprocess_test_helper.h" | 19 #include "mojo/edk/test/multiprocess_test_helper.h" |
| 21 #include "mojo/edk/test/scoped_ipc_support.h" | 20 #include "mojo/edk/test/scoped_ipc_support.h" |
| 22 #include "mojo/edk/util/command_line.h" | 21 #include "mojo/edk/util/command_line.h" |
| 23 #include "mojo/edk/util/mutex.h" | 22 #include "mojo/edk/util/mutex.h" |
| 24 #include "mojo/edk/util/thread_annotations.h" | 23 #include "mojo/edk/util/thread_annotations.h" |
| 24 #include "mojo/edk/util/waitable_event.h" |
| 25 #include "mojo/public/c/system/core.h" | 25 #include "mojo/public/c/system/core.h" |
| 26 #include "mojo/public/cpp/system/handle.h" | 26 #include "mojo/public/cpp/system/handle.h" |
| 27 #include "mojo/public/cpp/system/macros.h" | 27 #include "mojo/public/cpp/system/macros.h" |
| 28 #include "mojo/public/cpp/system/message_pipe.h" | 28 #include "mojo/public/cpp/system/message_pipe.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 30 |
| 31 using mojo::system::test::TestIOThread; | 31 using mojo::system::test::TestIOThread; |
| 32 using mojo::util::ManualResetWaitableEvent; |
| 32 using mojo::util::Mutex; | 33 using mojo::util::Mutex; |
| 33 using mojo::util::MutexLocker; | 34 using mojo::util::MutexLocker; |
| 34 | 35 |
| 35 namespace mojo { | 36 namespace mojo { |
| 36 namespace embedder { | 37 namespace embedder { |
| 37 namespace { | 38 namespace { |
| 38 | 39 |
| 39 const MojoHandleSignals kSignalReadadableWritable = | 40 const MojoHandleSignals kSignalReadadableWritable = |
| 40 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE; | 41 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE; |
| 41 | 42 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 | 112 |
| 112 // Valid from creation until whenever it gets closed (by the "owner" of this | 113 // Valid from creation until whenever it gets closed (by the "owner" of this |
| 113 // object). | 114 // object). |
| 114 // Note: We don't want use the C++ wrappers here, since we want to test the | 115 // Note: We don't want use the C++ wrappers here, since we want to test the |
| 115 // API at the lowest level. | 116 // API at the lowest level. |
| 116 MojoHandle bootstrap_message_pipe_; | 117 MojoHandle bootstrap_message_pipe_; |
| 117 | 118 |
| 118 // Set after channel creation has been completed (i.e., the callback to | 119 // Set after channel creation has been completed (i.e., the callback to |
| 119 // |CreateChannel()| has been called). Also used in the destructor to wait for | 120 // |CreateChannel()| has been called). Also used in the destructor to wait for |
| 120 // |DestroyChannel()| completion. | 121 // |DestroyChannel()| completion. |
| 121 mojo::system::ManualResetWaitableEvent event_; | 122 ManualResetWaitableEvent event_; |
| 122 | 123 |
| 123 // Valid after channel creation completion until destruction. | 124 // Valid after channel creation completion until destruction. |
| 124 ChannelInfo* channel_info_; | 125 ChannelInfo* channel_info_; |
| 125 | 126 |
| 126 // Whether the destructor should wait until the channel is destroyed. | 127 // Whether the destructor should wait until the channel is destroyed. |
| 127 bool wait_on_shutdown_; | 128 bool wait_on_shutdown_; |
| 128 | 129 |
| 129 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedTestChannel); | 130 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedTestChannel); |
| 130 }; | 131 }; |
| 131 | 132 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 bool TryWait() { | 209 bool TryWait() { |
| 209 return !event_.WaitWithTimeout(mojo::system::test::ActionTimeout()); | 210 return !event_.WaitWithTimeout(mojo::system::test::ActionTimeout()); |
| 210 } | 211 } |
| 211 | 212 |
| 212 MojoResult wait_result() const { | 213 MojoResult wait_result() const { |
| 213 MutexLocker l(&wait_result_mutex_); | 214 MutexLocker l(&wait_result_mutex_); |
| 214 return wait_result_; | 215 return wait_result_; |
| 215 } | 216 } |
| 216 | 217 |
| 217 private: | 218 private: |
| 218 mojo::system::ManualResetWaitableEvent event_; | 219 ManualResetWaitableEvent event_; |
| 219 | 220 |
| 220 mutable Mutex wait_result_mutex_; | 221 mutable Mutex wait_result_mutex_; |
| 221 MojoResult wait_result_ MOJO_GUARDED_BY(wait_result_mutex_); | 222 MojoResult wait_result_ MOJO_GUARDED_BY(wait_result_mutex_); |
| 222 | 223 |
| 223 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaiter); | 224 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaiter); |
| 224 }; | 225 }; |
| 225 | 226 |
| 226 void WriteHello(MessagePipeHandle pipe) { | 227 void WriteHello(MessagePipeHandle pipe) { |
| 227 static const char kHello[] = "hello"; | 228 static const char kHello[] = "hello"; |
| 228 CHECK_EQ(MOJO_RESULT_OK, | 229 CHECK_EQ(MOJO_RESULT_OK, |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 // TODO(vtl): I'm guessing this is true of this test too? | 403 // TODO(vtl): I'm guessing this is true of this test too? |
| 403 #define MAYBE_MultiprocessMasterSlave DISABLED_MultiprocessMasterSlave | 404 #define MAYBE_MultiprocessMasterSlave DISABLED_MultiprocessMasterSlave |
| 404 #else | 405 #else |
| 405 #define MAYBE_MultiprocessMasterSlave MultiprocessMasterSlave | 406 #define MAYBE_MultiprocessMasterSlave MultiprocessMasterSlave |
| 406 #endif // defined(OS_ANDROID) | 407 #endif // defined(OS_ANDROID) |
| 407 TEST_F(EmbedderTest, MAYBE_MultiprocessMasterSlave) { | 408 TEST_F(EmbedderTest, MAYBE_MultiprocessMasterSlave) { |
| 408 mojo::test::ScopedMasterIPCSupport ipc_support(test_io_task_runner()); | 409 mojo::test::ScopedMasterIPCSupport ipc_support(test_io_task_runner()); |
| 409 | 410 |
| 410 mojo::test::MultiprocessTestHelper multiprocess_test_helper; | 411 mojo::test::MultiprocessTestHelper multiprocess_test_helper; |
| 411 std::string connection_id; | 412 std::string connection_id; |
| 412 mojo::system::ManualResetWaitableEvent event; | 413 ManualResetWaitableEvent event; |
| 413 ChannelInfo* channel_info = nullptr; | 414 ChannelInfo* channel_info = nullptr; |
| 414 ScopedMessagePipeHandle mp = ConnectToSlave( | 415 ScopedMessagePipeHandle mp = ConnectToSlave( |
| 415 nullptr, multiprocess_test_helper.server_platform_handle.Pass(), | 416 nullptr, multiprocess_test_helper.server_platform_handle.Pass(), |
| 416 base::Bind(&mojo::system::ManualResetWaitableEvent::Signal, | 417 base::Bind(&ManualResetWaitableEvent::Signal, base::Unretained(&event)), |
| 417 base::Unretained(&event)), | |
| 418 nullptr, &connection_id, &channel_info); | 418 nullptr, &connection_id, &channel_info); |
| 419 ASSERT_TRUE(mp.is_valid()); | 419 ASSERT_TRUE(mp.is_valid()); |
| 420 EXPECT_TRUE(channel_info); | 420 EXPECT_TRUE(channel_info); |
| 421 ASSERT_FALSE(connection_id.empty()); | 421 ASSERT_FALSE(connection_id.empty()); |
| 422 | 422 |
| 423 multiprocess_test_helper.StartChildWithExtraSwitch( | 423 multiprocess_test_helper.StartChildWithExtraSwitch( |
| 424 "MultiprocessMasterSlave", kConnectionIdFlag, connection_id); | 424 "MultiprocessMasterSlave", kConnectionIdFlag, connection_id); |
| 425 | 425 |
| 426 // Send a message saying "hello". | 426 // Send a message saying "hello". |
| 427 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(mp.get(), "hello", 5, nullptr, 0, | 427 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(mp.get(), "hello", 5, nullptr, 0, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 test::InitWithSimplePlatformSupport(); | 480 test::InitWithSimplePlatformSupport(); |
| 481 | 481 |
| 482 { | 482 { |
| 483 mojo::test::ScopedSlaveIPCSupport ipc_support( | 483 mojo::test::ScopedSlaveIPCSupport ipc_support( |
| 484 test_io_thread.task_runner(), client_platform_handle.Pass()); | 484 test_io_thread.task_runner(), client_platform_handle.Pass()); |
| 485 | 485 |
| 486 std::string connection_id; | 486 std::string connection_id; |
| 487 ASSERT_TRUE(mojo::system::test::GetTestCommandLine()->GetOptionValue( | 487 ASSERT_TRUE(mojo::system::test::GetTestCommandLine()->GetOptionValue( |
| 488 kConnectionIdFlag, &connection_id)); | 488 kConnectionIdFlag, &connection_id)); |
| 489 ASSERT_FALSE(connection_id.empty()); | 489 ASSERT_FALSE(connection_id.empty()); |
| 490 mojo::system::ManualResetWaitableEvent event; | 490 ManualResetWaitableEvent event; |
| 491 ChannelInfo* channel_info = nullptr; | 491 ChannelInfo* channel_info = nullptr; |
| 492 ScopedMessagePipeHandle mp = ConnectToMaster( | 492 ScopedMessagePipeHandle mp = ConnectToMaster( |
| 493 connection_id, | 493 connection_id, |
| 494 base::Bind(&mojo::system::ManualResetWaitableEvent::Signal, | 494 base::Bind(&ManualResetWaitableEvent::Signal, base::Unretained(&event)), |
| 495 base::Unretained(&event)), | |
| 496 nullptr, &channel_info); | 495 nullptr, &channel_info); |
| 497 ASSERT_TRUE(mp.is_valid()); | 496 ASSERT_TRUE(mp.is_valid()); |
| 498 EXPECT_TRUE(channel_info); | 497 EXPECT_TRUE(channel_info); |
| 499 | 498 |
| 500 // Wait for the master to send us a message. | 499 // Wait for the master to send us a message. |
| 501 EXPECT_EQ(MOJO_RESULT_OK, | 500 EXPECT_EQ(MOJO_RESULT_OK, |
| 502 Wait(mp.get(), MOJO_HANDLE_SIGNAL_READABLE, | 501 Wait(mp.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 503 mojo::system::test::ActionTimeout(), nullptr)); | 502 mojo::system::test::ActionTimeout(), nullptr)); |
| 504 | 503 |
| 505 // It should say "hello". | 504 // It should say "hello". |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 782 | 781 |
| 783 EXPECT_TRUE(test::Shutdown()); | 782 EXPECT_TRUE(test::Shutdown()); |
| 784 } | 783 } |
| 785 | 784 |
| 786 // TODO(vtl): Test immediate write & close. | 785 // TODO(vtl): Test immediate write & close. |
| 787 // TODO(vtl): Test broken-connection cases. | 786 // TODO(vtl): Test broken-connection cases. |
| 788 | 787 |
| 789 } // namespace | 788 } // namespace |
| 790 } // namespace embedder | 789 } // namespace embedder |
| 791 } // namespace mojo | 790 } // namespace mojo |
| OLD | NEW |