OLD | NEW |
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/system/channel.h" | 5 #include "mojo/system/channel.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/test/test_io_thread.h" | 10 #include "base/test/test_io_thread.h" |
11 #include "mojo/embedder/platform_channel_pair.h" | 11 #include "mojo/embedder/platform_channel_pair.h" |
12 #include "mojo/embedder/simple_platform_support.h" | 12 #include "mojo/embedder/simple_platform_support.h" |
| 13 #include "mojo/system/channel_endpoint.h" |
13 #include "mojo/system/message_in_transit.h" | 14 #include "mojo/system/message_in_transit.h" |
14 #include "mojo/system/message_pipe.h" | 15 #include "mojo/system/message_pipe.h" |
15 #include "mojo/system/raw_channel.h" | 16 #include "mojo/system/raw_channel.h" |
16 #include "mojo/system/test_utils.h" | 17 #include "mojo/system/test_utils.h" |
17 #include "mojo/system/waiter.h" | 18 #include "mojo/system/waiter.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace mojo { | 21 namespace mojo { |
21 namespace system { | 22 namespace system { |
22 namespace { | 23 namespace { |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 base::Unretained(this))); | 189 base::Unretained(this))); |
189 ASSERT_TRUE(channel()); | 190 ASSERT_TRUE(channel()); |
190 | 191 |
191 io_thread()->PostTaskAndWait( | 192 io_thread()->PostTaskAndWait( |
192 FROM_HERE, | 193 FROM_HERE, |
193 base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); | 194 base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); |
194 EXPECT_EQ(TRISTATE_TRUE, init_result()); | 195 EXPECT_EQ(TRISTATE_TRUE, init_result()); |
195 | 196 |
196 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); | 197 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); |
197 | 198 |
198 MessageInTransit::EndpointId local_id = | 199 MessageInTransit::EndpointId local_id = channel()->AttachEndpoint( |
199 channel()->AttachMessagePipeEndpoint(mp, 1); | 200 make_scoped_refptr(new ChannelEndpoint(mp.get(), 1))); |
200 EXPECT_EQ(Channel::kBootstrapEndpointId, local_id); | 201 EXPECT_EQ(Channel::kBootstrapEndpointId, local_id); |
201 | 202 |
202 mp->Close(0); | 203 mp->Close(0); |
203 | 204 |
204 // TODO(vtl): Currently, the |Close()| above won't detach (since it thinks | 205 // TODO(vtl): Currently, the |Close()| above won't detach (since it thinks |
205 // we're still expecting a "run" message from the other side), so the | 206 // we're still expecting a "run" message from the other side), so the |
206 // |RunMessagePipeEndpoint()| below will return true. We need to refactor | 207 // |RunMessagePipeEndpoint()| below will return true. We need to refactor |
207 // |AttachMessagePipeEndpoint()| to indicate whether |Run...()| will | 208 // |AttachEndpoint()| to indicate whether |Run...()| will necessarily be |
208 // necessarily be called or not. (Then, in the case that it may not be called, | 209 // called or not. (Then, in the case that it may not be called, this will |
209 // this will return false.) | 210 // return false.) |
210 EXPECT_TRUE(channel()->RunMessagePipeEndpoint(local_id, | 211 EXPECT_TRUE(channel()->RunMessagePipeEndpoint(local_id, |
211 Channel::kBootstrapEndpointId)); | 212 Channel::kBootstrapEndpointId)); |
212 | 213 |
213 io_thread()->PostTaskAndWait( | 214 io_thread()->PostTaskAndWait( |
214 FROM_HERE, | 215 FROM_HERE, |
215 base::Bind(&ChannelTest::ShutdownChannelOnIOThread, | 216 base::Bind(&ChannelTest::ShutdownChannelOnIOThread, |
216 base::Unretained(this))); | 217 base::Unretained(this))); |
217 | 218 |
218 EXPECT_TRUE(channel()->HasOneRef()); | 219 EXPECT_TRUE(channel()->HasOneRef()); |
219 } | 220 } |
220 | 221 |
221 // ChannelTest.ShutdownAfterAttachAndRun --------------------------------------- | 222 // ChannelTest.ShutdownAfterAttachAndRun --------------------------------------- |
222 | 223 |
223 TEST_F(ChannelTest, ShutdownAfterAttach) { | 224 TEST_F(ChannelTest, ShutdownAfterAttach) { |
224 io_thread()->PostTaskAndWait(FROM_HERE, | 225 io_thread()->PostTaskAndWait(FROM_HERE, |
225 base::Bind(&ChannelTest::CreateChannelOnIOThread, | 226 base::Bind(&ChannelTest::CreateChannelOnIOThread, |
226 base::Unretained(this))); | 227 base::Unretained(this))); |
227 ASSERT_TRUE(channel()); | 228 ASSERT_TRUE(channel()); |
228 | 229 |
229 io_thread()->PostTaskAndWait( | 230 io_thread()->PostTaskAndWait( |
230 FROM_HERE, | 231 FROM_HERE, |
231 base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); | 232 base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); |
232 EXPECT_EQ(TRISTATE_TRUE, init_result()); | 233 EXPECT_EQ(TRISTATE_TRUE, init_result()); |
233 | 234 |
234 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); | 235 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); |
235 | 236 |
236 MessageInTransit::EndpointId local_id = | 237 MessageInTransit::EndpointId local_id = channel()->AttachEndpoint( |
237 channel()->AttachMessagePipeEndpoint(mp, 1); | 238 make_scoped_refptr(new ChannelEndpoint(mp.get(), 1))); |
238 EXPECT_EQ(Channel::kBootstrapEndpointId, local_id); | 239 EXPECT_EQ(Channel::kBootstrapEndpointId, local_id); |
239 | 240 |
240 // TODO(vtl): Currently, we always "expect" a |RunMessagePipeEndpoint()| after | 241 // TODO(vtl): Currently, we always "expect" a |RunMessagePipeEndpoint()| after |
241 // an |AttachMessagePipeEndpoint()| (which is actually incorrect). We need to | 242 // an |AttachEndpoint()| (which is actually incorrect). We need to refactor |
242 // refactor |AttachMessagePipeEndpoint()| to indicate whether |Run...()| will | 243 // |AttachEndpoint()| to indicate whether |Run...()| will necessarily be |
243 // necessarily be called or not. (Then, in the case that it may not be called, | 244 // called or not. (Then, in the case that it may not be called, we should test |
244 // we should test a |Shutdown()| without the |Run...()|.) | 245 // a |Shutdown()| without the |Run...()|.) |
245 EXPECT_TRUE(channel()->RunMessagePipeEndpoint(local_id, | 246 EXPECT_TRUE(channel()->RunMessagePipeEndpoint(local_id, |
246 Channel::kBootstrapEndpointId)); | 247 Channel::kBootstrapEndpointId)); |
247 | 248 |
248 Waiter waiter; | 249 Waiter waiter; |
249 waiter.Init(); | 250 waiter.Init(); |
250 ASSERT_EQ(MOJO_RESULT_OK, | 251 ASSERT_EQ(MOJO_RESULT_OK, |
251 mp->AddWaiter(0, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, NULL)); | 252 mp->AddWaiter(0, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, NULL)); |
252 | 253 |
253 // Don't wait for the shutdown to run ... | 254 // Don't wait for the shutdown to run ... |
254 io_thread()->PostTask(FROM_HERE, | 255 io_thread()->PostTask(FROM_HERE, |
(...skipping 21 matching lines...) Expand all Loading... |
276 base::Unretained(this))); | 277 base::Unretained(this))); |
277 ASSERT_TRUE(channel()); | 278 ASSERT_TRUE(channel()); |
278 | 279 |
279 io_thread()->PostTaskAndWait( | 280 io_thread()->PostTaskAndWait( |
280 FROM_HERE, | 281 FROM_HERE, |
281 base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); | 282 base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); |
282 EXPECT_EQ(TRISTATE_TRUE, init_result()); | 283 EXPECT_EQ(TRISTATE_TRUE, init_result()); |
283 | 284 |
284 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); | 285 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy()); |
285 | 286 |
286 MessageInTransit::EndpointId local_id = | 287 MessageInTransit::EndpointId local_id = channel()->AttachEndpoint( |
287 channel()->AttachMessagePipeEndpoint(mp, 1); | 288 make_scoped_refptr(new ChannelEndpoint(mp.get(), 1))); |
288 EXPECT_EQ(Channel::kBootstrapEndpointId, local_id); | 289 EXPECT_EQ(Channel::kBootstrapEndpointId, local_id); |
289 | 290 |
290 EXPECT_TRUE(channel()->RunMessagePipeEndpoint(local_id, | 291 EXPECT_TRUE(channel()->RunMessagePipeEndpoint(local_id, |
291 Channel::kBootstrapEndpointId)); | 292 Channel::kBootstrapEndpointId)); |
292 | 293 |
293 io_thread()->PostTaskAndWait( | 294 io_thread()->PostTaskAndWait( |
294 FROM_HERE, | 295 FROM_HERE, |
295 base::Bind(&ChannelTest::ShutdownChannelOnIOThread, | 296 base::Bind(&ChannelTest::ShutdownChannelOnIOThread, |
296 base::Unretained(this))); | 297 base::Unretained(this))); |
297 | 298 |
298 Waiter waiter; | 299 Waiter waiter; |
299 waiter.Init(); | 300 waiter.Init(); |
300 HandleSignalsState hss; | 301 HandleSignalsState hss; |
301 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 302 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
302 mp->AddWaiter(0, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, &hss)); | 303 mp->AddWaiter(0, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, &hss)); |
303 EXPECT_EQ(0u, hss.satisfied_signals); | 304 EXPECT_EQ(0u, hss.satisfied_signals); |
304 EXPECT_EQ(0u, hss.satisfiable_signals); | 305 EXPECT_EQ(0u, hss.satisfiable_signals); |
305 | 306 |
306 mp->Close(0); | 307 mp->Close(0); |
307 | 308 |
308 EXPECT_TRUE(channel()->HasOneRef()); | 309 EXPECT_TRUE(channel()->HasOneRef()); |
309 } | 310 } |
310 | 311 |
311 // TODO(vtl): More. ------------------------------------------------------------ | 312 // TODO(vtl): More. ------------------------------------------------------------ |
312 | 313 |
313 } // namespace | 314 } // namespace |
314 } // namespace system | 315 } // namespace system |
315 } // namespace mojo | 316 } // namespace mojo |
OLD | NEW |