OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // TODO(vtl): Enable this on non-POSIX once we have a non-POSIX implementation. | 5 // TODO(vtl): Enable this on non-POSIX once we have a non-POSIX implementation. |
6 #include "build/build_config.h" | 6 #include "build/build_config.h" |
7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
8 | 8 |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/location.h" | 16 #include "base/location.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
20 #include "mojo/common/test/multiprocess_test_base.h" | 20 #include "mojo/common/test/multiprocess_test_base.h" |
21 #include "mojo/system/channel.h" | 21 #include "mojo/system/channel.h" |
22 #include "mojo/system/local_message_pipe_endpoint.h" | 22 #include "mojo/system/local_message_pipe_endpoint.h" |
23 #include "mojo/system/message_pipe.h" | 23 #include "mojo/system/message_pipe.h" |
24 #include "mojo/system/platform_channel.h" | |
25 #include "mojo/system/proxy_message_pipe_endpoint.h" | 24 #include "mojo/system/proxy_message_pipe_endpoint.h" |
| 25 #include "mojo/system/scoped_platform_handle.h" |
26 #include "mojo/system/test_utils.h" | 26 #include "mojo/system/test_utils.h" |
27 #include "mojo/system/waiter.h" | 27 #include "mojo/system/waiter.h" |
28 | 28 |
29 namespace mojo { | 29 namespace mojo { |
30 namespace system { | 30 namespace system { |
31 namespace { | 31 namespace { |
32 | 32 |
33 class IOThreadWrapper { | 33 class IOThreadWrapper { |
34 public: | 34 public: |
35 IOThreadWrapper() : io_thread_("io_thread") {} | 35 IOThreadWrapper() : io_thread_("io_thread") {} |
36 ~IOThreadWrapper() { | 36 ~IOThreadWrapper() { |
37 CHECK(!channel_.get()); | 37 CHECK(!channel_.get()); |
38 CHECK(!io_thread_.IsRunning()); | 38 CHECK(!io_thread_.IsRunning()); |
39 } | 39 } |
40 | 40 |
41 void PostTask(const tracked_objects::Location& from_here, | 41 void PostTask(const tracked_objects::Location& from_here, |
42 const base::Closure& task) { | 42 const base::Closure& task) { |
43 task_runner()->PostTask(from_here, task); | 43 task_runner()->PostTask(from_here, task); |
44 } | 44 } |
45 | 45 |
46 void PostTaskAndWait(const tracked_objects::Location& from_here, | 46 void PostTaskAndWait(const tracked_objects::Location& from_here, |
47 const base::Closure& task) { | 47 const base::Closure& task) { |
48 test::PostTaskAndWait(task_runner(), from_here, task); | 48 test::PostTaskAndWait(task_runner(), from_here, task); |
49 } | 49 } |
50 | 50 |
51 void Init(PlatformChannel* platform_channel, scoped_refptr<MessagePipe> mp) { | 51 void Init(ScopedPlatformHandle platform_handle, |
| 52 scoped_refptr<MessagePipe> mp) { |
52 io_thread_.StartWithOptions( | 53 io_thread_.StartWithOptions( |
53 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 54 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
54 PostTask(FROM_HERE, | 55 PostTask(FROM_HERE, |
55 base::Bind(&IOThreadWrapper::InitOnIOThread, | 56 base::Bind(&IOThreadWrapper::InitOnIOThread, |
56 base::Unretained(this), | 57 base::Unretained(this), |
57 platform_channel, mp)); | 58 base::Passed(&platform_handle), mp)); |
58 } | 59 } |
59 | 60 |
60 void Shutdown() { | 61 void Shutdown() { |
61 PostTaskAndWait(FROM_HERE, | 62 PostTaskAndWait(FROM_HERE, |
62 base::Bind(&IOThreadWrapper::ShutdownOnIOThread, | 63 base::Bind(&IOThreadWrapper::ShutdownOnIOThread, |
63 base::Unretained(this))); | 64 base::Unretained(this))); |
64 io_thread_.Stop(); | 65 io_thread_.Stop(); |
65 } | 66 } |
66 | 67 |
67 bool is_initialized() const { return !!channel_.get(); } | 68 bool is_initialized() const { return !!channel_.get(); } |
68 | 69 |
69 base::MessageLoop* message_loop() { | 70 base::MessageLoop* message_loop() { |
70 return io_thread_.message_loop(); | 71 return io_thread_.message_loop(); |
71 } | 72 } |
72 | 73 |
73 scoped_refptr<base::TaskRunner> task_runner() { | 74 scoped_refptr<base::TaskRunner> task_runner() { |
74 return message_loop()->message_loop_proxy(); | 75 return message_loop()->message_loop_proxy(); |
75 } | 76 } |
76 | 77 |
77 private: | 78 private: |
78 void InitOnIOThread(PlatformChannel* platform_channel, | 79 void InitOnIOThread(ScopedPlatformHandle platform_handle, |
79 scoped_refptr<MessagePipe> mp) { | 80 scoped_refptr<MessagePipe> mp) { |
80 CHECK_EQ(base::MessageLoop::current(), message_loop()); | 81 CHECK_EQ(base::MessageLoop::current(), message_loop()); |
81 CHECK(platform_channel); | 82 CHECK(platform_handle.is_valid()); |
82 CHECK(platform_channel->is_valid()); | |
83 | 83 |
84 // Create and initialize |Channel|. | 84 // Create and initialize |Channel|. |
85 channel_ = new Channel(); | 85 channel_ = new Channel(); |
86 CHECK(channel_->Init(platform_channel->PassHandle())); | 86 CHECK(channel_->Init(platform_handle.Pass())); |
87 | 87 |
88 // Attach the message pipe endpoint. | 88 // Attach the message pipe endpoint. |
89 // Note: On the "server" (parent process) side, we need not attach the | 89 // Note: On the "server" (parent process) side, we need not attach the |
90 // message pipe endpoint immediately. However, on the "client" (child | 90 // message pipe endpoint immediately. However, on the "client" (child |
91 // process) side, this *must* be done here -- otherwise, the |Channel| may | 91 // process) side, this *must* be done here -- otherwise, the |Channel| may |
92 // receive/process messages (which it can do as soon as it's hooked up to | 92 // receive/process messages (which it can do as soon as it's hooked up to |
93 // the IO thread message loop, and that message loop runs) before the | 93 // the IO thread message loop, and that message loop runs) before the |
94 // message pipe endpoint is attached. | 94 // message pipe endpoint is attached. |
95 CHECK_EQ(channel_->AttachMessagePipeEndpoint(mp, 1), | 95 CHECK_EQ(channel_->AttachMessagePipeEndpoint(mp, 1), |
96 Channel::kBootstrapEndpointId); | 96 Channel::kBootstrapEndpointId); |
(...skipping 18 matching lines...) Expand all Loading... |
115 MultiprocessMessagePipeTest() {} | 115 MultiprocessMessagePipeTest() {} |
116 virtual ~MultiprocessMessagePipeTest() {} | 116 virtual ~MultiprocessMessagePipeTest() {} |
117 | 117 |
118 virtual void TearDown() OVERRIDE { | 118 virtual void TearDown() OVERRIDE { |
119 if (io_thread_wrapper_.is_initialized()) | 119 if (io_thread_wrapper_.is_initialized()) |
120 io_thread_wrapper_.Shutdown(); | 120 io_thread_wrapper_.Shutdown(); |
121 mojo::test::MultiprocessTestBase::TearDown(); | 121 mojo::test::MultiprocessTestBase::TearDown(); |
122 } | 122 } |
123 | 123 |
124 void Init(scoped_refptr<MessagePipe> mp) { | 124 void Init(scoped_refptr<MessagePipe> mp) { |
125 io_thread_wrapper_.Init(server_platform_channel.get(), mp); | 125 io_thread_wrapper_.Init(server_platform_handle.Pass(), mp); |
126 } | 126 } |
127 | 127 |
128 private: | 128 private: |
129 IOThreadWrapper io_thread_wrapper_; | 129 IOThreadWrapper io_thread_wrapper_; |
130 | 130 |
131 DISALLOW_COPY_AND_ASSIGN(MultiprocessMessagePipeTest); | 131 DISALLOW_COPY_AND_ASSIGN(MultiprocessMessagePipeTest); |
132 }; | 132 }; |
133 | 133 |
134 MojoResult WaitIfNecessary(scoped_refptr<MessagePipe> mp, MojoWaitFlags flags) { | 134 MojoResult WaitIfNecessary(scoped_refptr<MessagePipe> mp, MojoWaitFlags flags) { |
135 Waiter waiter; | 135 Waiter waiter; |
136 waiter.Init(); | 136 waiter.Init(); |
137 | 137 |
138 MojoResult add_result = mp->AddWaiter(0, &waiter, flags, MOJO_RESULT_OK); | 138 MojoResult add_result = mp->AddWaiter(0, &waiter, flags, MOJO_RESULT_OK); |
139 if (add_result != MOJO_RESULT_OK) { | 139 if (add_result != MOJO_RESULT_OK) { |
140 return (add_result == MOJO_RESULT_ALREADY_EXISTS) ? MOJO_RESULT_OK : | 140 return (add_result == MOJO_RESULT_ALREADY_EXISTS) ? MOJO_RESULT_OK : |
141 add_result; | 141 add_result; |
142 } | 142 } |
143 | 143 |
144 MojoResult wait_result = waiter.Wait(MOJO_DEADLINE_INDEFINITE); | 144 MojoResult wait_result = waiter.Wait(MOJO_DEADLINE_INDEFINITE); |
145 mp->RemoveWaiter(0, &waiter); | 145 mp->RemoveWaiter(0, &waiter); |
146 return wait_result; | 146 return wait_result; |
147 } | 147 } |
148 | 148 |
149 // For each message received, sends a reply message with the same contents | 149 // For each message received, sends a reply message with the same contents |
150 // repeated twice, until the other end is closed or it receives "quitquitquit" | 150 // repeated twice, until the other end is closed or it receives "quitquitquit" |
151 // (which it doesn't reply to). It'll return the number of messages received, | 151 // (which it doesn't reply to). It'll return the number of messages received, |
152 // not including any "quitquitquit" message, modulo 100. | 152 // not including any "quitquitquit" message, modulo 100. |
153 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(EchoEcho) { | 153 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(EchoEcho) { |
154 IOThreadWrapper io_thread_wrapper; | 154 IOThreadWrapper io_thread_wrapper; |
155 PlatformChannel* const client_platform_channel = | 155 ScopedPlatformHandle client_platform_handle = |
156 MultiprocessMessagePipeTest::client_platform_channel.get(); | 156 MultiprocessMessagePipeTest::client_platform_handle.Pass(); |
157 CHECK(client_platform_channel); | 157 CHECK(client_platform_handle.is_valid()); |
158 CHECK(client_platform_channel->is_valid()); | |
159 scoped_refptr<MessagePipe> mp(new MessagePipe( | 158 scoped_refptr<MessagePipe> mp(new MessagePipe( |
160 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), | 159 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), |
161 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); | 160 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); |
162 io_thread_wrapper.Init(client_platform_channel, mp); | 161 io_thread_wrapper.Init(client_platform_handle.Pass(), mp); |
163 | 162 |
164 const std::string quitquitquit("quitquitquit"); | 163 const std::string quitquitquit("quitquitquit"); |
165 int rv = 0; | 164 int rv = 0; |
166 for (;; rv = (rv + 1) % 100) { | 165 for (;; rv = (rv + 1) % 100) { |
167 // Wait for our end of the message pipe to be readable. | 166 // Wait for our end of the message pipe to be readable. |
168 MojoResult result = WaitIfNecessary(mp, MOJO_WAIT_FLAG_READABLE); | 167 MojoResult result = WaitIfNecessary(mp, MOJO_WAIT_FLAG_READABLE); |
169 if (result != MOJO_RESULT_OK) { | 168 if (result != MOJO_RESULT_OK) { |
170 // It was closed, probably. | 169 // It was closed, probably. |
171 CHECK_EQ(result, MOJO_RESULT_FAILED_PRECONDITION); | 170 CHECK_EQ(result, MOJO_RESULT_FAILED_PRECONDITION); |
172 break; | 171 break; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 mp->Close(0); | 288 mp->Close(0); |
290 | 289 |
291 EXPECT_EQ(static_cast<int>(kNumMessages % 100), WaitForChildShutdown()); | 290 EXPECT_EQ(static_cast<int>(kNumMessages % 100), WaitForChildShutdown()); |
292 } | 291 } |
293 | 292 |
294 } // namespace | 293 } // namespace |
295 } // namespace system | 294 } // namespace system |
296 } // namespace mojo | 295 } // namespace mojo |
297 | 296 |
298 #endif // defined(OS_POSIX) | 297 #endif // defined(OS_POSIX) |
OLD | NEW |