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

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

Issue 2770153003: mojo: Support sync calls through ThreadSafeInterfacePtr (Closed)
Patch Set: Fix more tests 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) {
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>> tsip =
409 ThreadSafeInterfacePtr<TypeParam>::Create(std::move(ptr));
410 // The binding lives on the same thread as the interface pointer.
411 impl.set_ping_handler([&tsip](const TestSync::PingCallback& callback) {
412 tsip = nullptr;
413 callback.Run();
414 });
415 ASSERT_FALSE((*tsip)->Ping());
416 }
417
398 TYPED_TEST(SyncMethodCommonTest, BindingDestroyedDuringSyncCall) { 418 TYPED_TEST(SyncMethodCommonTest, BindingDestroyedDuringSyncCall) {
399 // Test that it won't result in crash or hang if a binding is 419 // 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 420 // closed (and therefore the message pipe handle is closed) while the
401 // corresponding interface pointer is waiting for a sync call response. 421 // corresponding interface pointer is waiting for a sync call response.
402 422
403 InterfacePtr<TypeParam> ptr; 423 InterfacePtr<TypeParam> ptr;
404 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); 424 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
405 impl.set_ping_handler([&impl](const TestSync::PingCallback& callback) { 425 impl.set_ping_handler([&impl](const TestSync::PingCallback& callback) {
406 impl.binding()->Close(); 426 impl.binding()->Close();
407 callback.Run(); 427 callback.Run();
408 }); 428 });
409 ASSERT_FALSE(ptr->Ping()); 429 ASSERT_FALSE(ptr->Ping());
410 } 430 }
411 431
432 TYPED_TEST(SyncMethodCommonTest, TSIPBindingDestroyedDuringSyncCall) {
433 // Test that it won't result in crash or hang if a binding is
434 // closed (and therefore the message pipe handle is closed) while the
435 // corresponding interface pointer is waiting for a sync call response.
436
437 InterfacePtr<TypeParam> ptr;
438 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
439 scoped_refptr<ThreadSafeInterfacePtr<TypeParam>> tsip =
440 ThreadSafeInterfacePtr<TypeParam>::Create(std::move(ptr));
441 impl.set_ping_handler([&impl](const TestSync::PingCallback& callback) {
442 impl.binding()->Close();
443 callback.Run();
444 });
445 ASSERT_FALSE((*tsip)->Ping());
446 }
447
412 TYPED_TEST(SyncMethodCommonTest, NestedSyncCallsWithInOrderResponses) { 448 TYPED_TEST(SyncMethodCommonTest, NestedSyncCallsWithInOrderResponses) {
413 // Test that we can call a sync method on an interface ptr, while there is 449 // Test that we can call a sync method on an interface ptr, while there is
414 // already a sync call ongoing. The responses arrive in order. 450 // already a sync call ongoing. The responses arrive in order.
415 451
416 InterfacePtr<TypeParam> ptr; 452 InterfacePtr<TypeParam> ptr;
417 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); 453 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
418 454
419 // The same variable is used to store the output of the two sync calls, in 455 // The same variable is used to store the output of the two sync calls, in
420 // order to test that responses are handled in the correct order. 456 // order to test that responses are handled in the correct order.
421 int32_t result_value = -1; 457 int32_t result_value = -1;
422 458
423 bool first_call = true; 459 bool first_call = true;
424 impl.set_echo_handler([&first_call, &ptr, &result_value]( 460 impl.set_echo_handler([&first_call, &ptr, &result_value](
425 int32_t value, const TestSync::EchoCallback& callback) { 461 int32_t value, const TestSync::EchoCallback& callback) {
426 if (first_call) { 462 if (first_call) {
427 first_call = false; 463 first_call = false;
428 ASSERT_TRUE(ptr->Echo(456, &result_value)); 464 ASSERT_TRUE(ptr->Echo(456, &result_value));
429 EXPECT_EQ(456, result_value); 465 EXPECT_EQ(456, result_value);
430 } 466 }
431 callback.Run(value); 467 callback.Run(value);
432 }); 468 });
433 469
434 ASSERT_TRUE(ptr->Echo(123, &result_value)); 470 ASSERT_TRUE(ptr->Echo(123, &result_value));
435 EXPECT_EQ(123, result_value); 471 EXPECT_EQ(123, result_value);
436 } 472 }
437 473
474 TYPED_TEST(SyncMethodCommonTest, TSIPNestedSyncCallsWithInOrderResponses) {
475 // Test that we can call a sync method on an interface ptr, while there is
476 // already a sync call ongoing. The responses arrive in order.
477
478 InterfacePtr<TypeParam> ptr;
479 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
480 scoped_refptr<ThreadSafeInterfacePtr<TypeParam>> tsip =
481 ThreadSafeInterfacePtr<TypeParam>::Create(std::move(ptr));
482
483 // The same variable is used to store the output of the two sync calls, in
484 // order to test that responses are handled in the correct order.
485 int32_t result_value = -1;
486
487 bool first_call = true;
488 impl.set_echo_handler(
489 [&first_call, &tsip, &result_value](
490 int32_t value, const TestSync::EchoCallback& callback) {
491 if (first_call) {
492 first_call = false;
493 ASSERT_TRUE((*tsip)->Echo(456, &result_value));
494 EXPECT_EQ(456, result_value);
495 }
496 callback.Run(value);
497 });
498
499 ASSERT_TRUE((*tsip)->Echo(123, &result_value));
500 EXPECT_EQ(123, result_value);
501 }
502
438 TYPED_TEST(SyncMethodCommonTest, NestedSyncCallsWithOutOfOrderResponses) { 503 TYPED_TEST(SyncMethodCommonTest, NestedSyncCallsWithOutOfOrderResponses) {
439 // Test that we can call a sync method on an interface ptr, while there is 504 // 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. 505 // already a sync call ongoing. The responses arrive out of order.
441 506
442 InterfacePtr<TypeParam> ptr; 507 InterfacePtr<TypeParam> ptr;
443 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); 508 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
444 509
445 // The same variable is used to store the output of the two sync calls, in 510 // 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. 511 // order to test that responses are handled in the correct order.
447 int32_t result_value = -1; 512 int32_t result_value = -1;
448 513
449 bool first_call = true; 514 bool first_call = true;
450 impl.set_echo_handler([&first_call, &ptr, &result_value]( 515 impl.set_echo_handler([&first_call, &ptr, &result_value](
451 int32_t value, const TestSync::EchoCallback& callback) { 516 int32_t value, const TestSync::EchoCallback& callback) {
452 callback.Run(value); 517 callback.Run(value);
453 if (first_call) { 518 if (first_call) {
454 first_call = false; 519 first_call = false;
455 ASSERT_TRUE(ptr->Echo(456, &result_value)); 520 ASSERT_TRUE(ptr->Echo(456, &result_value));
456 EXPECT_EQ(456, result_value); 521 EXPECT_EQ(456, result_value);
457 } 522 }
458 }); 523 });
459 524
460 ASSERT_TRUE(ptr->Echo(123, &result_value)); 525 ASSERT_TRUE(ptr->Echo(123, &result_value));
461 EXPECT_EQ(123, result_value); 526 EXPECT_EQ(123, result_value);
462 } 527 }
463 528
529 TYPED_TEST(SyncMethodCommonTest, TSIPNestedSyncCallsWithOutOfOrderResponses) {
530 // Test that we can call a sync method on an interface ptr, while there is
531 // already a sync call ongoing. The responses arrive out of order.
532
533 InterfacePtr<TypeParam> ptr;
534 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
535 scoped_refptr<ThreadSafeInterfacePtr<TypeParam>> tsip =
536 ThreadSafeInterfacePtr<TypeParam>::Create(std::move(ptr));
537
538 // The same variable is used to store the output of the two sync calls, in
539 // order to test that responses are handled in the correct order.
540 int32_t result_value = -1;
541
542 bool first_call = true;
543 impl.set_echo_handler(
544 [&first_call, &tsip, &result_value](
545 int32_t value, const TestSync::EchoCallback& callback) {
546 callback.Run(value);
547 if (first_call) {
548 first_call = false;
549 ASSERT_TRUE((*tsip)->Echo(456, &result_value));
550 EXPECT_EQ(456, result_value);
551 }
552 });
553
554 ASSERT_TRUE((*tsip)->Echo(123, &result_value));
555 EXPECT_EQ(123, result_value);
556 }
557
464 TYPED_TEST(SyncMethodCommonTest, AsyncResponseQueuedDuringSyncCall) { 558 TYPED_TEST(SyncMethodCommonTest, AsyncResponseQueuedDuringSyncCall) {
465 // Test that while an interface pointer is waiting for the response to a sync 559 // 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. 560 // call, async responses are queued until the sync call completes.
467 561
468 InterfacePtr<TypeParam> ptr; 562 InterfacePtr<TypeParam> ptr;
469 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); 563 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
470 564
471 int32_t async_echo_request_value = -1; 565 int32_t async_echo_request_value = -1;
472 TestSync::AsyncEchoCallback async_echo_request_callback; 566 TestSync::AsyncEchoCallback async_echo_request_callback;
473 base::RunLoop run_loop1; 567 base::RunLoop run_loop1;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 // Although the AsyncEcho response arrives before the Echo response, it should 603 // Although the AsyncEcho response arrives before the Echo response, it should
510 // be queued and not yet dispatched. 604 // be queued and not yet dispatched.
511 EXPECT_FALSE(async_echo_response_dispatched); 605 EXPECT_FALSE(async_echo_response_dispatched);
512 606
513 // Run until the AsyncEcho response is dispatched. 607 // Run until the AsyncEcho response is dispatched.
514 run_loop2.Run(); 608 run_loop2.Run();
515 609
516 EXPECT_TRUE(async_echo_response_dispatched); 610 EXPECT_TRUE(async_echo_response_dispatched);
517 } 611 }
518 612
613 TYPED_TEST(SyncMethodCommonTest, TSIPAsyncResponseQueuedDuringSyncCall) {
614 // Test that while an interface pointer is waiting for the response to a sync
615 // call, async responses are queued until the sync call completes.
616
617 InterfacePtr<TypeParam> ptr;
618 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
619 scoped_refptr<ThreadSafeInterfacePtr<TypeParam>> tsip =
620 ThreadSafeInterfacePtr<TypeParam>::Create(std::move(ptr));
621
622 int32_t async_echo_request_value = -1;
623 TestSync::AsyncEchoCallback async_echo_request_callback;
624 base::RunLoop run_loop1;
625 impl.set_async_echo_handler(
626 [&async_echo_request_value, &async_echo_request_callback, &run_loop1](
627 int32_t value, const TestSync::AsyncEchoCallback& callback) {
628 async_echo_request_value = value;
629 async_echo_request_callback = callback;
630 run_loop1.Quit();
631 });
632
633 bool async_echo_response_dispatched = false;
634 base::RunLoop run_loop2;
635 (*tsip)->AsyncEcho(
636 123, BindAsyncEchoCallback(
637 [&async_echo_response_dispatched, &run_loop2](int32_t result) {
638 async_echo_response_dispatched = true;
639 EXPECT_EQ(123, result);
640 run_loop2.Quit();
641 }));
642 // Run until the AsyncEcho request reaches the service side.
643 run_loop1.Run();
644
645 impl.set_echo_handler(
646 [&async_echo_request_value, &async_echo_request_callback](
647 int32_t value, const TestSync::EchoCallback& callback) {
648 // Send back the async response first.
649 EXPECT_FALSE(async_echo_request_callback.is_null());
650 async_echo_request_callback.Run(async_echo_request_value);
651
652 callback.Run(value);
653 });
654
655 int32_t result_value = -1;
656 ASSERT_TRUE((*tsip)->Echo(456, &result_value));
657 EXPECT_EQ(456, result_value);
658
659 // Although the AsyncEcho response arrives before the Echo response, it should
660 // be queued and not yet dispatched.
661 EXPECT_FALSE(async_echo_response_dispatched);
662
663 // Run until the AsyncEcho response is dispatched.
664 run_loop2.Run();
665
666 EXPECT_TRUE(async_echo_response_dispatched);
667 }
668
519 TYPED_TEST(SyncMethodCommonTest, AsyncRequestQueuedDuringSyncCall) { 669 TYPED_TEST(SyncMethodCommonTest, AsyncRequestQueuedDuringSyncCall) {
520 // Test that while an interface pointer is waiting for the response to a sync 670 // Test that while an interface pointer is waiting for the response to a sync
521 // call, async requests for a binding running on the same thread are queued 671 // call, async requests for a binding running on the same thread are queued
522 // until the sync call completes. 672 // until the sync call completes.
523 673
524 InterfacePtr<TypeParam> ptr; 674 InterfacePtr<TypeParam> ptr;
525 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); 675 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
526 676
527 bool async_echo_request_dispatched = false; 677 bool async_echo_request_dispatched = false;
528 impl.set_async_echo_handler([&async_echo_request_dispatched]( 678 impl.set_async_echo_handler([&async_echo_request_dispatched](
(...skipping 29 matching lines...) Expand all
558 // Although the AsyncEcho request is sent before the Echo request, it 708 // Although the AsyncEcho request is sent before the Echo request, it
559 // shouldn't be dispatched yet. 709 // shouldn't be dispatched yet.
560 EXPECT_FALSE(async_echo_request_dispatched); 710 EXPECT_FALSE(async_echo_request_dispatched);
561 711
562 // Run until the AsyncEcho response is dispatched. 712 // Run until the AsyncEcho response is dispatched.
563 run_loop.Run(); 713 run_loop.Run();
564 714
565 EXPECT_TRUE(async_echo_response_dispatched); 715 EXPECT_TRUE(async_echo_response_dispatched);
566 } 716 }
567 717
718 TYPED_TEST(SyncMethodCommonTest, TSIPAsyncRequestQueuedDuringSyncCall) {
719 // Test that while an interface pointer is waiting for the response to a sync
720 // call, async requests for a binding running on the same thread are queued
721 // until the sync call completes.
722
723 InterfacePtr<TypeParam> ptr;
724 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
725 scoped_refptr<ThreadSafeInterfacePtr<TypeParam>> tsip =
726 ThreadSafeInterfacePtr<TypeParam>::Create(std::move(ptr));
727
728 bool async_echo_request_dispatched = false;
729 impl.set_async_echo_handler(
730 [&async_echo_request_dispatched](
731 int32_t value, const TestSync::AsyncEchoCallback& callback) {
732 async_echo_request_dispatched = true;
733 callback.Run(value);
734 });
735
736 bool async_echo_response_dispatched = false;
737 base::RunLoop run_loop;
738 (*tsip)->AsyncEcho(
739 123, BindAsyncEchoCallback(
740 [&async_echo_response_dispatched, &run_loop](int32_t result) {
741 async_echo_response_dispatched = true;
742 EXPECT_EQ(123, result);
743 run_loop.Quit();
744 }));
745
746 impl.set_echo_handler(
747 [&async_echo_request_dispatched](int32_t value,
748 const TestSync::EchoCallback& callback) {
749 // Although the AsyncEcho request is sent before the Echo request, it
750 // shouldn't be dispatched yet at this point, because there is an
751 // ongoing sync call on the same thread.
752 EXPECT_FALSE(async_echo_request_dispatched);
753 callback.Run(value);
754 });
755
756 int32_t result_value = -1;
757 ASSERT_TRUE((*tsip)->Echo(456, &result_value));
758 EXPECT_EQ(456, result_value);
759
760 // Although the AsyncEcho request is sent before the Echo request, it
761 // shouldn't be dispatched yet.
762 EXPECT_FALSE(async_echo_request_dispatched);
763
764 // Run until the AsyncEcho response is dispatched.
765 run_loop.Run();
766
767 EXPECT_TRUE(async_echo_response_dispatched);
768 }
769
568 TYPED_TEST(SyncMethodCommonTest, 770 TYPED_TEST(SyncMethodCommonTest,
569 QueuedMessagesProcessedBeforeErrorNotification) { 771 QueuedMessagesProcessedBeforeErrorNotification) {
570 // Test that while an interface pointer is waiting for the response to a sync 772 // Test that while an interface pointer is waiting for the response to a sync
571 // call, async responses are queued. If the message pipe is disconnected 773 // call, async responses are queued. If the message pipe is disconnected
572 // before the queued messages are processed, the connection error 774 // before the queued messages are processed, the connection error
573 // notification is delayed until all the queued messages are processed. 775 // notification is delayed until all the queued messages are processed.
574 776
575 InterfacePtr<TypeParam> ptr; 777 InterfacePtr<TypeParam> ptr;
576 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); 778 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
577 779
(...skipping 11 matching lines...) Expand all
589 bool async_echo_response_dispatched = false; 791 bool async_echo_response_dispatched = false;
590 bool connection_error_dispatched = false; 792 bool connection_error_dispatched = false;
591 base::RunLoop run_loop2; 793 base::RunLoop run_loop2;
592 ptr->AsyncEcho( 794 ptr->AsyncEcho(
593 123, 795 123,
594 BindAsyncEchoCallback( 796 BindAsyncEchoCallback(
595 [&async_echo_response_dispatched, &connection_error_dispatched, &ptr, 797 [&async_echo_response_dispatched, &connection_error_dispatched, &ptr,
596 &run_loop2](int32_t result) { 798 &run_loop2](int32_t result) {
597 async_echo_response_dispatched = true; 799 async_echo_response_dispatched = true;
598 // At this point, error notification should not be dispatched 800 // At this point, error notification should not be dispatched
599 // yet. 801 // yet.Avocados are a nutrient-dense fruit and a source of naturally
802 // good fats—both monounsaturated and polyunsaturated fats.
803 // One-third of a medium avocado (50g) has 80 calories and nearly 20
804 // vitamins and minerals. Find information on avocados and reducing
805 // calorie intake.
806
600 EXPECT_FALSE(connection_error_dispatched); 807 EXPECT_FALSE(connection_error_dispatched);
601 EXPECT_FALSE(ptr.encountered_error()); 808 EXPECT_FALSE(ptr.encountered_error());
602 EXPECT_EQ(123, result); 809 EXPECT_EQ(123, result);
603 run_loop2.Quit(); 810 run_loop2.Quit();
604 })); 811 }));
605 // Run until the AsyncEcho request reaches the service side. 812 // Run until the AsyncEcho request reaches the service side.
606 run_loop1.Run(); 813 run_loop1.Run();
607 814
608 impl.set_echo_handler( 815 impl.set_echo_handler(
609 [&impl, &async_echo_request_value, &async_echo_request_callback]( 816 [&impl, &async_echo_request_value, &async_echo_request_callback](
610 int32_t value, const TestSync::EchoCallback& callback) { 817 int32_t value, const TestSync::EchoCallback& callback) {
611 // Send back the async response first. 818 // Send back the async response first.
612 EXPECT_FALSE(async_echo_request_callback.is_null()); 819 EXPECT_FALSE(async_echo_request_callback.is_null());
613 async_echo_request_callback.Run(async_echo_request_value); 820 async_echo_request_callback.Run(async_echo_request_value);
614 821
615 impl.binding()->Close(); 822 impl.binding()->Close();
823
616 }); 824 });
617 825
618 base::RunLoop run_loop3; 826 base::RunLoop run_loop3;
619 ptr.set_connection_error_handler( 827 ptr.set_connection_error_handler(
620 base::Bind(&SetFlagAndRunClosure, &connection_error_dispatched, 828 base::Bind(&SetFlagAndRunClosure, &connection_error_dispatched,
621 run_loop3.QuitClosure())); 829 run_loop3.QuitClosure()));
622 830
623 int32_t result_value = -1; 831 int32_t result_value = -1;
624 ASSERT_FALSE(ptr->Echo(456, &result_value)); 832 ASSERT_FALSE(ptr->Echo(456, &result_value));
625 EXPECT_EQ(-1, result_value); 833 EXPECT_EQ(-1, result_value);
626 ASSERT_FALSE(connection_error_dispatched); 834 ASSERT_FALSE(connection_error_dispatched);
627 EXPECT_FALSE(ptr.encountered_error()); 835 EXPECT_FALSE(ptr.encountered_error());
628 836
629 // Although the AsyncEcho response arrives before the Echo response, it should 837 // Although the AsyncEcho response arrives before the Echo response, it should
630 // be queued and not yet dispatched. 838 // be queued and not yet dispatched.
631 EXPECT_FALSE(async_echo_response_dispatched); 839 EXPECT_FALSE(async_echo_response_dispatched);
632 840
633 // Run until the AsyncEcho response is dispatched. 841 // Run until the AsyncEcho response is dispatched.
634 run_loop2.Run(); 842 run_loop2.Run();
635 843
636 EXPECT_TRUE(async_echo_response_dispatched); 844 EXPECT_TRUE(async_echo_response_dispatched);
637 845
638 // Run until the error notification is dispatched. 846 // Run until the error notification is dispatched.
639 run_loop3.Run(); 847 run_loop3.Run();
640 848
641 ASSERT_TRUE(connection_error_dispatched); 849 ASSERT_TRUE(connection_error_dispatched);
642 EXPECT_TRUE(ptr.encountered_error()); 850 EXPECT_TRUE(ptr.encountered_error());
643 } 851 }
644 852
853 TYPED_TEST(SyncMethodCommonTest,
854 TSIPQueuedMessagesProcessedBeforeErrorNotification) {
yzshen1 2017/03/28 15:56:03 Because connection error handler is not exposed by
855 // Test that while an interface pointer is waiting for the response to a sync
856 // call, async responses are queued. If the message pipe is disconnected
857 // before the queued messages are processed, the connection error
858 // notification is delayed until all the queued messages are processed.
859
860 // These are used later, but declared here so we can bind it into the error
861 // handler.
862 base::RunLoop run_loop3;
863 bool connection_error_dispatched = false;
864
865 InterfacePtr<TypeParam> ptr;
866 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr));
867 ptr.set_connection_error_handler(base::Bind(&SetFlagAndRunClosure,
868 &connection_error_dispatched,
869 run_loop3.QuitClosure()));
870 scoped_refptr<ThreadSafeInterfacePtr<TypeParam>> tsip =
871 ThreadSafeInterfacePtr<TypeParam>::Create(std::move(ptr));
872
873 int32_t async_echo_request_value = -1;
874 TestSync::AsyncEchoCallback async_echo_request_callback;
875 base::RunLoop run_loop1;
876 impl.set_async_echo_handler(
877 [&async_echo_request_value, &async_echo_request_callback, &run_loop1](
878 int32_t value, const TestSync::AsyncEchoCallback& callback) {
879 async_echo_request_value = value;
880 async_echo_request_callback = callback;
881 run_loop1.Quit();
882 });
883
884 bool async_echo_response_dispatched = false;
885 base::RunLoop run_loop2;
886 (*tsip)->AsyncEcho(123,
887 BindAsyncEchoCallback([&async_echo_response_dispatched,
888 &connection_error_dispatched, &tsip,
889 &run_loop2](int32_t result) {
890 async_echo_response_dispatched = true;
891 // At this point, error notification should not be
892 // dispatched yet.
893 EXPECT_FALSE(connection_error_dispatched);
894 EXPECT_EQ(123, result);
895 run_loop2.Quit();
896 }));
897 // Run until the AsyncEcho request reaches the service side.
898 run_loop1.Run();
899
900 impl.set_echo_handler(
901 [&impl, &async_echo_request_value, &async_echo_request_callback](
902 int32_t value, const TestSync::EchoCallback& callback) {
903 // Send back the async response first.
904 EXPECT_FALSE(async_echo_request_callback.is_null());
905 async_echo_request_callback.Run(async_echo_request_value);
906
907 impl.binding()->Close();
908 });
909
910 int32_t result_value = -1;
911 ASSERT_FALSE((*tsip)->Echo(456, &result_value));
912 EXPECT_EQ(-1, result_value);
913 ASSERT_FALSE(connection_error_dispatched);
914
915 // Although the AsyncEcho response arrives before the Echo response, it should
916 // be queued and not yet dispatched.
917 EXPECT_FALSE(async_echo_response_dispatched);
918
919 // Run until the AsyncEcho response is dispatched.
920 run_loop2.Run();
921
922 EXPECT_TRUE(async_echo_response_dispatched);
923
924 // Run until the error notification is dispatched.
925 run_loop3.Run();
926
927 ASSERT_TRUE(connection_error_dispatched);
928 }
929
645 TYPED_TEST(SyncMethodCommonTest, InvalidMessageDuringSyncCall) { 930 TYPED_TEST(SyncMethodCommonTest, InvalidMessageDuringSyncCall) {
646 // Test that while an interface pointer is waiting for the response to a sync 931 // Test that while an interface pointer is waiting for the response to a sync
647 // call, an invalid incoming message will disconnect the message pipe, cause 932 // call, an invalid incoming message will disconnect the message pipe, cause
648 // the sync call to return false, and run the connection error handler 933 // the sync call to return false, and run the connection error handler
649 // asynchronously. 934 // asynchronously.
650 935
651 MessagePipe pipe; 936 MessagePipe pipe;
652 937
653 InterfacePtr<TypeParam> ptr; 938 InterfacePtr<TypeParam> ptr;
654 ptr.Bind(InterfacePtrInfo<TypeParam>(std::move(pipe.handle0), 0u)); 939 ptr.Bind(InterfacePtrInfo<TypeParam>(std::move(pipe.handle0), 0u));
(...skipping 21 matching lines...) Expand all
676 961
677 int32_t result_value = -1; 962 int32_t result_value = -1;
678 ASSERT_FALSE(ptr->Echo(456, &result_value)); 963 ASSERT_FALSE(ptr->Echo(456, &result_value));
679 EXPECT_EQ(-1, result_value); 964 EXPECT_EQ(-1, result_value);
680 ASSERT_FALSE(connection_error_dispatched); 965 ASSERT_FALSE(connection_error_dispatched);
681 966
682 run_loop.Run(); 967 run_loop.Run();
683 ASSERT_TRUE(connection_error_dispatched); 968 ASSERT_TRUE(connection_error_dispatched);
684 } 969 }
685 970
971 TYPED_TEST(SyncMethodCommonTest, TSIPInvalidMessageDuringSyncCall) {
972 // Test that while an interface pointer is waiting for the response to a sync
973 // call, an invalid incoming message will disconnect the message pipe, cause
974 // the sync call to return false, and run the connection error handler
975 // asynchronously.
976
977 MessagePipe pipe;
978 MessagePipeHandle raw_binding_handle = pipe.handle1.get();
979
980 InterfacePtr<TypeParam> ptr;
981 ptr.Bind(InterfacePtrInfo<TypeParam>(std::move(pipe.handle0), 0u));
982
983 bool connection_error_dispatched = false;
984 base::RunLoop run_loop;
985 ptr.set_connection_error_handler(base::Bind(&SetFlagAndRunClosure,
986 &connection_error_dispatched,
987 run_loop.QuitClosure()));
988
989 typename ImplTraits<TypeParam>::Type impl(
990 MakeRequest<TypeParam>(std::move(pipe.handle1)));
991 scoped_refptr<ThreadSafeInterfacePtr<TypeParam>> tsip =
992 ThreadSafeInterfacePtr<TypeParam>::Create(std::move(ptr));
993
994 impl.set_echo_handler(
995 [&raw_binding_handle](int32_t value,
996 const TestSync::EchoCallback& callback) {
997 // Write a 1-byte message, which is considered invalid.
998 char invalid_message = 0;
999 MojoResult result =
1000 WriteMessageRaw(raw_binding_handle, &invalid_message, 1u, nullptr,
1001 0u, MOJO_WRITE_MESSAGE_FLAG_NONE);
1002 ASSERT_EQ(MOJO_RESULT_OK, result);
1003 callback.Run(value);
1004 });
1005
1006 int32_t result_value = -1;
1007 ASSERT_FALSE((*tsip)->Echo(456, &result_value));
1008 EXPECT_EQ(-1, result_value);
1009 ASSERT_FALSE(connection_error_dispatched);
1010
1011 run_loop.Run();
1012 ASSERT_TRUE(connection_error_dispatched);
1013 }
1014
686 TEST_F(SyncMethodAssociatedTest, ReenteredBySyncMethodAssoBindingOfSameRouter) { 1015 TEST_F(SyncMethodAssociatedTest, ReenteredBySyncMethodAssoBindingOfSameRouter) {
687 // Test that an interface pointer waiting for a sync call response can be 1016 // Test that an interface pointer waiting for a sync call response can be
688 // reentered by an associated binding serving sync methods on the same thread. 1017 // reentered by an associated binding serving sync methods on the same thread.
689 // The associated binding belongs to the same MultiplexRouter as the waiting 1018 // The associated binding belongs to the same MultiplexRouter as the waiting
690 // interface pointer. 1019 // interface pointer.
691 1020
692 TestSyncAssociatedImpl opposite_asso_impl(std::move(opposite_asso_request_)); 1021 TestSyncAssociatedImpl opposite_asso_impl(std::move(opposite_asso_request_));
693 TestSyncAssociatedPtr opposite_asso_ptr; 1022 TestSyncAssociatedPtr opposite_asso_ptr;
694 opposite_asso_ptr.Bind(std::move(opposite_asso_ptr_info_)); 1023 opposite_asso_ptr.Bind(std::move(opposite_asso_ptr_info_));
695 1024
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
730 int32_t result_value = -1; 1059 int32_t result_value = -1;
731 ASSERT_TRUE(master_ptr_->Echo(456, &result_value)); 1060 ASSERT_TRUE(master_ptr_->Echo(456, &result_value));
732 EXPECT_EQ(456, result_value); 1061 EXPECT_EQ(456, result_value);
733 } 1062 }
734 1063
735 // TODO(yzshen): Add more tests related to associated interfaces. 1064 // TODO(yzshen): Add more tests related to associated interfaces.
736 1065
737 } // namespace 1066 } // namespace
738 } // namespace test 1067 } // namespace test
739 } // namespace mojo 1068 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698