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 "mojo/embedder/platform_channel_pair.h" | 10 #include "mojo/embedder/platform_channel_pair.h" |
| 11 #include "mojo/system/local_message_pipe_endpoint.h" |
| 12 #include "mojo/system/message_in_transit.h" |
| 13 #include "mojo/system/message_pipe.h" |
| 14 #include "mojo/system/proxy_message_pipe_endpoint.h" |
11 #include "mojo/system/raw_channel.h" | 15 #include "mojo/system/raw_channel.h" |
12 #include "mojo/system/test_utils.h" | 16 #include "mojo/system/test_utils.h" |
| 17 #include "mojo/system/waiter.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
14 | 19 |
15 namespace mojo { | 20 namespace mojo { |
16 namespace system { | 21 namespace system { |
17 namespace { | 22 namespace { |
18 | 23 |
19 enum Tristate { | 24 enum Tristate { |
20 TRISTATE_UNKNOWN = -1, | 25 TRISTATE_UNKNOWN = -1, |
21 TRISTATE_FALSE = 0, | 26 TRISTATE_FALSE = 0, |
22 TRISTATE_TRUE = 1 | 27 TRISTATE_TRUE = 1 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 FROM_HERE, | 170 FROM_HERE, |
166 base::Bind(&ChannelTest::InitChannelOnIOThread, | 171 base::Bind(&ChannelTest::InitChannelOnIOThread, |
167 base::Unretained(this))); | 172 base::Unretained(this))); |
168 EXPECT_EQ(TRISTATE_FALSE, init_result()); | 173 EXPECT_EQ(TRISTATE_FALSE, init_result()); |
169 | 174 |
170 // Should destroy |Channel| with no |Shutdown()| (on not-the-I/O-thread). | 175 // Should destroy |Channel| with no |Shutdown()| (on not-the-I/O-thread). |
171 EXPECT_TRUE(channel()->HasOneRef()); | 176 EXPECT_TRUE(channel()->HasOneRef()); |
172 *mutable_channel() = NULL; | 177 *mutable_channel() = NULL; |
173 } | 178 } |
174 | 179 |
175 // TODO(vtl): More. | 180 // ChannelTest.CloseBeforeRun -------------------------------------------------- |
| 181 |
| 182 TEST_F(ChannelTest, CloseBeforeRun) { |
| 183 io_thread()->PostTaskAndWait( |
| 184 FROM_HERE, |
| 185 base::Bind(&ChannelTest::CreateChannelOnIOThread, |
| 186 base::Unretained(this))); |
| 187 ASSERT_TRUE(channel()); |
| 188 |
| 189 io_thread()->PostTaskAndWait( |
| 190 FROM_HERE, |
| 191 base::Bind(&ChannelTest::InitChannelOnIOThread, |
| 192 base::Unretained(this))); |
| 193 EXPECT_EQ(TRISTATE_TRUE, init_result()); |
| 194 |
| 195 scoped_refptr<MessagePipe> mp(new MessagePipe( |
| 196 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), |
| 197 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); |
| 198 |
| 199 MessageInTransit::EndpointId local_id = |
| 200 channel()->AttachMessagePipeEndpoint(mp, 1); |
| 201 EXPECT_EQ(Channel::kBootstrapEndpointId, local_id); |
| 202 |
| 203 mp->Close(0); |
| 204 |
| 205 // TODO(vtl): Currently, the |Close()| above won't detach (since it thinks |
| 206 // we're still expecting a "run" message from the other side), so the |
| 207 // |RunMessagePipeEndpoint()| below will return true. We need to refactor |
| 208 // |AttachMessagePipeEndpoint()| to indicate whether |Run...()| will |
| 209 // necessarily be called or not. (Then, in the case that it may not be called, |
| 210 // this will return false.) |
| 211 EXPECT_TRUE(channel()->RunMessagePipeEndpoint(local_id, |
| 212 Channel::kBootstrapEndpointId)); |
| 213 |
| 214 io_thread()->PostTaskAndWait( |
| 215 FROM_HERE, |
| 216 base::Bind(&ChannelTest::ShutdownChannelOnIOThread, |
| 217 base::Unretained(this))); |
| 218 |
| 219 EXPECT_TRUE(channel()->HasOneRef()); |
| 220 } |
| 221 |
| 222 // ChannelTest.ShutdownAfterAttachAndRun --------------------------------------- |
| 223 |
| 224 TEST_F(ChannelTest, ShutdownAfterAttach) { |
| 225 io_thread()->PostTaskAndWait( |
| 226 FROM_HERE, |
| 227 base::Bind(&ChannelTest::CreateChannelOnIOThread, |
| 228 base::Unretained(this))); |
| 229 ASSERT_TRUE(channel()); |
| 230 |
| 231 io_thread()->PostTaskAndWait( |
| 232 FROM_HERE, |
| 233 base::Bind(&ChannelTest::InitChannelOnIOThread, |
| 234 base::Unretained(this))); |
| 235 EXPECT_EQ(TRISTATE_TRUE, init_result()); |
| 236 |
| 237 scoped_refptr<MessagePipe> mp(new MessagePipe( |
| 238 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), |
| 239 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); |
| 240 |
| 241 MessageInTransit::EndpointId local_id = |
| 242 channel()->AttachMessagePipeEndpoint(mp, 1); |
| 243 EXPECT_EQ(Channel::kBootstrapEndpointId, local_id); |
| 244 |
| 245 // TODO(vtl): Currently, we always "expect" a |RunMessagePipeEndpoint()| after |
| 246 // an |AttachMessagePipeEndpoint()| (which is actually incorrect). We need to |
| 247 // refactor |AttachMessagePipeEndpoint()| to indicate whether |Run...()| will |
| 248 // necessarily be called or not. (Then, in the case that it may not be called, |
| 249 // we should test a |Shutdown()| without the |Run...()|.) |
| 250 EXPECT_TRUE(channel()->RunMessagePipeEndpoint(local_id, |
| 251 Channel::kBootstrapEndpointId)); |
| 252 |
| 253 Waiter waiter; |
| 254 waiter.Init(); |
| 255 EXPECT_EQ(MOJO_RESULT_OK, |
| 256 mp->AddWaiter(0, &waiter, MOJO_WAIT_FLAG_READABLE, 123)); |
| 257 |
| 258 // Don't wait for the shutdown to run ... |
| 259 io_thread()->PostTask(FROM_HERE, |
| 260 base::Bind(&ChannelTest::ShutdownChannelOnIOThread, |
| 261 base::Unretained(this))); |
| 262 |
| 263 // ... since this |Wait()| should fail once the channel is shut down. |
| 264 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 265 waiter.Wait(MOJO_DEADLINE_INDEFINITE)); |
| 266 mp->RemoveWaiter(0, &waiter); |
| 267 |
| 268 mp->Close(0); |
| 269 |
| 270 EXPECT_TRUE(channel()->HasOneRef()); |
| 271 } |
| 272 |
| 273 // ChannelTest.WaitAfterAttachRunAndShutdown ----------------------------------- |
| 274 |
| 275 TEST_F(ChannelTest, WaitAfterAttachRunAndShutdown) { |
| 276 io_thread()->PostTaskAndWait( |
| 277 FROM_HERE, |
| 278 base::Bind(&ChannelTest::CreateChannelOnIOThread, |
| 279 base::Unretained(this))); |
| 280 ASSERT_TRUE(channel()); |
| 281 |
| 282 io_thread()->PostTaskAndWait( |
| 283 FROM_HERE, |
| 284 base::Bind(&ChannelTest::InitChannelOnIOThread, |
| 285 base::Unretained(this))); |
| 286 EXPECT_EQ(TRISTATE_TRUE, init_result()); |
| 287 |
| 288 scoped_refptr<MessagePipe> mp(new MessagePipe( |
| 289 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), |
| 290 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); |
| 291 |
| 292 MessageInTransit::EndpointId local_id = |
| 293 channel()->AttachMessagePipeEndpoint(mp, 1); |
| 294 EXPECT_EQ(Channel::kBootstrapEndpointId, local_id); |
| 295 |
| 296 EXPECT_TRUE(channel()->RunMessagePipeEndpoint(local_id, |
| 297 Channel::kBootstrapEndpointId)); |
| 298 |
| 299 io_thread()->PostTaskAndWait( |
| 300 FROM_HERE, |
| 301 base::Bind(&ChannelTest::ShutdownChannelOnIOThread, |
| 302 base::Unretained(this))); |
| 303 |
| 304 Waiter waiter; |
| 305 waiter.Init(); |
| 306 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 307 mp->AddWaiter(0, &waiter, MOJO_WAIT_FLAG_READABLE, 123)); |
| 308 |
| 309 mp->Close(0); |
| 310 |
| 311 EXPECT_TRUE(channel()->HasOneRef()); |
| 312 } |
| 313 |
| 314 // TODO(vtl): More. ------------------------------------------------------------ |
176 | 315 |
177 } // namespace | 316 } // namespace |
178 } // namespace system | 317 } // namespace system |
179 } // namespace mojo | 318 } // namespace mojo |
OLD | NEW |