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

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
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
725 void RunProxy() { proxy_->Init(std::move(channel_factory_), true); }
726
727 IPC::ChannelProxy* proxy() { return proxy_.get(); }
728
729 private:
730 std::unique_ptr<IPC::ChannelFactory> channel_factory_;
731 std::unique_ptr<IPC::ChannelProxy> proxy_;
732 base::Thread io_thread_;
733
734 DISALLOW_COPY_AND_ASSIGN(ChannelProxyRunner);
735 };
736
737 class ChannelProxyClient {
738 public:
739 void Init(mojo::ScopedMessagePipeHandle handle) {
740 runner_.reset(new ChannelProxyRunner(
741 IPC::ChannelMojo::CreateClientFactory(std::move(handle))));
742 }
743 void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); }
744 void RunProxy() { runner_->RunProxy(); }
745
746 IPC::ChannelProxy* proxy() { return runner_->proxy(); }
747
748 private:
749 base::MessageLoop message_loop_;
750 std::unique_ptr<ChannelProxyRunner> runner_;
751 };
752
753 class IPCChannelProxyMojoTest : public IPCChannelMojoTestBase {
754 public:
755 void InitWithMojo(const std::string& client_name) {
756 IPCChannelMojoTestBase::InitWithMojo(client_name);
757 runner_.reset(new ChannelProxyRunner(
758 IPC::ChannelMojo::CreateServerFactory(TakeHandle())));
759 }
760 void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); }
761 void RunProxy() { runner_->RunProxy(); }
762
763 IPC::ChannelProxy* proxy() { return runner_->proxy(); }
764
765 private:
766 base::MessageLoop message_loop_;
767 std::unique_ptr<ChannelProxyRunner> runner_;
768 };
769
770 class ListenerWithSimpleProxyAssociatedInterface
771 : public IPC::Listener,
772 public IPC::mojom::SimpleTestDriver {
773 public:
774 static const int kNumMessages;
775
776 ListenerWithSimpleProxyAssociatedInterface() : binding_(this) {}
777
778 ~ListenerWithSimpleProxyAssociatedInterface() override {}
779
780 bool OnMessageReceived(const IPC::Message& message) override {
781 base::PickleIterator iter(message);
782 std::string should_be_expected;
783 EXPECT_TRUE(iter.ReadString(&should_be_expected));
784 EXPECT_EQ(should_be_expected, next_expected_string_);
785 num_messages_received_++;
786 return true;
787 }
788
789 void OnChannelError() override {
790 DCHECK(received_quit_);
791 }
792
793 void RegisterInterfaceFactory(IPC::ChannelProxy* proxy) {
794 proxy->AddAssociatedInterface(
795 base::Bind(&ListenerWithSimpleProxyAssociatedInterface::BindRequest,
796 base::Unretained(this)));
797 }
798
799 private:
800 // IPC::mojom::SimpleTestDriver:
801 void ExpectString(const mojo::String& str) override {
802 next_expected_string_ = str;
803 }
804
805 void RequestQuit(const RequestQuitCallback& callback) override {
806 EXPECT_EQ(kNumMessages, num_messages_received_);
807 received_quit_ = true;
808 callback.Run();
809 base::MessageLoop::current()->QuitWhenIdle();
810 }
811
812 void BindRequest(IPC::mojom::SimpleTestDriverAssociatedRequest request) {
813 DCHECK(!binding_.is_bound());
814 binding_.Bind(std::move(request));
815 }
816
817 std::string next_expected_string_;
818 int num_messages_received_ = 0;
819 bool received_quit_ = false;
820
821 mojo::AssociatedBinding<IPC::mojom::SimpleTestDriver> binding_;
822 };
823
824 const int ListenerWithSimpleProxyAssociatedInterface::kNumMessages = 1000;
825
826 class ListenerThatWaitsForConnect : public IPC::Listener {
827 public:
828 ListenerThatWaitsForConnect() {}
829
830 void WaitForConnect() { connect_loop_.Run(); }
831
832 // IPC::Listener
833 bool OnMessageReceived(const IPC::Message& message) override { return true; }
834 void OnChannelConnected(int32_t) override { connect_loop_.Quit(); }
835
836 private:
837 base::RunLoop connect_loop_;
838 };
839
840 TEST_F(IPCChannelProxyMojoTest, ProxyThreadAssociatedInterface) {
841 InitWithMojo("ProxyThreadAssociatedInterfaceClient");
842
843 ListenerWithSimpleProxyAssociatedInterface listener;
844 CreateProxy(&listener);
845 listener.RegisterInterfaceFactory(proxy());
846 RunProxy();
847
848 base::RunLoop().Run();
849
850 EXPECT_TRUE(WaitForClientShutdown());
851 }
852
853 DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ProxyThreadAssociatedInterfaceClient,
854 ChannelProxyClient) {
855 ListenerThatWaitsForConnect listener;
856 CreateProxy(&listener);
857 RunProxy();
858 listener.WaitForConnect();
859
860 IPC::mojom::SimpleTestDriverAssociatedPtr driver_;
861 proxy()->GetRemoteAssociatedInterface(&driver_);
862
863 // Send a bunch of interleaved messages, alternating between the associated
864 // interface and a legacy IPC::Message.
865 for (int i = 0; i < ListenerWithSimpleProxyAssociatedInterface::kNumMessages;
866 ++i) {
867 std::string str = base::StringPrintf("Hello! %d", i);
868 driver_->ExpectString(str);
869 SendString(proxy(), str);
870 }
871 driver_->RequestQuit(base::MessageLoop::QuitWhenIdleClosure());
872
873 base::RunLoop().Run();
874 }
875
702 #if defined(OS_POSIX) 876 #if defined(OS_POSIX)
877
703 class ListenerThatExpectsFile : public IPC::Listener { 878 class ListenerThatExpectsFile : public IPC::Listener {
704 public: 879 public:
705 ListenerThatExpectsFile() : sender_(NULL) {} 880 ListenerThatExpectsFile() : sender_(NULL) {}
706 881
707 ~ListenerThatExpectsFile() override {} 882 ~ListenerThatExpectsFile() override {}
708 883
709 bool OnMessageReceived(const IPC::Message& message) override { 884 bool OnMessageReceived(const IPC::Message& message) override {
710 base::PickleIterator iter(message); 885 base::PickleIterator iter(message);
711 HandleSendingHelper::ReadReceivedFile(message, &iter); 886 HandleSendingHelper::ReadReceivedFile(message, &iter);
712 ListenerThatExpectsOK::SendOK(sender_); 887 ListenerThatExpectsOK::SendOK(sender_);
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
853 Connect(&listener); 1028 Connect(&listener);
854 1029
855 base::MessageLoop::current()->Run(); 1030 base::MessageLoop::current()->Run();
856 1031
857 Close(); 1032 Close();
858 } 1033 }
859 1034
860 #endif // OS_LINUX 1035 #endif // OS_LINUX
861 1036
862 } // namespace 1037 } // namespace
OLDNEW
« no previous file with comments | « ipc/ipc_channel_mojo.cc ('k') | ipc/ipc_channel_proxy.h » ('j') | ipc/ipc_channel_proxy.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698