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

Side by Side Diff: mojo/public/cpp/bindings/tests/connector_unittest.cc

Issue 1455063004: Mojo C++ bindings: introduce MultiplexRouter and related classes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 1 month 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 <stdlib.h> 5 #include <stdlib.h>
6 #include <string.h> 6 #include <string.h>
7 7
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "mojo/message_pump/message_pump_mojo.h" 9 #include "mojo/message_pump/message_pump_mojo.h"
10 #include "mojo/public/cpp/bindings/lib/connector.h" 10 #include "mojo/public/cpp/bindings/lib/connector.h"
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 93
94 protected: 94 protected:
95 ScopedMessagePipeHandle handle0_; 95 ScopedMessagePipeHandle handle0_;
96 ScopedMessagePipeHandle handle1_; 96 ScopedMessagePipeHandle handle1_;
97 97
98 private: 98 private:
99 base::MessageLoop loop_; 99 base::MessageLoop loop_;
100 }; 100 };
101 101
102 TEST_F(ConnectorTest, Basic) { 102 TEST_F(ConnectorTest, Basic) {
103 internal::Connector connector0(handle0_.Pass()); 103 internal::Connector connector0(handle0_.Pass(),
104 internal::Connector connector1(handle1_.Pass()); 104 internal::Connector::SINGLE_THREADED_SEND);
105 internal::Connector connector1(handle1_.Pass(),
106 internal::Connector::SINGLE_THREADED_SEND);
105 107
106 const char kText[] = "hello world"; 108 const char kText[] = "hello world";
107 109
108 Message message; 110 Message message;
109 AllocMessage(kText, &message); 111 AllocMessage(kText, &message);
110 112
111 connector0.Accept(&message); 113 connector0.Accept(&message);
112 114
113 MessageAccumulator accumulator; 115 MessageAccumulator accumulator;
114 connector1.set_incoming_receiver(&accumulator); 116 connector1.set_incoming_receiver(&accumulator);
115 117
116 PumpMessages(); 118 PumpMessages();
117 119
118 ASSERT_FALSE(accumulator.IsEmpty()); 120 ASSERT_FALSE(accumulator.IsEmpty());
119 121
120 Message message_received; 122 Message message_received;
121 accumulator.Pop(&message_received); 123 accumulator.Pop(&message_received);
122 124
123 EXPECT_EQ( 125 EXPECT_EQ(
124 std::string(kText), 126 std::string(kText),
125 std::string(reinterpret_cast<const char*>(message_received.payload()))); 127 std::string(reinterpret_cast<const char*>(message_received.payload())));
126 } 128 }
127 129
128 TEST_F(ConnectorTest, Basic_Synchronous) { 130 TEST_F(ConnectorTest, Basic_Synchronous) {
129 internal::Connector connector0(handle0_.Pass()); 131 internal::Connector connector0(handle0_.Pass(),
130 internal::Connector connector1(handle1_.Pass()); 132 internal::Connector::SINGLE_THREADED_SEND);
133 internal::Connector connector1(handle1_.Pass(),
134 internal::Connector::SINGLE_THREADED_SEND);
131 135
132 const char kText[] = "hello world"; 136 const char kText[] = "hello world";
133 137
134 Message message; 138 Message message;
135 AllocMessage(kText, &message); 139 AllocMessage(kText, &message);
136 140
137 connector0.Accept(&message); 141 connector0.Accept(&message);
138 142
139 MessageAccumulator accumulator; 143 MessageAccumulator accumulator;
140 connector1.set_incoming_receiver(&accumulator); 144 connector1.set_incoming_receiver(&accumulator);
141 145
142 connector1.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); 146 connector1.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE);
143 147
144 ASSERT_FALSE(accumulator.IsEmpty()); 148 ASSERT_FALSE(accumulator.IsEmpty());
145 149
146 Message message_received; 150 Message message_received;
147 accumulator.Pop(&message_received); 151 accumulator.Pop(&message_received);
148 152
149 EXPECT_EQ( 153 EXPECT_EQ(
150 std::string(kText), 154 std::string(kText),
151 std::string(reinterpret_cast<const char*>(message_received.payload()))); 155 std::string(reinterpret_cast<const char*>(message_received.payload())));
152 } 156 }
153 157
154 TEST_F(ConnectorTest, Basic_EarlyIncomingReceiver) { 158 TEST_F(ConnectorTest, Basic_EarlyIncomingReceiver) {
155 internal::Connector connector0(handle0_.Pass()); 159 internal::Connector connector0(handle0_.Pass(),
156 internal::Connector connector1(handle1_.Pass()); 160 internal::Connector::SINGLE_THREADED_SEND);
161 internal::Connector connector1(handle1_.Pass(),
162 internal::Connector::SINGLE_THREADED_SEND);
157 163
158 MessageAccumulator accumulator; 164 MessageAccumulator accumulator;
159 connector1.set_incoming_receiver(&accumulator); 165 connector1.set_incoming_receiver(&accumulator);
160 166
161 const char kText[] = "hello world"; 167 const char kText[] = "hello world";
162 168
163 Message message; 169 Message message;
164 AllocMessage(kText, &message); 170 AllocMessage(kText, &message);
165 171
166 connector0.Accept(&message); 172 connector0.Accept(&message);
167 173
168 PumpMessages(); 174 PumpMessages();
169 175
170 ASSERT_FALSE(accumulator.IsEmpty()); 176 ASSERT_FALSE(accumulator.IsEmpty());
171 177
172 Message message_received; 178 Message message_received;
173 accumulator.Pop(&message_received); 179 accumulator.Pop(&message_received);
174 180
175 EXPECT_EQ( 181 EXPECT_EQ(
176 std::string(kText), 182 std::string(kText),
177 std::string(reinterpret_cast<const char*>(message_received.payload()))); 183 std::string(reinterpret_cast<const char*>(message_received.payload())));
178 } 184 }
179 185
180 TEST_F(ConnectorTest, Basic_TwoMessages) { 186 TEST_F(ConnectorTest, Basic_TwoMessages) {
181 internal::Connector connector0(handle0_.Pass()); 187 internal::Connector connector0(handle0_.Pass(),
182 internal::Connector connector1(handle1_.Pass()); 188 internal::Connector::SINGLE_THREADED_SEND);
189 internal::Connector connector1(handle1_.Pass(),
190 internal::Connector::SINGLE_THREADED_SEND);
183 191
184 const char* kText[] = {"hello", "world"}; 192 const char* kText[] = {"hello", "world"};
185 193
186 for (size_t i = 0; i < MOJO_ARRAYSIZE(kText); ++i) { 194 for (size_t i = 0; i < MOJO_ARRAYSIZE(kText); ++i) {
187 Message message; 195 Message message;
188 AllocMessage(kText[i], &message); 196 AllocMessage(kText[i], &message);
189 197
190 connector0.Accept(&message); 198 connector0.Accept(&message);
191 } 199 }
192 200
193 MessageAccumulator accumulator; 201 MessageAccumulator accumulator;
194 connector1.set_incoming_receiver(&accumulator); 202 connector1.set_incoming_receiver(&accumulator);
195 203
196 PumpMessages(); 204 PumpMessages();
197 205
198 for (size_t i = 0; i < MOJO_ARRAYSIZE(kText); ++i) { 206 for (size_t i = 0; i < MOJO_ARRAYSIZE(kText); ++i) {
199 ASSERT_FALSE(accumulator.IsEmpty()); 207 ASSERT_FALSE(accumulator.IsEmpty());
200 208
201 Message message_received; 209 Message message_received;
202 accumulator.Pop(&message_received); 210 accumulator.Pop(&message_received);
203 211
204 EXPECT_EQ( 212 EXPECT_EQ(
205 std::string(kText[i]), 213 std::string(kText[i]),
206 std::string(reinterpret_cast<const char*>(message_received.payload()))); 214 std::string(reinterpret_cast<const char*>(message_received.payload())));
207 } 215 }
208 } 216 }
209 217
210 TEST_F(ConnectorTest, Basic_TwoMessages_Synchronous) { 218 TEST_F(ConnectorTest, Basic_TwoMessages_Synchronous) {
211 internal::Connector connector0(handle0_.Pass()); 219 internal::Connector connector0(handle0_.Pass(),
212 internal::Connector connector1(handle1_.Pass()); 220 internal::Connector::SINGLE_THREADED_SEND);
221 internal::Connector connector1(handle1_.Pass(),
222 internal::Connector::SINGLE_THREADED_SEND);
213 223
214 const char* kText[] = {"hello", "world"}; 224 const char* kText[] = {"hello", "world"};
215 225
216 for (size_t i = 0; i < MOJO_ARRAYSIZE(kText); ++i) { 226 for (size_t i = 0; i < MOJO_ARRAYSIZE(kText); ++i) {
217 Message message; 227 Message message;
218 AllocMessage(kText[i], &message); 228 AllocMessage(kText[i], &message);
219 229
220 connector0.Accept(&message); 230 connector0.Accept(&message);
221 } 231 }
222 232
223 MessageAccumulator accumulator; 233 MessageAccumulator accumulator;
224 connector1.set_incoming_receiver(&accumulator); 234 connector1.set_incoming_receiver(&accumulator);
225 235
226 connector1.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); 236 connector1.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE);
227 237
228 ASSERT_FALSE(accumulator.IsEmpty()); 238 ASSERT_FALSE(accumulator.IsEmpty());
229 239
230 Message message_received; 240 Message message_received;
231 accumulator.Pop(&message_received); 241 accumulator.Pop(&message_received);
232 242
233 EXPECT_EQ( 243 EXPECT_EQ(
234 std::string(kText[0]), 244 std::string(kText[0]),
235 std::string(reinterpret_cast<const char*>(message_received.payload()))); 245 std::string(reinterpret_cast<const char*>(message_received.payload())));
236 246
237 ASSERT_TRUE(accumulator.IsEmpty()); 247 ASSERT_TRUE(accumulator.IsEmpty());
238 } 248 }
239 249
240 TEST_F(ConnectorTest, WriteToClosedPipe) { 250 TEST_F(ConnectorTest, WriteToClosedPipe) {
241 internal::Connector connector0(handle0_.Pass()); 251 internal::Connector connector0(handle0_.Pass(),
252 internal::Connector::SINGLE_THREADED_SEND);
242 253
243 const char kText[] = "hello world"; 254 const char kText[] = "hello world";
244 255
245 Message message; 256 Message message;
246 AllocMessage(kText, &message); 257 AllocMessage(kText, &message);
247 258
248 // Close the other end of the pipe. 259 // Close the other end of the pipe.
249 handle1_.reset(); 260 handle1_.reset();
250 261
251 // Not observed yet because we haven't spun the message loop yet. 262 // Not observed yet because we haven't spun the message loop yet.
252 EXPECT_FALSE(connector0.encountered_error()); 263 EXPECT_FALSE(connector0.encountered_error());
253 264
254 // Write failures are not reported. 265 // Write failures are not reported.
255 bool ok = connector0.Accept(&message); 266 bool ok = connector0.Accept(&message);
256 EXPECT_TRUE(ok); 267 EXPECT_TRUE(ok);
257 268
258 // Still not observed. 269 // Still not observed.
259 EXPECT_FALSE(connector0.encountered_error()); 270 EXPECT_FALSE(connector0.encountered_error());
260 271
261 // Spin the message loop, and then we should start observing the closed pipe. 272 // Spin the message loop, and then we should start observing the closed pipe.
262 PumpMessages(); 273 PumpMessages();
263 274
264 EXPECT_TRUE(connector0.encountered_error()); 275 EXPECT_TRUE(connector0.encountered_error());
265 } 276 }
266 277
267 TEST_F(ConnectorTest, MessageWithHandles) { 278 TEST_F(ConnectorTest, MessageWithHandles) {
268 internal::Connector connector0(handle0_.Pass()); 279 internal::Connector connector0(handle0_.Pass(),
269 internal::Connector connector1(handle1_.Pass()); 280 internal::Connector::SINGLE_THREADED_SEND);
281 internal::Connector connector1(handle1_.Pass(),
282 internal::Connector::SINGLE_THREADED_SEND);
270 283
271 const char kText[] = "hello world"; 284 const char kText[] = "hello world";
272 285
273 Message message1; 286 Message message1;
274 AllocMessage(kText, &message1); 287 AllocMessage(kText, &message1);
275 288
276 MessagePipe pipe; 289 MessagePipe pipe;
277 message1.mutable_handles()->push_back(pipe.handle0.release()); 290 message1.mutable_handles()->push_back(pipe.handle0.release());
278 291
279 connector0.Accept(&message1); 292 connector0.Accept(&message1);
(...skipping 17 matching lines...) Expand all
297 ASSERT_EQ(1U, message_received.handles()->size()); 310 ASSERT_EQ(1U, message_received.handles()->size());
298 311
299 // Now send a message to the transferred handle and confirm it's sent through 312 // Now send a message to the transferred handle and confirm it's sent through
300 // to the orginal pipe. 313 // to the orginal pipe.
301 // TODO(vtl): Do we need a better way of "downcasting" the handle types? 314 // TODO(vtl): Do we need a better way of "downcasting" the handle types?
302 ScopedMessagePipeHandle smph; 315 ScopedMessagePipeHandle smph;
303 smph.reset(MessagePipeHandle(message_received.handles()->front().value())); 316 smph.reset(MessagePipeHandle(message_received.handles()->front().value()));
304 message_received.mutable_handles()->front() = Handle(); 317 message_received.mutable_handles()->front() = Handle();
305 // |smph| now owns this handle. 318 // |smph| now owns this handle.
306 319
307 internal::Connector connector_received(smph.Pass()); 320 internal::Connector connector_received(
308 internal::Connector connector_original(pipe.handle1.Pass()); 321 smph.Pass(), internal::Connector::SINGLE_THREADED_SEND);
322 internal::Connector connector_original(
323 pipe.handle1.Pass(), internal::Connector::SINGLE_THREADED_SEND);
309 324
310 Message message2; 325 Message message2;
311 AllocMessage(kText, &message2); 326 AllocMessage(kText, &message2);
312 327
313 connector_received.Accept(&message2); 328 connector_received.Accept(&message2);
314 connector_original.set_incoming_receiver(&accumulator); 329 connector_original.set_incoming_receiver(&accumulator);
315 PumpMessages(); 330 PumpMessages();
316 331
317 ASSERT_FALSE(accumulator.IsEmpty()); 332 ASSERT_FALSE(accumulator.IsEmpty());
318 333
319 accumulator.Pop(&message_received); 334 accumulator.Pop(&message_received);
320 335
321 EXPECT_EQ( 336 EXPECT_EQ(
322 std::string(kText), 337 std::string(kText),
323 std::string(reinterpret_cast<const char*>(message_received.payload()))); 338 std::string(reinterpret_cast<const char*>(message_received.payload())));
324 } 339 }
325 340
326 TEST_F(ConnectorTest, WaitForIncomingMessageWithError) { 341 TEST_F(ConnectorTest, WaitForIncomingMessageWithError) {
327 internal::Connector connector0(handle0_.Pass()); 342 internal::Connector connector0(handle0_.Pass(),
343 internal::Connector::SINGLE_THREADED_SEND);
328 // Close the other end of the pipe. 344 // Close the other end of the pipe.
329 handle1_.reset(); 345 handle1_.reset();
330 ASSERT_FALSE(connector0.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE)); 346 ASSERT_FALSE(connector0.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE));
331 } 347 }
332 348
333 TEST_F(ConnectorTest, WaitForIncomingMessageWithDeletion) { 349 TEST_F(ConnectorTest, WaitForIncomingMessageWithDeletion) {
334 internal::Connector connector0(handle0_.Pass()); 350 internal::Connector connector0(handle0_.Pass(),
335 internal::Connector* connector1 = new internal::Connector(handle1_.Pass()); 351 internal::Connector::SINGLE_THREADED_SEND);
352 internal::Connector* connector1 = new internal::Connector(
353 handle1_.Pass(), internal::Connector::SINGLE_THREADED_SEND);
336 354
337 const char kText[] = "hello world"; 355 const char kText[] = "hello world";
338 356
339 Message message; 357 Message message;
340 AllocMessage(kText, &message); 358 AllocMessage(kText, &message);
341 359
342 connector0.Accept(&message); 360 connector0.Accept(&message);
343 361
344 ConnectorDeletingMessageAccumulator accumulator(&connector1); 362 ConnectorDeletingMessageAccumulator accumulator(&connector1);
345 connector1->set_incoming_receiver(&accumulator); 363 connector1->set_incoming_receiver(&accumulator);
346 364
347 connector1->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); 365 connector1->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE);
348 366
349 ASSERT_FALSE(connector1); 367 ASSERT_FALSE(connector1);
350 ASSERT_FALSE(accumulator.IsEmpty()); 368 ASSERT_FALSE(accumulator.IsEmpty());
351 369
352 Message message_received; 370 Message message_received;
353 accumulator.Pop(&message_received); 371 accumulator.Pop(&message_received);
354 372
355 EXPECT_EQ( 373 EXPECT_EQ(
356 std::string(kText), 374 std::string(kText),
357 std::string(reinterpret_cast<const char*>(message_received.payload()))); 375 std::string(reinterpret_cast<const char*>(message_received.payload())));
358 } 376 }
359 377
360 TEST_F(ConnectorTest, WaitForIncomingMessageWithReentrancy) { 378 TEST_F(ConnectorTest, WaitForIncomingMessageWithReentrancy) {
361 internal::Connector connector0(handle0_.Pass()); 379 internal::Connector connector0(handle0_.Pass(),
362 internal::Connector connector1(handle1_.Pass()); 380 internal::Connector::SINGLE_THREADED_SEND);
381 internal::Connector connector1(handle1_.Pass(),
382 internal::Connector::SINGLE_THREADED_SEND);
363 383
364 const char* kText[] = {"hello", "world"}; 384 const char* kText[] = {"hello", "world"};
365 385
366 for (size_t i = 0; i < MOJO_ARRAYSIZE(kText); ++i) { 386 for (size_t i = 0; i < MOJO_ARRAYSIZE(kText); ++i) {
367 Message message; 387 Message message;
368 AllocMessage(kText[i], &message); 388 AllocMessage(kText[i], &message);
369 389
370 connector0.Accept(&message); 390 connector0.Accept(&message);
371 } 391 }
372 392
(...skipping 10 matching lines...) Expand all
383 403
384 EXPECT_EQ( 404 EXPECT_EQ(
385 std::string(kText[i]), 405 std::string(kText[i]),
386 std::string(reinterpret_cast<const char*>(message_received.payload()))); 406 std::string(reinterpret_cast<const char*>(message_received.payload())));
387 } 407 }
388 408
389 ASSERT_EQ(2, accumulator.number_of_calls()); 409 ASSERT_EQ(2, accumulator.number_of_calls());
390 } 410 }
391 411
392 TEST_F(ConnectorTest, RaiseError) { 412 TEST_F(ConnectorTest, RaiseError) {
393 internal::Connector connector0(handle0_.Pass()); 413 internal::Connector connector0(handle0_.Pass(),
414 internal::Connector::SINGLE_THREADED_SEND);
394 bool error_handler_called0 = false; 415 bool error_handler_called0 = false;
395 connector0.set_connection_error_handler( 416 connector0.set_connection_error_handler(
396 [&error_handler_called0]() { error_handler_called0 = true; }); 417 [&error_handler_called0]() { error_handler_called0 = true; });
397 418
398 internal::Connector connector1(handle1_.Pass()); 419 internal::Connector connector1(handle1_.Pass(),
420 internal::Connector::SINGLE_THREADED_SEND);
399 bool error_handler_called1 = false; 421 bool error_handler_called1 = false;
400 connector1.set_connection_error_handler( 422 connector1.set_connection_error_handler(
401 [&error_handler_called1]() { error_handler_called1 = true; }); 423 [&error_handler_called1]() { error_handler_called1 = true; });
402 424
403 const char kText[] = "hello world"; 425 const char kText[] = "hello world";
404 426
405 Message message; 427 Message message;
406 AllocMessage(kText, &message); 428 AllocMessage(kText, &message);
407 429
408 connector0.Accept(&message); 430 connector0.Accept(&message);
(...skipping 25 matching lines...) Expand all
434 EXPECT_TRUE(connector1.encountered_error()); 456 EXPECT_TRUE(connector1.encountered_error());
435 457
436 // The message pipe handle is valid at both sides. 458 // The message pipe handle is valid at both sides.
437 EXPECT_TRUE(connector0.is_valid()); 459 EXPECT_TRUE(connector0.is_valid());
438 EXPECT_TRUE(connector1.is_valid()); 460 EXPECT_TRUE(connector1.is_valid());
439 } 461 }
440 462
441 } // namespace 463 } // namespace
442 } // namespace test 464 } // namespace test
443 } // namespace mojo 465 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/BUILD.gn ('k') | mojo/public/cpp/bindings/tests/multiplex_router_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698