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/edk/system/raw_channel_unittest.cc

Issue 859333004: Allow mojo::system::RawChannel::Delegate methods to destroy the RawChannel. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 11 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/system/raw_channel.h" 5 #include "mojo/edk/system/raw_channel.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <stdio.h> 8 #include <stdio.h>
9 9
10 #include <vector> 10 #include <vector>
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass())); 548 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass()));
549 io_thread()->PostTaskAndWait( 549 io_thread()->PostTaskAndWait(
550 FROM_HERE, 550 FROM_HERE,
551 base::Bind(&InitOnIOThread, rc.get(), base::Unretained(&delegate))); 551 base::Bind(&InitOnIOThread, rc.get(), base::Unretained(&delegate)));
552 io_thread()->PostTaskAndWait( 552 io_thread()->PostTaskAndWait(
553 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc.get()))); 553 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc.get())));
554 554
555 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); 555 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1)));
556 } 556 }
557 557
558 // RawChannelTest.ShutdownOnReadMessage ---------------------------------------- 558 // RawChannelTest.{Shutdown, ShutdownAndDestroy}OnReadMessage ------------------
559 559
560 class ShutdownOnReadMessageRawChannelDelegate : public RawChannel::Delegate { 560 class ShutdownOnReadMessageRawChannelDelegate : public RawChannel::Delegate {
561 public: 561 public:
562 explicit ShutdownOnReadMessageRawChannelDelegate(RawChannel* raw_channel) 562 explicit ShutdownOnReadMessageRawChannelDelegate(RawChannel* raw_channel,
563 bool should_destroy)
563 : raw_channel_(raw_channel), 564 : raw_channel_(raw_channel),
565 should_destroy_(should_destroy),
564 done_event_(false, false), 566 done_event_(false, false),
565 did_shutdown_(false) {} 567 did_shutdown_(false) {}
566 ~ShutdownOnReadMessageRawChannelDelegate() override {} 568 ~ShutdownOnReadMessageRawChannelDelegate() override {}
567 569
568 // |RawChannel::Delegate| implementation (called on the I/O thread): 570 // |RawChannel::Delegate| implementation (called on the I/O thread):
569 void OnReadMessage( 571 void OnReadMessage(
570 const MessageInTransit::View& message_view, 572 const MessageInTransit::View& message_view,
571 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { 573 embedder::ScopedPlatformHandleVectorPtr platform_handles) override {
572 EXPECT_FALSE(platform_handles); 574 EXPECT_FALSE(platform_handles);
573 EXPECT_FALSE(did_shutdown_); 575 EXPECT_FALSE(did_shutdown_);
574 EXPECT_TRUE( 576 EXPECT_TRUE(
575 CheckMessageData(message_view.bytes(), message_view.num_bytes())); 577 CheckMessageData(message_view.bytes(), message_view.num_bytes()));
576 raw_channel_->Shutdown(); 578 raw_channel_->Shutdown();
579 if (should_destroy_)
580 delete raw_channel_;
577 did_shutdown_ = true; 581 did_shutdown_ = true;
578 done_event_.Signal(); 582 done_event_.Signal();
579 } 583 }
580 void OnError(Error /*error*/) override { 584 void OnError(Error /*error*/) override {
581 CHECK(false); // Should not get called. 585 CHECK(false); // Should not get called.
582 } 586 }
583 587
584 // Waits for shutdown. 588 // Waits for shutdown.
585 void Wait() { 589 void Wait() {
586 done_event_.Wait(); 590 done_event_.Wait();
587 EXPECT_TRUE(did_shutdown_); 591 EXPECT_TRUE(did_shutdown_);
588 } 592 }
589 593
590 private: 594 private:
591 RawChannel* const raw_channel_; 595 RawChannel* const raw_channel_;
596 const bool should_destroy_;
592 base::WaitableEvent done_event_; 597 base::WaitableEvent done_event_;
593 bool did_shutdown_; 598 bool did_shutdown_;
594 599
595 DISALLOW_COPY_AND_ASSIGN(ShutdownOnReadMessageRawChannelDelegate); 600 DISALLOW_COPY_AND_ASSIGN(ShutdownOnReadMessageRawChannelDelegate);
596 }; 601 };
597 602
598 TEST_F(RawChannelTest, ShutdownOnReadMessage) { 603 TEST_F(RawChannelTest, ShutdownOnReadMessage) {
599 // Write a few messages into the other end. 604 // Write a few messages into the other end.
600 for (size_t count = 0; count < 5; count++) 605 for (size_t count = 0; count < 5; count++)
601 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), 10)); 606 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), 10));
602 607
603 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass())); 608 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass()));
604 ShutdownOnReadMessageRawChannelDelegate delegate(rc.get()); 609 ShutdownOnReadMessageRawChannelDelegate delegate(rc.get(), false);
605 io_thread()->PostTaskAndWait( 610 io_thread()->PostTaskAndWait(
606 FROM_HERE, 611 FROM_HERE,
607 base::Bind(&InitOnIOThread, rc.get(), base::Unretained(&delegate))); 612 base::Bind(&InitOnIOThread, rc.get(), base::Unretained(&delegate)));
608 613
609 // Wait for the delegate, which will shut the |RawChannel| down. 614 // Wait for the delegate, which will shut the |RawChannel| down.
610 delegate.Wait(); 615 delegate.Wait();
611 } 616 }
612 617
613 // RawChannelTest.ShutdownOnError{Read, Write} --------------------------------- 618 TEST_F(RawChannelTest, ShutdownAndDestroyOnReadMessage) {
yzshen1 2015/01/21 23:03:42 An alternative is to use value parameterized tests
viettrungluu 2015/01/21 23:34:32 I don't think it's worth the extra complication (t
619 // Write a message into the other end.
620 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), 10));
621
622 // The delegate will destroy |rc|.
623 RawChannel* rc = RawChannel::Create(handles[0].Pass()).release();
624 ShutdownOnReadMessageRawChannelDelegate delegate(rc, true);
625 io_thread()->PostTaskAndWait(
626 FROM_HERE, base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate)));
627
628 // Wait for the delegate, which will shut the |RawChannel| down.
629 delegate.Wait();
630 }
631
632 // RawChannelTest.{Shutdown, ShutdownAndDestroy}OnError{Read, Write} -----------
614 633
615 class ShutdownOnErrorRawChannelDelegate : public RawChannel::Delegate { 634 class ShutdownOnErrorRawChannelDelegate : public RawChannel::Delegate {
616 public: 635 public:
617 ShutdownOnErrorRawChannelDelegate(RawChannel* raw_channel, 636 ShutdownOnErrorRawChannelDelegate(RawChannel* raw_channel,
637 bool should_destroy,
618 Error shutdown_on_error_type) 638 Error shutdown_on_error_type)
619 : raw_channel_(raw_channel), 639 : raw_channel_(raw_channel),
640 should_destroy_(should_destroy),
620 shutdown_on_error_type_(shutdown_on_error_type), 641 shutdown_on_error_type_(shutdown_on_error_type),
621 done_event_(false, false), 642 done_event_(false, false),
622 did_shutdown_(false) {} 643 did_shutdown_(false) {}
623 ~ShutdownOnErrorRawChannelDelegate() override {} 644 ~ShutdownOnErrorRawChannelDelegate() override {}
624 645
625 // |RawChannel::Delegate| implementation (called on the I/O thread): 646 // |RawChannel::Delegate| implementation (called on the I/O thread):
626 void OnReadMessage( 647 void OnReadMessage(
627 const MessageInTransit::View& /*message_view*/, 648 const MessageInTransit::View& /*message_view*/,
628 embedder::ScopedPlatformHandleVectorPtr /*platform_handles*/) override { 649 embedder::ScopedPlatformHandleVectorPtr /*platform_handles*/) override {
629 CHECK(false); // Should not get called. 650 CHECK(false); // Should not get called.
630 } 651 }
631 void OnError(Error error) override { 652 void OnError(Error error) override {
632 EXPECT_FALSE(did_shutdown_); 653 EXPECT_FALSE(did_shutdown_);
633 if (error != shutdown_on_error_type_) 654 if (error != shutdown_on_error_type_)
634 return; 655 return;
635 raw_channel_->Shutdown(); 656 raw_channel_->Shutdown();
657 if (should_destroy_)
658 delete raw_channel_;
636 did_shutdown_ = true; 659 did_shutdown_ = true;
637 done_event_.Signal(); 660 done_event_.Signal();
638 } 661 }
639 662
640 // Waits for shutdown. 663 // Waits for shutdown.
641 void Wait() { 664 void Wait() {
642 done_event_.Wait(); 665 done_event_.Wait();
643 EXPECT_TRUE(did_shutdown_); 666 EXPECT_TRUE(did_shutdown_);
644 } 667 }
645 668
646 private: 669 private:
647 RawChannel* const raw_channel_; 670 RawChannel* const raw_channel_;
671 const bool should_destroy_;
648 const Error shutdown_on_error_type_; 672 const Error shutdown_on_error_type_;
649 base::WaitableEvent done_event_; 673 base::WaitableEvent done_event_;
650 bool did_shutdown_; 674 bool did_shutdown_;
651 675
652 DISALLOW_COPY_AND_ASSIGN(ShutdownOnErrorRawChannelDelegate); 676 DISALLOW_COPY_AND_ASSIGN(ShutdownOnErrorRawChannelDelegate);
653 }; 677 };
654 678
655 TEST_F(RawChannelTest, ShutdownOnErrorRead) { 679 TEST_F(RawChannelTest, ShutdownOnErrorRead) {
656 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass())); 680 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass()));
657 ShutdownOnErrorRawChannelDelegate delegate( 681 ShutdownOnErrorRawChannelDelegate delegate(
658 rc.get(), RawChannel::Delegate::ERROR_READ_SHUTDOWN); 682 rc.get(), false, RawChannel::Delegate::ERROR_READ_SHUTDOWN);
659 io_thread()->PostTaskAndWait( 683 io_thread()->PostTaskAndWait(
660 FROM_HERE, 684 FROM_HERE,
661 base::Bind(&InitOnIOThread, rc.get(), base::Unretained(&delegate))); 685 base::Bind(&InitOnIOThread, rc.get(), base::Unretained(&delegate)));
662 686
663 // Close the handle of the other end, which should stuff fail. 687 // Close the handle of the other end, which should stuff fail.
664 handles[1].reset(); 688 handles[1].reset();
665 689
666 // Wait for the delegate, which will shut the |RawChannel| down. 690 // Wait for the delegate, which will shut the |RawChannel| down.
667 delegate.Wait(); 691 delegate.Wait();
668 } 692 }
669 693
694 TEST_F(RawChannelTest, ShutdownAndDestroyOnErrorRead) {
695 RawChannel* rc = RawChannel::Create(handles[0].Pass()).release();
696 ShutdownOnErrorRawChannelDelegate delegate(
697 rc, true, RawChannel::Delegate::ERROR_READ_SHUTDOWN);
698 io_thread()->PostTaskAndWait(
699 FROM_HERE, base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate)));
700
701 // Close the handle of the other end, which should stuff fail.
702 handles[1].reset();
703
704 // Wait for the delegate, which will shut the |RawChannel| down.
705 delegate.Wait();
706 }
707
670 TEST_F(RawChannelTest, ShutdownOnErrorWrite) { 708 TEST_F(RawChannelTest, ShutdownOnErrorWrite) {
671 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass())); 709 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass()));
672 ShutdownOnErrorRawChannelDelegate delegate(rc.get(), 710 ShutdownOnErrorRawChannelDelegate delegate(rc.get(), false,
673 RawChannel::Delegate::ERROR_WRITE); 711 RawChannel::Delegate::ERROR_WRITE);
674 io_thread()->PostTaskAndWait( 712 io_thread()->PostTaskAndWait(
675 FROM_HERE, 713 FROM_HERE,
676 base::Bind(&InitOnIOThread, rc.get(), base::Unretained(&delegate))); 714 base::Bind(&InitOnIOThread, rc.get(), base::Unretained(&delegate)));
677 715
678 // Close the handle of the other end, which should stuff fail. 716 // Close the handle of the other end, which should stuff fail.
679 handles[1].reset(); 717 handles[1].reset();
680 718
681 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); 719 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1)));
682 720
683 // Wait for the delegate, which will shut the |RawChannel| down. 721 // Wait for the delegate, which will shut the |RawChannel| down.
684 delegate.Wait(); 722 delegate.Wait();
685 } 723 }
686 724
725 TEST_F(RawChannelTest, ShutdownAndDestroyOnErrorWrite) {
726 RawChannel* rc = RawChannel::Create(handles[0].Pass()).release();
727 ShutdownOnErrorRawChannelDelegate delegate(rc, true,
728 RawChannel::Delegate::ERROR_WRITE);
729 io_thread()->PostTaskAndWait(
730 FROM_HERE, base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate)));
731
732 // Close the handle of the other end, which should stuff fail.
733 handles[1].reset();
734
735 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1)));
736
737 // Wait for the delegate, which will shut the |RawChannel| down.
738 delegate.Wait();
739 }
740
687 // RawChannelTest.ReadWritePlatformHandles ------------------------------------- 741 // RawChannelTest.ReadWritePlatformHandles -------------------------------------
688 742
689 class ReadPlatformHandlesCheckerRawChannelDelegate 743 class ReadPlatformHandlesCheckerRawChannelDelegate
690 : public RawChannel::Delegate { 744 : public RawChannel::Delegate {
691 public: 745 public:
692 ReadPlatformHandlesCheckerRawChannelDelegate() : done_event_(false, false) {} 746 ReadPlatformHandlesCheckerRawChannelDelegate() : done_event_(false, false) {}
693 ~ReadPlatformHandlesCheckerRawChannelDelegate() override {} 747 ~ReadPlatformHandlesCheckerRawChannelDelegate() override {}
694 748
695 // |RawChannel::Delegate| implementation (called on the I/O thread): 749 // |RawChannel::Delegate| implementation (called on the I/O thread):
696 void OnReadMessage( 750 void OnReadMessage(
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
796 FROM_HERE, 850 FROM_HERE,
797 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read.get()))); 851 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read.get())));
798 io_thread()->PostTaskAndWait( 852 io_thread()->PostTaskAndWait(
799 FROM_HERE, 853 FROM_HERE,
800 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write.get()))); 854 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write.get())));
801 } 855 }
802 856
803 } // namespace 857 } // namespace
804 } // namespace system 858 } // namespace system
805 } // namespace mojo 859 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698