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

Side by Side Diff: mojo/edk/test/mojo_test_base.cc

Issue 2741033003: Mojo EDK: Introduce MojoQueryHandleSignalsState API (Closed)
Patch Set: fix stupid bad DCHECK Created 3 years, 9 months 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/test/mojo_test_base.h ('k') | mojo/public/c/system/functions.h » ('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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/test/mojo_test_base.h" 5 #include "mojo/edk/test/mojo_test_base.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/memory/ref_counted.h"
8 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/synchronization/waitable_event.h"
10 #include "mojo/edk/embedder/embedder.h" 12 #include "mojo/edk/embedder/embedder.h"
11 #include "mojo/edk/system/handle_signals_state.h" 13 #include "mojo/edk/system/handle_signals_state.h"
12 #include "mojo/public/c/system/buffer.h" 14 #include "mojo/public/c/system/buffer.h"
13 #include "mojo/public/c/system/data_pipe.h" 15 #include "mojo/public/c/system/data_pipe.h"
14 #include "mojo/public/c/system/functions.h" 16 #include "mojo/public/c/system/functions.h"
17 #include "mojo/public/c/system/watcher.h"
15 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
16 19
17 #if defined(OS_MACOSX) && !defined(OS_IOS) 20 #if defined(OS_MACOSX) && !defined(OS_IOS)
18 #include "base/mac/mach_port_broker.h" 21 #include "base/mac/mach_port_broker.h"
19 #endif 22 #endif
20 23
21 namespace mojo { 24 namespace mojo {
22 namespace edk { 25 namespace edk {
23 namespace test { 26 namespace test {
24 27
28 namespace {
29
30 class Waiter {
31 public:
32 Waiter() {}
33 ~Waiter() {}
34
35 MojoResult Wait(MojoHandle handle,
36 MojoHandleSignals signals,
37 MojoHandleSignalsState* state) {
38 MojoHandle watcher;
39 MojoCreateWatcher(&Context::OnNotification, &watcher);
40
41 context_ = new Context();
42
43 // Balanced by OnNotification in the |MOJO_RESULT_CANCELLED| case.
44 context_->AddRef();
45
46 MojoResult rv = MojoWatch(watcher, handle, signals,
47 reinterpret_cast<uintptr_t>(context_.get()));
48 DCHECK_EQ(MOJO_RESULT_OK, rv);
49
50 uint32_t num_ready_contexts = 1;
51 uintptr_t ready_context;
52 MojoResult ready_result;
53 MojoHandleSignalsState ready_state;
54 rv = MojoArmWatcher(watcher, &num_ready_contexts, &ready_context,
55 &ready_result, &ready_state);
56 if (rv == MOJO_RESULT_FAILED_PRECONDITION) {
57 MojoClose(watcher);
58 DCHECK_EQ(1u, num_ready_contexts);
59 if (state)
60 *state = ready_state;
61 return ready_result;
62 }
63
64 // Wait for the first notification.
65 context_->event().Wait();
66
67 ready_result = context_->wait_result();
68 DCHECK_NE(MOJO_RESULT_UNKNOWN, ready_result);
69
70 if (state)
71 *state = context_->wait_state();
72
73 MojoClose(watcher);
74
75 return ready_result;
76 }
77
78 private:
79 class Context : public base::RefCountedThreadSafe<Context> {
80 public:
81 Context()
82 : event_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
83 base::WaitableEvent::InitialState::NOT_SIGNALED) {}
84
85 base::WaitableEvent& event() { return event_; }
86 MojoResult wait_result() const { return wait_result_; }
87 MojoHandleSignalsState wait_state() const { return wait_state_; }
88
89 static void OnNotification(uintptr_t context_value,
90 MojoResult result,
91 MojoHandleSignalsState state,
92 MojoWatcherNotificationFlags flags) {
93 auto* context = reinterpret_cast<Context*>(context_value);
94 context->Notify(result, state);
95 if (result == MOJO_RESULT_CANCELLED)
96 context->Release();
97 }
98
99 private:
100 friend class base::RefCountedThreadSafe<Context>;
101
102 ~Context() {}
103
104 void Notify(MojoResult result, MojoHandleSignalsState state) {
105 if (wait_result_ == MOJO_RESULT_UNKNOWN) {
106 wait_result_ = result;
107 wait_state_ = state;
108 }
109 event_.Signal();
110 }
111
112 base::WaitableEvent event_;
113 MojoResult wait_result_ = MOJO_RESULT_UNKNOWN;
114 MojoHandleSignalsState wait_state_ = {0, 0};
115
116 DISALLOW_COPY_AND_ASSIGN(Context);
117 };
118
119 scoped_refptr<Context> context_;
120
121 DISALLOW_COPY_AND_ASSIGN(Waiter);
122 };
123
124 } // namespace
25 125
26 #if defined(OS_MACOSX) && !defined(OS_IOS) 126 #if defined(OS_MACOSX) && !defined(OS_IOS)
27 namespace { 127 namespace {
28 base::MachPortBroker* g_mach_broker = nullptr; 128 base::MachPortBroker* g_mach_broker = nullptr;
29 } 129 }
30 #endif 130 #endif
31 131
32 MojoTestBase::MojoTestBase() { 132 MojoTestBase::MojoTestBase() {
33 #if defined(OS_MACOSX) && !defined(OS_IOS) 133 #if defined(OS_MACOSX) && !defined(OS_IOS)
34 if (!g_mach_broker) { 134 if (!g_mach_broker) {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 // static 223 // static
124 void MojoTestBase::WriteMessage(MojoHandle mp, const std::string& message) { 224 void MojoTestBase::WriteMessage(MojoHandle mp, const std::string& message) {
125 WriteMessageWithHandles(mp, message, nullptr, 0); 225 WriteMessageWithHandles(mp, message, nullptr, 0);
126 } 226 }
127 227
128 // static 228 // static
129 std::string MojoTestBase::ReadMessageWithHandles( 229 std::string MojoTestBase::ReadMessageWithHandles(
130 MojoHandle mp, 230 MojoHandle mp,
131 MojoHandle* handles, 231 MojoHandle* handles,
132 uint32_t expected_num_handles) { 232 uint32_t expected_num_handles) {
133 CHECK_EQ(MojoWait(mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, 233 CHECK_EQ(WaitForSignals(mp, MOJO_HANDLE_SIGNAL_READABLE), MOJO_RESULT_OK);
134 nullptr),
135 MOJO_RESULT_OK);
136 234
137 uint32_t message_size = 0; 235 uint32_t message_size = 0;
138 uint32_t num_handles = 0; 236 uint32_t num_handles = 0;
139 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles, 237 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles,
140 MOJO_READ_MESSAGE_FLAG_NONE), 238 MOJO_READ_MESSAGE_FLAG_NONE),
141 MOJO_RESULT_RESOURCE_EXHAUSTED); 239 MOJO_RESULT_RESOURCE_EXHAUSTED);
142 CHECK_EQ(expected_num_handles, num_handles); 240 CHECK_EQ(expected_num_handles, num_handles);
143 241
144 std::string message(message_size, 'x'); 242 std::string message(message_size, 'x');
145 CHECK_EQ(MojoReadMessage(mp, &message[0], &message_size, handles, 243 CHECK_EQ(MojoReadMessage(mp, &message[0], &message_size, handles,
146 &num_handles, MOJO_READ_MESSAGE_FLAG_NONE), 244 &num_handles, MOJO_READ_MESSAGE_FLAG_NONE),
147 MOJO_RESULT_OK); 245 MOJO_RESULT_OK);
148 CHECK_EQ(message_size, message.size()); 246 CHECK_EQ(message_size, message.size());
149 CHECK_EQ(num_handles, expected_num_handles); 247 CHECK_EQ(num_handles, expected_num_handles);
150 248
151 return message; 249 return message;
152 } 250 }
153 251
154 // static 252 // static
155 std::string MojoTestBase::ReadMessageWithOptionalHandle(MojoHandle mp, 253 std::string MojoTestBase::ReadMessageWithOptionalHandle(MojoHandle mp,
156 MojoHandle* handle) { 254 MojoHandle* handle) {
157 CHECK_EQ(MojoWait(mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, 255 CHECK_EQ(WaitForSignals(mp, MOJO_HANDLE_SIGNAL_READABLE), MOJO_RESULT_OK);
158 nullptr),
159 MOJO_RESULT_OK);
160 256
161 uint32_t message_size = 0; 257 uint32_t message_size = 0;
162 uint32_t num_handles = 0; 258 uint32_t num_handles = 0;
163 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles, 259 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles,
164 MOJO_READ_MESSAGE_FLAG_NONE), 260 MOJO_READ_MESSAGE_FLAG_NONE),
165 MOJO_RESULT_RESOURCE_EXHAUSTED); 261 MOJO_RESULT_RESOURCE_EXHAUSTED);
166 CHECK(num_handles == 0 || num_handles == 1); 262 CHECK(num_handles == 0 || num_handles == 1);
167 263
168 CHECK(handle); 264 CHECK(handle);
169 265
(...skipping 14 matching lines...) Expand all
184 280
185 // static 281 // static
186 std::string MojoTestBase::ReadMessage(MojoHandle mp) { 282 std::string MojoTestBase::ReadMessage(MojoHandle mp) {
187 return ReadMessageWithHandles(mp, nullptr, 0); 283 return ReadMessageWithHandles(mp, nullptr, 0);
188 } 284 }
189 285
190 // static 286 // static
191 void MojoTestBase::ReadMessage(MojoHandle mp, 287 void MojoTestBase::ReadMessage(MojoHandle mp,
192 char* data, 288 char* data,
193 size_t num_bytes) { 289 size_t num_bytes) {
194 CHECK_EQ(MojoWait(mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, 290 CHECK_EQ(WaitForSignals(mp, MOJO_HANDLE_SIGNAL_READABLE), MOJO_RESULT_OK);
195 nullptr),
196 MOJO_RESULT_OK);
197 291
198 uint32_t message_size = 0; 292 uint32_t message_size = 0;
199 uint32_t num_handles = 0; 293 uint32_t num_handles = 0;
200 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles, 294 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles,
201 MOJO_READ_MESSAGE_FLAG_NONE), 295 MOJO_READ_MESSAGE_FLAG_NONE),
202 MOJO_RESULT_RESOURCE_EXHAUSTED); 296 MOJO_RESULT_RESOURCE_EXHAUSTED);
203 CHECK_EQ(num_handles, 0u); 297 CHECK_EQ(num_handles, 0u);
204 CHECK_EQ(message_size, num_bytes); 298 CHECK_EQ(message_size, num_bytes);
205 299
206 CHECK_EQ(MojoReadMessage(mp, data, &message_size, nullptr, &num_handles, 300 CHECK_EQ(MojoReadMessage(mp, data, &message_size, nullptr, &num_handles,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 options.element_num_bytes = 1; 375 options.element_num_bytes = 1;
282 options.capacity_num_bytes = static_cast<uint32_t>(capacity); 376 options.capacity_num_bytes = static_cast<uint32_t>(capacity);
283 377
284 MojoCreateDataPipe(&options, p0, p1); 378 MojoCreateDataPipe(&options, p0, p1);
285 CHECK_NE(*p0, MOJO_HANDLE_INVALID); 379 CHECK_NE(*p0, MOJO_HANDLE_INVALID);
286 CHECK_NE(*p1, MOJO_HANDLE_INVALID); 380 CHECK_NE(*p1, MOJO_HANDLE_INVALID);
287 } 381 }
288 382
289 // static 383 // static
290 void MojoTestBase::WriteData(MojoHandle producer, const std::string& data) { 384 void MojoTestBase::WriteData(MojoHandle producer, const std::string& data) {
291 CHECK_EQ(MojoWait(producer, MOJO_HANDLE_SIGNAL_WRITABLE, 385 CHECK_EQ(WaitForSignals(producer, MOJO_HANDLE_SIGNAL_WRITABLE),
292 MOJO_DEADLINE_INDEFINITE, nullptr),
293 MOJO_RESULT_OK); 386 MOJO_RESULT_OK);
294 uint32_t num_bytes = static_cast<uint32_t>(data.size()); 387 uint32_t num_bytes = static_cast<uint32_t>(data.size());
295 CHECK_EQ(MojoWriteData(producer, data.data(), &num_bytes, 388 CHECK_EQ(MojoWriteData(producer, data.data(), &num_bytes,
296 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), 389 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE),
297 MOJO_RESULT_OK); 390 MOJO_RESULT_OK);
298 CHECK_EQ(num_bytes, static_cast<uint32_t>(data.size())); 391 CHECK_EQ(num_bytes, static_cast<uint32_t>(data.size()));
299 } 392 }
300 393
301 // static 394 // static
302 std::string MojoTestBase::ReadData(MojoHandle consumer, size_t size) { 395 std::string MojoTestBase::ReadData(MojoHandle consumer, size_t size) {
303 CHECK_EQ(MojoWait(consumer, MOJO_HANDLE_SIGNAL_READABLE, 396 CHECK_EQ(WaitForSignals(consumer, MOJO_HANDLE_SIGNAL_READABLE),
304 MOJO_DEADLINE_INDEFINITE, nullptr),
305 MOJO_RESULT_OK); 397 MOJO_RESULT_OK);
306 std::vector<char> buffer(size); 398 std::vector<char> buffer(size);
307 uint32_t num_bytes = static_cast<uint32_t>(size); 399 uint32_t num_bytes = static_cast<uint32_t>(size);
308 CHECK_EQ(MojoReadData(consumer, buffer.data(), &num_bytes, 400 CHECK_EQ(MojoReadData(consumer, buffer.data(), &num_bytes,
309 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), 401 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE),
310 MOJO_RESULT_OK); 402 MOJO_RESULT_OK);
311 CHECK_EQ(num_bytes, static_cast<uint32_t>(size)); 403 CHECK_EQ(num_bytes, static_cast<uint32_t>(size));
312 404
313 return std::string(buffer.data(), buffer.size()); 405 return std::string(buffer.data(), buffer.size());
314 } 406 }
315 407
408 // static
409 MojoHandleSignalsState MojoTestBase::GetSignalsState(MojoHandle handle) {
410 MojoHandleSignalsState signals_state;
411 CHECK_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(handle, &signals_state));
412 return signals_state;
413 }
414
415 // static
416 MojoResult MojoTestBase::WaitForSignals(MojoHandle handle,
417 MojoHandleSignals signals,
418 MojoHandleSignalsState* state) {
419 Waiter waiter;
420 return waiter.Wait(handle, signals, state);
421 }
422
316 } // namespace test 423 } // namespace test
317 } // namespace edk 424 } // namespace edk
318 } // namespace mojo 425 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/test/mojo_test_base.h ('k') | mojo/public/c/system/functions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698