OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "third_party/mojo/src/mojo/edk/system/wait_set_dispatcher.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "mojo/public/cpp/system/macros.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/mojo/src/mojo/edk/system/message_pipe.h" |
| 13 #include "third_party/mojo/src/mojo/edk/system/message_pipe_dispatcher.h" |
| 14 #include "third_party/mojo/src/mojo/edk/system/test_utils.h" |
| 15 #include "third_party/mojo/src/mojo/edk/system/waiter.h" |
| 16 |
| 17 namespace mojo { |
| 18 namespace system { |
| 19 namespace { |
| 20 |
| 21 class WaitSetDispatcherTest : public ::testing::Test { |
| 22 public: |
| 23 WaitSetDispatcherTest() {} |
| 24 ~WaitSetDispatcherTest() override {} |
| 25 |
| 26 void SetUp() override { |
| 27 dispatcher0_ = MessagePipeDispatcher::Create( |
| 28 MessagePipeDispatcher::kDefaultCreateOptions); |
| 29 dispatcher1_ = MessagePipeDispatcher::Create( |
| 30 MessagePipeDispatcher::kDefaultCreateOptions); |
| 31 |
| 32 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalLocal()); |
| 33 dispatcher0_->Init(mp, 0); |
| 34 dispatcher1_->Init(mp, 1); |
| 35 |
| 36 dispatchers_to_close_.push_back(dispatcher0_); |
| 37 dispatchers_to_close_.push_back(dispatcher1_); |
| 38 } |
| 39 |
| 40 void TearDown() override { |
| 41 for (auto& d : dispatchers_to_close_) |
| 42 d->Close(); |
| 43 } |
| 44 |
| 45 MojoResult GetOneReadyDispatcher( |
| 46 const scoped_refptr<WaitSetDispatcher>& wait_set, |
| 47 scoped_refptr<Dispatcher>* ready_dispatcher, |
| 48 uintptr_t* context) { |
| 49 uint32_t count = 1; |
| 50 MojoResult dispatcher_result = MOJO_RESULT_UNKNOWN; |
| 51 DispatcherVector dispatchers; |
| 52 MojoResult result = wait_set->GetReadyDispatchers( |
| 53 MakeUserPointer(&count), |
| 54 &dispatchers, |
| 55 MakeUserPointer(&dispatcher_result), |
| 56 MakeUserPointer(context)); |
| 57 if (result == MOJO_RESULT_OK) { |
| 58 CHECK_EQ(1u, dispatchers.size()); |
| 59 *ready_dispatcher = dispatchers[0]; |
| 60 return dispatcher_result; |
| 61 } |
| 62 return result; |
| 63 } |
| 64 |
| 65 void CloseOnShutdown(const scoped_refptr<Dispatcher>& dispatcher) { |
| 66 dispatchers_to_close_.push_back(dispatcher); |
| 67 } |
| 68 |
| 69 protected: |
| 70 scoped_refptr<MessagePipeDispatcher> dispatcher0_; |
| 71 scoped_refptr<MessagePipeDispatcher> dispatcher1_; |
| 72 |
| 73 DispatcherVector dispatchers_to_close_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(WaitSetDispatcherTest); |
| 76 }; |
| 77 |
| 78 TEST_F(WaitSetDispatcherTest, Basic) { |
| 79 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher(); |
| 80 CloseOnShutdown(wait_set); |
| 81 ASSERT_EQ(MOJO_RESULT_OK, |
| 82 wait_set->AddWaitingDispatcher(dispatcher0_, |
| 83 MOJO_HANDLE_SIGNAL_READABLE, 1)); |
| 84 ASSERT_EQ(MOJO_RESULT_OK, |
| 85 wait_set->AddWaitingDispatcher(dispatcher1_, |
| 86 MOJO_HANDLE_SIGNAL_WRITABLE, 2)); |
| 87 |
| 88 Waiter w; |
| 89 uintptr_t context = 0; |
| 90 w.Init(); |
| 91 HandleSignalsState hss; |
| 92 // |dispatcher1_| should already be writable. |
| 93 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 94 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); |
| 95 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
| 96 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
| 97 |
| 98 scoped_refptr<Dispatcher> woken_dispatcher; |
| 99 EXPECT_EQ(MOJO_RESULT_OK, |
| 100 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context)); |
| 101 EXPECT_EQ(dispatcher1_, woken_dispatcher); |
| 102 EXPECT_EQ(2u, context); |
| 103 // If a ready dispatcher isn't removed, it will continue to be returned. |
| 104 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 105 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); |
| 106 woken_dispatcher = nullptr; |
| 107 context = 0; |
| 108 EXPECT_EQ(MOJO_RESULT_OK, |
| 109 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context)); |
| 110 EXPECT_EQ(dispatcher1_, woken_dispatcher); |
| 111 EXPECT_EQ(2u, context); |
| 112 ASSERT_EQ(MOJO_RESULT_OK, wait_set->RemoveWaitingDispatcher(dispatcher1_)); |
| 113 |
| 114 // No ready dispatcher. |
| 115 hss = HandleSignalsState(); |
| 116 EXPECT_EQ(MOJO_RESULT_OK, |
| 117 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); |
| 118 EXPECT_FALSE(hss.satisfies(MOJO_HANDLE_SIGNAL_READABLE)); |
| 119 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0, nullptr)); |
| 120 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, |
| 121 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); |
| 122 |
| 123 // Write to |dispatcher1_|, which should make |dispatcher0_| readable. |
| 124 char buffer[] = "abcd"; |
| 125 w.Init(); |
| 126 ASSERT_EQ(MOJO_RESULT_OK, |
| 127 dispatcher1_->WriteMessage(UserPointer<const void>(buffer), |
| 128 sizeof(buffer), nullptr, |
| 129 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 130 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(0, nullptr)); |
| 131 woken_dispatcher = nullptr; |
| 132 context = 0; |
| 133 EXPECT_EQ(MOJO_RESULT_OK, |
| 134 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context)); |
| 135 EXPECT_EQ(dispatcher0_, woken_dispatcher); |
| 136 EXPECT_EQ(1u, context); |
| 137 |
| 138 // Again, if a ready dispatcher isn't removed, it will continue to be |
| 139 // returned. |
| 140 woken_dispatcher = nullptr; |
| 141 EXPECT_EQ(MOJO_RESULT_OK, |
| 142 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); |
| 143 EXPECT_EQ(dispatcher0_, woken_dispatcher); |
| 144 |
| 145 wait_set->RemoveAwakable(&w, nullptr); |
| 146 } |
| 147 |
| 148 TEST_F(WaitSetDispatcherTest, HandleWithoutRemoving) { |
| 149 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher(); |
| 150 CloseOnShutdown(wait_set); |
| 151 ASSERT_EQ(MOJO_RESULT_OK, |
| 152 wait_set->AddWaitingDispatcher(dispatcher0_, |
| 153 MOJO_HANDLE_SIGNAL_READABLE, 1)); |
| 154 |
| 155 Waiter w; |
| 156 uintptr_t context = 0; |
| 157 w.Init(); |
| 158 HandleSignalsState hss; |
| 159 // No ready dispatcher. |
| 160 hss = HandleSignalsState(); |
| 161 EXPECT_EQ(MOJO_RESULT_OK, |
| 162 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); |
| 163 EXPECT_FALSE(hss.satisfies(MOJO_HANDLE_SIGNAL_READABLE)); |
| 164 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0, nullptr)); |
| 165 scoped_refptr<Dispatcher> woken_dispatcher; |
| 166 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, |
| 167 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); |
| 168 |
| 169 // The tested behaviour below should be repeatable. |
| 170 for (size_t i = 0; i < 3; i++) { |
| 171 // Write to |dispatcher1_|, which should make |dispatcher0_| readable. |
| 172 char buffer[] = "abcd"; |
| 173 w.Init(); |
| 174 ASSERT_EQ(MOJO_RESULT_OK, |
| 175 dispatcher1_->WriteMessage(UserPointer<const void>(buffer), |
| 176 sizeof(buffer), nullptr, |
| 177 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 178 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(0, nullptr)); |
| 179 woken_dispatcher = nullptr; |
| 180 context = 0; |
| 181 EXPECT_EQ(MOJO_RESULT_OK, |
| 182 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context)); |
| 183 EXPECT_EQ(dispatcher0_, woken_dispatcher); |
| 184 EXPECT_EQ(1u, context); |
| 185 |
| 186 // Read from |dispatcher0_| which should change it's state to non-readable. |
| 187 char read_buffer[sizeof(buffer) + 5]; |
| 188 uint32_t num_bytes = sizeof(read_buffer); |
| 189 ASSERT_EQ(MOJO_RESULT_OK, |
| 190 dispatcher0_->ReadMessage(UserPointer<void>(read_buffer), |
| 191 MakeUserPointer(&num_bytes), |
| 192 nullptr, nullptr, |
| 193 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 194 EXPECT_EQ(sizeof(buffer), num_bytes); |
| 195 |
| 196 // No dispatchers are ready. |
| 197 w.Init(); |
| 198 woken_dispatcher = nullptr; |
| 199 context = 0; |
| 200 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, |
| 201 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context)); |
| 202 EXPECT_EQ(nullptr, woken_dispatcher); |
| 203 EXPECT_EQ(0u, context); |
| 204 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0, nullptr)); |
| 205 } |
| 206 |
| 207 wait_set->RemoveAwakable(&w, nullptr); |
| 208 } |
| 209 |
| 210 TEST_F(WaitSetDispatcherTest, MultipleReady) { |
| 211 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher(); |
| 212 CloseOnShutdown(wait_set); |
| 213 |
| 214 scoped_refptr<MessagePipeDispatcher> mp1_dispatcher0 = |
| 215 MessagePipeDispatcher::Create( |
| 216 MessagePipeDispatcher::kDefaultCreateOptions); |
| 217 scoped_refptr<MessagePipeDispatcher> mp1_dispatcher1 = |
| 218 MessagePipeDispatcher::Create( |
| 219 MessagePipeDispatcher::kDefaultCreateOptions); |
| 220 CloseOnShutdown(mp1_dispatcher0); |
| 221 CloseOnShutdown(mp1_dispatcher1); |
| 222 { |
| 223 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalLocal()); |
| 224 mp1_dispatcher0->Init(mp, 0); |
| 225 mp1_dispatcher1->Init(mp, 1); |
| 226 } |
| 227 |
| 228 ASSERT_EQ(MOJO_RESULT_OK, |
| 229 wait_set->AddWaitingDispatcher(dispatcher0_, |
| 230 MOJO_HANDLE_SIGNAL_READABLE, 0)); |
| 231 ASSERT_EQ(MOJO_RESULT_OK, |
| 232 wait_set->AddWaitingDispatcher(dispatcher1_, |
| 233 MOJO_HANDLE_SIGNAL_WRITABLE, 0)); |
| 234 ASSERT_EQ(MOJO_RESULT_OK, |
| 235 wait_set->AddWaitingDispatcher(mp1_dispatcher0, |
| 236 MOJO_HANDLE_SIGNAL_WRITABLE, 0)); |
| 237 ASSERT_EQ(MOJO_RESULT_OK, |
| 238 wait_set->AddWaitingDispatcher(mp1_dispatcher1, |
| 239 MOJO_HANDLE_SIGNAL_WRITABLE, 0)); |
| 240 |
| 241 Waiter w; |
| 242 w.Init(); |
| 243 HandleSignalsState hss; |
| 244 // The three writable dispatchers should be ready. |
| 245 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 246 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); |
| 247 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
| 248 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
| 249 |
| 250 scoped_refptr<Dispatcher> woken_dispatcher; |
| 251 EXPECT_EQ(MOJO_RESULT_OK, |
| 252 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); |
| 253 // Don't know which dispatcher was returned, just that it was one of the |
| 254 // writable ones. |
| 255 EXPECT_TRUE(woken_dispatcher == dispatcher1_ || |
| 256 woken_dispatcher == mp1_dispatcher0 || |
| 257 woken_dispatcher == mp1_dispatcher1); |
| 258 |
| 259 DispatcherVector dispatchers_vector; |
| 260 uint32_t count = 4; |
| 261 MojoResult results[4]; |
| 262 EXPECT_EQ(MOJO_RESULT_OK, |
| 263 wait_set->GetReadyDispatchers(MakeUserPointer(&count), |
| 264 &dispatchers_vector, |
| 265 MakeUserPointer(results), |
| 266 MakeUserPointer<uintptr_t>(nullptr))); |
| 267 EXPECT_EQ(3u, count); |
| 268 std::sort(dispatchers_vector.begin(), dispatchers_vector.end()); |
| 269 DispatcherVector expected_dispatchers; |
| 270 expected_dispatchers.push_back(dispatcher1_); |
| 271 expected_dispatchers.push_back(mp1_dispatcher0); |
| 272 expected_dispatchers.push_back(mp1_dispatcher1); |
| 273 std::sort(expected_dispatchers.begin(), expected_dispatchers.end()); |
| 274 EXPECT_EQ(expected_dispatchers, dispatchers_vector); |
| 275 |
| 276 // If a ready dispatcher isn't removed, it will continue to be returned. |
| 277 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 278 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); |
| 279 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
| 280 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
| 281 count = 4; |
| 282 dispatchers_vector.clear(); |
| 283 EXPECT_EQ(MOJO_RESULT_OK, |
| 284 wait_set->GetReadyDispatchers(MakeUserPointer(&count), |
| 285 &dispatchers_vector, |
| 286 MakeUserPointer(results), |
| 287 MakeUserPointer<uintptr_t>(nullptr))); |
| 288 EXPECT_EQ(3u, count); |
| 289 std::sort(dispatchers_vector.begin(), dispatchers_vector.end()); |
| 290 EXPECT_EQ(expected_dispatchers, dispatchers_vector); |
| 291 |
| 292 // Remove one. It shouldn't be returned any longer. |
| 293 ASSERT_EQ(MOJO_RESULT_OK, |
| 294 wait_set->RemoveWaitingDispatcher(expected_dispatchers.back())); |
| 295 expected_dispatchers.pop_back(); |
| 296 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 297 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); |
| 298 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
| 299 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
| 300 count = 4; |
| 301 dispatchers_vector.clear(); |
| 302 EXPECT_EQ(MOJO_RESULT_OK, |
| 303 wait_set->GetReadyDispatchers(MakeUserPointer(&count), |
| 304 &dispatchers_vector, |
| 305 MakeUserPointer(results), |
| 306 MakeUserPointer<uintptr_t>(nullptr))); |
| 307 EXPECT_EQ(2u, count); |
| 308 std::sort(dispatchers_vector.begin(), dispatchers_vector.end()); |
| 309 EXPECT_EQ(expected_dispatchers, dispatchers_vector); |
| 310 |
| 311 // Write to |dispatcher1_|, which should make |dispatcher0_| readable. |
| 312 char buffer[] = "abcd"; |
| 313 w.Init(); |
| 314 ASSERT_EQ(MOJO_RESULT_OK, |
| 315 dispatcher1_->WriteMessage(UserPointer<const void>(buffer), |
| 316 sizeof(buffer), nullptr, |
| 317 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 318 expected_dispatchers.push_back(dispatcher0_); |
| 319 std::sort(expected_dispatchers.begin(), expected_dispatchers.end()); |
| 320 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 321 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); |
| 322 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
| 323 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
| 324 count = 4; |
| 325 dispatchers_vector.clear(); |
| 326 EXPECT_EQ(MOJO_RESULT_OK, |
| 327 wait_set->GetReadyDispatchers(MakeUserPointer(&count), |
| 328 &dispatchers_vector, |
| 329 MakeUserPointer(results), |
| 330 MakeUserPointer<uintptr_t>(nullptr))); |
| 331 EXPECT_EQ(3u, count); |
| 332 std::sort(dispatchers_vector.begin(), dispatchers_vector.end()); |
| 333 EXPECT_EQ(expected_dispatchers, dispatchers_vector); |
| 334 } |
| 335 |
| 336 TEST_F(WaitSetDispatcherTest, InvalidParams) { |
| 337 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher(); |
| 338 |
| 339 // Can't add a wait set to itself. |
| 340 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 341 wait_set->AddWaitingDispatcher(wait_set, |
| 342 MOJO_HANDLE_SIGNAL_READABLE, 0)); |
| 343 |
| 344 // Can't add twice. |
| 345 EXPECT_EQ(MOJO_RESULT_OK, |
| 346 wait_set->AddWaitingDispatcher(dispatcher0_, |
| 347 MOJO_HANDLE_SIGNAL_READABLE, 0)); |
| 348 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 349 wait_set->AddWaitingDispatcher(dispatcher0_, |
| 350 MOJO_HANDLE_SIGNAL_READABLE, 0)); |
| 351 |
| 352 // Remove a dispatcher that wasn't added. |
| 353 EXPECT_EQ(MOJO_RESULT_NOT_FOUND, |
| 354 wait_set->RemoveWaitingDispatcher(dispatcher1_)); |
| 355 |
| 356 // Add to a closed wait set. |
| 357 wait_set->Close(); |
| 358 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 359 wait_set->AddWaitingDispatcher(dispatcher0_, |
| 360 MOJO_HANDLE_SIGNAL_READABLE, 0)); |
| 361 } |
| 362 |
| 363 TEST_F(WaitSetDispatcherTest, NotSatisfiable) { |
| 364 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher(); |
| 365 CloseOnShutdown(wait_set); |
| 366 |
| 367 // Wait sets can only satisfy MOJO_HANDLE_SIGNAL_READABLE. |
| 368 Waiter w; |
| 369 w.Init(); |
| 370 HandleSignalsState hss; |
| 371 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 372 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); |
| 373 EXPECT_EQ(MOJO_HANDLE_SIGNAL_NONE, hss.satisfied_signals); |
| 374 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
| 375 |
| 376 hss = HandleSignalsState(); |
| 377 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 378 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_PEER_CLOSED, 0, &hss)); |
| 379 EXPECT_EQ(MOJO_HANDLE_SIGNAL_NONE, hss.satisfied_signals); |
| 380 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
| 381 } |
| 382 |
| 383 TEST_F(WaitSetDispatcherTest, ClosedDispatchers) { |
| 384 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher(); |
| 385 CloseOnShutdown(wait_set); |
| 386 |
| 387 Waiter w; |
| 388 w.Init(); |
| 389 HandleSignalsState hss; |
| 390 // A dispatcher that was added and then closed will be cancelled. |
| 391 ASSERT_EQ(MOJO_RESULT_OK, |
| 392 wait_set->AddWaitingDispatcher(dispatcher0_, |
| 393 MOJO_HANDLE_SIGNAL_READABLE, 0)); |
| 394 EXPECT_EQ(MOJO_RESULT_OK, |
| 395 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); |
| 396 dispatcher0_->Close(); |
| 397 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(0, nullptr)); |
| 398 EXPECT_TRUE(wait_set->GetHandleSignalsState().satisfies( |
| 399 MOJO_HANDLE_SIGNAL_READABLE)); |
| 400 scoped_refptr<Dispatcher> woken_dispatcher; |
| 401 EXPECT_EQ(MOJO_RESULT_CANCELLED, |
| 402 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); |
| 403 EXPECT_EQ(dispatcher0_, woken_dispatcher); |
| 404 |
| 405 // Dispatcher will be implicitly removed because it may be impossible to |
| 406 // remove explicitly. |
| 407 woken_dispatcher = nullptr; |
| 408 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, |
| 409 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); |
| 410 EXPECT_EQ(MOJO_RESULT_NOT_FOUND, |
| 411 wait_set->RemoveWaitingDispatcher(dispatcher0_)); |
| 412 |
| 413 // A dispatcher that's not satisfiable should give an error. |
| 414 w.Init(); |
| 415 EXPECT_EQ(MOJO_RESULT_OK, |
| 416 wait_set->AddWaitingDispatcher(dispatcher1_, |
| 417 MOJO_HANDLE_SIGNAL_READABLE, 0)); |
| 418 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(0, nullptr)); |
| 419 EXPECT_TRUE(wait_set->GetHandleSignalsState().satisfies( |
| 420 MOJO_HANDLE_SIGNAL_READABLE)); |
| 421 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 422 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); |
| 423 EXPECT_EQ(dispatcher1_, woken_dispatcher); |
| 424 |
| 425 wait_set->RemoveAwakable(&w, nullptr); |
| 426 } |
| 427 |
| 428 TEST_F(WaitSetDispatcherTest, NestedSets) { |
| 429 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher(); |
| 430 CloseOnShutdown(wait_set); |
| 431 scoped_refptr<WaitSetDispatcher> nested_wait_set = new WaitSetDispatcher(); |
| 432 CloseOnShutdown(nested_wait_set); |
| 433 |
| 434 Waiter w; |
| 435 w.Init(); |
| 436 EXPECT_EQ(MOJO_RESULT_OK, |
| 437 wait_set->AddWaitingDispatcher(nested_wait_set, |
| 438 MOJO_HANDLE_SIGNAL_READABLE, 0)); |
| 439 EXPECT_EQ(MOJO_RESULT_OK, |
| 440 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, nullptr)); |
| 441 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0, nullptr)); |
| 442 |
| 443 // Writable signal is immediately satisfied by the message pipe. |
| 444 w.Init(); |
| 445 EXPECT_EQ(MOJO_RESULT_OK, |
| 446 nested_wait_set->AddWaitingDispatcher( |
| 447 dispatcher0_, MOJO_HANDLE_SIGNAL_WRITABLE, 0)); |
| 448 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(0, nullptr)); |
| 449 scoped_refptr<Dispatcher> woken_dispatcher; |
| 450 EXPECT_EQ(MOJO_RESULT_OK, |
| 451 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); |
| 452 EXPECT_EQ(nested_wait_set, woken_dispatcher); |
| 453 |
| 454 wait_set->RemoveAwakable(&w, nullptr); |
| 455 } |
| 456 |
| 457 } // namespace |
| 458 } // namespace system |
| 459 } // namespace mojo |
OLD | NEW |