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/edk/embedder/embedder.h" | 5 #include "mojo/edk/embedder/embedder.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
15 #include "base/test/test_io_thread.h" | 15 #include "base/test/test_io_thread.h" |
| 16 #include "base/test/test_timeouts.h" |
16 #include "mojo/edk/embedder/platform_channel_pair.h" | 17 #include "mojo/edk/embedder/platform_channel_pair.h" |
17 #include "mojo/edk/embedder/test_embedder.h" | 18 #include "mojo/edk/embedder/test_embedder.h" |
18 #include "mojo/edk/system/test_utils.h" | 19 #include "mojo/edk/system/test_utils.h" |
19 #include "mojo/edk/test/multiprocess_test_helper.h" | 20 #include "mojo/edk/test/multiprocess_test_helper.h" |
20 #include "mojo/public/c/system/core.h" | 21 #include "mojo/public/c/system/core.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
23 namespace mojo { | 24 namespace mojo { |
24 namespace embedder { | 25 namespace embedder { |
25 namespace { | 26 namespace { |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 // the server and client channels were completely created). | 154 // the server and client channels were completely created). |
154 server_channel.WaitForChannelCreationCompletion(); | 155 server_channel.WaitForChannelCreationCompletion(); |
155 client_channel.WaitForChannelCreationCompletion(); | 156 client_channel.WaitForChannelCreationCompletion(); |
156 EXPECT_TRUE(server_channel.channel_info()); | 157 EXPECT_TRUE(server_channel.channel_info()); |
157 EXPECT_TRUE(client_channel.channel_info()); | 158 EXPECT_TRUE(client_channel.channel_info()); |
158 } | 159 } |
159 | 160 |
160 EXPECT_TRUE(test::Shutdown()); | 161 EXPECT_TRUE(test::Shutdown()); |
161 } | 162 } |
162 | 163 |
| 164 class TestAsyncWaiter { |
| 165 public: |
| 166 TestAsyncWaiter() : event_(true, false), wait_result_(MOJO_RESULT_UNKNOWN) {} |
| 167 |
| 168 void Awake(MojoResult result) { |
| 169 base::AutoLock l(wait_result_lock_); |
| 170 wait_result_ = result; |
| 171 event_.Signal(); |
| 172 } |
| 173 |
| 174 bool TryWait() { return event_.TimedWait(TestTimeouts::action_timeout()); } |
| 175 |
| 176 MojoResult wait_result() const { |
| 177 base::AutoLock l(wait_result_lock_); |
| 178 return wait_result_; |
| 179 } |
| 180 |
| 181 private: |
| 182 base::WaitableEvent event_; |
| 183 |
| 184 mutable base::Lock wait_result_lock_; |
| 185 MojoResult wait_result_; |
| 186 |
| 187 DISALLOW_COPY_AND_ASSIGN(TestAsyncWaiter); |
| 188 }; |
| 189 |
| 190 void WriteHello(MessagePipeHandle pipe) { |
| 191 static const char kHello[] = "hello"; |
| 192 CHECK_EQ(MOJO_RESULT_OK, |
| 193 WriteMessageRaw(pipe, kHello, static_cast<uint32_t>(sizeof(kHello)), |
| 194 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 195 } |
| 196 |
| 197 void CloseScopedHandle(ScopedMessagePipeHandle handle) { |
| 198 // Do nothing and the destructor will close it. |
| 199 } |
| 200 |
| 201 TEST_F(EmbedderTest, AsyncWait) { |
| 202 mojo::embedder::test::InitWithSimplePlatformSupport(); |
| 203 |
| 204 { |
| 205 ScopedMessagePipeHandle client_mp; |
| 206 ScopedMessagePipeHandle server_mp; |
| 207 EXPECT_EQ(MOJO_RESULT_OK, |
| 208 mojo::CreateMessagePipe(nullptr, &client_mp, &server_mp)); |
| 209 |
| 210 TestAsyncWaiter waiter; |
| 211 EXPECT_EQ(MOJO_RESULT_OK, |
| 212 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE, |
| 213 base::Bind(&TestAsyncWaiter::Awake, |
| 214 base::Unretained(&waiter)))); |
| 215 |
| 216 test_io_thread()->task_runner()->PostTask( |
| 217 FROM_HERE, base::Bind(&WriteHello, server_mp.get())); |
| 218 EXPECT_TRUE(waiter.TryWait()); |
| 219 EXPECT_EQ(MOJO_RESULT_OK, waiter.wait_result()); |
| 220 |
| 221 // If message is in the queue, it does't allow us to wait. |
| 222 TestAsyncWaiter waiter_that_doesnt_wait; |
| 223 EXPECT_EQ( |
| 224 MOJO_RESULT_ALREADY_EXISTS, |
| 225 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE, |
| 226 base::Bind(&TestAsyncWaiter::Awake, |
| 227 base::Unretained(&waiter_that_doesnt_wait)))); |
| 228 |
| 229 char buffer[1000]; |
| 230 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
| 231 CHECK_EQ(MOJO_RESULT_OK, |
| 232 ReadMessageRaw(client_mp.get(), buffer, &num_bytes, nullptr, |
| 233 nullptr, MOJO_READ_MESSAGE_FLAG_NONE)); |
| 234 |
| 235 TestAsyncWaiter unsatisfiable_waiter; |
| 236 EXPECT_EQ(MOJO_RESULT_OK, |
| 237 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE, |
| 238 base::Bind(&TestAsyncWaiter::Awake, |
| 239 base::Unretained(&unsatisfiable_waiter)))); |
| 240 |
| 241 test_io_thread()->task_runner()->PostTask( |
| 242 FROM_HERE, |
| 243 base::Bind(&CloseScopedHandle, base::Passed(server_mp.Pass()))); |
| 244 |
| 245 EXPECT_TRUE(unsatisfiable_waiter.TryWait()); |
| 246 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 247 unsatisfiable_waiter.wait_result()); |
| 248 } |
| 249 |
| 250 EXPECT_TRUE(test::Shutdown()); |
| 251 } |
| 252 |
163 TEST_F(EmbedderTest, ChannelsHandlePassing) { | 253 TEST_F(EmbedderTest, ChannelsHandlePassing) { |
164 mojo::embedder::test::InitWithSimplePlatformSupport(); | 254 mojo::embedder::test::InitWithSimplePlatformSupport(); |
165 | 255 |
166 { | 256 { |
167 PlatformChannelPair channel_pair; | 257 PlatformChannelPair channel_pair; |
168 ScopedTestChannel server_channel(test_io_thread()->task_runner(), | 258 ScopedTestChannel server_channel(test_io_thread()->task_runner(), |
169 channel_pair.PassServerHandle()); | 259 channel_pair.PassServerHandle()); |
170 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | 260 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); |
171 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | 261 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); |
172 ScopedTestChannel client_channel(test_io_thread()->task_runner(), | 262 ScopedTestChannel client_channel(test_io_thread()->task_runner(), |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 | 635 |
546 EXPECT_TRUE(test::Shutdown()); | 636 EXPECT_TRUE(test::Shutdown()); |
547 } | 637 } |
548 | 638 |
549 // TODO(vtl): Test immediate write & close. | 639 // TODO(vtl): Test immediate write & close. |
550 // TODO(vtl): Test broken-connection cases. | 640 // TODO(vtl): Test broken-connection cases. |
551 | 641 |
552 } // namespace | 642 } // namespace |
553 } // namespace embedder | 643 } // namespace embedder |
554 } // namespace mojo | 644 } // namespace mojo |
OLD | NEW |