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

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

Issue 2750273002: Revert of Mojo EDK: Introduce MojoQueryHandleSignalsState API (Closed)
Patch Set: 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"
9 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 9 #include "base/run_loop.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "mojo/edk/embedder/embedder.h" 10 #include "mojo/edk/embedder/embedder.h"
13 #include "mojo/edk/system/handle_signals_state.h" 11 #include "mojo/edk/system/handle_signals_state.h"
14 #include "mojo/public/c/system/buffer.h" 12 #include "mojo/public/c/system/buffer.h"
15 #include "mojo/public/c/system/data_pipe.h" 13 #include "mojo/public/c/system/data_pipe.h"
16 #include "mojo/public/c/system/functions.h" 14 #include "mojo/public/c/system/functions.h"
17 #include "mojo/public/c/system/watcher.h"
18 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
19 16
20 #if defined(OS_MACOSX) && !defined(OS_IOS) 17 #if defined(OS_MACOSX) && !defined(OS_IOS)
21 #include "base/mac/mach_port_broker.h" 18 #include "base/mac/mach_port_broker.h"
22 #endif 19 #endif
23 20
24 namespace mojo { 21 namespace mojo {
25 namespace edk { 22 namespace edk {
26 namespace test { 23 namespace test {
27 24
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 DCHECK_EQ(MOJO_RESULT_OK,
47 MojoWatch(watcher, handle, signals,
48 reinterpret_cast<uintptr_t>(context_.get())));
49
50 uint32_t num_ready_contexts = 1;
51 uintptr_t ready_context;
52 MojoResult ready_result;
53 MojoHandleSignalsState ready_state;
54 MojoResult 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
125 25
126 #if defined(OS_MACOSX) && !defined(OS_IOS) 26 #if defined(OS_MACOSX) && !defined(OS_IOS)
127 namespace { 27 namespace {
128 base::MachPortBroker* g_mach_broker = nullptr; 28 base::MachPortBroker* g_mach_broker = nullptr;
129 } 29 }
130 #endif 30 #endif
131 31
132 MojoTestBase::MojoTestBase() { 32 MojoTestBase::MojoTestBase() {
133 #if defined(OS_MACOSX) && !defined(OS_IOS) 33 #if defined(OS_MACOSX) && !defined(OS_IOS)
134 if (!g_mach_broker) { 34 if (!g_mach_broker) {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 // static 123 // static
224 void MojoTestBase::WriteMessage(MojoHandle mp, const std::string& message) { 124 void MojoTestBase::WriteMessage(MojoHandle mp, const std::string& message) {
225 WriteMessageWithHandles(mp, message, nullptr, 0); 125 WriteMessageWithHandles(mp, message, nullptr, 0);
226 } 126 }
227 127
228 // static 128 // static
229 std::string MojoTestBase::ReadMessageWithHandles( 129 std::string MojoTestBase::ReadMessageWithHandles(
230 MojoHandle mp, 130 MojoHandle mp,
231 MojoHandle* handles, 131 MojoHandle* handles,
232 uint32_t expected_num_handles) { 132 uint32_t expected_num_handles) {
233 CHECK_EQ(WaitForSignals(mp, MOJO_HANDLE_SIGNAL_READABLE), MOJO_RESULT_OK); 133 CHECK_EQ(MojoWait(mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE,
134 nullptr),
135 MOJO_RESULT_OK);
234 136
235 uint32_t message_size = 0; 137 uint32_t message_size = 0;
236 uint32_t num_handles = 0; 138 uint32_t num_handles = 0;
237 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles, 139 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles,
238 MOJO_READ_MESSAGE_FLAG_NONE), 140 MOJO_READ_MESSAGE_FLAG_NONE),
239 MOJO_RESULT_RESOURCE_EXHAUSTED); 141 MOJO_RESULT_RESOURCE_EXHAUSTED);
240 CHECK_EQ(expected_num_handles, num_handles); 142 CHECK_EQ(expected_num_handles, num_handles);
241 143
242 std::string message(message_size, 'x'); 144 std::string message(message_size, 'x');
243 CHECK_EQ(MojoReadMessage(mp, &message[0], &message_size, handles, 145 CHECK_EQ(MojoReadMessage(mp, &message[0], &message_size, handles,
244 &num_handles, MOJO_READ_MESSAGE_FLAG_NONE), 146 &num_handles, MOJO_READ_MESSAGE_FLAG_NONE),
245 MOJO_RESULT_OK); 147 MOJO_RESULT_OK);
246 CHECK_EQ(message_size, message.size()); 148 CHECK_EQ(message_size, message.size());
247 CHECK_EQ(num_handles, expected_num_handles); 149 CHECK_EQ(num_handles, expected_num_handles);
248 150
249 return message; 151 return message;
250 } 152 }
251 153
252 // static 154 // static
253 std::string MojoTestBase::ReadMessageWithOptionalHandle(MojoHandle mp, 155 std::string MojoTestBase::ReadMessageWithOptionalHandle(MojoHandle mp,
254 MojoHandle* handle) { 156 MojoHandle* handle) {
255 CHECK_EQ(WaitForSignals(mp, MOJO_HANDLE_SIGNAL_READABLE), MOJO_RESULT_OK); 157 CHECK_EQ(MojoWait(mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE,
158 nullptr),
159 MOJO_RESULT_OK);
256 160
257 uint32_t message_size = 0; 161 uint32_t message_size = 0;
258 uint32_t num_handles = 0; 162 uint32_t num_handles = 0;
259 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles, 163 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles,
260 MOJO_READ_MESSAGE_FLAG_NONE), 164 MOJO_READ_MESSAGE_FLAG_NONE),
261 MOJO_RESULT_RESOURCE_EXHAUSTED); 165 MOJO_RESULT_RESOURCE_EXHAUSTED);
262 CHECK(num_handles == 0 || num_handles == 1); 166 CHECK(num_handles == 0 || num_handles == 1);
263 167
264 CHECK(handle); 168 CHECK(handle);
265 169
(...skipping 14 matching lines...) Expand all
280 184
281 // static 185 // static
282 std::string MojoTestBase::ReadMessage(MojoHandle mp) { 186 std::string MojoTestBase::ReadMessage(MojoHandle mp) {
283 return ReadMessageWithHandles(mp, nullptr, 0); 187 return ReadMessageWithHandles(mp, nullptr, 0);
284 } 188 }
285 189
286 // static 190 // static
287 void MojoTestBase::ReadMessage(MojoHandle mp, 191 void MojoTestBase::ReadMessage(MojoHandle mp,
288 char* data, 192 char* data,
289 size_t num_bytes) { 193 size_t num_bytes) {
290 CHECK_EQ(WaitForSignals(mp, MOJO_HANDLE_SIGNAL_READABLE), MOJO_RESULT_OK); 194 CHECK_EQ(MojoWait(mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE,
195 nullptr),
196 MOJO_RESULT_OK);
291 197
292 uint32_t message_size = 0; 198 uint32_t message_size = 0;
293 uint32_t num_handles = 0; 199 uint32_t num_handles = 0;
294 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles, 200 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles,
295 MOJO_READ_MESSAGE_FLAG_NONE), 201 MOJO_READ_MESSAGE_FLAG_NONE),
296 MOJO_RESULT_RESOURCE_EXHAUSTED); 202 MOJO_RESULT_RESOURCE_EXHAUSTED);
297 CHECK_EQ(num_handles, 0u); 203 CHECK_EQ(num_handles, 0u);
298 CHECK_EQ(message_size, num_bytes); 204 CHECK_EQ(message_size, num_bytes);
299 205
300 CHECK_EQ(MojoReadMessage(mp, data, &message_size, nullptr, &num_handles, 206 CHECK_EQ(MojoReadMessage(mp, data, &message_size, nullptr, &num_handles,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 options.element_num_bytes = 1; 281 options.element_num_bytes = 1;
376 options.capacity_num_bytes = static_cast<uint32_t>(capacity); 282 options.capacity_num_bytes = static_cast<uint32_t>(capacity);
377 283
378 MojoCreateDataPipe(&options, p0, p1); 284 MojoCreateDataPipe(&options, p0, p1);
379 CHECK_NE(*p0, MOJO_HANDLE_INVALID); 285 CHECK_NE(*p0, MOJO_HANDLE_INVALID);
380 CHECK_NE(*p1, MOJO_HANDLE_INVALID); 286 CHECK_NE(*p1, MOJO_HANDLE_INVALID);
381 } 287 }
382 288
383 // static 289 // static
384 void MojoTestBase::WriteData(MojoHandle producer, const std::string& data) { 290 void MojoTestBase::WriteData(MojoHandle producer, const std::string& data) {
385 CHECK_EQ(WaitForSignals(producer, MOJO_HANDLE_SIGNAL_WRITABLE), 291 CHECK_EQ(MojoWait(producer, MOJO_HANDLE_SIGNAL_WRITABLE,
292 MOJO_DEADLINE_INDEFINITE, nullptr),
386 MOJO_RESULT_OK); 293 MOJO_RESULT_OK);
387 uint32_t num_bytes = static_cast<uint32_t>(data.size()); 294 uint32_t num_bytes = static_cast<uint32_t>(data.size());
388 CHECK_EQ(MojoWriteData(producer, data.data(), &num_bytes, 295 CHECK_EQ(MojoWriteData(producer, data.data(), &num_bytes,
389 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), 296 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE),
390 MOJO_RESULT_OK); 297 MOJO_RESULT_OK);
391 CHECK_EQ(num_bytes, static_cast<uint32_t>(data.size())); 298 CHECK_EQ(num_bytes, static_cast<uint32_t>(data.size()));
392 } 299 }
393 300
394 // static 301 // static
395 std::string MojoTestBase::ReadData(MojoHandle consumer, size_t size) { 302 std::string MojoTestBase::ReadData(MojoHandle consumer, size_t size) {
396 CHECK_EQ(WaitForSignals(consumer, MOJO_HANDLE_SIGNAL_READABLE), 303 CHECK_EQ(MojoWait(consumer, MOJO_HANDLE_SIGNAL_READABLE,
304 MOJO_DEADLINE_INDEFINITE, nullptr),
397 MOJO_RESULT_OK); 305 MOJO_RESULT_OK);
398 std::vector<char> buffer(size); 306 std::vector<char> buffer(size);
399 uint32_t num_bytes = static_cast<uint32_t>(size); 307 uint32_t num_bytes = static_cast<uint32_t>(size);
400 CHECK_EQ(MojoReadData(consumer, buffer.data(), &num_bytes, 308 CHECK_EQ(MojoReadData(consumer, buffer.data(), &num_bytes,
401 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), 309 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE),
402 MOJO_RESULT_OK); 310 MOJO_RESULT_OK);
403 CHECK_EQ(num_bytes, static_cast<uint32_t>(size)); 311 CHECK_EQ(num_bytes, static_cast<uint32_t>(size));
404 312
405 return std::string(buffer.data(), buffer.size()); 313 return std::string(buffer.data(), buffer.size());
406 } 314 }
407 315
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
423 } // namespace test 316 } // namespace test
424 } // namespace edk 317 } // namespace edk
425 } // namespace mojo 318 } // 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