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

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

Issue 2770153003: mojo: Support sync calls through ThreadSafeInterfacePtr (Closed)
Patch Set: Created 3 years, 8 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 <utility> 5 #include <utility>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); 342 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
343 343
344 base::RunLoop run_loop; 344 base::RunLoop run_loop;
345 ptr->Echo(123, base::Bind(&ExpectValueAndRunClosure, 123, 345 ptr->Echo(123, base::Bind(&ExpectValueAndRunClosure, 123,
346 run_loop.QuitClosure())); 346 run_loop.QuitClosure()));
347 run_loop.Run(); 347 run_loop.Run();
348 } 348 }
349 349
350 TYPED_TEST(SyncMethodCommonTest, BasicSyncCalls) { 350 TYPED_TEST(SyncMethodCommonTest, BasicSyncCalls) {
351 InterfacePtr<TypeParam> ptr; 351 InterfacePtr<TypeParam> ptr;
352 InterfaceRequest<TypeParam> request(&ptr);
353 TestSyncServiceThread<TypeParam> service_thread;
354 scoped_refptr<ThreadSafeInterfacePtr<TypeParam>> tsip =
355 ThreadSafeInterfacePtr<TypeParam>::Create(
356 ptr.PassInterface(), service_thread.thread()->task_runner());
352 357
353 TestSyncServiceThread<TypeParam> service_thread;
354 service_thread.thread()->task_runner()->PostTask( 358 service_thread.thread()->task_runner()->PostTask(
355 FROM_HERE, base::Bind(&TestSyncServiceThread<TypeParam>::SetUp, 359 FROM_HERE,
356 base::Unretained(&service_thread), 360 base::Bind(&TestSyncServiceThread<TypeParam>::SetUp,
357 base::Passed(MakeRequest(&ptr)))); 361 base::Unretained(&service_thread), base::Passed(&request)));
358 ASSERT_TRUE(ptr->Ping()); 362 ASSERT_TRUE((*tsip)->Ping());
359 ASSERT_TRUE(service_thread.ping_called()); 363 ASSERT_TRUE(service_thread.ping_called());
360 364
361 int32_t output_value = -1; 365 int32_t output_value = -1;
362 ASSERT_TRUE(ptr->Echo(42, &output_value)); 366 ASSERT_TRUE((*tsip)->Echo(42, &output_value));
363 ASSERT_EQ(42, output_value); 367 ASSERT_EQ(42, output_value);
364 368
365 base::RunLoop run_loop; 369 base::RunLoop run_loop;
366 service_thread.thread()->task_runner()->PostTaskAndReply( 370 service_thread.thread()->task_runner()->PostTaskAndReply(
367 FROM_HERE, base::Bind(&TestSyncServiceThread<TypeParam>::TearDown, 371 FROM_HERE, base::Bind(&TestSyncServiceThread<TypeParam>::TearDown,
368 base::Unretained(&service_thread)), 372 base::Unretained(&service_thread)),
369 run_loop.QuitClosure()); 373 run_loop.QuitClosure());
370 run_loop.Run(); 374 run_loop.Run();
371 } 375 }
372 376
(...skipping 15 matching lines...) Expand all
388 392
389 InterfacePtr<TypeParam> ptr; 393 InterfacePtr<TypeParam> ptr;
390 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); 394 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
391 impl.set_ping_handler([&ptr](const TestSync::PingCallback& callback) { 395 impl.set_ping_handler([&ptr](const TestSync::PingCallback& callback) {
392 ptr.reset(); 396 ptr.reset();
393 callback.Run(); 397 callback.Run();
394 }); 398 });
395 ASSERT_FALSE(ptr->Ping()); 399 ASSERT_FALSE(ptr->Ping());
396 } 400 }
397 401
402 TYPED_TEST(SyncMethodCommonTest, TSIPInterfacePtrDestroyedDuringSyncCall) {
watk 2017/03/24 02:40:52 I just added these for local testing. I'm thinki
403 // Test that it won't result in crash or hang if an interface pointer is
404 // destroyed while it is waiting for a sync call response.
405
406 InterfacePtr<TypeParam> ptr;
407 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
408 scoped_refptr<ThreadSafeInterfacePtr<TypeParam>> thread_safe_ptr =
409 ThreadSafeInterfacePtr<TypeParam>::Create(std::move(ptr));
410 // The binding lives on the same thread as the interface pointer.
411 impl.set_ping_handler(
412 [&thread_safe_ptr](const TestSync::PingCallback& callback) {
413 thread_safe_ptr = nullptr;
414 callback.Run();
415 });
416 ASSERT_FALSE((*thread_safe_ptr)->Ping());
417 }
418
398 TYPED_TEST(SyncMethodCommonTest, BindingDestroyedDuringSyncCall) { 419 TYPED_TEST(SyncMethodCommonTest, BindingDestroyedDuringSyncCall) {
399 // Test that it won't result in crash or hang if a binding is 420 // Test that it won't result in crash or hang if a binding is
400 // closed (and therefore the message pipe handle is closed) while the 421 // closed (and therefore the message pipe handle is closed) while the
401 // corresponding interface pointer is waiting for a sync call response. 422 // corresponding interface pointer is waiting for a sync call response.
402 423
403 InterfacePtr<TypeParam> ptr; 424 InterfacePtr<TypeParam> ptr;
404 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); 425 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
405 impl.set_ping_handler([&impl](const TestSync::PingCallback& callback) { 426 impl.set_ping_handler([&impl](const TestSync::PingCallback& callback) {
406 impl.binding()->Close(); 427 impl.binding()->Close();
407 callback.Run(); 428 callback.Run();
(...skipping 20 matching lines...) Expand all
428 ASSERT_TRUE(ptr->Echo(456, &result_value)); 449 ASSERT_TRUE(ptr->Echo(456, &result_value));
429 EXPECT_EQ(456, result_value); 450 EXPECT_EQ(456, result_value);
430 } 451 }
431 callback.Run(value); 452 callback.Run(value);
432 }); 453 });
433 454
434 ASSERT_TRUE(ptr->Echo(123, &result_value)); 455 ASSERT_TRUE(ptr->Echo(123, &result_value));
435 EXPECT_EQ(123, result_value); 456 EXPECT_EQ(123, result_value);
436 } 457 }
437 458
459 TYPED_TEST(SyncMethodCommonTest, TSIPNestedSyncCallsWithInOrderResponses) {
460 // Test that we can call a sync method on an interface ptr, while there is
461 // already a sync call ongoing. The responses arrive in order.
462
463 InterfacePtr<TypeParam> ptr;
464 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
465 scoped_refptr<ThreadSafeInterfacePtr<TypeParam>> tsip =
466 ThreadSafeInterfacePtr<TypeParam>::Create(std::move(ptr));
467
468 // The same variable is used to store the output of the two sync calls, in
469 // order to test that responses are handled in the correct order.
470 int32_t result_value = -1;
471
472 bool first_call = true;
473 impl.set_echo_handler(
474 [&first_call, &tsip, &result_value](
475 int32_t value, const TestSync::EchoCallback& callback) {
476 if (first_call) {
477 first_call = false;
478 ASSERT_TRUE((*tsip)->Echo(456, &result_value));
479 EXPECT_EQ(456, result_value);
480 }
481 callback.Run(value);
482 });
483
484 ASSERT_TRUE((*tsip)->Echo(123, &result_value));
485 EXPECT_EQ(123, result_value);
486 }
487
438 TYPED_TEST(SyncMethodCommonTest, NestedSyncCallsWithOutOfOrderResponses) { 488 TYPED_TEST(SyncMethodCommonTest, NestedSyncCallsWithOutOfOrderResponses) {
439 // Test that we can call a sync method on an interface ptr, while there is 489 // Test that we can call a sync method on an interface ptr, while there is
440 // already a sync call ongoing. The responses arrive out of order. 490 // already a sync call ongoing. The responses arrive out of order.
441 491
442 InterfacePtr<TypeParam> ptr; 492 InterfacePtr<TypeParam> ptr;
443 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); 493 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
444 494
445 // The same variable is used to store the output of the two sync calls, in 495 // The same variable is used to store the output of the two sync calls, in
446 // order to test that responses are handled in the correct order. 496 // order to test that responses are handled in the correct order.
447 int32_t result_value = -1; 497 int32_t result_value = -1;
448 498
449 bool first_call = true; 499 bool first_call = true;
450 impl.set_echo_handler([&first_call, &ptr, &result_value]( 500 impl.set_echo_handler([&first_call, &ptr, &result_value](
451 int32_t value, const TestSync::EchoCallback& callback) { 501 int32_t value, const TestSync::EchoCallback& callback) {
452 callback.Run(value); 502 callback.Run(value);
453 if (first_call) { 503 if (first_call) {
454 first_call = false; 504 first_call = false;
455 ASSERT_TRUE(ptr->Echo(456, &result_value)); 505 ASSERT_TRUE(ptr->Echo(456, &result_value));
456 EXPECT_EQ(456, result_value); 506 EXPECT_EQ(456, result_value);
457 } 507 }
458 }); 508 });
459 509
460 ASSERT_TRUE(ptr->Echo(123, &result_value)); 510 ASSERT_TRUE(ptr->Echo(123, &result_value));
461 EXPECT_EQ(123, result_value); 511 EXPECT_EQ(123, result_value);
462 } 512 }
463 513
514 TYPED_TEST(SyncMethodCommonTest, TSIPNestedSyncCallsWithOutOfOrderResponses) {
515 // Test that we can call a sync method on an interface ptr, while there is
516 // already a sync call ongoing. The responses arrive out of order.
517
518 InterfacePtr<TypeParam> ptr;
519 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
520 scoped_refptr<ThreadSafeInterfacePtr<TypeParam>> tsip =
521 ThreadSafeInterfacePtr<TypeParam>::Create(std::move(ptr));
522
523 // The same variable is used to store the output of the two sync calls, in
524 // order to test that responses are handled in the correct order.
525 int32_t result_value = -1;
526
527 bool first_call = true;
528 impl.set_echo_handler(
529 [&first_call, &tsip, &result_value](
530 int32_t value, const TestSync::EchoCallback& callback) {
531 callback.Run(value);
532 if (first_call) {
533 first_call = false;
534 ASSERT_TRUE((*tsip)->Echo(456, &result_value));
535 EXPECT_EQ(456, result_value);
536 }
537 });
538
539 ASSERT_TRUE((*tsip)->Echo(123, &result_value));
540 EXPECT_EQ(123, result_value);
541 }
542
464 TYPED_TEST(SyncMethodCommonTest, AsyncResponseQueuedDuringSyncCall) { 543 TYPED_TEST(SyncMethodCommonTest, AsyncResponseQueuedDuringSyncCall) {
465 // Test that while an interface pointer is waiting for the response to a sync 544 // Test that while an interface pointer is waiting for the response to a sync
466 // call, async responses are queued until the sync call completes. 545 // call, async responses are queued until the sync call completes.
467 546
468 InterfacePtr<TypeParam> ptr; 547 InterfacePtr<TypeParam> ptr;
469 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); 548 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
470 549
471 int32_t async_echo_request_value = -1; 550 int32_t async_echo_request_value = -1;
472 TestSync::AsyncEchoCallback async_echo_request_callback; 551 TestSync::AsyncEchoCallback async_echo_request_callback;
473 base::RunLoop run_loop1; 552 base::RunLoop run_loop1;
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
730 int32_t result_value = -1; 809 int32_t result_value = -1;
731 ASSERT_TRUE(master_ptr_->Echo(456, &result_value)); 810 ASSERT_TRUE(master_ptr_->Echo(456, &result_value));
732 EXPECT_EQ(456, result_value); 811 EXPECT_EQ(456, result_value);
733 } 812 }
734 813
735 // TODO(yzshen): Add more tests related to associated interfaces. 814 // TODO(yzshen): Add more tests related to associated interfaces.
736 815
737 } // namespace 816 } // namespace
738 } // namespace test 817 } // namespace test
739 } // namespace mojo 818 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc ('k') | mojo/public/cpp/bindings/thread_safe_interface_ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698