| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/system/wait_set_dispatcher.h" | 5 #include "mojo/edk/system/wait_set_dispatcher.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "mojo/edk/embedder/embedder_internal.h" |
| 15 #include "mojo/edk/system/core.h" |
| 14 #include "mojo/edk/system/message_pipe_dispatcher.h" | 16 #include "mojo/edk/system/message_pipe_dispatcher.h" |
| 15 #include "mojo/edk/system/test_utils.h" | 17 #include "mojo/edk/system/test_utils.h" |
| 16 #include "mojo/edk/system/waiter.h" | 18 #include "mojo/edk/system/waiter.h" |
| 17 #include "mojo/public/cpp/system/macros.h" | 19 #include "mojo/public/cpp/system/macros.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 20 namespace mojo { | 22 namespace mojo { |
| 21 namespace edk { | 23 namespace edk { |
| 22 namespace { | 24 namespace { |
| 23 | 25 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 47 if (result == MOJO_RESULT_OK) { | 49 if (result == MOJO_RESULT_OK) { |
| 48 CHECK_EQ(1u, dispatchers.size()); | 50 CHECK_EQ(1u, dispatchers.size()); |
| 49 *ready_dispatcher = dispatchers[0]; | 51 *ready_dispatcher = dispatchers[0]; |
| 50 return dispatcher_result; | 52 return dispatcher_result; |
| 51 } | 53 } |
| 52 return result; | 54 return result; |
| 53 } | 55 } |
| 54 | 56 |
| 55 void CreateMessagePipe(scoped_refptr<MessagePipeDispatcher>* d0, | 57 void CreateMessagePipe(scoped_refptr<MessagePipeDispatcher>* d0, |
| 56 scoped_refptr<MessagePipeDispatcher>* d1) { | 58 scoped_refptr<MessagePipeDispatcher>* d1) { |
| 57 *d0 = MessagePipeDispatcher::Create( | 59 MojoHandle h0, h1; |
| 58 MessagePipeDispatcher::kDefaultCreateOptions); | 60 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1)); |
| 59 *d1 = MessagePipeDispatcher::Create( | 61 |
| 60 MessagePipeDispatcher::kDefaultCreateOptions); | 62 Core* core = mojo::edk::internal::GetCore(); |
| 61 (*d0)->InitNonTransferable(pipe_id_generator_); | 63 *d0 = scoped_refptr<MessagePipeDispatcher>( |
| 62 (*d1)->InitNonTransferable(pipe_id_generator_); | 64 static_cast<MessagePipeDispatcher*>(core->GetDispatcher(h0).get())); |
| 65 *d1 = scoped_refptr<MessagePipeDispatcher>( |
| 66 static_cast<MessagePipeDispatcher*>(core->GetDispatcher(h1).get())); |
| 63 pipe_id_generator_++; | 67 pipe_id_generator_++; |
| 64 | 68 |
| 65 dispatchers_to_close_.push_back(*d0); | 69 dispatchers_to_close_.push_back(*d0); |
| 66 dispatchers_to_close_.push_back(*d1); | 70 dispatchers_to_close_.push_back(*d1); |
| 67 } | 71 } |
| 68 | 72 |
| 69 void CloseOnShutdown(const scoped_refptr<Dispatcher>& dispatcher) { | 73 void CloseOnShutdown(const scoped_refptr<Dispatcher>& dispatcher) { |
| 70 dispatchers_to_close_.push_back(dispatcher); | 74 dispatchers_to_close_.push_back(dispatcher); |
| 71 } | 75 } |
| 72 | 76 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); | 130 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); |
| 127 EXPECT_FALSE(hss.satisfies(MOJO_HANDLE_SIGNAL_READABLE)); | 131 EXPECT_FALSE(hss.satisfies(MOJO_HANDLE_SIGNAL_READABLE)); |
| 128 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0, nullptr)); | 132 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0, nullptr)); |
| 129 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, | 133 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, |
| 130 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); | 134 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); |
| 131 | 135 |
| 132 // Write to |dispatcher1_|, which should make |dispatcher0_| readable. | 136 // Write to |dispatcher1_|, which should make |dispatcher0_| readable. |
| 133 char buffer[] = "abcd"; | 137 char buffer[] = "abcd"; |
| 134 w.Init(); | 138 w.Init(); |
| 135 ASSERT_EQ(MOJO_RESULT_OK, | 139 ASSERT_EQ(MOJO_RESULT_OK, |
| 136 dispatcher1_->WriteMessage(buffer, sizeof(buffer), nullptr, | 140 dispatcher1_->WriteMessage(buffer, sizeof(buffer), nullptr, 0, |
| 137 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 141 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 138 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(MOJO_DEADLINE_INDEFINITE, nullptr)); | 142 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 139 woken_dispatcher = nullptr; | 143 woken_dispatcher = nullptr; |
| 140 context = 0; | 144 context = 0; |
| 141 EXPECT_EQ(MOJO_RESULT_OK, | 145 EXPECT_EQ(MOJO_RESULT_OK, |
| 142 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context)); | 146 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context)); |
| 143 EXPECT_EQ(dispatcher0_, woken_dispatcher); | 147 EXPECT_EQ(dispatcher0_, woken_dispatcher); |
| 144 EXPECT_EQ(1u, context); | 148 EXPECT_EQ(1u, context); |
| 145 | 149 |
| 146 // Again, if a ready dispatcher isn't removed, it will continue to be | 150 // Again, if a ready dispatcher isn't removed, it will continue to be |
| (...skipping 26 matching lines...) Expand all Loading... |
| 173 scoped_refptr<Dispatcher> woken_dispatcher; | 177 scoped_refptr<Dispatcher> woken_dispatcher; |
| 174 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, | 178 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, |
| 175 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); | 179 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); |
| 176 | 180 |
| 177 // The tested behaviour below should be repeatable. | 181 // The tested behaviour below should be repeatable. |
| 178 for (size_t i = 0; i < 3; i++) { | 182 for (size_t i = 0; i < 3; i++) { |
| 179 // Write to |dispatcher1_|, which should make |dispatcher0_| readable. | 183 // Write to |dispatcher1_|, which should make |dispatcher0_| readable. |
| 180 char buffer[] = "abcd"; | 184 char buffer[] = "abcd"; |
| 181 w.Init(); | 185 w.Init(); |
| 182 ASSERT_EQ(MOJO_RESULT_OK, | 186 ASSERT_EQ(MOJO_RESULT_OK, |
| 183 dispatcher1_->WriteMessage(buffer, sizeof(buffer), nullptr, | 187 dispatcher1_->WriteMessage(buffer, sizeof(buffer), nullptr, 0, |
| 184 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 188 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 185 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(MOJO_DEADLINE_INDEFINITE, nullptr)); | 189 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 186 woken_dispatcher = nullptr; | 190 woken_dispatcher = nullptr; |
| 187 context = 0; | 191 context = 0; |
| 188 EXPECT_EQ(MOJO_RESULT_OK, | 192 EXPECT_EQ(MOJO_RESULT_OK, |
| 189 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context)); | 193 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context)); |
| 190 EXPECT_EQ(dispatcher0_, woken_dispatcher); | 194 EXPECT_EQ(dispatcher0_, woken_dispatcher); |
| 191 EXPECT_EQ(1u, context); | 195 EXPECT_EQ(1u, context); |
| 192 | 196 |
| 193 // Read from |dispatcher0_| which should change it's state to non-readable. | 197 // Read from |dispatcher0_| which should change it's state to non-readable. |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 results, | 304 results, |
| 301 nullptr)); | 305 nullptr)); |
| 302 EXPECT_EQ(2u, count); | 306 EXPECT_EQ(2u, count); |
| 303 std::sort(dispatchers_vector.begin(), dispatchers_vector.end()); | 307 std::sort(dispatchers_vector.begin(), dispatchers_vector.end()); |
| 304 EXPECT_EQ(expected_dispatchers, dispatchers_vector); | 308 EXPECT_EQ(expected_dispatchers, dispatchers_vector); |
| 305 | 309 |
| 306 // Write to |dispatcher1_|, which should make |dispatcher0_| readable. | 310 // Write to |dispatcher1_|, which should make |dispatcher0_| readable. |
| 307 char buffer[] = "abcd"; | 311 char buffer[] = "abcd"; |
| 308 w.Init(); | 312 w.Init(); |
| 309 ASSERT_EQ(MOJO_RESULT_OK, | 313 ASSERT_EQ(MOJO_RESULT_OK, |
| 310 dispatcher1_->WriteMessage(buffer, sizeof(buffer), nullptr, | 314 dispatcher1_->WriteMessage(buffer, sizeof(buffer), nullptr, 0, |
| 311 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 315 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 312 { | 316 { |
| 313 Waiter mp_w; | 317 Waiter mp_w; |
| 314 mp_w.Init(); | 318 mp_w.Init(); |
| 315 // Wait for |dispatcher0_| to be readable. | 319 // Wait for |dispatcher0_| to be readable. |
| 316 if (dispatcher0_->AddAwakable(&mp_w, MOJO_HANDLE_SIGNAL_READABLE, 0, | 320 if (dispatcher0_->AddAwakable(&mp_w, MOJO_HANDLE_SIGNAL_READABLE, 0, |
| 317 nullptr) == MOJO_RESULT_OK) { | 321 nullptr) == MOJO_RESULT_OK) { |
| 318 EXPECT_EQ(MOJO_RESULT_OK, mp_w.Wait(MOJO_DEADLINE_INDEFINITE, 0)); | 322 EXPECT_EQ(MOJO_RESULT_OK, mp_w.Wait(MOJO_DEADLINE_INDEFINITE, 0)); |
| 319 dispatcher0_->RemoveAwakable(&mp_w, nullptr); | 323 dispatcher0_->RemoveAwakable(&mp_w, nullptr); |
| 320 } | 324 } |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 EXPECT_EQ(MOJO_RESULT_OK, | 458 EXPECT_EQ(MOJO_RESULT_OK, |
| 455 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); | 459 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); |
| 456 EXPECT_EQ(nested_wait_set, woken_dispatcher); | 460 EXPECT_EQ(nested_wait_set, woken_dispatcher); |
| 457 | 461 |
| 458 wait_set->RemoveAwakable(&w, nullptr); | 462 wait_set->RemoveAwakable(&w, nullptr); |
| 459 } | 463 } |
| 460 | 464 |
| 461 } // namespace | 465 } // namespace |
| 462 } // namespace edk | 466 } // namespace edk |
| 463 } // namespace mojo | 467 } // namespace mojo |
| OLD | NEW |