Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: mojo/edk/embedder/embedder_unittest.cc

Issue 1350023003: Add a Mojo EDK for Chrome that uses one OS pipe per message pipe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: convert remaining MP tests and simplify RawChannel destruction Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
8
9 #include "base/bind.h" 7 #include "base/bind.h"
10 #include "base/command_line.h" 8 #include "base/command_line.h"
11 #include "base/location.h"
12 #include "base/logging.h" 9 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
14 #include "base/synchronization/waitable_event.h" 11 #include "base/synchronization/waitable_event.h"
15 #include "base/test/test_io_thread.h" 12 #include "base/test/test_io_thread.h"
16 #include "base/test/test_timeouts.h" 13 #include "base/test/test_timeouts.h"
14 #include "mojo/edk/embedder/embedder.h"
17 #include "mojo/edk/embedder/platform_channel_pair.h" 15 #include "mojo/edk/embedder/platform_channel_pair.h"
16 #include "mojo/edk/embedder/simple_platform_support.h"
18 #include "mojo/edk/embedder/test_embedder.h" 17 #include "mojo/edk/embedder/test_embedder.h"
19 #include "mojo/edk/system/mutex.h"
20 #include "mojo/edk/system/test_utils.h" 18 #include "mojo/edk/system/test_utils.h"
21 #include "mojo/edk/test/multiprocess_test_helper.h" 19 #include "mojo/edk/test/multiprocess_test_helper.h"
22 #include "mojo/edk/test/scoped_ipc_support.h" 20 #include "mojo/edk/test/scoped_ipc_support.h"
21 #include "mojo/message_pump/message_pump_mojo.h"
23 #include "mojo/public/c/system/core.h" 22 #include "mojo/public/c/system/core.h"
24 #include "mojo/public/cpp/system/handle.h" 23 #include "mojo/public/cpp/system/handle.h"
25 #include "mojo/public/cpp/system/macros.h" 24 #include "mojo/public/cpp/system/macros.h"
26 #include "mojo/public/cpp/system/message_pipe.h" 25 #include "mojo/public/cpp/system/message_pipe.h"
27 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
28 27
29 namespace mojo { 28 namespace mojo {
30 namespace embedder { 29 namespace embedder {
31 namespace { 30 namespace {
32 31
33 const MojoHandleSignals kSignalReadadableWritable = 32 const MojoHandleSignals kSignalReadadableWritable =
34 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE; 33 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE;
35 34
36 const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE | 35 const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE |
37 MOJO_HANDLE_SIGNAL_WRITABLE | 36 MOJO_HANDLE_SIGNAL_WRITABLE |
38 MOJO_HANDLE_SIGNAL_PEER_CLOSED; 37 MOJO_HANDLE_SIGNAL_PEER_CLOSED;
39 38
40 const char kConnectionIdFlag[] = "test-connection-id"; 39 class EmbedderTest : public testing::Test {
41
42 void DoNothing() {}
43
44 class ScopedTestChannel {
45 public: 40 public:
46 // Creates a channel, which lives on the I/O thread given to 41 EmbedderTest()
47 // |InitIPCSupport()|. After construction, |bootstrap_message_pipe()| gives 42 : test_io_thread_(base::TestIOThread::kAutoStart),
48 // the Mojo handle for the bootstrap message pipe on this channel; it is up to 43 ipc_support_(test_io_thread_.task_runner()) {}
49 // the caller to close this handle. Note: The I/O thread must outlive this 44 ~EmbedderTest() override {}
50 // object (and its message loop must continue pumping messages while this
51 // object is alive).
52 explicit ScopedTestChannel(ScopedPlatformHandle platform_handle)
53 : bootstrap_message_pipe_(MOJO_HANDLE_INVALID),
54 event_(true, false), // Manual reset.
55 channel_info_(nullptr),
56 wait_on_shutdown_(true) {
57 bootstrap_message_pipe_ =
58 CreateChannel(platform_handle.Pass(),
59 base::Bind(&ScopedTestChannel::DidCreateChannel,
60 base::Unretained(this)),
61 nullptr)
62 .release()
63 .value();
64 CHECK_NE(bootstrap_message_pipe_, MOJO_HANDLE_INVALID);
65 }
66
67 // Destructor: Shuts down the channel. (As noted above, for this to happen,
68 // the I/O thread must be alive and pumping messages.)
69 ~ScopedTestChannel() {
70 // |WaitForChannelCreationCompletion()| must be called before destruction.
71 CHECK(event_.IsSignaled());
72 event_.Reset();
73 if (wait_on_shutdown_) {
74 DestroyChannel(channel_info_,
75 base::Bind(&ScopedTestChannel::DidDestroyChannel,
76 base::Unretained(this)),
77 nullptr);
78 event_.Wait();
79 } else {
80 DestroyChannel(channel_info_, base::Bind(&DoNothing), nullptr);
81 }
82 }
83
84 // Waits for channel creation to be completed.
85 void WaitForChannelCreationCompletion() { event_.Wait(); }
86
87 MojoHandle bootstrap_message_pipe() const { return bootstrap_message_pipe_; }
88
89 // Call only after |WaitForChannelCreationCompletion()|. Use only to check
90 // that it's not null.
91 const ChannelInfo* channel_info() const { return channel_info_; }
92
93 // Don't wait for the channel shutdown to finish on destruction. Used to
94 // exercise races.
95 void NoWaitOnShutdown() { wait_on_shutdown_ = false; }
96 45
97 private: 46 private:
98 void DidCreateChannel(ChannelInfo* channel_info) { 47 base::MessageLoop message_loop_;
99 CHECK(channel_info);
100 CHECK(!channel_info_);
101 channel_info_ = channel_info;
102 event_.Signal();
103 }
104
105 void DidDestroyChannel() { event_.Signal(); }
106
107 // Valid from creation until whenever it gets closed (by the "owner" of this
108 // object).
109 // Note: We don't want use the C++ wrappers here, since we want to test the
110 // API at the lowest level.
111 MojoHandle bootstrap_message_pipe_;
112
113 // Set after channel creation has been completed (i.e., the callback to
114 // |CreateChannel()| has been called). Also used in the destructor to wait for
115 // |DestroyChannel()| completion.
116 base::WaitableEvent event_;
117
118 // Valid after channel creation completion until destruction.
119 ChannelInfo* channel_info_;
120
121 // Whether the destructor should wait until the channel is destroyed.
122 bool wait_on_shutdown_;
123
124 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedTestChannel);
125 };
126
127 class EmbedderTest : public testing::Test {
128 public:
129 EmbedderTest() : test_io_thread_(base::TestIOThread::kAutoStart) {}
130 ~EmbedderTest() override {}
131
132 protected:
133 base::TestIOThread& test_io_thread() { return test_io_thread_; }
134 scoped_refptr<base::TaskRunner> test_io_task_runner() {
135 return test_io_thread_.task_runner();
136 }
137
138 private:
139 void SetUp() override { test::InitWithSimplePlatformSupport(); }
140
141 void TearDown() override { EXPECT_TRUE(test::Shutdown()); }
142
143 base::TestIOThread test_io_thread_; 48 base::TestIOThread test_io_thread_;
49 mojo::test::ScopedIPCSupport ipc_support_;
144 50
145 MOJO_DISALLOW_COPY_AND_ASSIGN(EmbedderTest); 51 MOJO_DISALLOW_COPY_AND_ASSIGN(EmbedderTest);
146 }; 52 };
147 53
148 TEST_F(EmbedderTest, ChannelsBasic) { 54 TEST_F(EmbedderTest, ChannelBasic) {
149 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); 55 MojoHandle server_mp, client_mp;
150 56 ASSERT_EQ(MOJO_RESULT_OK,
151 PlatformChannelPair channel_pair; 57 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp));
152 ScopedTestChannel server_channel(channel_pair.PassServerHandle());
153 MojoHandle server_mp = server_channel.bootstrap_message_pipe();
154 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
155 ScopedTestChannel client_channel(channel_pair.PassClientHandle());
156 MojoHandle client_mp = client_channel.bootstrap_message_pipe();
157 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID);
158 58
159 // We can write to a message pipe handle immediately. 59 // We can write to a message pipe handle immediately.
160 const char kHello[] = "hello"; 60 const char kHello[] = "hello";
161 EXPECT_EQ( 61
162 MOJO_RESULT_OK, 62 size_t write_size = sizeof(kHello);
163 MojoWriteMessage(server_mp, kHello, static_cast<uint32_t>(sizeof(kHello)), 63 const char* write_buffer = kHello;
164 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); 64 EXPECT_EQ(MOJO_RESULT_OK,
65 MojoWriteMessage(server_mp, write_buffer,
66 static_cast<uint32_t>(write_size), nullptr, 0,
67 MOJO_WRITE_MESSAGE_FLAG_NONE));
165 68
166 // Now wait for the other side to become readable. 69 // Now wait for the other side to become readable.
167 MojoHandleSignalsState state; 70 MojoHandleSignalsState state;
168 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, 71 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE,
169 MOJO_DEADLINE_INDEFINITE, &state)); 72 MOJO_DEADLINE_INDEFINITE, &state));
170 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); 73 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
171 EXPECT_EQ(kSignalAll, state.satisfiable_signals); 74 EXPECT_EQ(kSignalAll, state.satisfiable_signals);
172 75
173 char buffer[1000] = {}; 76 char read_buffer[1000] = {};
174 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); 77 uint32_t num_bytes = static_cast<uint32_t>(sizeof(read_buffer));
175 EXPECT_EQ(MOJO_RESULT_OK, 78 EXPECT_EQ(MOJO_RESULT_OK,
176 MojoReadMessage(client_mp, buffer, &num_bytes, nullptr, nullptr, 79 MojoReadMessage(client_mp, read_buffer, &num_bytes, nullptr,
177 MOJO_READ_MESSAGE_FLAG_NONE)); 80 nullptr, MOJO_READ_MESSAGE_FLAG_NONE));
178 EXPECT_EQ(sizeof(kHello), num_bytes); 81 EXPECT_EQ(write_size, num_bytes);
179 EXPECT_STREQ(kHello, buffer); 82 EXPECT_STREQ(kHello, read_buffer);
180 83
181 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); 84 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
182 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); 85 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
183
184 // By this point, these waits should basically be no-ops (since we've waited
185 // for the client message pipe to become readable, which implies that both
186 // the server and client channels were completely created).
187 server_channel.WaitForChannelCreationCompletion();
188 client_channel.WaitForChannelCreationCompletion();
189 EXPECT_TRUE(server_channel.channel_info());
190 EXPECT_TRUE(client_channel.channel_info());
191 } 86 }
192 87
193 class TestAsyncWaiter { 88 // Test sending a MP which has read messages out of the pipe.
194 public: 89 TEST_F(EmbedderTest, SendReadableMessagePipe) {
195 TestAsyncWaiter() : event_(true, false), wait_result_(MOJO_RESULT_UNKNOWN) {} 90 MojoHandle server_mp, client_mp;
91 ASSERT_EQ(MOJO_RESULT_OK,
92 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp));
196 93
197 void Awake(MojoResult result) { 94 MojoHandle server_mp2, client_mp2;
198 system::MutexLocker l(&wait_result_mutex_); 95 ASSERT_EQ(MOJO_RESULT_OK,
199 wait_result_ = result; 96 MojoCreateMessagePipe(nullptr, &server_mp2, &client_mp2));
200 event_.Signal();
201 }
202 97
203 bool TryWait() { return event_.TimedWait(TestTimeouts::action_timeout()); } 98 // Write to server2 and wait for client2 to be readable before sending it.
99 const char kHello[] = "hello";
100 size_t write_size;
101 const char* write_buffer;
102 write_buffer = kHello;
103 write_size = sizeof(kHello);
104 EXPECT_EQ(MOJO_RESULT_OK,
105 MojoWriteMessage(server_mp2, write_buffer,
106 static_cast<uint32_t>(write_size), nullptr, 0,
107 MOJO_WRITE_MESSAGE_FLAG_NONE));
204 108
205 MojoResult wait_result() const { 109 MojoHandleSignalsState state;
206 system::MutexLocker l(&wait_result_mutex_); 110 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp2, MOJO_HANDLE_SIGNAL_READABLE,
207 return wait_result_; 111 MOJO_DEADLINE_INDEFINITE, &state));
208 } 112 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
113 EXPECT_EQ(kSignalAll, state.satisfiable_signals);
209 114
210 private: 115 // Now send client2
211 base::WaitableEvent event_; 116 EXPECT_EQ(MOJO_RESULT_OK,
117 MojoWriteMessage(server_mp, write_buffer,
118 static_cast<uint32_t>(write_size), &client_mp2, 1,
119 MOJO_WRITE_MESSAGE_FLAG_NONE));
212 120
213 mutable system::Mutex wait_result_mutex_;
214 MojoResult wait_result_ MOJO_GUARDED_BY(wait_result_mutex_);
215 121
216 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaiter); 122 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE,
217 }; 123 MOJO_DEADLINE_INDEFINITE, &state));
124 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
125 EXPECT_EQ(kSignalAll, state.satisfiable_signals);
218 126
219 void WriteHello(MessagePipeHandle pipe) { 127 char read_buffer[20000] = {};
220 static const char kHello[] = "hello"; 128 uint32_t num_bytes = static_cast<uint32_t>(sizeof(read_buffer));
221 CHECK_EQ(MOJO_RESULT_OK, 129 MojoHandle ports[10];
222 WriteMessageRaw(pipe, kHello, static_cast<uint32_t>(sizeof(kHello)), 130 uint32_t num_ports;
223 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); 131 EXPECT_EQ(MOJO_RESULT_OK,
224 } 132 MojoReadMessage(client_mp, read_buffer, &num_bytes, &ports[0],
133 &num_ports, MOJO_READ_MESSAGE_FLAG_NONE));
134 EXPECT_EQ(write_size, num_bytes);
135 EXPECT_STREQ(kHello, read_buffer);
136 EXPECT_EQ(1, num_ports);
225 137
226 void CloseScopedHandle(ScopedMessagePipeHandle handle) {
227 // Do nothing and the destructor will close it.
228 }
229 138
230 TEST_F(EmbedderTest, AsyncWait) { 139 client_mp2 = ports[0];
231 ScopedMessagePipeHandle client_mp; 140 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp2, MOJO_HANDLE_SIGNAL_READABLE,
232 ScopedMessagePipeHandle server_mp; 141 MOJO_DEADLINE_INDEFINITE, &state));
233 EXPECT_EQ(MOJO_RESULT_OK, CreateMessagePipe(nullptr, &client_mp, &server_mp)); 142 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
143 EXPECT_EQ(kSignalAll, state.satisfiable_signals);
234 144
235 TestAsyncWaiter waiter; 145
236 EXPECT_EQ(MOJO_RESULT_OK, 146 EXPECT_EQ(MOJO_RESULT_OK,
237 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE, 147 MojoReadMessage(client_mp2, read_buffer, &num_bytes, nullptr, 0,
238 base::Bind(&TestAsyncWaiter::Awake, 148 MOJO_READ_MESSAGE_FLAG_NONE));
239 base::Unretained(&waiter)))); 149 EXPECT_EQ(write_size, num_bytes);
150 EXPECT_STREQ(kHello, read_buffer);;
240 151
241 test_io_task_runner()->PostTask(FROM_HERE,
242 base::Bind(&WriteHello, server_mp.get()));
243 EXPECT_TRUE(waiter.TryWait());
244 EXPECT_EQ(MOJO_RESULT_OK, waiter.wait_result());
245 152
246 // If message is in the queue, it does't allow us to wait. 153 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp2));
247 TestAsyncWaiter waiter_that_doesnt_wait; 154 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp2));
248 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
249 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
250 base::Bind(&TestAsyncWaiter::Awake,
251 base::Unretained(&waiter_that_doesnt_wait))));
252 155
253 char buffer[1000];
254 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
255 CHECK_EQ(MOJO_RESULT_OK,
256 ReadMessageRaw(client_mp.get(), buffer, &num_bytes, nullptr, nullptr,
257 MOJO_READ_MESSAGE_FLAG_NONE));
258 156
259 TestAsyncWaiter unsatisfiable_waiter; 157 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
260 EXPECT_EQ(MOJO_RESULT_OK, 158 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
261 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
262 base::Bind(&TestAsyncWaiter::Awake,
263 base::Unretained(&unsatisfiable_waiter))));
264
265 test_io_task_runner()->PostTask(
266 FROM_HERE,
267 base::Bind(&CloseScopedHandle, base::Passed(server_mp.Pass())));
268
269 EXPECT_TRUE(unsatisfiable_waiter.TryWait());
270 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
271 unsatisfiable_waiter.wait_result());
272 } 159 }
273 160
274 TEST_F(EmbedderTest, ChannelsHandlePassing) { 161 TEST_F(EmbedderTest, ChannelsHandlePassing) {
275 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); 162 MojoHandle server_mp, client_mp;
276 163 ASSERT_EQ(MOJO_RESULT_OK,
277 PlatformChannelPair channel_pair; 164 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp));
278 ScopedTestChannel server_channel(channel_pair.PassServerHandle());
279 MojoHandle server_mp = server_channel.bootstrap_message_pipe();
280 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); 165 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
281 ScopedTestChannel client_channel(channel_pair.PassClientHandle());
282 MojoHandle client_mp = client_channel.bootstrap_message_pipe();
283 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); 166 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID);
284 167
285 MojoHandle h0, h1; 168 MojoHandle h0, h1;
286 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1)); 169 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1));
287 170
288 // Write a message to |h0| (attaching nothing). 171 // Write a message to |h0| (attaching nothing).
289 const char kHello[] = "hello"; 172 const char kHello[] = "hello";
290 EXPECT_EQ(MOJO_RESULT_OK, 173 EXPECT_EQ(MOJO_RESULT_OK,
291 MojoWriteMessage(h0, kHello, static_cast<uint32_t>(sizeof(kHello)), 174 MojoWriteMessage(h0, kHello, static_cast<uint32_t>(sizeof(kHello)),
292 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); 175 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 EXPECT_EQ(MOJO_RESULT_OK, 261 EXPECT_EQ(MOJO_RESULT_OK,
379 MojoReadMessage(h0, buffer, &num_bytes, nullptr, nullptr, 262 MojoReadMessage(h0, buffer, &num_bytes, nullptr, nullptr,
380 MOJO_READ_MESSAGE_FLAG_NONE)); 263 MOJO_READ_MESSAGE_FLAG_NONE));
381 EXPECT_EQ(sizeof(kBarBaz), num_bytes); 264 EXPECT_EQ(sizeof(kBarBaz), num_bytes);
382 EXPECT_STREQ(kBarBaz, buffer); 265 EXPECT_STREQ(kBarBaz, buffer);
383 266
384 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); 267 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
385 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); 268 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
386 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0)); 269 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0));
387 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1)); 270 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1));
388
389 server_channel.WaitForChannelCreationCompletion();
390 client_channel.WaitForChannelCreationCompletion();
391 EXPECT_TRUE(server_channel.channel_info());
392 EXPECT_TRUE(client_channel.channel_info());
393 }
394
395 #if defined(OS_ANDROID)
396 // Android multi-process tests are not executing the new process. This is flaky.
397 // TODO(vtl): I'm guessing this is true of this test too?
398 #define MAYBE_MultiprocessMasterSlave DISABLED_MultiprocessMasterSlave
399 #else
400 #define MAYBE_MultiprocessMasterSlave MultiprocessMasterSlave
401 #endif // defined(OS_ANDROID)
402 TEST_F(EmbedderTest, MAYBE_MultiprocessMasterSlave) {
403 mojo::test::ScopedMasterIPCSupport ipc_support(test_io_task_runner());
404
405 mojo::test::MultiprocessTestHelper multiprocess_test_helper;
406 std::string connection_id;
407 base::WaitableEvent event(true, false);
408 ChannelInfo* channel_info = nullptr;
409 ScopedMessagePipeHandle mp = ConnectToSlave(
410 nullptr, multiprocess_test_helper.server_platform_handle.Pass(),
411 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)),
412 nullptr, &connection_id, &channel_info);
413 ASSERT_TRUE(mp.is_valid());
414 EXPECT_TRUE(channel_info);
415 ASSERT_FALSE(connection_id.empty());
416
417 multiprocess_test_helper.StartChildWithExtraSwitch(
418 "MultiprocessMasterSlave", kConnectionIdFlag, connection_id);
419
420 // Send a message saying "hello".
421 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(mp.get(), "hello", 5, nullptr, 0,
422 MOJO_WRITE_MESSAGE_FLAG_NONE));
423
424 // Wait for a response.
425 EXPECT_EQ(MOJO_RESULT_OK,
426 Wait(mp.get(), MOJO_HANDLE_SIGNAL_READABLE,
427 mojo::system::test::ActionDeadline(), nullptr));
428
429 // The response message should say "world".
430 char buffer[100];
431 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
432 EXPECT_EQ(MOJO_RESULT_OK,
433 ReadMessageRaw(mp.get(), buffer, &num_bytes, nullptr, nullptr,
434 MOJO_READ_MESSAGE_FLAG_NONE));
435 EXPECT_EQ(5u, num_bytes);
436 EXPECT_EQ(0, memcmp(buffer, "world", 5));
437
438 mp.reset();
439
440 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown());
441
442 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout()));
443 test_io_thread().PostTaskAndWait(
444 FROM_HERE,
445 base::Bind(&DestroyChannelOnIOThread, base::Unretained(channel_info)));
446 }
447
448 TEST_F(EmbedderTest, ChannelShutdownRace_MessagePipeClose) {
449 const size_t kIterations = 1000;
450 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner());
451
452 for (size_t i = 0; i < kIterations; i++) {
453 PlatformChannelPair channel_pair;
454 scoped_ptr<ScopedTestChannel> server_channel(
455 new ScopedTestChannel(channel_pair.PassServerHandle()));
456 server_channel->WaitForChannelCreationCompletion();
457 server_channel->NoWaitOnShutdown();
458
459 MojoHandle server_mp = server_channel->bootstrap_message_pipe();
460 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
461
462 // Race between channel shutdown and closing a message pipe. The message
463 // pipe doesn't have to be the bootstrap pipe. It just has to be bound to
464 // the channel.
465 server_channel.reset();
466 MojoClose(server_mp);
467 }
468 }
469
470 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessMasterSlave) {
471 ScopedPlatformHandle client_platform_handle =
472 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass();
473 EXPECT_TRUE(client_platform_handle.is_valid());
474
475 base::TestIOThread test_io_thread(base::TestIOThread::kAutoStart);
476 test::InitWithSimplePlatformSupport();
477
478 {
479 mojo::test::ScopedSlaveIPCSupport ipc_support(
480 test_io_thread.task_runner(), client_platform_handle.Pass());
481
482 const base::CommandLine& command_line =
483 *base::CommandLine::ForCurrentProcess();
484 ASSERT_TRUE(command_line.HasSwitch(kConnectionIdFlag));
485 std::string connection_id =
486 command_line.GetSwitchValueASCII(kConnectionIdFlag);
487 ASSERT_FALSE(connection_id.empty());
488 base::WaitableEvent event(true, false);
489 ChannelInfo* channel_info = nullptr;
490 ScopedMessagePipeHandle mp = ConnectToMaster(
491 connection_id,
492 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)),
493 nullptr, &channel_info);
494 ASSERT_TRUE(mp.is_valid());
495 EXPECT_TRUE(channel_info);
496
497 // Wait for the master to send us a message.
498 EXPECT_EQ(MOJO_RESULT_OK,
499 Wait(mp.get(), MOJO_HANDLE_SIGNAL_READABLE,
500 mojo::system::test::ActionDeadline(), nullptr));
501
502 // It should say "hello".
503 char buffer[100];
504 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
505 EXPECT_EQ(MOJO_RESULT_OK,
506 ReadMessageRaw(mp.get(), buffer, &num_bytes, nullptr, nullptr,
507 MOJO_READ_MESSAGE_FLAG_NONE));
508 EXPECT_EQ(5u, num_bytes);
509 EXPECT_EQ(0, memcmp(buffer, "hello", 5));
510
511 // In response send a message saying "world".
512 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(mp.get(), "world", 5, nullptr, 0,
513 MOJO_WRITE_MESSAGE_FLAG_NONE));
514
515 mp.reset();
516
517 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout()));
518 test_io_thread.PostTaskAndWait(
519 FROM_HERE,
520 base::Bind(&DestroyChannelOnIOThread, base::Unretained(channel_info)));
521 }
522
523 EXPECT_TRUE(test::Shutdown());
524 } 271 }
525 272
526 // The sequence of messages sent is: 273 // The sequence of messages sent is:
527 // server_mp client_mp mp0 mp1 mp2 mp3 274 // server_mp client_mp mp0 mp1 mp2 mp3
528 // 1. "hello" 275 // 1. "hello"
529 // 2. "world!" 276 // 2. "world!"
530 // 3. "FOO" 277 // 3. "FOO"
531 // 4. "Bar"+mp1 278 // 4. "Bar"+mp1
532 // 5. (close) 279 // 5. (close)
533 // 6. (close) 280 // 6. (close)
534 // 7. "baz" 281 // 7. "baz"
535 // 8. (closed) 282 // 8. (closed)
536 // 9. "quux"+mp2 283 // 9. "quux"+mp2
537 // 10. (close) 284 // 10. (close)
538 // 11. (wait/cl.) 285 // 11. (wait/cl.)
539 // 12. (wait/cl.) 286 // 12. (wait/cl.)
540 287
541 #if defined(OS_ANDROID) 288 #if defined(OS_ANDROID)
542 // Android multi-process tests are not executing the new process. This is flaky. 289 // Android multi-process tests are not executing the new process. This is flaky.
543 #define MAYBE_MultiprocessChannels DISABLED_MultiprocessChannels 290 #define MAYBE_MultiprocessChannels DISABLED_MultiprocessChannels
544 #else 291 #else
545 #define MAYBE_MultiprocessChannels MultiprocessChannels 292 #define MAYBE_MultiprocessChannels MultiprocessChannels
546 #endif // defined(OS_ANDROID) 293 #endif // defined(OS_ANDROID)
547 TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) { 294 TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) {
548 // TODO(vtl): This should eventually initialize a master process instead,
549 // probably.
550 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner());
551
552 mojo::test::MultiprocessTestHelper multiprocess_test_helper; 295 mojo::test::MultiprocessTestHelper multiprocess_test_helper;
553 multiprocess_test_helper.StartChild("MultiprocessChannelsClient"); 296 multiprocess_test_helper.StartChild("MultiprocessChannelsClient");
554 297
555 { 298 {
556 ScopedTestChannel server_channel( 299 MojoHandle server_mp = mojo::embedder::CreateMessagePipe(
557 multiprocess_test_helper.server_platform_handle.Pass()); 300 multiprocess_test_helper.server_platform_handle.Pass()).release().
558 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); 301 value();
559 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
560 server_channel.WaitForChannelCreationCompletion();
561 EXPECT_TRUE(server_channel.channel_info());
562 302
563 // 1. Write a message to |server_mp| (attaching nothing). 303 // 1. Write a message to |server_mp| (attaching nothing).
564 const char kHello[] = "hello"; 304 const char kHello[] = "hello";
565 EXPECT_EQ(MOJO_RESULT_OK, 305 EXPECT_EQ(MOJO_RESULT_OK,
566 MojoWriteMessage(server_mp, kHello, 306 MojoWriteMessage(server_mp, kHello,
567 static_cast<uint32_t>(sizeof(kHello)), nullptr, 307 static_cast<uint32_t>(sizeof(kHello)), nullptr,
568 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); 308 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
569 309
570 // TODO(vtl): If the scope were ended immediately here (maybe after closing 310 // TODO(vtl): If the scope were ended immediately here (maybe after closing
571 // |server_mp|), we die with a fatal error in |Channel::HandleLocalError()|. 311 // |server_mp|), we die with a fatal error in |Channel::HandleLocalError()|.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 EXPECT_EQ(MOJO_RESULT_OK, 360 EXPECT_EQ(MOJO_RESULT_OK,
621 MojoReadMessage(mp0, buffer, &num_bytes, &mp2, &num_handles, 361 MojoReadMessage(mp0, buffer, &num_bytes, &mp2, &num_handles,
622 MOJO_READ_MESSAGE_FLAG_NONE)); 362 MOJO_READ_MESSAGE_FLAG_NONE));
623 const char kQuux[] = "quux"; 363 const char kQuux[] = "quux";
624 EXPECT_EQ(sizeof(kQuux), num_bytes); 364 EXPECT_EQ(sizeof(kQuux), num_bytes);
625 EXPECT_STREQ(kQuux, buffer); 365 EXPECT_STREQ(kQuux, buffer);
626 EXPECT_EQ(1u, num_handles); 366 EXPECT_EQ(1u, num_handles);
627 EXPECT_NE(mp2, MOJO_HANDLE_INVALID); 367 EXPECT_NE(mp2, MOJO_HANDLE_INVALID);
628 368
629 // 7. Read a message from |mp2|. 369 // 7. Read a message from |mp2|.
630 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp2, MOJO_HANDLE_SIGNAL_READABLE, 370 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp2, MOJO_HANDLE_SIGNAL_PEER_CLOSED,
631 MOJO_DEADLINE_INDEFINITE, &state)); 371 MOJO_DEADLINE_INDEFINITE, &state));
632 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, 372 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
633 state.satisfied_signals); 373 state.satisfied_signals);
634 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, 374 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
635 state.satisfiable_signals); 375 state.satisfiable_signals);
636 376
637 memset(buffer, 0, sizeof(buffer)); 377 memset(buffer, 0, sizeof(buffer));
638 num_bytes = static_cast<uint32_t>(sizeof(buffer)); 378 num_bytes = static_cast<uint32_t>(sizeof(buffer));
639 EXPECT_EQ(MOJO_RESULT_OK, 379 EXPECT_EQ(MOJO_RESULT_OK,
640 MojoReadMessage(mp2, buffer, &num_bytes, nullptr, nullptr, 380 MojoReadMessage(mp2, buffer, &num_bytes, nullptr, nullptr,
(...skipping 20 matching lines...) Expand all
661 401
662 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown()); 402 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown());
663 } 403 }
664 404
665 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessChannelsClient) { 405 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessChannelsClient) {
666 ScopedPlatformHandle client_platform_handle = 406 ScopedPlatformHandle client_platform_handle =
667 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); 407 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass();
668 EXPECT_TRUE(client_platform_handle.is_valid()); 408 EXPECT_TRUE(client_platform_handle.is_valid());
669 409
670 base::TestIOThread test_io_thread(base::TestIOThread::kAutoStart); 410 base::TestIOThread test_io_thread(base::TestIOThread::kAutoStart);
671 test::InitWithSimplePlatformSupport();
672 411
673 { 412 {
674 // TODO(vtl): This should eventually initialize a slave process instead,
675 // probably.
676 mojo::test::ScopedIPCSupport ipc_support(test_io_thread.task_runner()); 413 mojo::test::ScopedIPCSupport ipc_support(test_io_thread.task_runner());
677 414 MojoHandle client_mp = mojo::embedder::CreateMessagePipe(
678 ScopedTestChannel client_channel(client_platform_handle.Pass()); 415 client_platform_handle.Pass()).release().value();
679 MojoHandle client_mp = client_channel.bootstrap_message_pipe();
680 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID);
681 client_channel.WaitForChannelCreationCompletion();
682 CHECK(client_channel.channel_info() != nullptr);
683 416
684 // 1. Read the first message from |client_mp|. 417 // 1. Read the first message from |client_mp|.
685 MojoHandleSignalsState state; 418 MojoHandleSignalsState state;
686 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, 419 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE,
687 MOJO_DEADLINE_INDEFINITE, &state)); 420 MOJO_DEADLINE_INDEFINITE, &state));
688 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); 421 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
689 EXPECT_EQ(kSignalAll, state.satisfiable_signals); 422 EXPECT_EQ(kSignalAll, state.satisfiable_signals);
690 423
691 char buffer[1000] = {}; 424 char buffer[1000] = {};
692 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); 425 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
770 EXPECT_STREQ(kFoo, buffer); 503 EXPECT_STREQ(kFoo, buffer);
771 504
772 // 11. Wait on |mp1| (which should eventually fail) and then close it. 505 // 11. Wait on |mp1| (which should eventually fail) and then close it.
773 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 506 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
774 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, 507 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE,
775 MOJO_DEADLINE_INDEFINITE, &state)); 508 MOJO_DEADLINE_INDEFINITE, &state));
776 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals); 509 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals);
777 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); 510 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals);
778 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp1)); 511 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp1));
779 } 512 }
780
781 EXPECT_TRUE(test::Shutdown());
782 } 513 }
783 514
784 // TODO(vtl): Test immediate write & close. 515 // TODO(vtl): Test immediate write & close.
785 // TODO(vtl): Test broken-connection cases. 516 // TODO(vtl): Test broken-connection cases.
786 517
787 } // namespace 518 } // namespace
788 } // namespace embedder 519 } // namespace embedder
789 } // namespace mojo 520 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698