Bluetooth: add Device events, and cleanup JS API
[chromium-blink-merge.git] / mojo / system / local_data_pipe_unittest.cc
blob5fd45a2c7518a4a8b779d147197b92135fc86c86
1 // Copyright 2013 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.
5 #include "mojo/system/local_data_pipe.h"
7 #include <string.h>
9 #include "base/basictypes.h"
10 #include "base/memory/ref_counted.h"
11 #include "mojo/system/data_pipe.h"
12 #include "mojo/system/waiter.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace mojo {
16 namespace system {
17 namespace {
19 const uint32_t kSizeOfOptions =
20 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions));
22 // Validate options.
23 TEST(LocalDataPipeTest, Creation) {
24 // Create using default options.
26 // Get default options.
27 MojoCreateDataPipeOptions default_options = { 0 };
28 EXPECT_EQ(MOJO_RESULT_OK,
29 DataPipe::ValidateOptions(NULL, &default_options));
30 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(default_options));
31 dp->ProducerClose();
32 dp->ConsumerClose();
35 // Create using non-default options.
37 const MojoCreateDataPipeOptions options = {
38 kSizeOfOptions, // |struct_size|.
39 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
40 1, // |element_num_bytes|.
41 1000 // |capacity_num_bytes|.
43 MojoCreateDataPipeOptions validated_options = { 0 };
44 EXPECT_EQ(MOJO_RESULT_OK,
45 DataPipe::ValidateOptions(&options, &validated_options));
46 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
47 dp->ProducerClose();
48 dp->ConsumerClose();
51 const MojoCreateDataPipeOptions options = {
52 kSizeOfOptions, // |struct_size|.
53 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
54 4, // |element_num_bytes|.
55 4000 // |capacity_num_bytes|.
57 MojoCreateDataPipeOptions validated_options = { 0 };
58 EXPECT_EQ(MOJO_RESULT_OK,
59 DataPipe::ValidateOptions(&options, &validated_options));
60 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
61 dp->ProducerClose();
62 dp->ConsumerClose();
65 const MojoCreateDataPipeOptions options = {
66 kSizeOfOptions, // |struct_size|.
67 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|.
68 7, // |element_num_bytes|.
69 7000000 // |capacity_num_bytes|.
71 MojoCreateDataPipeOptions validated_options = { 0 };
72 EXPECT_EQ(MOJO_RESULT_OK,
73 DataPipe::ValidateOptions(&options, &validated_options));
74 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
75 dp->ProducerClose();
76 dp->ConsumerClose();
78 // Default capacity.
80 const MojoCreateDataPipeOptions options = {
81 kSizeOfOptions, // |struct_size|.
82 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|.
83 100, // |element_num_bytes|.
84 0 // |capacity_num_bytes|.
86 MojoCreateDataPipeOptions validated_options = { 0 };
87 EXPECT_EQ(MOJO_RESULT_OK,
88 DataPipe::ValidateOptions(&options, &validated_options));
89 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
90 dp->ProducerClose();
91 dp->ConsumerClose();
95 TEST(LocalDataPipeTest, SimpleReadWrite) {
96 const MojoCreateDataPipeOptions options = {
97 kSizeOfOptions, // |struct_size|.
98 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
99 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
100 1000 * sizeof(int32_t) // |capacity_num_bytes|.
102 MojoCreateDataPipeOptions validated_options = { 0 };
103 EXPECT_EQ(MOJO_RESULT_OK,
104 DataPipe::ValidateOptions(&options, &validated_options));
106 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
108 int32_t elements[10] = { 0 };
109 uint32_t num_bytes = 0;
111 // Try reading; nothing there yet.
112 num_bytes = static_cast<uint32_t>(arraysize(elements) * sizeof(elements[0]));
113 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
114 dp->ConsumerReadData(elements, &num_bytes, false));
116 // Query; nothing there yet.
117 num_bytes = 0;
118 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
119 EXPECT_EQ(0u, num_bytes);
121 // Discard; nothing there yet.
122 num_bytes = static_cast<uint32_t>(5u * sizeof(elements[0]));
123 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
124 dp->ConsumerDiscardData(&num_bytes, false));
126 // Read with invalid |num_bytes|.
127 num_bytes = sizeof(elements[0]) + 1;
128 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
129 dp->ConsumerReadData(elements, &num_bytes, false));
131 // Write two elements.
132 elements[0] = 123;
133 elements[1] = 456;
134 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
135 EXPECT_EQ(MOJO_RESULT_OK,
136 dp->ProducerWriteData(elements, &num_bytes, false));
137 // It should have written everything (even without "all or none").
138 EXPECT_EQ(2u * sizeof(elements[0]), num_bytes);
140 // Query.
141 num_bytes = 0;
142 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
143 EXPECT_EQ(2 * sizeof(elements[0]), num_bytes);
145 // Read one element.
146 elements[0] = -1;
147 elements[1] = -1;
148 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
149 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(elements, &num_bytes, false));
150 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes);
151 EXPECT_EQ(123, elements[0]);
152 EXPECT_EQ(-1, elements[1]);
154 // Query.
155 num_bytes = 0;
156 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
157 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes);
159 // Try to read two elements, with "all or none".
160 elements[0] = -1;
161 elements[1] = -1;
162 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
163 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
164 dp->ConsumerReadData(elements, &num_bytes, true));
165 EXPECT_EQ(-1, elements[0]);
166 EXPECT_EQ(-1, elements[1]);
168 // Try to read two elements, without "all or none".
169 elements[0] = -1;
170 elements[1] = -1;
171 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
172 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(elements, &num_bytes, false));
173 EXPECT_EQ(456, elements[0]);
174 EXPECT_EQ(-1, elements[1]);
176 // Query.
177 num_bytes = 0;
178 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
179 EXPECT_EQ(0u, num_bytes);
181 dp->ProducerClose();
182 dp->ConsumerClose();
185 // Note: The "basic" waiting tests test that the "wait states" are correct in
186 // various situations; they don't test that waiters are properly awoken on state
187 // changes. (For that, we need to use multiple threads.)
188 TEST(LocalDataPipeTest, BasicProducerWaiting) {
189 // Note: We take advantage of the fact that for |LocalDataPipe|, capacities
190 // are strict maximums. This is not guaranteed by the API.
192 const MojoCreateDataPipeOptions options = {
193 kSizeOfOptions, // |struct_size|.
194 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
195 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
196 2 * sizeof(int32_t) // |capacity_num_bytes|.
198 MojoCreateDataPipeOptions validated_options = { 0 };
199 EXPECT_EQ(MOJO_RESULT_OK,
200 DataPipe::ValidateOptions(&options, &validated_options));
202 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
203 Waiter waiter;
205 // Never readable.
206 waiter.Init();
207 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
208 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 12));
210 // Already writable.
211 waiter.Init();
212 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
213 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 34));
215 // Write two elements.
216 int32_t elements[2] = { 123, 456 };
217 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
218 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, true));
219 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes);
221 // Adding a waiter should now succeed.
222 waiter.Init();
223 EXPECT_EQ(MOJO_RESULT_OK,
224 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 56));
225 // And it shouldn't be writable yet.
226 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0));
227 dp->ProducerRemoveWaiter(&waiter);
229 // Do it again.
230 waiter.Init();
231 EXPECT_EQ(MOJO_RESULT_OK,
232 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 78));
234 // Read one element.
235 elements[0] = -1;
236 elements[1] = -1;
237 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
238 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(elements, &num_bytes, true));
239 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
240 EXPECT_EQ(123, elements[0]);
241 EXPECT_EQ(-1, elements[1]);
243 // Waiting should now succeed.
244 EXPECT_EQ(78, waiter.Wait(1000));
245 dp->ProducerRemoveWaiter(&waiter);
247 // Try writing, using a two-phase write.
248 void* buffer = NULL;
249 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0]));
250 EXPECT_EQ(MOJO_RESULT_OK,
251 dp->ProducerBeginWriteData(&buffer, &num_bytes, false));
252 EXPECT_TRUE(buffer != NULL);
253 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
255 static_cast<int32_t*>(buffer)[0] = 789;
256 EXPECT_EQ(MOJO_RESULT_OK,
257 dp->ProducerEndWriteData(
258 static_cast<uint32_t>(1u * sizeof(elements[0]))));
260 // Add a waiter.
261 waiter.Init();
262 EXPECT_EQ(MOJO_RESULT_OK,
263 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 90));
265 // Read one element, using a two-phase read.
266 const void* read_buffer = NULL;
267 num_bytes = 0u;
268 EXPECT_EQ(MOJO_RESULT_OK,
269 dp->ConsumerBeginReadData(&read_buffer, &num_bytes, false));
270 EXPECT_TRUE(read_buffer != NULL);
271 // Since we only read one element (after having written three in all), the
272 // two-phase read should only allow us to read one. This checks an
273 // implementation detail!
274 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
275 EXPECT_EQ(456, static_cast<const int32_t*>(read_buffer)[0]);
276 EXPECT_EQ(MOJO_RESULT_OK,
277 dp->ConsumerEndReadData(
278 static_cast<uint32_t>(1u * sizeof(elements[0]))));
280 // Waiting should succeed.
281 EXPECT_EQ(90, waiter.Wait(1000));
282 dp->ProducerRemoveWaiter(&waiter);
284 // Write one element.
285 elements[0] = 123;
286 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
287 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, false));
288 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
290 // Add a waiter.
291 waiter.Init();
292 EXPECT_EQ(MOJO_RESULT_OK,
293 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 12));
295 // Close the consumer.
296 dp->ConsumerClose();
298 // It should now be never-writable.
299 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000));
300 dp->ProducerRemoveWaiter(&waiter);
302 dp->ProducerClose();
305 TEST(LocalDataPipeTest, BasicConsumerWaiting) {
306 const MojoCreateDataPipeOptions options = {
307 kSizeOfOptions, // |struct_size|.
308 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
309 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
310 1000 * sizeof(int32_t) // |capacity_num_bytes|.
312 MojoCreateDataPipeOptions validated_options = { 0 };
313 EXPECT_EQ(MOJO_RESULT_OK,
314 DataPipe::ValidateOptions(&options, &validated_options));
317 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
318 Waiter waiter;
320 // Never writable.
321 waiter.Init();
322 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
323 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 12));
325 // Not yet readable.
326 waiter.Init();
327 EXPECT_EQ(MOJO_RESULT_OK,
328 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 34));
329 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0));
330 dp->ConsumerRemoveWaiter(&waiter);
332 // Write two elements.
333 int32_t elements[2] = { 123, 456 };
334 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
335 EXPECT_EQ(MOJO_RESULT_OK,
336 dp->ProducerWriteData(elements, &num_bytes, true));
338 // Should already be readable.
339 waiter.Init();
340 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
341 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 56));
343 // Discard one element.
344 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
345 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerDiscardData(&num_bytes, true));
346 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
348 // Should still be readable.
349 waiter.Init();
350 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
351 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 78));
353 // Read one element.
354 elements[0] = -1;
355 elements[1] = -1;
356 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
357 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(elements, &num_bytes, true));
358 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
359 EXPECT_EQ(456, elements[0]);
360 EXPECT_EQ(-1, elements[1]);
362 // Adding a waiter should now succeed.
363 waiter.Init();
364 EXPECT_EQ(MOJO_RESULT_OK,
365 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 90));
367 // Write one element.
368 elements[0] = 789;
369 elements[1] = -1;
370 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
371 EXPECT_EQ(MOJO_RESULT_OK,
372 dp->ProducerWriteData(elements, &num_bytes, true));
374 // Waiting should now succeed.
375 EXPECT_EQ(90, waiter.Wait(1000));
376 dp->ConsumerRemoveWaiter(&waiter);
378 // Close the producer.
379 dp->ProducerClose();
381 // Should still be readable.
382 waiter.Init();
383 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
384 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 12));
386 // Read one element.
387 elements[0] = -1;
388 elements[1] = -1;
389 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
390 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(elements, &num_bytes, true));
391 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
392 EXPECT_EQ(789, elements[0]);
393 EXPECT_EQ(-1, elements[1]);
395 // Should be never-readable.
396 waiter.Init();
397 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
398 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 34));
400 dp->ConsumerClose();
403 // Test with two-phase APIs and closing the producer with an active consumer
404 // waiter.
406 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
407 Waiter waiter;
409 // Write two elements.
410 int32_t* elements = NULL;
411 void* buffer = NULL;
412 // Request room for three (but we'll only write two).
413 uint32_t num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0]));
414 EXPECT_EQ(MOJO_RESULT_OK,
415 dp->ProducerBeginWriteData(&buffer, &num_bytes, true));
416 EXPECT_TRUE(buffer != NULL);
417 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0])));
418 elements = static_cast<int32_t*>(buffer);
419 elements[0] = 123;
420 elements[1] = 456;
421 EXPECT_EQ(MOJO_RESULT_OK,
422 dp->ProducerEndWriteData(
423 static_cast<uint32_t>(2u * sizeof(elements[0]))));
425 // Should already be readable.
426 waiter.Init();
427 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
428 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 12));
430 // Read one element.
431 // Request two in all-or-none mode, but only read one.
432 const void* read_buffer = NULL;
433 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
434 EXPECT_EQ(MOJO_RESULT_OK,
435 dp->ConsumerBeginReadData(&read_buffer, &num_bytes, true));
436 EXPECT_TRUE(read_buffer != NULL);
437 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes);
438 const int32_t* read_elements = static_cast<const int32_t*>(read_buffer);
439 EXPECT_EQ(123, read_elements[0]);
440 EXPECT_EQ(MOJO_RESULT_OK,
441 dp->ConsumerEndReadData(
442 static_cast<uint32_t>(1u * sizeof(elements[0]))));
444 // Should still be readable.
445 waiter.Init();
446 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
447 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 34));
449 // Read one element.
450 // Request three, but not in all-or-none mode.
451 read_buffer = NULL;
452 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0]));
453 EXPECT_EQ(MOJO_RESULT_OK,
454 dp->ConsumerBeginReadData(&read_buffer, &num_bytes, false));
455 EXPECT_TRUE(read_buffer != NULL);
456 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
457 read_elements = static_cast<const int32_t*>(read_buffer);
458 EXPECT_EQ(456, read_elements[0]);
459 EXPECT_EQ(MOJO_RESULT_OK,
460 dp->ConsumerEndReadData(
461 static_cast<uint32_t>(1u * sizeof(elements[0]))));
463 // Adding a waiter should now succeed.
464 waiter.Init();
465 EXPECT_EQ(MOJO_RESULT_OK,
466 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 56));
468 // Close the producer.
469 dp->ProducerClose();
471 // Should be never-readable.
472 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000));
473 dp->ConsumerRemoveWaiter(&waiter);
475 dp->ConsumerClose();
479 // Tests that data pipes aren't writable/readable during two-phase writes/reads.
480 TEST(LocalDataPipeTest, BasicTwoPhaseWaiting) {
481 const MojoCreateDataPipeOptions options = {
482 kSizeOfOptions, // |struct_size|.
483 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
484 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
485 1000 * sizeof(int32_t) // |capacity_num_bytes|.
487 MojoCreateDataPipeOptions validated_options = { 0 };
488 EXPECT_EQ(MOJO_RESULT_OK,
489 DataPipe::ValidateOptions(&options, &validated_options));
491 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
492 Waiter waiter;
494 // It should be writable.
495 waiter.Init();
496 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
497 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 0));
499 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t));
500 void* write_ptr = NULL;
501 EXPECT_EQ(MOJO_RESULT_OK,
502 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false));
503 EXPECT_TRUE(write_ptr != NULL);
504 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t)));
506 // At this point, it shouldn't be writable.
507 waiter.Init();
508 EXPECT_EQ(MOJO_RESULT_OK,
509 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 1));
510 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0));
511 dp->ProducerRemoveWaiter(&waiter);
513 // It shouldn't be readable yet either.
514 waiter.Init();
515 EXPECT_EQ(MOJO_RESULT_OK,
516 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 2));
517 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0));
518 dp->ConsumerRemoveWaiter(&waiter);
520 static_cast<int32_t*>(write_ptr)[0] = 123;
521 EXPECT_EQ(MOJO_RESULT_OK,
522 dp->ProducerEndWriteData(
523 static_cast<uint32_t>(1u * sizeof(int32_t))));
525 // It should be writable again.
526 waiter.Init();
527 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
528 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 3));
530 // And readable.
531 waiter.Init();
532 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
533 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 4));
535 // Start another two-phase write and check that it's readable even in the
536 // middle of it.
537 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t));
538 write_ptr = NULL;
539 EXPECT_EQ(MOJO_RESULT_OK,
540 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false));
541 EXPECT_TRUE(write_ptr != NULL);
542 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t)));
544 // It should be readable.
545 waiter.Init();
546 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
547 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 5));
549 // End the two-phase write without writing anything.
550 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u));
552 // Start a two-phase read.
553 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t));
554 const void* read_ptr = NULL;
555 EXPECT_EQ(MOJO_RESULT_OK,
556 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false));
557 EXPECT_TRUE(read_ptr != NULL);
558 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(int32_t)), num_bytes);
560 // At this point, it should still be writable.
561 waiter.Init();
562 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
563 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 6));
565 // But not readable.
566 waiter.Init();
567 EXPECT_EQ(MOJO_RESULT_OK,
568 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 7));
569 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0));
570 dp->ConsumerRemoveWaiter(&waiter);
572 // End the two-phase read without reading anything.
573 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u));
575 // It should be readable again.
576 waiter.Init();
577 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
578 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 8));
580 dp->ProducerClose();
581 dp->ConsumerClose();
584 // Test that a "may discard" data pipe is writable even when it's full.
585 TEST(LocalDataPipeTest, BasicMayDiscardWaiting) {
586 const MojoCreateDataPipeOptions options = {
587 kSizeOfOptions, // |struct_size|.
588 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|.
589 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
590 1 * sizeof(int32_t) // |capacity_num_bytes|.
592 MojoCreateDataPipeOptions validated_options = { 0 };
593 EXPECT_EQ(MOJO_RESULT_OK,
594 DataPipe::ValidateOptions(&options, &validated_options));
596 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
597 Waiter waiter;
599 // Writable.
600 waiter.Init();
601 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
602 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 0));
604 // Not readable.
605 waiter.Init();
606 EXPECT_EQ(MOJO_RESULT_OK,
607 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 1));
608 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0));
609 dp->ConsumerRemoveWaiter(&waiter);
611 uint32_t num_bytes = static_cast<uint32_t>(sizeof(int32_t));
612 int32_t element = 123;
613 EXPECT_EQ(MOJO_RESULT_OK,
614 dp->ProducerWriteData(&element, &num_bytes, false));
615 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes);
617 // Still writable (even though it's full.
618 waiter.Init();
619 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
620 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 2));
622 // Now readable.
623 waiter.Init();
624 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
625 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 3));
627 // Overwrite that element.
628 num_bytes = static_cast<uint32_t>(sizeof(int32_t));
629 element = 456;
630 EXPECT_EQ(MOJO_RESULT_OK,
631 dp->ProducerWriteData(&element, &num_bytes, false));
632 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes);
634 // Still writable.
635 waiter.Init();
636 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
637 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 4));
639 // And still readable.
640 waiter.Init();
641 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
642 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 5));
644 // Read that element.
645 num_bytes = static_cast<uint32_t>(sizeof(int32_t));
646 element = 0;
647 EXPECT_EQ(MOJO_RESULT_OK,
648 dp->ConsumerReadData(&element, &num_bytes, false));
649 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes);
650 EXPECT_EQ(456, element);
652 // Still writable.
653 waiter.Init();
654 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
655 dp->ProducerAddWaiter(&waiter, MOJO_WAIT_FLAG_WRITABLE, 6));
657 // No longer readable.
658 waiter.Init();
659 EXPECT_EQ(MOJO_RESULT_OK,
660 dp->ConsumerAddWaiter(&waiter, MOJO_WAIT_FLAG_READABLE, 7));
661 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0));
662 dp->ConsumerRemoveWaiter(&waiter);
664 dp->ProducerClose();
665 dp->ConsumerClose();
668 void Seq(int32_t start, size_t count, int32_t* out) {
669 for (size_t i = 0; i < count; i++)
670 out[i] = start + static_cast<int32_t>(i);
673 TEST(LocalDataPipeTest, MayDiscard) {
674 const MojoCreateDataPipeOptions options = {
675 kSizeOfOptions, // |struct_size|.
676 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|.
677 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
678 10 * sizeof(int32_t) // |capacity_num_bytes|.
680 MojoCreateDataPipeOptions validated_options = { 0 };
681 EXPECT_EQ(MOJO_RESULT_OK,
682 DataPipe::ValidateOptions(&options, &validated_options));
684 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
686 int32_t buffer[100] = { 0 };
687 uint32_t num_bytes = 0;
689 num_bytes = 20u * sizeof(int32_t);
690 Seq(0, arraysize(buffer), buffer);
691 // Try writing more than capacity. (This test relies on the implementation
692 // enforcing the capacity strictly.)
693 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, false));
694 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
696 // Read half of what we wrote.
697 num_bytes = 5u * sizeof(int32_t);
698 memset(buffer, 0xab, sizeof(buffer));
699 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, false));
700 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
701 int32_t expected_buffer[100];
702 memset(expected_buffer, 0xab, sizeof(expected_buffer));
703 Seq(0, 5u, expected_buffer);
704 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
705 // Internally, a circular buffer would now look like:
706 // -, -, -, -, -, 5, 6, 7, 8, 9
708 // Write a bit more than the space that's available.
709 num_bytes = 8u * sizeof(int32_t);
710 Seq(100, arraysize(buffer), buffer);
711 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, false));
712 EXPECT_EQ(8u * sizeof(int32_t), num_bytes);
713 // Internally, a circular buffer would now look like:
714 // 100, 101, 102, 103, 104, 105, 106, 107, 8, 9
716 // Read half of what's available.
717 num_bytes = 5u * sizeof(int32_t);
718 memset(buffer, 0xab, sizeof(buffer));
719 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, false));
720 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
721 memset(expected_buffer, 0xab, sizeof(expected_buffer));
722 expected_buffer[0] = 8;
723 expected_buffer[1] = 9;
724 expected_buffer[2] = 100;
725 expected_buffer[3] = 101;
726 expected_buffer[4] = 102;
727 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
728 // Internally, a circular buffer would now look like:
729 // -, -, -, 103, 104, 105, 106, 107, -, -
731 // Write one integer.
732 num_bytes = 1u * sizeof(int32_t);
733 Seq(200, arraysize(buffer), buffer);
734 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, false));
735 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
736 // Internally, a circular buffer would now look like:
737 // -, -, -, 103, 104, 105, 106, 107, 200, -
739 // Write five more.
740 num_bytes = 5u * sizeof(int32_t);
741 Seq(300, arraysize(buffer), buffer);
742 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, false));
743 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
744 // Internally, a circular buffer would now look like:
745 // 301, 302, 303, 304, 104, 105, 106, 107, 200, 300
747 // Read it all.
748 num_bytes = sizeof(buffer);
749 memset(buffer, 0xab, sizeof(buffer));
750 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, false));
751 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
752 memset(expected_buffer, 0xab, sizeof(expected_buffer));
753 expected_buffer[0] = 104;
754 expected_buffer[1] = 105;
755 expected_buffer[2] = 106;
756 expected_buffer[3] = 107;
757 expected_buffer[4] = 200;
758 expected_buffer[5] = 300;
759 expected_buffer[6] = 301;
760 expected_buffer[7] = 302;
761 expected_buffer[8] = 303;
762 expected_buffer[9] = 304;
763 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
765 // Test two-phase writes, including in all-or-none mode.
766 // Note: Again, the following depends on an implementation detail -- namely
767 // that the write pointer will point at the 5th element of the buffer (and the
768 // buffer has exactly the capacity requested).
770 num_bytes = 0u;
771 void* write_ptr = NULL;
772 EXPECT_EQ(MOJO_RESULT_OK,
773 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false));
774 EXPECT_TRUE(write_ptr != NULL);
775 EXPECT_EQ(6u * sizeof(int32_t), num_bytes);
776 Seq(400, 6, static_cast<int32_t*>(write_ptr));
777 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(6u * sizeof(int32_t)));
778 // Internally, a circular buffer would now look like:
779 // -, -, -, -, 400, 401, 402, 403, 404, 405
781 // |ProducerBeginWriteData()| ignores |*num_bytes| except in "all-or-none"
782 // mode.
783 num_bytes = 6u * sizeof(int32_t);
784 write_ptr = NULL;
785 EXPECT_EQ(MOJO_RESULT_OK,
786 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false));
787 EXPECT_EQ(4u * sizeof(int32_t), num_bytes);
788 static_cast<int32_t*>(write_ptr)[0] = 500;
789 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(1u * sizeof(int32_t)));
790 // Internally, a circular buffer would now look like:
791 // 500, -, -, -, 400, 401, 402, 403, 404, 405
793 // Requesting a 10-element buffer in all-or-none mode fails at this point.
794 num_bytes = 10u * sizeof(int32_t);
795 write_ptr = NULL;
796 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
797 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true));
799 // But requesting, say, a 5-element (up to 9, really) buffer should be okay.
800 // It will discard two elements.
801 num_bytes = 5u * sizeof(int32_t);
802 write_ptr = NULL;
803 EXPECT_EQ(MOJO_RESULT_OK,
804 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true));
805 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
806 // Only write 4 elements though.
807 Seq(600, 4, static_cast<int32_t*>(write_ptr));
808 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(4u * sizeof(int32_t)));
809 // Internally, a circular buffer would now look like:
810 // 500, 600, 601, 602, 603, -, 402, 403, 404, 405
812 // Do this again. Make sure we can get a buffer all the way out to the end of
813 // the internal buffer.
814 num_bytes = 5u * sizeof(int32_t);
815 write_ptr = NULL;
816 EXPECT_EQ(MOJO_RESULT_OK,
817 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true));
818 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
819 // Only write 3 elements though.
820 Seq(700, 3, static_cast<int32_t*>(write_ptr));
821 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(3u * sizeof(int32_t)));
822 // Internally, a circular buffer would now look like:
823 // 500, 600, 601, 602, 603, 700, 701, 702, -, -
825 // Read everything.
826 num_bytes = sizeof(buffer);
827 memset(buffer, 0xab, sizeof(buffer));
828 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, false));
829 EXPECT_EQ(8u * sizeof(int32_t), num_bytes);
830 memset(expected_buffer, 0xab, sizeof(expected_buffer));
831 expected_buffer[0] = 500;
832 expected_buffer[1] = 600;
833 expected_buffer[2] = 601;
834 expected_buffer[3] = 602;
835 expected_buffer[4] = 603;
836 expected_buffer[5] = 700;
837 expected_buffer[6] = 701;
838 expected_buffer[7] = 702;
839 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
841 dp->ProducerClose();
842 dp->ConsumerClose();
845 TEST(LocalDataPipeTest, AllOrNone) {
846 const MojoCreateDataPipeOptions options = {
847 kSizeOfOptions, // |struct_size|.
848 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
849 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
850 10 * sizeof(int32_t) // |capacity_num_bytes|.
852 MojoCreateDataPipeOptions validated_options = { 0 };
853 EXPECT_EQ(MOJO_RESULT_OK,
854 DataPipe::ValidateOptions(&options, &validated_options));
856 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
858 // Try writing way too much.
859 uint32_t num_bytes = 20u * sizeof(int32_t);
860 int32_t buffer[100];
861 Seq(0, arraysize(buffer), buffer);
862 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
863 dp->ProducerWriteData(buffer, &num_bytes, true));
865 // Should still be empty.
866 num_bytes = ~0u;
867 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
868 EXPECT_EQ(0u, num_bytes);
870 // Write some data.
871 num_bytes = 5u * sizeof(int32_t);
872 Seq(100, arraysize(buffer), buffer);
873 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true));
874 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
876 // Half full.
877 num_bytes = 0u;
878 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
879 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
881 // Too much.
882 num_bytes = 6u * sizeof(int32_t);
883 Seq(200, arraysize(buffer), buffer);
884 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
885 dp->ProducerWriteData(buffer, &num_bytes, true));
887 // Try reading too much.
888 num_bytes = 11u * sizeof(int32_t);
889 memset(buffer, 0xab, sizeof(buffer));
890 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
891 dp->ConsumerReadData(buffer, &num_bytes, true));
892 int32_t expected_buffer[100];
893 memset(expected_buffer, 0xab, sizeof(expected_buffer));
894 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
896 // Try discarding too much.
897 num_bytes = 11u * sizeof(int32_t);
898 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
899 dp->ConsumerDiscardData(&num_bytes, true));
901 // Just a little.
902 num_bytes = 2u * sizeof(int32_t);
903 Seq(300, arraysize(buffer), buffer);
904 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true));
905 EXPECT_EQ(2u * sizeof(int32_t), num_bytes);
907 // Just right.
908 num_bytes = 3u * sizeof(int32_t);
909 Seq(400, arraysize(buffer), buffer);
910 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true));
911 EXPECT_EQ(3u * sizeof(int32_t), num_bytes);
913 // Exactly full.
914 num_bytes = 0u;
915 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
916 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
918 // Read half.
919 num_bytes = 5u * sizeof(int32_t);
920 memset(buffer, 0xab, sizeof(buffer));
921 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, true));
922 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
923 memset(expected_buffer, 0xab, sizeof(expected_buffer));
924 Seq(100, 5, expected_buffer);
925 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
927 // Try reading too much again.
928 num_bytes = 6u * sizeof(int32_t);
929 memset(buffer, 0xab, sizeof(buffer));
930 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
931 dp->ConsumerReadData(buffer, &num_bytes, true));
932 memset(expected_buffer, 0xab, sizeof(expected_buffer));
933 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
935 // Try discarding too much again.
936 num_bytes = 6u * sizeof(int32_t);
937 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
938 dp->ConsumerDiscardData(&num_bytes, true));
940 // Discard a little.
941 num_bytes = 2u * sizeof(int32_t);
942 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerDiscardData(&num_bytes, true));
943 EXPECT_EQ(2u * sizeof(int32_t), num_bytes);
945 // Three left.
946 num_bytes = 0u;
947 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
948 EXPECT_EQ(3u * sizeof(int32_t), num_bytes);
950 // Close the producer, then test producer-closed cases.
951 dp->ProducerClose();
953 // Try reading too much; "failed precondition" since the producer is closed.
954 num_bytes = 4u * sizeof(int32_t);
955 memset(buffer, 0xab, sizeof(buffer));
956 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
957 dp->ConsumerReadData(buffer, &num_bytes, true));
958 memset(expected_buffer, 0xab, sizeof(expected_buffer));
959 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
961 // Try discarding too much; "failed precondition" again.
962 num_bytes = 4u * sizeof(int32_t);
963 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
964 dp->ConsumerDiscardData(&num_bytes, true));
966 // Read a little.
967 num_bytes = 2u * sizeof(int32_t);
968 memset(buffer, 0xab, sizeof(buffer));
969 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, true));
970 EXPECT_EQ(2u * sizeof(int32_t), num_bytes);
971 memset(expected_buffer, 0xab, sizeof(expected_buffer));
972 Seq(400, 2, expected_buffer);
973 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
975 // Discard the remaining element.
976 num_bytes = 1u * sizeof(int32_t);
977 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerDiscardData(&num_bytes, true));
978 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
980 // Empty again.
981 num_bytes = ~0u;
982 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
983 EXPECT_EQ(0u, num_bytes);
985 dp->ConsumerClose();
988 TEST(LocalDataPipeTest, AllOrNoneMayDiscard) {
989 const MojoCreateDataPipeOptions options = {
990 kSizeOfOptions, // |struct_size|.
991 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|.
992 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
993 10 * sizeof(int32_t) // |capacity_num_bytes|.
995 MojoCreateDataPipeOptions validated_options = { 0 };
996 EXPECT_EQ(MOJO_RESULT_OK,
997 DataPipe::ValidateOptions(&options, &validated_options));
999 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1001 // Try writing way too much.
1002 uint32_t num_bytes = 20u * sizeof(int32_t);
1003 int32_t buffer[100];
1004 Seq(0, arraysize(buffer), buffer);
1005 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1006 dp->ProducerWriteData(buffer, &num_bytes, true));
1008 // Write some stuff.
1009 num_bytes = 5u * sizeof(int32_t);
1010 Seq(100, arraysize(buffer), buffer);
1011 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true));
1012 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1014 // Write lots of stuff (discarding all but "104").
1015 num_bytes = 9u * sizeof(int32_t);
1016 Seq(200, arraysize(buffer), buffer);
1017 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true));
1018 EXPECT_EQ(9u * sizeof(int32_t), num_bytes);
1020 // Read one.
1021 num_bytes = 1u * sizeof(int32_t);
1022 memset(buffer, 0xab, sizeof(buffer));
1023 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, true));
1024 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
1025 int32_t expected_buffer[100];
1026 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1027 expected_buffer[0] = 104;
1028 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1030 // Try reading too many.
1031 num_bytes = 10u * sizeof(int32_t);
1032 memset(buffer, 0xab, sizeof(buffer));
1033 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1034 dp->ConsumerReadData(buffer, &num_bytes, true));
1035 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1036 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1038 // Try discarding too many.
1039 num_bytes = 10u * sizeof(int32_t);
1040 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1041 dp->ConsumerDiscardData(&num_bytes, true));
1043 // Discard a bunch.
1044 num_bytes = 4u * sizeof(int32_t);
1045 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerDiscardData(&num_bytes, true));
1047 // Half full.
1048 num_bytes = 0u;
1049 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
1050 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1052 // Write as much as possible.
1053 num_bytes = 10u * sizeof(int32_t);
1054 Seq(300, arraysize(buffer), buffer);
1055 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true));
1056 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
1058 // Read everything.
1059 num_bytes = 10u * sizeof(int32_t);
1060 memset(buffer, 0xab, sizeof(buffer));
1061 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, true));
1062 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1063 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
1064 Seq(300, 10, expected_buffer);
1065 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1067 // Note: All-or-none two-phase writes on a "may discard" data pipe are tested
1068 // in LocalDataPipeTest.MayDiscard.
1070 dp->ProducerClose();
1071 dp->ConsumerClose();
1074 TEST(LocalDataPipeTest, TwoPhaseAllOrNone) {
1075 const MojoCreateDataPipeOptions options = {
1076 kSizeOfOptions, // |struct_size|.
1077 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
1078 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
1079 10 * sizeof(int32_t) // |capacity_num_bytes|.
1081 MojoCreateDataPipeOptions validated_options = { 0 };
1082 EXPECT_EQ(MOJO_RESULT_OK,
1083 DataPipe::ValidateOptions(&options, &validated_options));
1085 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1087 // Try writing way too much (two-phase).
1088 uint32_t num_bytes = 20u * sizeof(int32_t);
1089 void* write_ptr = NULL;
1090 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1091 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true));
1093 // Try writing an amount which isn't a multiple of the element size
1094 // (two-phase).
1095 COMPILE_ASSERT(sizeof(int32_t) > 1u, wow_int32_ts_have_size_1);
1096 num_bytes = 1u;
1097 write_ptr = NULL;
1098 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
1099 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true));
1101 // Try reading way too much (two-phase).
1102 num_bytes = 20u * sizeof(int32_t);
1103 const void* read_ptr = NULL;
1104 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1105 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true));
1107 // Write half (two-phase).
1108 num_bytes = 5u * sizeof(int32_t);
1109 write_ptr = NULL;
1110 EXPECT_EQ(MOJO_RESULT_OK,
1111 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true));
1112 // May provide more space than requested.
1113 EXPECT_GE(num_bytes, 5u * sizeof(int32_t));
1114 EXPECT_TRUE(write_ptr != NULL);
1115 Seq(0, 5, static_cast<int32_t*>(write_ptr));
1116 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(5u * sizeof(int32_t)));
1118 // Try reading an amount which isn't a multiple of the element size
1119 // (two-phase).
1120 num_bytes = 1u;
1121 read_ptr = NULL;
1122 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
1123 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true));
1125 // Read one (two-phase).
1126 num_bytes = 1u * sizeof(int32_t);
1127 read_ptr = NULL;
1128 EXPECT_EQ(MOJO_RESULT_OK,
1129 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true));
1130 EXPECT_GE(num_bytes, 1u * sizeof(int32_t));
1131 EXPECT_EQ(0, static_cast<const int32_t*>(read_ptr)[0]);
1132 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(1u * sizeof(int32_t)));
1134 // We should have four left, leaving room for six.
1135 num_bytes = 0u;
1136 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
1137 EXPECT_EQ(4u * sizeof(int32_t), num_bytes);
1139 // Assuming a tight circular buffer of the specified capacity, we can't do a
1140 // two-phase write of six now.
1141 num_bytes = 6u * sizeof(int32_t);
1142 write_ptr = NULL;
1143 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1144 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true));
1146 // Write six elements (simple), filling the buffer.
1147 num_bytes = 6u * sizeof(int32_t);
1148 int32_t buffer[100];
1149 Seq(100, 6, buffer);
1150 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true));
1151 EXPECT_EQ(6u * sizeof(int32_t), num_bytes);
1153 // We have ten.
1154 num_bytes = 0u;
1155 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
1156 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
1158 // But a two-phase read of ten should fail.
1159 num_bytes = 10u * sizeof(int32_t);
1160 read_ptr = NULL;
1161 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1162 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true));
1164 // Close the producer.
1165 dp->ProducerClose();
1167 // A two-phase read of nine should work.
1168 num_bytes = 9u * sizeof(int32_t);
1169 read_ptr = NULL;
1170 EXPECT_EQ(MOJO_RESULT_OK,
1171 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true));
1172 EXPECT_GE(num_bytes, 9u * sizeof(int32_t));
1173 EXPECT_EQ(1, static_cast<const int32_t*>(read_ptr)[0]);
1174 EXPECT_EQ(2, static_cast<const int32_t*>(read_ptr)[1]);
1175 EXPECT_EQ(3, static_cast<const int32_t*>(read_ptr)[2]);
1176 EXPECT_EQ(4, static_cast<const int32_t*>(read_ptr)[3]);
1177 EXPECT_EQ(100, static_cast<const int32_t*>(read_ptr)[4]);
1178 EXPECT_EQ(101, static_cast<const int32_t*>(read_ptr)[5]);
1179 EXPECT_EQ(102, static_cast<const int32_t*>(read_ptr)[6]);
1180 EXPECT_EQ(103, static_cast<const int32_t*>(read_ptr)[7]);
1181 EXPECT_EQ(104, static_cast<const int32_t*>(read_ptr)[8]);
1182 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(9u * sizeof(int32_t)));
1184 // A two-phase read of two should fail, with "failed precondition".
1185 num_bytes = 2u * sizeof(int32_t);
1186 read_ptr = NULL;
1187 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1188 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true));
1190 dp->ConsumerClose();
1193 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads,
1194 // respectively, as much as possible, even if it has to "wrap around" the
1195 // internal circular buffer. (Note that the two-phase write and read do not do
1196 // this.)
1197 TEST(LocalDataPipeTest, WrapAround) {
1198 unsigned char test_data[1000];
1199 for (size_t i = 0; i < arraysize(test_data); i++)
1200 test_data[i] = static_cast<unsigned char>(i);
1202 const MojoCreateDataPipeOptions options = {
1203 kSizeOfOptions, // |struct_size|.
1204 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
1205 1u, // |element_num_bytes|.
1206 100u // |capacity_num_bytes|.
1208 MojoCreateDataPipeOptions validated_options = { 0 };
1209 EXPECT_EQ(MOJO_RESULT_OK,
1210 DataPipe::ValidateOptions(&options, &validated_options));
1211 // This test won't be valid if |ValidateOptions()| decides to give the pipe
1212 // more space.
1213 ASSERT_EQ(100u, validated_options.capacity_num_bytes);
1215 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1217 // Write 20 bytes.
1218 uint32_t num_bytes = 20u;
1219 EXPECT_EQ(MOJO_RESULT_OK,
1220 dp->ProducerWriteData(&test_data[0], &num_bytes, false));
1221 EXPECT_EQ(20u, num_bytes);
1223 // Read 10 bytes.
1224 unsigned char read_buffer[1000] = { 0 };
1225 num_bytes = 10u;
1226 EXPECT_EQ(MOJO_RESULT_OK,
1227 dp->ConsumerReadData(read_buffer, &num_bytes, false));
1228 EXPECT_EQ(10u, num_bytes);
1229 EXPECT_EQ(0, memcmp(read_buffer, &test_data[0], 10u));
1231 // Check that a two-phase write can now only write (at most) 80 bytes. (This
1232 // checks an implementation detail; this behavior is not guaranteed, but we
1233 // need it for this test.)
1234 void* write_buffer_ptr = NULL;
1235 num_bytes = 0u;
1236 EXPECT_EQ(MOJO_RESULT_OK,
1237 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false));
1238 EXPECT_TRUE(write_buffer_ptr != NULL);
1239 EXPECT_EQ(80u, num_bytes);
1240 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u));
1242 // Write as much data as we can (using |ProducerWriteData()|. We should write
1243 // 90 bytes.
1244 num_bytes = 200u;
1245 EXPECT_EQ(MOJO_RESULT_OK,
1246 dp->ProducerWriteData(&test_data[20], &num_bytes, false));
1247 EXPECT_EQ(90u, num_bytes);
1249 // Check that a two-phase read can now only read (at most) 90 bytes. (This
1250 // checks an implementation detail; this behavior is not guaranteed, but we
1251 // need it for this test.)
1252 const void* read_buffer_ptr = NULL;
1253 num_bytes = 0u;
1254 EXPECT_EQ(MOJO_RESULT_OK,
1255 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false));
1256 EXPECT_TRUE(read_buffer_ptr != NULL);
1257 EXPECT_EQ(90u, num_bytes);
1258 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u));
1260 // Read as much as possible (using |ConsumerReadData()|. We should read 100
1261 // bytes.
1262 num_bytes =
1263 static_cast<uint32_t>(arraysize(read_buffer) * sizeof(read_buffer[0]));
1264 memset(read_buffer, 0, num_bytes);
1265 EXPECT_EQ(MOJO_RESULT_OK,
1266 dp->ConsumerReadData(read_buffer, &num_bytes, false));
1267 EXPECT_EQ(100u, num_bytes);
1268 EXPECT_EQ(0, memcmp(read_buffer, &test_data[10], 100u));
1270 dp->ProducerClose();
1271 dp->ConsumerClose();
1274 // Tests the behavior of closing the producer or consumer with respect to
1275 // writes and reads (simple and two-phase).
1276 TEST(LocalDataPipeTest, CloseWriteRead) {
1277 const char kTestData[] = "hello world";
1278 const uint32_t kTestDataSize = static_cast<uint32_t>(sizeof(kTestData));
1280 const MojoCreateDataPipeOptions options = {
1281 kSizeOfOptions, // |struct_size|.
1282 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
1283 1u, // |element_num_bytes|.
1284 1000u // |capacity_num_bytes|.
1286 MojoCreateDataPipeOptions validated_options = { 0 };
1287 EXPECT_EQ(MOJO_RESULT_OK,
1288 DataPipe::ValidateOptions(&options, &validated_options));
1290 // Close producer first, then consumer.
1292 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1294 // Write some data, so we'll have something to read.
1295 uint32_t num_bytes = kTestDataSize;
1296 EXPECT_EQ(MOJO_RESULT_OK,
1297 dp->ProducerWriteData(kTestData, &num_bytes, false));
1298 EXPECT_EQ(kTestDataSize, num_bytes);
1300 // Write it again, so we'll have something left over.
1301 num_bytes = kTestDataSize;
1302 EXPECT_EQ(MOJO_RESULT_OK,
1303 dp->ProducerWriteData(kTestData, &num_bytes, false));
1304 EXPECT_EQ(kTestDataSize, num_bytes);
1306 // Start two-phase write.
1307 void* write_buffer_ptr = NULL;
1308 num_bytes = 0u;
1309 EXPECT_EQ(MOJO_RESULT_OK,
1310 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false));
1311 EXPECT_TRUE(write_buffer_ptr != NULL);
1312 EXPECT_GT(num_bytes, 0u);
1314 // Start two-phase read.
1315 const void* read_buffer_ptr = NULL;
1316 num_bytes = 0u;
1317 EXPECT_EQ(MOJO_RESULT_OK,
1318 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false));
1319 EXPECT_TRUE(read_buffer_ptr != NULL);
1320 EXPECT_EQ(2u * kTestDataSize, num_bytes);
1322 // Close the producer.
1323 dp->ProducerClose();
1325 // The consumer can finish its two-phase read.
1326 EXPECT_EQ(0, memcmp(read_buffer_ptr, kTestData, kTestDataSize));
1327 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(kTestDataSize));
1329 // And start another.
1330 read_buffer_ptr = NULL;
1331 num_bytes = 0u;
1332 EXPECT_EQ(MOJO_RESULT_OK,
1333 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false));
1334 EXPECT_TRUE(read_buffer_ptr != NULL);
1335 EXPECT_EQ(kTestDataSize, num_bytes);
1337 // Close the consumer, which cancels the two-phase read.
1338 dp->ConsumerClose();
1341 // Close consumer first, then producer.
1343 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1345 // Write some data, so we'll have something to read.
1346 uint32_t num_bytes = kTestDataSize;
1347 EXPECT_EQ(MOJO_RESULT_OK,
1348 dp->ProducerWriteData(kTestData, &num_bytes, false));
1349 EXPECT_EQ(kTestDataSize, num_bytes);
1351 // Start two-phase write.
1352 void* write_buffer_ptr = NULL;
1353 num_bytes = 0u;
1354 EXPECT_EQ(MOJO_RESULT_OK,
1355 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false));
1356 EXPECT_TRUE(write_buffer_ptr != NULL);
1357 ASSERT_GT(num_bytes, kTestDataSize);
1359 // Start two-phase read.
1360 const void* read_buffer_ptr = NULL;
1361 num_bytes = 0u;
1362 EXPECT_EQ(MOJO_RESULT_OK,
1363 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false));
1364 EXPECT_TRUE(read_buffer_ptr != NULL);
1365 EXPECT_EQ(kTestDataSize, num_bytes);
1367 // Close the consumer.
1368 dp->ConsumerClose();
1370 // Actually write some data. (Note: Premature freeing of the buffer would
1371 // probably only be detected under ASAN or similar.)
1372 memcpy(write_buffer_ptr, kTestData, kTestDataSize);
1373 // Note: Even though the consumer has been closed, ending the two-phase
1374 // write will report success.
1375 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(kTestDataSize));
1377 // But trying to write should result in failure.
1378 num_bytes = kTestDataSize;
1379 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1380 dp->ProducerWriteData(kTestData, &num_bytes, false));
1382 // As will trying to start another two-phase write.
1383 write_buffer_ptr = NULL;
1384 num_bytes = 0u;
1385 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1386 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false));
1388 dp->ProducerClose();
1391 // Test closing the consumer first, then the producer, with an active
1392 // two-phase write.
1394 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1396 // Start two-phase write.
1397 void* write_buffer_ptr = NULL;
1398 uint32_t num_bytes = 0u;
1399 EXPECT_EQ(MOJO_RESULT_OK,
1400 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false));
1401 EXPECT_TRUE(write_buffer_ptr != NULL);
1402 ASSERT_GT(num_bytes, kTestDataSize);
1404 dp->ConsumerClose();
1405 dp->ProducerClose();
1408 // Test closing the producer and then trying to read (with no data).
1410 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1412 // Write some data, so we'll have something to read.
1413 uint32_t num_bytes = kTestDataSize;
1414 EXPECT_EQ(MOJO_RESULT_OK,
1415 dp->ProducerWriteData(kTestData, &num_bytes, false));
1416 EXPECT_EQ(kTestDataSize, num_bytes);
1418 // Close the producer.
1419 dp->ProducerClose();
1421 // Read that data.
1422 char buffer[1000];
1423 num_bytes = static_cast<uint32_t>(sizeof(buffer));
1424 EXPECT_EQ(MOJO_RESULT_OK,
1425 dp->ConsumerReadData(buffer, &num_bytes, false));
1426 EXPECT_EQ(kTestDataSize, num_bytes);
1427 EXPECT_EQ(0, memcmp(buffer, kTestData, kTestDataSize));
1429 // A second read should fail.
1430 num_bytes = static_cast<uint32_t>(sizeof(buffer));
1431 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1432 dp->ConsumerReadData(buffer, &num_bytes, false));
1434 // A two-phase read should also fail.
1435 const void* read_buffer_ptr = NULL;
1436 num_bytes = 0u;
1437 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1438 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false));
1440 // Ditto for discard.
1441 num_bytes = 10u;
1442 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1443 dp->ConsumerDiscardData(&num_bytes, false));
1445 dp->ConsumerClose();
1449 TEST(LocalDataPipeTest, TwoPhaseMoreInvalidArguments) {
1450 const MojoCreateDataPipeOptions options = {
1451 kSizeOfOptions, // |struct_size|.
1452 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
1453 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
1454 10 * sizeof(int32_t) // |capacity_num_bytes|.
1456 MojoCreateDataPipeOptions validated_options = { 0 };
1457 EXPECT_EQ(MOJO_RESULT_OK,
1458 DataPipe::ValidateOptions(&options, &validated_options));
1460 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1462 // No data.
1463 uint32_t num_bytes = 1000u;
1464 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
1465 EXPECT_EQ(0u, num_bytes);
1467 // Try "ending" a two-phase write when one isn't active.
1468 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1469 dp->ProducerEndWriteData(1u * sizeof(int32_t)));
1471 // Still no data.
1472 num_bytes = 1000u;
1473 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
1474 EXPECT_EQ(0u, num_bytes);
1476 // Try ending a two-phase write with an invalid amount (too much).
1477 num_bytes = 0u;
1478 void* write_ptr = NULL;
1479 EXPECT_EQ(MOJO_RESULT_OK,
1480 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false));
1481 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
1482 dp->ProducerEndWriteData(
1483 num_bytes + static_cast<uint32_t>(sizeof(int32_t))));
1485 // But the two-phase write still ended.
1486 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u));
1488 // Still no data.
1489 num_bytes = 1000u;
1490 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
1491 EXPECT_EQ(0u, num_bytes);
1493 // Try ending a two-phase write with an invalid amount (not a multiple of the
1494 // element size).
1495 num_bytes = 0u;
1496 write_ptr = NULL;
1497 EXPECT_EQ(MOJO_RESULT_OK,
1498 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false));
1499 EXPECT_GE(num_bytes, 1u);
1500 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ProducerEndWriteData(1u));
1502 // But the two-phase write still ended.
1503 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u));
1505 // Still no data.
1506 num_bytes = 1000u;
1507 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
1508 EXPECT_EQ(0u, num_bytes);
1510 // Now write some data, so we'll be able to try reading.
1511 int32_t element = 123;
1512 num_bytes = 1u * sizeof(int32_t);
1513 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(&element, &num_bytes, false));
1515 // One element available.
1516 num_bytes = 0u;
1517 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
1518 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
1520 // Try "ending" a two-phase read when one isn't active.
1521 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1522 dp->ConsumerEndReadData(1u * sizeof(int32_t)));
1524 // Still one element available.
1525 num_bytes = 0u;
1526 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
1527 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
1529 // Try ending a two-phase read with an invalid amount (too much).
1530 num_bytes = 0u;
1531 const void* read_ptr = NULL;
1532 EXPECT_EQ(MOJO_RESULT_OK,
1533 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false));
1534 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
1535 dp->ConsumerEndReadData(
1536 num_bytes + static_cast<uint32_t>(sizeof(int32_t))));
1538 // Still one element available.
1539 num_bytes = 0u;
1540 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
1541 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
1543 // Try ending a two-phase read with an invalid amount (not a multiple of the
1544 // element size).
1545 num_bytes = 0u;
1546 read_ptr = NULL;
1547 EXPECT_EQ(MOJO_RESULT_OK,
1548 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false));
1549 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
1550 EXPECT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]);
1551 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ConsumerEndReadData(1u));
1553 // Still one element available.
1554 num_bytes = 0u;
1555 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes));
1556 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
1558 dp->ProducerClose();
1559 dp->ConsumerClose();
1562 } // namespace
1563 } // namespace system
1564 } // namespace mojo