Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: mojo/edk/system/wait_set_dispatcher_unittest.cc

Issue 1504733002: Implementation of WaitSet for new EDK. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mojo-waitset-implementation
Patch Set: Add gyp rules. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/system/wait_set_dispatcher.cc ('k') | mojo/mojo_edk.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "third_party/mojo/src/mojo/edk/system/wait_set_dispatcher.h" 5 #include "mojo/edk/system/wait_set_dispatcher.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "mojo/edk/system/message_pipe_dispatcher.h"
11 #include "mojo/edk/system/test_utils.h"
12 #include "mojo/edk/system/waiter.h"
10 #include "mojo/public/cpp/system/macros.h" 13 #include "mojo/public/cpp/system/macros.h"
11 #include "testing/gtest/include/gtest/gtest.h" 14 #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 15
17 namespace mojo { 16 namespace mojo {
18 namespace system { 17 namespace edk {
19 namespace { 18 namespace {
20 19
21 class WaitSetDispatcherTest : public ::testing::Test { 20 class WaitSetDispatcherTest : public ::testing::Test {
22 public: 21 public:
23 WaitSetDispatcherTest() {} 22 WaitSetDispatcherTest() {}
24 ~WaitSetDispatcherTest() override {} 23 ~WaitSetDispatcherTest() override {}
25 24
26 void SetUp() override { 25 void SetUp() override {
27 dispatcher0_ = MessagePipeDispatcher::Create( 26 CreateMessagePipe(&dispatcher0_, &dispatcher1_);
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 } 27 }
39 28
40 void TearDown() override { 29 void TearDown() override {
41 for (auto& d : dispatchers_to_close_) 30 for (auto& d : dispatchers_to_close_)
42 d->Close(); 31 d->Close();
43 } 32 }
44 33
45 MojoResult GetOneReadyDispatcher( 34 MojoResult GetOneReadyDispatcher(
46 const scoped_refptr<WaitSetDispatcher>& wait_set, 35 const scoped_refptr<WaitSetDispatcher>& wait_set,
47 scoped_refptr<Dispatcher>* ready_dispatcher, 36 scoped_refptr<Dispatcher>* ready_dispatcher,
48 uintptr_t* context) { 37 uintptr_t* context) {
49 uint32_t count = 1; 38 uint32_t count = 1;
50 MojoResult dispatcher_result = MOJO_RESULT_UNKNOWN; 39 MojoResult dispatcher_result = MOJO_RESULT_UNKNOWN;
51 DispatcherVector dispatchers; 40 DispatcherVector dispatchers;
52 MojoResult result = wait_set->GetReadyDispatchers( 41 MojoResult result = wait_set->GetReadyDispatchers(
53 MakeUserPointer(&count), 42 &count, &dispatchers, &dispatcher_result, context);
54 &dispatchers,
55 MakeUserPointer(&dispatcher_result),
56 MakeUserPointer(context));
57 if (result == MOJO_RESULT_OK) { 43 if (result == MOJO_RESULT_OK) {
58 CHECK_EQ(1u, dispatchers.size()); 44 CHECK_EQ(1u, dispatchers.size());
59 *ready_dispatcher = dispatchers[0]; 45 *ready_dispatcher = dispatchers[0];
60 return dispatcher_result; 46 return dispatcher_result;
61 } 47 }
62 return result; 48 return result;
63 } 49 }
64 50
51 void CreateMessagePipe(scoped_refptr<MessagePipeDispatcher>* d0,
52 scoped_refptr<MessagePipeDispatcher>* d1) {
53 *d0 = MessagePipeDispatcher::Create(
54 MessagePipeDispatcher::kDefaultCreateOptions);
55 *d1 = MessagePipeDispatcher::Create(
56 MessagePipeDispatcher::kDefaultCreateOptions);
57 (*d0)->InitNonTransferable(pipe_id_generator_);
58 (*d1)->InitNonTransferable(pipe_id_generator_);
59 pipe_id_generator_++;
60
61 dispatchers_to_close_.push_back(*d0);
62 dispatchers_to_close_.push_back(*d1);
63 }
64
65 void CloseOnShutdown(const scoped_refptr<Dispatcher>& dispatcher) { 65 void CloseOnShutdown(const scoped_refptr<Dispatcher>& dispatcher) {
66 dispatchers_to_close_.push_back(dispatcher); 66 dispatchers_to_close_.push_back(dispatcher);
67 } 67 }
68 68
69 protected: 69 protected:
70 scoped_refptr<MessagePipeDispatcher> dispatcher0_; 70 scoped_refptr<MessagePipeDispatcher> dispatcher0_;
71 scoped_refptr<MessagePipeDispatcher> dispatcher1_; 71 scoped_refptr<MessagePipeDispatcher> dispatcher1_;
72 72
73 private:
74 static uint64_t pipe_id_generator_;
73 DispatcherVector dispatchers_to_close_; 75 DispatcherVector dispatchers_to_close_;
74 76
75 DISALLOW_COPY_AND_ASSIGN(WaitSetDispatcherTest); 77 DISALLOW_COPY_AND_ASSIGN(WaitSetDispatcherTest);
76 }; 78 };
77 79
80 // static
81 uint64_t WaitSetDispatcherTest::pipe_id_generator_ = 1;
82
78 TEST_F(WaitSetDispatcherTest, Basic) { 83 TEST_F(WaitSetDispatcherTest, Basic) {
79 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher(); 84 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher();
80 CloseOnShutdown(wait_set); 85 CloseOnShutdown(wait_set);
81 ASSERT_EQ(MOJO_RESULT_OK, 86 ASSERT_EQ(MOJO_RESULT_OK,
82 wait_set->AddWaitingDispatcher(dispatcher0_, 87 wait_set->AddWaitingDispatcher(dispatcher0_,
83 MOJO_HANDLE_SIGNAL_READABLE, 1)); 88 MOJO_HANDLE_SIGNAL_READABLE, 1));
84 ASSERT_EQ(MOJO_RESULT_OK, 89 ASSERT_EQ(MOJO_RESULT_OK,
85 wait_set->AddWaitingDispatcher(dispatcher1_, 90 wait_set->AddWaitingDispatcher(dispatcher1_,
86 MOJO_HANDLE_SIGNAL_WRITABLE, 2)); 91 MOJO_HANDLE_SIGNAL_WRITABLE, 2));
87 92
(...skipping 29 matching lines...) Expand all
117 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); 122 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss));
118 EXPECT_FALSE(hss.satisfies(MOJO_HANDLE_SIGNAL_READABLE)); 123 EXPECT_FALSE(hss.satisfies(MOJO_HANDLE_SIGNAL_READABLE));
119 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0, nullptr)); 124 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0, nullptr));
120 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, 125 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
121 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); 126 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr));
122 127
123 // Write to |dispatcher1_|, which should make |dispatcher0_| readable. 128 // Write to |dispatcher1_|, which should make |dispatcher0_| readable.
124 char buffer[] = "abcd"; 129 char buffer[] = "abcd";
125 w.Init(); 130 w.Init();
126 ASSERT_EQ(MOJO_RESULT_OK, 131 ASSERT_EQ(MOJO_RESULT_OK,
127 dispatcher1_->WriteMessage(UserPointer<const void>(buffer), 132 dispatcher1_->WriteMessage(buffer, sizeof(buffer), nullptr,
128 sizeof(buffer), nullptr, 133 MOJO_WRITE_MESSAGE_FLAG_NONE));
129 MOJO_WRITE_MESSAGE_FLAG_NONE)); 134 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(MOJO_DEADLINE_INDEFINITE, nullptr));
130 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(0, nullptr));
131 woken_dispatcher = nullptr; 135 woken_dispatcher = nullptr;
132 context = 0; 136 context = 0;
133 EXPECT_EQ(MOJO_RESULT_OK, 137 EXPECT_EQ(MOJO_RESULT_OK,
134 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context)); 138 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context));
135 EXPECT_EQ(dispatcher0_, woken_dispatcher); 139 EXPECT_EQ(dispatcher0_, woken_dispatcher);
136 EXPECT_EQ(1u, context); 140 EXPECT_EQ(1u, context);
137 141
138 // Again, if a ready dispatcher isn't removed, it will continue to be 142 // Again, if a ready dispatcher isn't removed, it will continue to be
139 // returned. 143 // returned.
140 woken_dispatcher = nullptr; 144 woken_dispatcher = nullptr;
(...skipping 24 matching lines...) Expand all
165 scoped_refptr<Dispatcher> woken_dispatcher; 169 scoped_refptr<Dispatcher> woken_dispatcher;
166 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, 170 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
167 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); 171 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr));
168 172
169 // The tested behaviour below should be repeatable. 173 // The tested behaviour below should be repeatable.
170 for (size_t i = 0; i < 3; i++) { 174 for (size_t i = 0; i < 3; i++) {
171 // Write to |dispatcher1_|, which should make |dispatcher0_| readable. 175 // Write to |dispatcher1_|, which should make |dispatcher0_| readable.
172 char buffer[] = "abcd"; 176 char buffer[] = "abcd";
173 w.Init(); 177 w.Init();
174 ASSERT_EQ(MOJO_RESULT_OK, 178 ASSERT_EQ(MOJO_RESULT_OK,
175 dispatcher1_->WriteMessage(UserPointer<const void>(buffer), 179 dispatcher1_->WriteMessage(buffer, sizeof(buffer), nullptr,
176 sizeof(buffer), nullptr,
177 MOJO_WRITE_MESSAGE_FLAG_NONE)); 180 MOJO_WRITE_MESSAGE_FLAG_NONE));
178 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(0, nullptr)); 181 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(MOJO_DEADLINE_INDEFINITE, nullptr));
179 woken_dispatcher = nullptr; 182 woken_dispatcher = nullptr;
180 context = 0; 183 context = 0;
181 EXPECT_EQ(MOJO_RESULT_OK, 184 EXPECT_EQ(MOJO_RESULT_OK,
182 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context)); 185 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context));
183 EXPECT_EQ(dispatcher0_, woken_dispatcher); 186 EXPECT_EQ(dispatcher0_, woken_dispatcher);
184 EXPECT_EQ(1u, context); 187 EXPECT_EQ(1u, context);
185 188
186 // Read from |dispatcher0_| which should change it's state to non-readable. 189 // Read from |dispatcher0_| which should change it's state to non-readable.
187 char read_buffer[sizeof(buffer) + 5]; 190 char read_buffer[sizeof(buffer) + 5];
188 uint32_t num_bytes = sizeof(read_buffer); 191 uint32_t num_bytes = sizeof(read_buffer);
189 ASSERT_EQ(MOJO_RESULT_OK, 192 ASSERT_EQ(MOJO_RESULT_OK,
190 dispatcher0_->ReadMessage(UserPointer<void>(read_buffer), 193 dispatcher0_->ReadMessage(read_buffer, &num_bytes, nullptr,
191 MakeUserPointer(&num_bytes), 194 nullptr, MOJO_READ_MESSAGE_FLAG_NONE));
192 nullptr, nullptr,
193 MOJO_READ_MESSAGE_FLAG_NONE));
194 EXPECT_EQ(sizeof(buffer), num_bytes); 195 EXPECT_EQ(sizeof(buffer), num_bytes);
195 196
196 // No dispatchers are ready. 197 // No dispatchers are ready.
197 w.Init(); 198 w.Init();
198 woken_dispatcher = nullptr; 199 woken_dispatcher = nullptr;
199 context = 0; 200 context = 0;
200 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, 201 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
201 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context)); 202 GetOneReadyDispatcher(wait_set, &woken_dispatcher, &context));
202 EXPECT_EQ(nullptr, woken_dispatcher); 203 EXPECT_EQ(nullptr, woken_dispatcher);
203 EXPECT_EQ(0u, context); 204 EXPECT_EQ(0u, context);
204 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0, nullptr)); 205 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0, nullptr));
205 } 206 }
206 207
207 wait_set->RemoveAwakable(&w, nullptr); 208 wait_set->RemoveAwakable(&w, nullptr);
208 } 209 }
209 210
210 TEST_F(WaitSetDispatcherTest, MultipleReady) { 211 TEST_F(WaitSetDispatcherTest, MultipleReady) {
211 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher(); 212 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher();
212 CloseOnShutdown(wait_set); 213 CloseOnShutdown(wait_set);
213 214
214 scoped_refptr<MessagePipeDispatcher> mp1_dispatcher0 = 215 scoped_refptr<MessagePipeDispatcher> mp1_dispatcher0;
215 MessagePipeDispatcher::Create( 216 scoped_refptr<MessagePipeDispatcher> mp1_dispatcher1;
216 MessagePipeDispatcher::kDefaultCreateOptions); 217 CreateMessagePipe(&mp1_dispatcher0, &mp1_dispatcher1);
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 218
228 ASSERT_EQ(MOJO_RESULT_OK, 219 ASSERT_EQ(MOJO_RESULT_OK,
229 wait_set->AddWaitingDispatcher(dispatcher0_, 220 wait_set->AddWaitingDispatcher(dispatcher0_,
230 MOJO_HANDLE_SIGNAL_READABLE, 0)); 221 MOJO_HANDLE_SIGNAL_READABLE, 0));
231 ASSERT_EQ(MOJO_RESULT_OK, 222 ASSERT_EQ(MOJO_RESULT_OK,
232 wait_set->AddWaitingDispatcher(dispatcher1_, 223 wait_set->AddWaitingDispatcher(dispatcher1_,
233 MOJO_HANDLE_SIGNAL_WRITABLE, 0)); 224 MOJO_HANDLE_SIGNAL_WRITABLE, 0));
234 ASSERT_EQ(MOJO_RESULT_OK, 225 ASSERT_EQ(MOJO_RESULT_OK,
235 wait_set->AddWaitingDispatcher(mp1_dispatcher0, 226 wait_set->AddWaitingDispatcher(mp1_dispatcher0,
236 MOJO_HANDLE_SIGNAL_WRITABLE, 0)); 227 MOJO_HANDLE_SIGNAL_WRITABLE, 0));
(...skipping 16 matching lines...) Expand all
253 // Don't know which dispatcher was returned, just that it was one of the 244 // Don't know which dispatcher was returned, just that it was one of the
254 // writable ones. 245 // writable ones.
255 EXPECT_TRUE(woken_dispatcher == dispatcher1_ || 246 EXPECT_TRUE(woken_dispatcher == dispatcher1_ ||
256 woken_dispatcher == mp1_dispatcher0 || 247 woken_dispatcher == mp1_dispatcher0 ||
257 woken_dispatcher == mp1_dispatcher1); 248 woken_dispatcher == mp1_dispatcher1);
258 249
259 DispatcherVector dispatchers_vector; 250 DispatcherVector dispatchers_vector;
260 uint32_t count = 4; 251 uint32_t count = 4;
261 MojoResult results[4]; 252 MojoResult results[4];
262 EXPECT_EQ(MOJO_RESULT_OK, 253 EXPECT_EQ(MOJO_RESULT_OK,
263 wait_set->GetReadyDispatchers(MakeUserPointer(&count), 254 wait_set->GetReadyDispatchers(&count,
264 &dispatchers_vector, 255 &dispatchers_vector,
265 MakeUserPointer(results), 256 results,
266 MakeUserPointer<uintptr_t>(nullptr))); 257 nullptr));
267 EXPECT_EQ(3u, count); 258 EXPECT_EQ(3u, count);
268 std::sort(dispatchers_vector.begin(), dispatchers_vector.end()); 259 std::sort(dispatchers_vector.begin(), dispatchers_vector.end());
269 DispatcherVector expected_dispatchers; 260 DispatcherVector expected_dispatchers;
270 expected_dispatchers.push_back(dispatcher1_); 261 expected_dispatchers.push_back(dispatcher1_);
271 expected_dispatchers.push_back(mp1_dispatcher0); 262 expected_dispatchers.push_back(mp1_dispatcher0);
272 expected_dispatchers.push_back(mp1_dispatcher1); 263 expected_dispatchers.push_back(mp1_dispatcher1);
273 std::sort(expected_dispatchers.begin(), expected_dispatchers.end()); 264 std::sort(expected_dispatchers.begin(), expected_dispatchers.end());
274 EXPECT_EQ(expected_dispatchers, dispatchers_vector); 265 EXPECT_EQ(expected_dispatchers, dispatchers_vector);
275 266
276 // If a ready dispatcher isn't removed, it will continue to be returned. 267 // If a ready dispatcher isn't removed, it will continue to be returned.
277 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 268 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
278 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); 269 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss));
279 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); 270 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
280 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); 271 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
281 count = 4; 272 count = 4;
282 dispatchers_vector.clear(); 273 dispatchers_vector.clear();
283 EXPECT_EQ(MOJO_RESULT_OK, 274 EXPECT_EQ(MOJO_RESULT_OK,
284 wait_set->GetReadyDispatchers(MakeUserPointer(&count), 275 wait_set->GetReadyDispatchers(&count,
285 &dispatchers_vector, 276 &dispatchers_vector,
286 MakeUserPointer(results), 277 results,
287 MakeUserPointer<uintptr_t>(nullptr))); 278 nullptr));
288 EXPECT_EQ(3u, count); 279 EXPECT_EQ(3u, count);
289 std::sort(dispatchers_vector.begin(), dispatchers_vector.end()); 280 std::sort(dispatchers_vector.begin(), dispatchers_vector.end());
290 EXPECT_EQ(expected_dispatchers, dispatchers_vector); 281 EXPECT_EQ(expected_dispatchers, dispatchers_vector);
291 282
292 // Remove one. It shouldn't be returned any longer. 283 // Remove one. It shouldn't be returned any longer.
293 ASSERT_EQ(MOJO_RESULT_OK, 284 ASSERT_EQ(MOJO_RESULT_OK,
294 wait_set->RemoveWaitingDispatcher(expected_dispatchers.back())); 285 wait_set->RemoveWaitingDispatcher(expected_dispatchers.back()));
295 expected_dispatchers.pop_back(); 286 expected_dispatchers.pop_back();
296 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 287 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
297 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); 288 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss));
298 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); 289 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
299 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); 290 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
300 count = 4; 291 count = 4;
301 dispatchers_vector.clear(); 292 dispatchers_vector.clear();
302 EXPECT_EQ(MOJO_RESULT_OK, 293 EXPECT_EQ(MOJO_RESULT_OK,
303 wait_set->GetReadyDispatchers(MakeUserPointer(&count), 294 wait_set->GetReadyDispatchers(&count,
304 &dispatchers_vector, 295 &dispatchers_vector,
305 MakeUserPointer(results), 296 results,
306 MakeUserPointer<uintptr_t>(nullptr))); 297 nullptr));
307 EXPECT_EQ(2u, count); 298 EXPECT_EQ(2u, count);
308 std::sort(dispatchers_vector.begin(), dispatchers_vector.end()); 299 std::sort(dispatchers_vector.begin(), dispatchers_vector.end());
309 EXPECT_EQ(expected_dispatchers, dispatchers_vector); 300 EXPECT_EQ(expected_dispatchers, dispatchers_vector);
310 301
311 // Write to |dispatcher1_|, which should make |dispatcher0_| readable. 302 // Write to |dispatcher1_|, which should make |dispatcher0_| readable.
312 char buffer[] = "abcd"; 303 char buffer[] = "abcd";
313 w.Init(); 304 w.Init();
314 ASSERT_EQ(MOJO_RESULT_OK, 305 ASSERT_EQ(MOJO_RESULT_OK,
315 dispatcher1_->WriteMessage(UserPointer<const void>(buffer), 306 dispatcher1_->WriteMessage(buffer, sizeof(buffer), nullptr,
316 sizeof(buffer), nullptr, 307 MOJO_WRITE_MESSAGE_FLAG_NONE));
317 MOJO_WRITE_MESSAGE_FLAG_NONE)); 308 {
309 Waiter mp_w;
310 mp_w.Init();
311 // Wait for |dispatcher0_| to be readable.
312 if (dispatcher0_->AddAwakable(&mp_w, MOJO_HANDLE_SIGNAL_READABLE, 0,
313 nullptr) == MOJO_RESULT_OK) {
314 EXPECT_EQ(MOJO_RESULT_OK, mp_w.Wait(MOJO_DEADLINE_INDEFINITE, 0));
315 dispatcher0_->RemoveAwakable(&mp_w, nullptr);
316 }
317 }
318 expected_dispatchers.push_back(dispatcher0_); 318 expected_dispatchers.push_back(dispatcher0_);
319 std::sort(expected_dispatchers.begin(), expected_dispatchers.end()); 319 std::sort(expected_dispatchers.begin(), expected_dispatchers.end());
320 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 320 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
321 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); 321 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss));
322 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); 322 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
323 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); 323 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
324 count = 4; 324 count = 4;
325 dispatchers_vector.clear(); 325 dispatchers_vector.clear();
326 EXPECT_EQ(MOJO_RESULT_OK, 326 EXPECT_EQ(MOJO_RESULT_OK,
327 wait_set->GetReadyDispatchers(MakeUserPointer(&count), 327 wait_set->GetReadyDispatchers(&count,
328 &dispatchers_vector, 328 &dispatchers_vector,
329 MakeUserPointer(results), 329 results,
330 MakeUserPointer<uintptr_t>(nullptr))); 330 nullptr));
331 EXPECT_EQ(3u, count); 331 EXPECT_EQ(3u, count);
332 std::sort(dispatchers_vector.begin(), dispatchers_vector.end()); 332 std::sort(dispatchers_vector.begin(), dispatchers_vector.end());
333 EXPECT_EQ(expected_dispatchers, dispatchers_vector); 333 EXPECT_EQ(expected_dispatchers, dispatchers_vector);
334 } 334 }
335 335
336 TEST_F(WaitSetDispatcherTest, InvalidParams) { 336 TEST_F(WaitSetDispatcherTest, InvalidParams) {
337 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher(); 337 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher();
338 338
339 // Can't add a wait set to itself. 339 // Can't add a wait set to itself.
340 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 340 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 Waiter w; 387 Waiter w;
388 w.Init(); 388 w.Init();
389 HandleSignalsState hss; 389 HandleSignalsState hss;
390 // A dispatcher that was added and then closed will be cancelled. 390 // A dispatcher that was added and then closed will be cancelled.
391 ASSERT_EQ(MOJO_RESULT_OK, 391 ASSERT_EQ(MOJO_RESULT_OK,
392 wait_set->AddWaitingDispatcher(dispatcher0_, 392 wait_set->AddWaitingDispatcher(dispatcher0_,
393 MOJO_HANDLE_SIGNAL_READABLE, 0)); 393 MOJO_HANDLE_SIGNAL_READABLE, 0));
394 EXPECT_EQ(MOJO_RESULT_OK, 394 EXPECT_EQ(MOJO_RESULT_OK,
395 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss)); 395 wait_set->AddAwakable(&w, MOJO_HANDLE_SIGNAL_READABLE, 0, &hss));
396 dispatcher0_->Close(); 396 dispatcher0_->Close();
397 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(0, nullptr)); 397 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(MOJO_DEADLINE_INDEFINITE, nullptr));
398 EXPECT_TRUE(wait_set->GetHandleSignalsState().satisfies( 398 EXPECT_TRUE(
399 MOJO_HANDLE_SIGNAL_READABLE)); 399 wait_set->GetHandleSignalsState().satisfies(MOJO_HANDLE_SIGNAL_READABLE));
400 scoped_refptr<Dispatcher> woken_dispatcher; 400 scoped_refptr<Dispatcher> woken_dispatcher;
401 EXPECT_EQ(MOJO_RESULT_CANCELLED, 401 EXPECT_EQ(MOJO_RESULT_CANCELLED,
402 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); 402 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr));
403 EXPECT_EQ(dispatcher0_, woken_dispatcher); 403 EXPECT_EQ(dispatcher0_, woken_dispatcher);
404 404
405 // Dispatcher will be implicitly removed because it may be impossible to 405 // Dispatcher will be implicitly removed because it may be impossible to
406 // remove explicitly. 406 // remove explicitly.
407 woken_dispatcher = nullptr; 407 woken_dispatcher = nullptr;
408 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, 408 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
409 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); 409 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr));
410 EXPECT_EQ(MOJO_RESULT_NOT_FOUND, 410 EXPECT_EQ(MOJO_RESULT_NOT_FOUND,
411 wait_set->RemoveWaitingDispatcher(dispatcher0_)); 411 wait_set->RemoveWaitingDispatcher(dispatcher0_));
412 412
413 // A dispatcher that's not satisfiable should give an error. 413 // A dispatcher that's not satisfiable should give an error.
414 w.Init(); 414 w.Init();
415 EXPECT_EQ(MOJO_RESULT_OK, 415 EXPECT_EQ(MOJO_RESULT_OK,
416 wait_set->AddWaitingDispatcher(dispatcher1_, 416 wait_set->AddWaitingDispatcher(dispatcher1_,
417 MOJO_HANDLE_SIGNAL_READABLE, 0)); 417 MOJO_HANDLE_SIGNAL_READABLE, 0));
418 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(0, nullptr)); 418 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(MOJO_DEADLINE_INDEFINITE, nullptr));
419 EXPECT_TRUE(wait_set->GetHandleSignalsState().satisfies( 419 EXPECT_TRUE(
420 MOJO_HANDLE_SIGNAL_READABLE)); 420 wait_set->GetHandleSignalsState().satisfies(MOJO_HANDLE_SIGNAL_READABLE));
421 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 421 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
422 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); 422 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr));
423 EXPECT_EQ(dispatcher1_, woken_dispatcher); 423 EXPECT_EQ(dispatcher1_, woken_dispatcher);
424 424
425 wait_set->RemoveAwakable(&w, nullptr); 425 wait_set->RemoveAwakable(&w, nullptr);
426 } 426 }
427 427
428 TEST_F(WaitSetDispatcherTest, NestedSets) { 428 TEST_F(WaitSetDispatcherTest, NestedSets) {
429 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher(); 429 scoped_refptr<WaitSetDispatcher> wait_set = new WaitSetDispatcher();
430 CloseOnShutdown(wait_set); 430 CloseOnShutdown(wait_set);
(...skipping 17 matching lines...) Expand all
448 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(0, nullptr)); 448 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(0, nullptr));
449 scoped_refptr<Dispatcher> woken_dispatcher; 449 scoped_refptr<Dispatcher> woken_dispatcher;
450 EXPECT_EQ(MOJO_RESULT_OK, 450 EXPECT_EQ(MOJO_RESULT_OK,
451 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr)); 451 GetOneReadyDispatcher(wait_set, &woken_dispatcher, nullptr));
452 EXPECT_EQ(nested_wait_set, woken_dispatcher); 452 EXPECT_EQ(nested_wait_set, woken_dispatcher);
453 453
454 wait_set->RemoveAwakable(&w, nullptr); 454 wait_set->RemoveAwakable(&w, nullptr);
455 } 455 }
456 456
457 } // namespace 457 } // namespace
458 } // namespace system 458 } // namespace edk
459 } // namespace mojo 459 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/wait_set_dispatcher.cc ('k') | mojo/mojo_edk.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698