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

Side by Side Diff: ipc/ipc_channel_mojo_unittest.cc

Issue 2147493006: Adds Channel-associated interface support on ChannelProxy's thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 5 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
« no previous file with comments | « ipc/ipc_channel_mojo.cc ('k') | ipc/ipc_channel_proxy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "ipc/ipc_channel_mojo.h" 5 #include "ipc/ipc_channel_mojo.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/base_paths.h" 13 #include "base/base_paths.h"
14 #include "base/bind.h"
14 #include "base/files/file.h" 15 #include "base/files/file.h"
15 #include "base/files/scoped_temp_dir.h" 16 #include "base/files/scoped_temp_dir.h"
16 #include "base/location.h" 17 #include "base/location.h"
18 #include "base/macros.h"
19 #include "base/message_loop/message_loop.h"
17 #include "base/path_service.h" 20 #include "base/path_service.h"
18 #include "base/pickle.h" 21 #include "base/pickle.h"
19 #include "base/run_loop.h" 22 #include "base/run_loop.h"
20 #include "base/single_thread_task_runner.h" 23 #include "base/single_thread_task_runner.h"
21 #include "base/strings/stringprintf.h" 24 #include "base/strings/stringprintf.h"
22 #include "base/test/test_io_thread.h" 25 #include "base/test/test_io_thread.h"
23 #include "base/test/test_timeouts.h" 26 #include "base/test/test_timeouts.h"
24 #include "base/threading/thread.h" 27 #include "base/threading/thread.h"
25 #include "base/threading/thread_task_runner_handle.h" 28 #include "base/threading/thread_task_runner_handle.h"
26 #include "build/build_config.h" 29 #include "build/build_config.h"
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 99
97 private: 100 private:
98 bool received_ok_; 101 bool received_ok_;
99 }; 102 };
100 103
101 class ChannelClient { 104 class ChannelClient {
102 public: 105 public:
103 void Init(mojo::ScopedMessagePipeHandle handle) { 106 void Init(mojo::ScopedMessagePipeHandle handle) {
104 handle_ = std::move(handle); 107 handle_ = std::move(handle);
105 } 108 }
109
106 void Connect(IPC::Listener* listener) { 110 void Connect(IPC::Listener* listener) {
107 channel_ = IPC::ChannelMojo::Create(std::move(handle_), 111 channel_ = IPC::ChannelMojo::Create(std::move(handle_),
108 IPC::Channel::MODE_CLIENT, listener); 112 IPC::Channel::MODE_CLIENT, listener);
109 CHECK(channel_->Connect()); 113 CHECK(channel_->Connect());
110 } 114 }
111 115
112 void Close() { 116 void Close() {
113 channel_->Close(); 117 channel_->Close();
114 118
115 base::RunLoop run_loop; 119 base::RunLoop run_loop;
116 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 120 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
117 run_loop.QuitClosure()); 121 run_loop.QuitClosure());
118 run_loop.Run(); 122 run_loop.Run();
119 } 123 }
120 124
121 IPC::ChannelMojo* channel() const { return channel_.get(); } 125 IPC::ChannelMojo* channel() const { return channel_.get(); }
122 126
123 private: 127 private:
124 base::MessageLoopForIO main_message_loop_; 128 base::MessageLoopForIO main_message_loop_;
125 mojo::ScopedMessagePipeHandle handle_; 129 mojo::ScopedMessagePipeHandle handle_;
126 std::unique_ptr<IPC::ChannelMojo> channel_; 130 std::unique_ptr<IPC::ChannelMojo> channel_;
127 }; 131 };
128 132
129 class IPCChannelMojoTest : public testing::Test { 133 class IPCChannelMojoTestBase : public testing::Test {
130 public: 134 public:
131 IPCChannelMojoTest() {}
132
133 void TearDown() override { base::RunLoop().RunUntilIdle(); }
134
135 void InitWithMojo(const std::string& test_client_name) { 135 void InitWithMojo(const std::string& test_client_name) {
136 handle_ = helper_.StartChild(test_client_name); 136 handle_ = helper_.StartChild(test_client_name);
137 } 137 }
138 138
139 bool WaitForClientShutdown() { return helper_.WaitForChildTestShutdown(); }
140
141 protected:
142 mojo::ScopedMessagePipeHandle TakeHandle() { return std::move(handle_); }
143
144 private:
145 mojo::ScopedMessagePipeHandle handle_;
146 mojo::edk::test::MultiprocessTestHelper helper_;
147 };
148
149 class IPCChannelMojoTest : public IPCChannelMojoTestBase {
150 public:
151 void TearDown() override { base::RunLoop().RunUntilIdle(); }
152
139 void CreateChannel(IPC::Listener* listener) { 153 void CreateChannel(IPC::Listener* listener) {
140 channel_ = IPC::ChannelMojo::Create(std::move(handle_), 154 channel_ = IPC::ChannelMojo::Create(
141 IPC::Channel::MODE_SERVER, listener); 155 TakeHandle(), IPC::Channel::MODE_SERVER, listener);
142 } 156 }
143 157
144 bool ConnectChannel() { return channel_->Connect(); } 158 bool ConnectChannel() { return channel_->Connect(); }
145 159
146 void DestroyChannel() { channel_.reset(); } 160 void DestroyChannel() { channel_.reset(); }
147 161
148 bool WaitForClientShutdown() { return helper_.WaitForChildTestShutdown(); }
149
150 IPC::Sender* sender() { return channel(); } 162 IPC::Sender* sender() { return channel(); }
151 IPC::Channel* channel() { return channel_.get(); } 163 IPC::Channel* channel() { return channel_.get(); }
152 164
153 private: 165 private:
154 base::MessageLoop message_loop_; 166 base::MessageLoop message_loop_;
155 mojo::edk::test::MultiprocessTestHelper helper_;
156 mojo::ScopedMessagePipeHandle handle_;
157 std::unique_ptr<IPC::Channel> channel_; 167 std::unique_ptr<IPC::Channel> channel_;
158 }; 168 };
159 169
160 class TestChannelListenerWithExtraExpectations 170 class TestChannelListenerWithExtraExpectations
161 : public IPC::TestChannelListener { 171 : public IPC::TestChannelListener {
162 public: 172 public:
163 TestChannelListenerWithExtraExpectations() : is_connected_called_(false) {} 173 TestChannelListenerWithExtraExpectations() : is_connected_called_(false) {}
164 174
165 void OnChannelConnected(int32_t peer_pid) override { 175 void OnChannelConnected(int32_t peer_pid) override {
166 IPC::TestChannelListener::OnChannelConnected(peer_pid); 176 IPC::TestChannelListener::OnChannelConnected(peer_pid);
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 ChannelClient) { 702 ChannelClient) {
693 ListenerSendingAssociatedMessages listener; 703 ListenerSendingAssociatedMessages listener;
694 Connect(&listener); 704 Connect(&listener);
695 listener.set_channel(channel()); 705 listener.set_channel(channel());
696 706
697 base::RunLoop().Run(); 707 base::RunLoop().Run();
698 708
699 Close(); 709 Close();
700 } 710 }
701 711
712 class ChannelProxyRunner {
713 public:
714 ChannelProxyRunner(std::unique_ptr<IPC::ChannelFactory> channel_factory)
715 : channel_factory_(std::move(channel_factory)),
716 io_thread_("ChannelProxyRunner IO thread") {
717 }
718
719 void CreateProxy(IPC::Listener* listener) {
720 io_thread_.StartWithOptions(
721 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
722 proxy_.reset(new IPC::ChannelProxy(listener, io_thread_.task_runner()));
723 }
724 void RunProxy() { proxy_->Init(std::move(channel_factory_), true); }
725
726 IPC::ChannelProxy* proxy() { return proxy_.get(); }
727
728 private:
729 std::unique_ptr<IPC::ChannelFactory> channel_factory_;
730
731 base::Thread io_thread_;
732 std::unique_ptr<IPC::ChannelProxy> proxy_;
733
734 DISALLOW_COPY_AND_ASSIGN(ChannelProxyRunner);
735 };
736
737 class IPCChannelProxyMojoTest : public IPCChannelMojoTestBase {
738 public:
739 void InitWithMojo(const std::string& client_name) {
740 IPCChannelMojoTestBase::InitWithMojo(client_name);
741 runner_.reset(new ChannelProxyRunner(
742 IPC::ChannelMojo::CreateServerFactory(TakeHandle())));
743 }
744 void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); }
745 void RunProxy() { runner_->RunProxy(); }
746
747 IPC::ChannelProxy* proxy() { return runner_->proxy(); }
748
749 private:
750 base::MessageLoop message_loop_;
751 std::unique_ptr<ChannelProxyRunner> runner_;
752 };
753
754 class ListenerWithSimpleProxyAssociatedInterface
755 : public IPC::Listener,
756 public IPC::mojom::SimpleTestDriver {
757 public:
758 static const int kNumMessages;
759
760 ListenerWithSimpleProxyAssociatedInterface() : binding_(this) {}
761
762 ~ListenerWithSimpleProxyAssociatedInterface() override {}
763
764 bool OnMessageReceived(const IPC::Message& message) override {
765 base::PickleIterator iter(message);
766 std::string should_be_expected;
767 EXPECT_TRUE(iter.ReadString(&should_be_expected));
768 EXPECT_EQ(should_be_expected, next_expected_string_);
769 num_messages_received_++;
770 return true;
771 }
772
773 void OnChannelError() override {
774 DCHECK(received_quit_);
775 }
776
777 void RegisterInterfaceFactory(IPC::ChannelProxy* proxy) {
778 proxy->AddAssociatedInterface(
779 base::Bind(&ListenerWithSimpleProxyAssociatedInterface::BindRequest,
780 base::Unretained(this)));
781 }
782
783 bool received_all_messages() const {
784 return num_messages_received_ == kNumMessages && received_quit_;
785 }
786
787 private:
788 // IPC::mojom::SimpleTestDriver:
789 void ExpectString(const mojo::String& str) override {
790 next_expected_string_ = str;
791 }
792
793 void RequestQuit(const RequestQuitCallback& callback) override {
794 received_quit_ = true;
795 callback.Run();
796 base::MessageLoop::current()->QuitWhenIdle();
797 }
798
799 void BindRequest(IPC::mojom::SimpleTestDriverAssociatedRequest request) {
800 DCHECK(!binding_.is_bound());
801 binding_.Bind(std::move(request));
802 }
803
804 std::string next_expected_string_;
805 int num_messages_received_ = 0;
806 bool received_quit_ = false;
807
808 mojo::AssociatedBinding<IPC::mojom::SimpleTestDriver> binding_;
809 };
810
811 const int ListenerWithSimpleProxyAssociatedInterface::kNumMessages = 1000;
812
813 TEST_F(IPCChannelProxyMojoTest, ProxyThreadAssociatedInterface) {
814 InitWithMojo("ProxyThreadAssociatedInterfaceClient");
815
816 ListenerWithSimpleProxyAssociatedInterface listener;
817 CreateProxy(&listener);
818 listener.RegisterInterfaceFactory(proxy());
819 RunProxy();
820
821 base::RunLoop().Run();
822
823 EXPECT_TRUE(WaitForClientShutdown());
824 EXPECT_TRUE(listener.received_all_messages());
825
826 base::RunLoop().RunUntilIdle();
827 }
828
829 class ChannelProxyClient {
830 public:
831 void Init(mojo::ScopedMessagePipeHandle handle) {
832 runner_.reset(new ChannelProxyRunner(
833 IPC::ChannelMojo::CreateClientFactory(std::move(handle))));
834 }
835 void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); }
836 void RunProxy() { runner_->RunProxy(); }
837
838 IPC::ChannelProxy* proxy() { return runner_->proxy(); }
839
840 private:
841 base::MessageLoop message_loop_;
842 std::unique_ptr<ChannelProxyRunner> runner_;
843 };
844
845 class ListenerThatWaitsForConnect : public IPC::Listener {
846 public:
847 explicit ListenerThatWaitsForConnect(const base::Closure& connect_handler)
848 : connect_handler_(connect_handler) {}
849
850 // IPC::Listener
851 bool OnMessageReceived(const IPC::Message& message) override { return true; }
852 void OnChannelConnected(int32_t) override { connect_handler_.Run(); }
853
854 private:
855 base::Closure connect_handler_;
856 };
857
858 DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ProxyThreadAssociatedInterfaceClient,
859 ChannelProxyClient) {
860 base::RunLoop connect_loop;
861 ListenerThatWaitsForConnect listener(connect_loop.QuitClosure());
862 CreateProxy(&listener);
863 RunProxy();
864 connect_loop.Run();
865
866 // Send a bunch of interleaved messages, alternating between the associated
867 // interface and a legacy IPC::Message.
868 IPC::mojom::SimpleTestDriverAssociatedPtr driver;
869 proxy()->GetRemoteAssociatedInterface(&driver);
870 for (int i = 0; i < ListenerWithSimpleProxyAssociatedInterface::kNumMessages;
871 ++i) {
872 std::string str = base::StringPrintf("Hello! %d", i);
873 driver->ExpectString(str);
874 SendString(proxy(), str);
875 }
876 driver->RequestQuit(base::MessageLoop::QuitWhenIdleClosure());
877 base::RunLoop().Run();
878 }
879
702 #if defined(OS_POSIX) 880 #if defined(OS_POSIX)
881
703 class ListenerThatExpectsFile : public IPC::Listener { 882 class ListenerThatExpectsFile : public IPC::Listener {
704 public: 883 public:
705 ListenerThatExpectsFile() : sender_(NULL) {} 884 ListenerThatExpectsFile() : sender_(NULL) {}
706 885
707 ~ListenerThatExpectsFile() override {} 886 ~ListenerThatExpectsFile() override {}
708 887
709 bool OnMessageReceived(const IPC::Message& message) override { 888 bool OnMessageReceived(const IPC::Message& message) override {
710 base::PickleIterator iter(message); 889 base::PickleIterator iter(message);
711 HandleSendingHelper::ReadReceivedFile(message, &iter); 890 HandleSendingHelper::ReadReceivedFile(message, &iter);
712 ListenerThatExpectsOK::SendOK(sender_); 891 ListenerThatExpectsOK::SendOK(sender_);
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
853 Connect(&listener); 1032 Connect(&listener);
854 1033
855 base::MessageLoop::current()->Run(); 1034 base::MessageLoop::current()->Run();
856 1035
857 Close(); 1036 Close();
858 } 1037 }
859 1038
860 #endif // OS_LINUX 1039 #endif // OS_LINUX
861 1040
862 } // namespace 1041 } // namespace
OLDNEW
« no previous file with comments | « ipc/ipc_channel_mojo.cc ('k') | ipc/ipc_channel_proxy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698