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 |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "mojo/edk/util/mutex.h" | 21 #include "mojo/edk/util/mutex.h" |
22 #include "mojo/edk/util/ref_ptr.h" | 22 #include "mojo/edk/util/ref_ptr.h" |
23 #include "mojo/edk/util/thread_annotations.h" | 23 #include "mojo/edk/util/thread_annotations.h" |
24 #include "mojo/edk/util/waitable_event.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::platform::PlatformHandleWatcher; |
31 using mojo::platform::ScopedPlatformHandle; | 32 using mojo::platform::ScopedPlatformHandle; |
32 using mojo::platform::TaskRunner; | 33 using mojo::platform::TaskRunner; |
33 using mojo::system::test::TestIOThread; | 34 using mojo::system::test::TestIOThread; |
34 using mojo::util::ManualResetWaitableEvent; | 35 using mojo::util::ManualResetWaitableEvent; |
35 using mojo::util::Mutex; | 36 using mojo::util::Mutex; |
36 using mojo::util::MutexLocker; | 37 using mojo::util::MutexLocker; |
37 using mojo::util::RefPtr; | 38 using mojo::util::RefPtr; |
38 | 39 |
39 namespace mojo { | 40 namespace mojo { |
40 namespace embedder { | 41 namespace embedder { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 class EmbedderTest : public testing::Test { | 126 class EmbedderTest : public testing::Test { |
126 public: | 127 public: |
127 EmbedderTest() : test_io_thread_(TestIOThread::StartMode::AUTO) {} | 128 EmbedderTest() : test_io_thread_(TestIOThread::StartMode::AUTO) {} |
128 ~EmbedderTest() override {} | 129 ~EmbedderTest() override {} |
129 | 130 |
130 protected: | 131 protected: |
131 TestIOThread& test_io_thread() { return test_io_thread_; } | 132 TestIOThread& test_io_thread() { return test_io_thread_; } |
132 const RefPtr<TaskRunner>& test_io_task_runner() { | 133 const RefPtr<TaskRunner>& test_io_task_runner() { |
133 return test_io_thread_.task_runner(); | 134 return test_io_thread_.task_runner(); |
134 } | 135 } |
| 136 PlatformHandleWatcher* test_io_watcher() { |
| 137 return test_io_thread_.platform_handle_watcher(); |
| 138 } |
135 | 139 |
136 private: | 140 private: |
137 void SetUp() override { test::InitWithSimplePlatformSupport(); } | 141 void SetUp() override { test::InitWithSimplePlatformSupport(); } |
138 | 142 |
139 void TearDown() override { EXPECT_TRUE(test::Shutdown()); } | 143 void TearDown() override { EXPECT_TRUE(test::Shutdown()); } |
140 | 144 |
141 TestIOThread test_io_thread_; | 145 TestIOThread test_io_thread_; |
142 | 146 |
143 MOJO_DISALLOW_COPY_AND_ASSIGN(EmbedderTest); | 147 MOJO_DISALLOW_COPY_AND_ASSIGN(EmbedderTest); |
144 }; | 148 }; |
145 | 149 |
146 TEST_F(EmbedderTest, ChannelsBasic) { | 150 TEST_F(EmbedderTest, ChannelsBasic) { |
147 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner().Clone()); | 151 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner().Clone(), |
| 152 test_io_watcher()); |
148 | 153 |
149 PlatformChannelPair channel_pair; | 154 PlatformChannelPair channel_pair; |
150 ScopedTestChannel server_channel(channel_pair.PassServerHandle()); | 155 ScopedTestChannel server_channel(channel_pair.PassServerHandle()); |
151 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | 156 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); |
152 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | 157 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); |
153 ScopedTestChannel client_channel(channel_pair.PassClientHandle()); | 158 ScopedTestChannel client_channel(channel_pair.PassClientHandle()); |
154 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); | 159 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); |
155 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | 160 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); |
156 | 161 |
157 // We can write to a message pipe handle immediately. | 162 // We can write to a message pipe handle immediately. |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 test_io_thread().PostTask( | 274 test_io_thread().PostTask( |
270 [server_mp_value]() { CloseRaw(server_mp_value); }); | 275 [server_mp_value]() { CloseRaw(server_mp_value); }); |
271 } | 276 } |
272 | 277 |
273 EXPECT_TRUE(unsatisfiable_waiter.TryWait()); | 278 EXPECT_TRUE(unsatisfiable_waiter.TryWait()); |
274 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 279 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
275 unsatisfiable_waiter.wait_result()); | 280 unsatisfiable_waiter.wait_result()); |
276 } | 281 } |
277 | 282 |
278 TEST_F(EmbedderTest, ChannelsHandlePassing) { | 283 TEST_F(EmbedderTest, ChannelsHandlePassing) { |
279 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner().Clone()); | 284 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner().Clone(), |
| 285 test_io_watcher()); |
280 | 286 |
281 PlatformChannelPair channel_pair; | 287 PlatformChannelPair channel_pair; |
282 ScopedTestChannel server_channel(channel_pair.PassServerHandle()); | 288 ScopedTestChannel server_channel(channel_pair.PassServerHandle()); |
283 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | 289 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); |
284 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | 290 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); |
285 ScopedTestChannel client_channel(channel_pair.PassClientHandle()); | 291 ScopedTestChannel client_channel(channel_pair.PassClientHandle()); |
286 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); | 292 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); |
287 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | 293 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); |
288 | 294 |
289 MojoHandle h0, h1; | 295 MojoHandle h0, h1; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 } | 403 } |
398 | 404 |
399 #if defined(OS_ANDROID) | 405 #if defined(OS_ANDROID) |
400 // Android multi-process tests are not executing the new process. This is flaky. | 406 // Android multi-process tests are not executing the new process. This is flaky. |
401 // TODO(vtl): I'm guessing this is true of this test too? | 407 // TODO(vtl): I'm guessing this is true of this test too? |
402 #define MAYBE_MultiprocessMasterSlave DISABLED_MultiprocessMasterSlave | 408 #define MAYBE_MultiprocessMasterSlave DISABLED_MultiprocessMasterSlave |
403 #else | 409 #else |
404 #define MAYBE_MultiprocessMasterSlave MultiprocessMasterSlave | 410 #define MAYBE_MultiprocessMasterSlave MultiprocessMasterSlave |
405 #endif // defined(OS_ANDROID) | 411 #endif // defined(OS_ANDROID) |
406 TEST_F(EmbedderTest, MAYBE_MultiprocessMasterSlave) { | 412 TEST_F(EmbedderTest, MAYBE_MultiprocessMasterSlave) { |
407 mojo::test::ScopedMasterIPCSupport ipc_support(test_io_task_runner().Clone()); | 413 mojo::test::ScopedMasterIPCSupport ipc_support(test_io_task_runner().Clone(), |
| 414 test_io_watcher()); |
408 | 415 |
409 mojo::test::MultiprocessTestHelper multiprocess_test_helper; | 416 mojo::test::MultiprocessTestHelper multiprocess_test_helper; |
410 std::string connection_id; | 417 std::string connection_id; |
411 ManualResetWaitableEvent event; | 418 ManualResetWaitableEvent event; |
412 ChannelInfo* channel_info = nullptr; | 419 ChannelInfo* channel_info = nullptr; |
413 ScopedMessagePipeHandle mp = ConnectToSlave( | 420 ScopedMessagePipeHandle mp = ConnectToSlave( |
414 nullptr, multiprocess_test_helper.server_platform_handle.Pass(), | 421 nullptr, multiprocess_test_helper.server_platform_handle.Pass(), |
415 [&event]() { event.Signal(); }, nullptr, &connection_id, &channel_info); | 422 [&event]() { event.Signal(); }, nullptr, &connection_id, &channel_info); |
416 ASSERT_TRUE(mp.is_valid()); | 423 ASSERT_TRUE(mp.is_valid()); |
417 EXPECT_TRUE(channel_info); | 424 EXPECT_TRUE(channel_info); |
(...skipping 23 matching lines...) Expand all Loading... |
441 | 448 |
442 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown()); | 449 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown()); |
443 | 450 |
444 EXPECT_FALSE(event.WaitWithTimeout(mojo::system::test::ActionTimeout())); | 451 EXPECT_FALSE(event.WaitWithTimeout(mojo::system::test::ActionTimeout())); |
445 test_io_thread().PostTaskAndWait( | 452 test_io_thread().PostTaskAndWait( |
446 [channel_info]() { DestroyChannelOnIOThread(channel_info); }); | 453 [channel_info]() { DestroyChannelOnIOThread(channel_info); }); |
447 } | 454 } |
448 | 455 |
449 TEST_F(EmbedderTest, ChannelShutdownRace_MessagePipeClose) { | 456 TEST_F(EmbedderTest, ChannelShutdownRace_MessagePipeClose) { |
450 const size_t kIterations = 1000; | 457 const size_t kIterations = 1000; |
451 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner().Clone()); | 458 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner().Clone(), |
| 459 test_io_watcher()); |
452 | 460 |
453 for (size_t i = 0; i < kIterations; i++) { | 461 for (size_t i = 0; i < kIterations; i++) { |
454 PlatformChannelPair channel_pair; | 462 PlatformChannelPair channel_pair; |
455 std::unique_ptr<ScopedTestChannel> server_channel( | 463 std::unique_ptr<ScopedTestChannel> server_channel( |
456 new ScopedTestChannel(channel_pair.PassServerHandle())); | 464 new ScopedTestChannel(channel_pair.PassServerHandle())); |
457 server_channel->WaitForChannelCreationCompletion(); | 465 server_channel->WaitForChannelCreationCompletion(); |
458 server_channel->NoWaitOnShutdown(); | 466 server_channel->NoWaitOnShutdown(); |
459 | 467 |
460 MojoHandle server_mp = server_channel->bootstrap_message_pipe(); | 468 MojoHandle server_mp = server_channel->bootstrap_message_pipe(); |
461 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | 469 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); |
462 | 470 |
463 // Race between channel shutdown and closing a message pipe. The message | 471 // Race between channel shutdown and closing a message pipe. The message |
464 // pipe doesn't have to be the bootstrap pipe. It just has to be bound to | 472 // pipe doesn't have to be the bootstrap pipe. It just has to be bound to |
465 // the channel. | 473 // the channel. |
466 server_channel.reset(); | 474 server_channel.reset(); |
467 MojoClose(server_mp); | 475 MojoClose(server_mp); |
468 } | 476 } |
469 } | 477 } |
470 | 478 |
471 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessMasterSlave) { | 479 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessMasterSlave) { |
472 ScopedPlatformHandle client_platform_handle = | 480 ScopedPlatformHandle client_platform_handle = |
473 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | 481 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); |
474 EXPECT_TRUE(client_platform_handle.is_valid()); | 482 EXPECT_TRUE(client_platform_handle.is_valid()); |
475 | 483 |
476 TestIOThread test_io_thread(TestIOThread::StartMode::AUTO); | 484 TestIOThread test_io_thread(TestIOThread::StartMode::AUTO); |
477 test::InitWithSimplePlatformSupport(); | 485 test::InitWithSimplePlatformSupport(); |
478 | 486 |
479 { | 487 { |
480 mojo::test::ScopedSlaveIPCSupport ipc_support( | 488 mojo::test::ScopedSlaveIPCSupport ipc_support( |
481 test_io_thread.task_runner().Clone(), client_platform_handle.Pass()); | 489 test_io_thread.task_runner().Clone(), |
| 490 test_io_thread.platform_handle_watcher(), |
| 491 client_platform_handle.Pass()); |
482 | 492 |
483 std::string connection_id; | 493 std::string connection_id; |
484 ASSERT_TRUE(mojo::system::test::GetTestCommandLine()->GetOptionValue( | 494 ASSERT_TRUE(mojo::system::test::GetTestCommandLine()->GetOptionValue( |
485 kConnectionIdFlag, &connection_id)); | 495 kConnectionIdFlag, &connection_id)); |
486 ASSERT_FALSE(connection_id.empty()); | 496 ASSERT_FALSE(connection_id.empty()); |
487 ManualResetWaitableEvent event; | 497 ManualResetWaitableEvent event; |
488 ChannelInfo* channel_info = nullptr; | 498 ChannelInfo* channel_info = nullptr; |
489 ScopedMessagePipeHandle mp = ConnectToMaster( | 499 ScopedMessagePipeHandle mp = ConnectToMaster( |
490 connection_id, [&event]() { event.Signal(); }, nullptr, &channel_info); | 500 connection_id, [&event]() { event.Signal(); }, nullptr, &channel_info); |
491 ASSERT_TRUE(mp.is_valid()); | 501 ASSERT_TRUE(mp.is_valid()); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 | 546 |
537 #if defined(OS_ANDROID) | 547 #if defined(OS_ANDROID) |
538 // Android multi-process tests are not executing the new process. This is flaky. | 548 // Android multi-process tests are not executing the new process. This is flaky. |
539 #define MAYBE_MultiprocessChannels DISABLED_MultiprocessChannels | 549 #define MAYBE_MultiprocessChannels DISABLED_MultiprocessChannels |
540 #else | 550 #else |
541 #define MAYBE_MultiprocessChannels MultiprocessChannels | 551 #define MAYBE_MultiprocessChannels MultiprocessChannels |
542 #endif // defined(OS_ANDROID) | 552 #endif // defined(OS_ANDROID) |
543 TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) { | 553 TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) { |
544 // TODO(vtl): This should eventually initialize a master process instead, | 554 // TODO(vtl): This should eventually initialize a master process instead, |
545 // probably. | 555 // probably. |
546 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner().Clone()); | 556 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner().Clone(), |
| 557 test_io_watcher()); |
547 | 558 |
548 mojo::test::MultiprocessTestHelper multiprocess_test_helper; | 559 mojo::test::MultiprocessTestHelper multiprocess_test_helper; |
549 multiprocess_test_helper.StartChild("MultiprocessChannelsClient"); | 560 multiprocess_test_helper.StartChild("MultiprocessChannelsClient"); |
550 | 561 |
551 { | 562 { |
552 ScopedTestChannel server_channel( | 563 ScopedTestChannel server_channel( |
553 multiprocess_test_helper.server_platform_handle.Pass()); | 564 multiprocess_test_helper.server_platform_handle.Pass()); |
554 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | 565 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); |
555 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | 566 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); |
556 server_channel.WaitForChannelCreationCompletion(); | 567 server_channel.WaitForChannelCreationCompletion(); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | 674 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); |
664 EXPECT_TRUE(client_platform_handle.is_valid()); | 675 EXPECT_TRUE(client_platform_handle.is_valid()); |
665 | 676 |
666 TestIOThread test_io_thread(TestIOThread::StartMode::AUTO); | 677 TestIOThread test_io_thread(TestIOThread::StartMode::AUTO); |
667 test::InitWithSimplePlatformSupport(); | 678 test::InitWithSimplePlatformSupport(); |
668 | 679 |
669 { | 680 { |
670 // TODO(vtl): This should eventually initialize a slave process instead, | 681 // TODO(vtl): This should eventually initialize a slave process instead, |
671 // probably. | 682 // probably. |
672 mojo::test::ScopedIPCSupport ipc_support( | 683 mojo::test::ScopedIPCSupport ipc_support( |
673 test_io_thread.task_runner().Clone()); | 684 test_io_thread.task_runner().Clone(), |
| 685 test_io_thread.platform_handle_watcher()); |
674 | 686 |
675 ScopedTestChannel client_channel(client_platform_handle.Pass()); | 687 ScopedTestChannel client_channel(client_platform_handle.Pass()); |
676 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); | 688 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); |
677 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | 689 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); |
678 client_channel.WaitForChannelCreationCompletion(); | 690 client_channel.WaitForChannelCreationCompletion(); |
679 CHECK(client_channel.channel_info() != nullptr); | 691 CHECK(client_channel.channel_info() != nullptr); |
680 | 692 |
681 // 1. Read the first message from |client_mp|. | 693 // 1. Read the first message from |client_mp|. |
682 MojoHandleSignalsState state; | 694 MojoHandleSignalsState state; |
683 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, | 695 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 | 789 |
778 EXPECT_TRUE(test::Shutdown()); | 790 EXPECT_TRUE(test::Shutdown()); |
779 } | 791 } |
780 | 792 |
781 // TODO(vtl): Test immediate write & close. | 793 // TODO(vtl): Test immediate write & close. |
782 // TODO(vtl): Test broken-connection cases. | 794 // TODO(vtl): Test broken-connection cases. |
783 | 795 |
784 } // namespace | 796 } // namespace |
785 } // namespace embedder | 797 } // namespace embedder |
786 } // namespace mojo | 798 } // namespace mojo |
OLD | NEW |