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

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

Issue 1365383004: Cleanup: Remove internal two-phase data pipe all-or-none support. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 2 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/system/data_pipe_impl.h ('k') | mojo/edk/system/data_pipe_producer_dispatcher.cc » ('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 // This file contains tests that are shared between different implementations of 5 // This file contains tests that are shared between different implementations of
6 // |DataPipeImpl|. 6 // |DataPipeImpl|.
7 7
8 #include "mojo/edk/system/data_pipe_impl.h" 8 #include "mojo/edk/system/data_pipe_impl.h"
9 9
10 #include <stdint.h> 10 #include <stdint.h>
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 helper_->SetUp(); 117 helper_->SetUp();
118 } 118 }
119 119
120 void ProducerClose() { helper_->ProducerClose(); } 120 void ProducerClose() { helper_->ProducerClose(); }
121 MojoResult ProducerWriteData(UserPointer<const void> elements, 121 MojoResult ProducerWriteData(UserPointer<const void> elements,
122 UserPointer<uint32_t> num_bytes, 122 UserPointer<uint32_t> num_bytes,
123 bool all_or_none) { 123 bool all_or_none) {
124 return dpp()->ProducerWriteData(elements, num_bytes, all_or_none); 124 return dpp()->ProducerWriteData(elements, num_bytes, all_or_none);
125 } 125 }
126 MojoResult ProducerBeginWriteData(UserPointer<void*> buffer, 126 MojoResult ProducerBeginWriteData(UserPointer<void*> buffer,
127 UserPointer<uint32_t> buffer_num_bytes, 127 UserPointer<uint32_t> buffer_num_bytes) {
128 bool all_or_none) { 128 return dpp()->ProducerBeginWriteData(buffer, buffer_num_bytes);
129 return dpp()->ProducerBeginWriteData(buffer, buffer_num_bytes, all_or_none);
130 } 129 }
131 MojoResult ProducerEndWriteData(uint32_t num_bytes_written) { 130 MojoResult ProducerEndWriteData(uint32_t num_bytes_written) {
132 return dpp()->ProducerEndWriteData(num_bytes_written); 131 return dpp()->ProducerEndWriteData(num_bytes_written);
133 } 132 }
134 MojoResult ProducerAddAwakable(Awakable* awakable, 133 MojoResult ProducerAddAwakable(Awakable* awakable,
135 MojoHandleSignals signals, 134 MojoHandleSignals signals,
136 uint32_t context, 135 uint32_t context,
137 HandleSignalsState* signals_state) { 136 HandleSignalsState* signals_state) {
138 return dpp()->ProducerAddAwakable(awakable, signals, context, 137 return dpp()->ProducerAddAwakable(awakable, signals, context,
139 signals_state); 138 signals_state);
(...skipping 11 matching lines...) Expand all
151 return dpc()->ConsumerReadData(elements, num_bytes, all_or_none, peek); 150 return dpc()->ConsumerReadData(elements, num_bytes, all_or_none, peek);
152 } 151 }
153 MojoResult ConsumerDiscardData(UserPointer<uint32_t> num_bytes, 152 MojoResult ConsumerDiscardData(UserPointer<uint32_t> num_bytes,
154 bool all_or_none) { 153 bool all_or_none) {
155 return dpc()->ConsumerDiscardData(num_bytes, all_or_none); 154 return dpc()->ConsumerDiscardData(num_bytes, all_or_none);
156 } 155 }
157 MojoResult ConsumerQueryData(UserPointer<uint32_t> num_bytes) { 156 MojoResult ConsumerQueryData(UserPointer<uint32_t> num_bytes) {
158 return dpc()->ConsumerQueryData(num_bytes); 157 return dpc()->ConsumerQueryData(num_bytes);
159 } 158 }
160 MojoResult ConsumerBeginReadData(UserPointer<const void*> buffer, 159 MojoResult ConsumerBeginReadData(UserPointer<const void*> buffer,
161 UserPointer<uint32_t> buffer_num_bytes, 160 UserPointer<uint32_t> buffer_num_bytes) {
162 bool all_or_none) { 161 return dpc()->ConsumerBeginReadData(buffer, buffer_num_bytes);
163 return dpc()->ConsumerBeginReadData(buffer, buffer_num_bytes, all_or_none);
164 } 162 }
165 MojoResult ConsumerEndReadData(uint32_t num_bytes_read) { 163 MojoResult ConsumerEndReadData(uint32_t num_bytes_read) {
166 return dpc()->ConsumerEndReadData(num_bytes_read); 164 return dpc()->ConsumerEndReadData(num_bytes_read);
167 } 165 }
168 MojoResult ConsumerAddAwakable(Awakable* awakable, 166 MojoResult ConsumerAddAwakable(Awakable* awakable,
169 MojoHandleSignals signals, 167 MojoHandleSignals signals,
170 uint32_t context, 168 uint32_t context,
171 HandleSignalsState* signals_state) { 169 HandleSignalsState* signals_state) {
172 return dpc()->ConsumerAddAwakable(awakable, signals, context, 170 return dpc()->ConsumerAddAwakable(awakable, signals, context,
173 signals_state); 171 signals_state);
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 1000 * sizeof(int32_t) // |capacity_num_bytes|. 602 1000 * sizeof(int32_t) // |capacity_num_bytes|.
605 }; 603 };
606 this->Create(options); 604 this->Create(options);
607 this->DoTransfer(); 605 this->DoTransfer();
608 606
609 Waiter waiter; 607 Waiter waiter;
610 HandleSignalsState hss; 608 HandleSignalsState hss;
611 uint32_t context; 609 uint32_t context;
612 610
613 int32_t elements[10] = {}; 611 int32_t elements[10] = {};
614 uint32_t num_bytes = 0; 612 uint32_t num_bytes = 0u;
615 613
616 // Try reading; nothing there yet. 614 // Try reading; nothing there yet.
617 num_bytes = 615 num_bytes =
618 static_cast<uint32_t>(MOJO_ARRAYSIZE(elements) * sizeof(elements[0])); 616 static_cast<uint32_t>(MOJO_ARRAYSIZE(elements) * sizeof(elements[0]));
619 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, 617 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
620 this->ConsumerReadData(UserPointer<void>(elements), 618 this->ConsumerReadData(UserPointer<void>(elements),
621 MakeUserPointer(&num_bytes), false, false)); 619 MakeUserPointer(&num_bytes), false, false));
622 620
623 // Query; nothing there yet. 621 // Query; nothing there yet.
624 num_bytes = 0; 622 num_bytes = 0u;
625 EXPECT_EQ(MOJO_RESULT_OK, 623 EXPECT_EQ(MOJO_RESULT_OK,
626 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); 624 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
627 EXPECT_EQ(0u, num_bytes); 625 EXPECT_EQ(0u, num_bytes);
628 626
629 // Discard; nothing there yet. 627 // Discard; nothing there yet.
630 num_bytes = static_cast<uint32_t>(5u * sizeof(elements[0])); 628 num_bytes = static_cast<uint32_t>(5u * sizeof(elements[0]));
631 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, 629 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
632 this->ConsumerDiscardData(MakeUserPointer(&num_bytes), false)); 630 this->ConsumerDiscardData(MakeUserPointer(&num_bytes), false));
633 631
634 // Read with invalid |num_bytes|. 632 // Read with invalid |num_bytes|.
(...skipping 26 matching lines...) Expand all
661 this->ConsumerRemoveAwakable(&waiter, &hss); 659 this->ConsumerRemoveAwakable(&waiter, &hss);
662 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); 660 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
663 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, 661 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
664 hss.satisfiable_signals); 662 hss.satisfiable_signals);
665 663
666 // Query. 664 // Query.
667 // TODO(vtl): It's theoretically possible (though not with the current 665 // TODO(vtl): It's theoretically possible (though not with the current
668 // implementation/configured limits) that not all the data has arrived yet. 666 // implementation/configured limits) that not all the data has arrived yet.
669 // (The theoretically-correct assertion here is that |num_bytes| is |1 * ...| 667 // (The theoretically-correct assertion here is that |num_bytes| is |1 * ...|
670 // or |2 * ...|.) 668 // or |2 * ...|.)
671 num_bytes = 0; 669 num_bytes = 0u;
672 EXPECT_EQ(MOJO_RESULT_OK, 670 EXPECT_EQ(MOJO_RESULT_OK,
673 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); 671 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
674 EXPECT_EQ(2 * sizeof(elements[0]), num_bytes); 672 EXPECT_EQ(2 * sizeof(elements[0]), num_bytes);
675 673
676 // Read one element. 674 // Read one element.
677 elements[0] = -1; 675 elements[0] = -1;
678 elements[1] = -1; 676 elements[1] = -1;
679 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); 677 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
680 EXPECT_EQ(MOJO_RESULT_OK, 678 EXPECT_EQ(MOJO_RESULT_OK,
681 this->ConsumerReadData(UserPointer<void>(elements), 679 this->ConsumerReadData(UserPointer<void>(elements),
682 MakeUserPointer(&num_bytes), false, false)); 680 MakeUserPointer(&num_bytes), false, false));
683 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); 681 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes);
684 EXPECT_EQ(123, elements[0]); 682 EXPECT_EQ(123, elements[0]);
685 EXPECT_EQ(-1, elements[1]); 683 EXPECT_EQ(-1, elements[1]);
686 684
687 // Query. 685 // Query.
688 // TODO(vtl): See previous TODO. (If we got 2 elements there, however, we 686 // TODO(vtl): See previous TODO. (If we got 2 elements there, however, we
689 // should get 1 here.) 687 // should get 1 here.)
690 num_bytes = 0; 688 num_bytes = 0u;
691 EXPECT_EQ(MOJO_RESULT_OK, 689 EXPECT_EQ(MOJO_RESULT_OK,
692 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); 690 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
693 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes); 691 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes);
694 692
695 // Peek one element. 693 // Peek one element.
696 elements[0] = -1; 694 elements[0] = -1;
697 elements[1] = -1; 695 elements[1] = -1;
698 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); 696 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
699 EXPECT_EQ(MOJO_RESULT_OK, 697 EXPECT_EQ(MOJO_RESULT_OK,
700 this->ConsumerReadData(UserPointer<void>(elements), 698 this->ConsumerReadData(UserPointer<void>(elements),
701 MakeUserPointer(&num_bytes), false, true)); 699 MakeUserPointer(&num_bytes), false, true));
702 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); 700 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes);
703 EXPECT_EQ(456, elements[0]); 701 EXPECT_EQ(456, elements[0]);
704 EXPECT_EQ(-1, elements[1]); 702 EXPECT_EQ(-1, elements[1]);
705 703
706 // Query. Still has 1 element remaining. 704 // Query. Still has 1 element remaining.
707 num_bytes = 0; 705 num_bytes = 0u;
708 EXPECT_EQ(MOJO_RESULT_OK, 706 EXPECT_EQ(MOJO_RESULT_OK,
709 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); 707 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
710 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes); 708 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes);
711 709
712 // Try to read two elements, with "all or none". 710 // Try to read two elements, with "all or none".
713 elements[0] = -1; 711 elements[0] = -1;
714 elements[1] = -1; 712 elements[1] = -1;
715 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); 713 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
716 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 714 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
717 this->ConsumerReadData(UserPointer<void>(elements), 715 this->ConsumerReadData(UserPointer<void>(elements),
718 MakeUserPointer(&num_bytes), true, false)); 716 MakeUserPointer(&num_bytes), true, false));
719 EXPECT_EQ(-1, elements[0]); 717 EXPECT_EQ(-1, elements[0]);
720 EXPECT_EQ(-1, elements[1]); 718 EXPECT_EQ(-1, elements[1]);
721 719
722 // Try to read two elements, without "all or none". 720 // Try to read two elements, without "all or none".
723 elements[0] = -1; 721 elements[0] = -1;
724 elements[1] = -1; 722 elements[1] = -1;
725 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); 723 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
726 EXPECT_EQ(MOJO_RESULT_OK, 724 EXPECT_EQ(MOJO_RESULT_OK,
727 this->ConsumerReadData(UserPointer<void>(elements), 725 this->ConsumerReadData(UserPointer<void>(elements),
728 MakeUserPointer(&num_bytes), false, false)); 726 MakeUserPointer(&num_bytes), false, false));
729 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); 727 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes);
730 EXPECT_EQ(456, elements[0]); 728 EXPECT_EQ(456, elements[0]);
731 EXPECT_EQ(-1, elements[1]); 729 EXPECT_EQ(-1, elements[1]);
732 730
733 // Query. 731 // Query.
734 num_bytes = 0; 732 num_bytes = 0u;
735 EXPECT_EQ(MOJO_RESULT_OK, 733 EXPECT_EQ(MOJO_RESULT_OK,
736 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); 734 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
737 EXPECT_EQ(0u, num_bytes); 735 EXPECT_EQ(0u, num_bytes);
738 736
739 this->ProducerClose(); 737 this->ProducerClose();
740 this->ConsumerClose(); 738 this->ConsumerClose();
741 } 739 }
742 740
743 // Note: The "basic" waiting tests test that the "wait states" are correct in 741 // Note: The "basic" waiting tests test that the "wait states" are correct in
744 // various situations; they don't test that waiters are properly awoken on state 742 // various situations; they don't test that waiters are properly awoken on state
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 this->ProducerRemoveAwakable(&pwaiter, &hss); 863 this->ProducerRemoveAwakable(&pwaiter, &hss);
866 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); 864 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
867 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, 865 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
868 hss.satisfiable_signals); 866 hss.satisfiable_signals);
869 867
870 // Try writing, using a two-phase write. 868 // Try writing, using a two-phase write.
871 void* buffer = nullptr; 869 void* buffer = nullptr;
872 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); 870 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0]));
873 EXPECT_EQ(MOJO_RESULT_OK, 871 EXPECT_EQ(MOJO_RESULT_OK,
874 this->ProducerBeginWriteData(MakeUserPointer(&buffer), 872 this->ProducerBeginWriteData(MakeUserPointer(&buffer),
875 MakeUserPointer(&num_bytes), false)); 873 MakeUserPointer(&num_bytes)));
876 EXPECT_TRUE(buffer); 874 EXPECT_TRUE(buffer);
877 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); 875 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
878 876
879 static_cast<int32_t*>(buffer)[0] = 789; 877 static_cast<int32_t*>(buffer)[0] = 789;
880 EXPECT_EQ(MOJO_RESULT_OK, this->ProducerEndWriteData(static_cast<uint32_t>( 878 EXPECT_EQ(MOJO_RESULT_OK, this->ProducerEndWriteData(static_cast<uint32_t>(
881 1u * sizeof(elements[0])))); 879 1u * sizeof(elements[0]))));
882 880
883 // Add a waiter. 881 // Add a waiter.
884 pwaiter.Init(); 882 pwaiter.Init();
885 ASSERT_EQ(MOJO_RESULT_OK, 883 ASSERT_EQ(MOJO_RESULT_OK,
886 this->ProducerAddAwakable(&pwaiter, MOJO_HANDLE_SIGNAL_WRITABLE, 90, 884 this->ProducerAddAwakable(&pwaiter, MOJO_HANDLE_SIGNAL_WRITABLE, 90,
887 nullptr)); 885 nullptr));
888 886
889 // Read one element, using a two-phase read. 887 // Read one element, using a two-phase read.
890 const void* read_buffer = nullptr; 888 const void* read_buffer = nullptr;
891 num_bytes = 0u; 889 num_bytes = 0u;
892 EXPECT_EQ(MOJO_RESULT_OK, 890 EXPECT_EQ(MOJO_RESULT_OK,
893 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer), 891 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer),
894 MakeUserPointer(&num_bytes), false)); 892 MakeUserPointer(&num_bytes)));
895 EXPECT_TRUE(read_buffer); 893 EXPECT_TRUE(read_buffer);
896 // Since we only read one element (after having written three in all), the 894 // Since we only read one element (after having written three in all), the
897 // two-phase read should only allow us to read one. This checks an 895 // two-phase read should only allow us to read one. This checks an
898 // implementation detail! 896 // implementation detail!
899 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); 897 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
900 EXPECT_EQ(456, static_cast<const int32_t*>(read_buffer)[0]); 898 EXPECT_EQ(456, static_cast<const int32_t*>(read_buffer)[0]);
901 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(static_cast<uint32_t>( 899 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(static_cast<uint32_t>(
902 1u * sizeof(elements[0])))); 900 1u * sizeof(elements[0]))));
903 901
904 // Waiting should succeed. 902 // Waiting should succeed.
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
1205 1203
1206 // Add waiter: not yet readable. 1204 // Add waiter: not yet readable.
1207 waiter.Init(); 1205 waiter.Init();
1208 ASSERT_EQ(MOJO_RESULT_OK, 1206 ASSERT_EQ(MOJO_RESULT_OK,
1209 this->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, 1207 this->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12,
1210 nullptr)); 1208 nullptr));
1211 1209
1212 // Write two elements. 1210 // Write two elements.
1213 int32_t* elements = nullptr; 1211 int32_t* elements = nullptr;
1214 void* buffer = nullptr; 1212 void* buffer = nullptr;
1215 // Request room for three (but we'll only write two). 1213 uint32_t num_bytes = 0u;
1216 uint32_t num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0]));
1217 EXPECT_EQ(MOJO_RESULT_OK, 1214 EXPECT_EQ(MOJO_RESULT_OK,
1218 this->ProducerBeginWriteData(MakeUserPointer(&buffer), 1215 this->ProducerBeginWriteData(MakeUserPointer(&buffer),
1219 MakeUserPointer(&num_bytes), true)); 1216 MakeUserPointer(&num_bytes)));
1220 EXPECT_TRUE(buffer); 1217 EXPECT_TRUE(buffer);
1221 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0]))); 1218 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0])));
1222 elements = static_cast<int32_t*>(buffer); 1219 elements = static_cast<int32_t*>(buffer);
1223 elements[0] = 123; 1220 elements[0] = 123;
1224 elements[1] = 456; 1221 elements[1] = 456;
1225 EXPECT_EQ(MOJO_RESULT_OK, this->ProducerEndWriteData(static_cast<uint32_t>( 1222 EXPECT_EQ(MOJO_RESULT_OK, this->ProducerEndWriteData(static_cast<uint32_t>(
1226 2u * sizeof(elements[0])))); 1223 2u * sizeof(elements[0]))));
1227 1224
1228 // Wait for readability (needed for remote cases). 1225 // Wait for readability (needed for remote cases).
1229 context = 0; 1226 context = 0;
1230 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(test::TinyDeadline(), &context)); 1227 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(test::TinyDeadline(), &context));
1231 EXPECT_EQ(12u, context); 1228 EXPECT_EQ(12u, context);
1232 hss = HandleSignalsState(); 1229 hss = HandleSignalsState();
1233 this->ConsumerRemoveAwakable(&waiter, &hss); 1230 this->ConsumerRemoveAwakable(&waiter, &hss);
1234 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); 1231 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
1235 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, 1232 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
1236 hss.satisfiable_signals); 1233 hss.satisfiable_signals);
1237 1234
1238 // Read one element. 1235 // Read one element.
1239 // Request two in all-or-none mode, but only read one.
1240 const void* read_buffer = nullptr; 1236 const void* read_buffer = nullptr;
1241 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); 1237 num_bytes = 0u;
1242 EXPECT_EQ(MOJO_RESULT_OK, 1238 EXPECT_EQ(MOJO_RESULT_OK,
1243 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer), 1239 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer),
1244 MakeUserPointer(&num_bytes), true)); 1240 MakeUserPointer(&num_bytes)));
1245 EXPECT_TRUE(read_buffer); 1241 EXPECT_TRUE(read_buffer);
1246 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); 1242 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes);
1247 const int32_t* read_elements = static_cast<const int32_t*>(read_buffer); 1243 const int32_t* read_elements = static_cast<const int32_t*>(read_buffer);
1248 EXPECT_EQ(123, read_elements[0]); 1244 EXPECT_EQ(123, read_elements[0]);
1249 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(static_cast<uint32_t>( 1245 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(static_cast<uint32_t>(
1250 1u * sizeof(elements[0])))); 1246 1u * sizeof(elements[0]))));
1251 1247
1252 // Should still be readable. 1248 // Should still be readable.
1253 waiter.Init(); 1249 waiter.Init();
1254 hss = HandleSignalsState(); 1250 hss = HandleSignalsState();
1255 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 1251 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
1256 this->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, 1252 this->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34,
1257 &hss)); 1253 &hss));
1258 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); 1254 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
1259 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, 1255 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
1260 hss.satisfiable_signals); 1256 hss.satisfiable_signals);
1261 1257
1262 // Read one element. 1258 // Read one element.
1263 // Request three, but not in all-or-none mode. 1259 // Request three, but not in all-or-none mode.
1264 read_buffer = nullptr; 1260 read_buffer = nullptr;
1265 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); 1261 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0]));
1266 EXPECT_EQ(MOJO_RESULT_OK, 1262 EXPECT_EQ(MOJO_RESULT_OK,
1267 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer), 1263 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer),
1268 MakeUserPointer(&num_bytes), false)); 1264 MakeUserPointer(&num_bytes)));
1269 EXPECT_TRUE(read_buffer); 1265 EXPECT_TRUE(read_buffer);
1270 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); 1266 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
1271 read_elements = static_cast<const int32_t*>(read_buffer); 1267 read_elements = static_cast<const int32_t*>(read_buffer);
1272 EXPECT_EQ(456, read_elements[0]); 1268 EXPECT_EQ(456, read_elements[0]);
1273 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(static_cast<uint32_t>( 1269 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(static_cast<uint32_t>(
1274 1u * sizeof(elements[0])))); 1270 1u * sizeof(elements[0]))));
1275 1271
1276 // Adding a waiter should now succeed. 1272 // Adding a waiter should now succeed.
1277 waiter.Init(); 1273 waiter.Init();
1278 ASSERT_EQ(MOJO_RESULT_OK, 1274 ASSERT_EQ(MOJO_RESULT_OK,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1313 // It should be writable. 1309 // It should be writable.
1314 pwaiter.Init(); 1310 pwaiter.Init();
1315 hss = HandleSignalsState(); 1311 hss = HandleSignalsState();
1316 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 1312 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
1317 this->ProducerAddAwakable(&pwaiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, 1313 this->ProducerAddAwakable(&pwaiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0,
1318 &hss)); 1314 &hss));
1319 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); 1315 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
1320 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, 1316 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
1321 hss.satisfiable_signals); 1317 hss.satisfiable_signals);
1322 1318
1323 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t));
1324 void* write_ptr = nullptr; 1319 void* write_ptr = nullptr;
1320 uint32_t num_bytes = 0u;
1325 EXPECT_EQ(MOJO_RESULT_OK, 1321 EXPECT_EQ(MOJO_RESULT_OK,
1326 this->ProducerBeginWriteData(MakeUserPointer(&write_ptr), 1322 this->ProducerBeginWriteData(MakeUserPointer(&write_ptr),
1327 MakeUserPointer(&num_bytes), false)); 1323 MakeUserPointer(&num_bytes)));
1328 EXPECT_TRUE(write_ptr); 1324 EXPECT_TRUE(write_ptr);
1329 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); 1325 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t)));
1330 1326
1331 // At this point, it shouldn't be writable. 1327 // At this point, it shouldn't be writable.
1332 pwaiter.Init(); 1328 pwaiter.Init();
1333 ASSERT_EQ(MOJO_RESULT_OK, 1329 ASSERT_EQ(MOJO_RESULT_OK,
1334 this->ProducerAddAwakable(&pwaiter, MOJO_HANDLE_SIGNAL_WRITABLE, 1, 1330 this->ProducerAddAwakable(&pwaiter, MOJO_HANDLE_SIGNAL_WRITABLE, 1,
1335 nullptr)); 1331 nullptr));
1336 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, pwaiter.Wait(0, nullptr)); 1332 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, pwaiter.Wait(0, nullptr));
1337 hss = HandleSignalsState(); 1333 hss = HandleSignalsState();
(...skipping 25 matching lines...) Expand all
1363 // It should become readable. 1359 // It should become readable.
1364 EXPECT_EQ(MOJO_RESULT_OK, cwaiter.Wait(test::TinyDeadline(), nullptr)); 1360 EXPECT_EQ(MOJO_RESULT_OK, cwaiter.Wait(test::TinyDeadline(), nullptr));
1365 hss = HandleSignalsState(); 1361 hss = HandleSignalsState();
1366 this->ConsumerRemoveAwakable(&cwaiter, &hss); 1362 this->ConsumerRemoveAwakable(&cwaiter, &hss);
1367 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); 1363 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
1368 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, 1364 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
1369 hss.satisfiable_signals); 1365 hss.satisfiable_signals);
1370 1366
1371 // Start another two-phase write and check that it's readable even in the 1367 // Start another two-phase write and check that it's readable even in the
1372 // middle of it. 1368 // middle of it.
1373 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t));
1374 write_ptr = nullptr; 1369 write_ptr = nullptr;
1370 num_bytes = 0u;
1375 EXPECT_EQ(MOJO_RESULT_OK, 1371 EXPECT_EQ(MOJO_RESULT_OK,
1376 this->ProducerBeginWriteData(MakeUserPointer(&write_ptr), 1372 this->ProducerBeginWriteData(MakeUserPointer(&write_ptr),
1377 MakeUserPointer(&num_bytes), false)); 1373 MakeUserPointer(&num_bytes)));
1378 EXPECT_TRUE(write_ptr); 1374 EXPECT_TRUE(write_ptr);
1379 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); 1375 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t)));
1380 1376
1381 // It should be readable. 1377 // It should be readable.
1382 cwaiter.Init(); 1378 cwaiter.Init();
1383 hss = HandleSignalsState(); 1379 hss = HandleSignalsState();
1384 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 1380 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
1385 this->ConsumerAddAwakable(&cwaiter, MOJO_HANDLE_SIGNAL_READABLE, 5, 1381 this->ConsumerAddAwakable(&cwaiter, MOJO_HANDLE_SIGNAL_READABLE, 5,
1386 &hss)); 1382 &hss));
1387 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); 1383 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
1388 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, 1384 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
1389 hss.satisfiable_signals); 1385 hss.satisfiable_signals);
1390 1386
1391 // End the two-phase write without writing anything. 1387 // End the two-phase write without writing anything.
1392 EXPECT_EQ(MOJO_RESULT_OK, this->ProducerEndWriteData(0u)); 1388 EXPECT_EQ(MOJO_RESULT_OK, this->ProducerEndWriteData(0u));
1393 1389
1394 // Start a two-phase read. 1390 // Start a two-phase read.
1395 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t));
1396 const void* read_ptr = nullptr; 1391 const void* read_ptr = nullptr;
1392 num_bytes = 0u;
1397 EXPECT_EQ(MOJO_RESULT_OK, 1393 EXPECT_EQ(MOJO_RESULT_OK,
1398 this->ConsumerBeginReadData(MakeUserPointer(&read_ptr), 1394 this->ConsumerBeginReadData(MakeUserPointer(&read_ptr),
1399 MakeUserPointer(&num_bytes), false)); 1395 MakeUserPointer(&num_bytes)));
1400 EXPECT_TRUE(read_ptr); 1396 EXPECT_TRUE(read_ptr);
1401 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(int32_t)), num_bytes); 1397 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(int32_t)), num_bytes);
1402 1398
1403 // At this point, it should still be writable. 1399 // At this point, it should still be writable.
1404 pwaiter.Init(); 1400 pwaiter.Init();
1405 hss = HandleSignalsState(); 1401 hss = HandleSignalsState();
1406 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 1402 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
1407 this->ProducerAddAwakable(&pwaiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6, 1403 this->ProducerAddAwakable(&pwaiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6,
1408 &hss)); 1404 &hss));
1409 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); 1405 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
1642 1638
1643 // Empty again. 1639 // Empty again.
1644 num_bytes = ~0u; 1640 num_bytes = ~0u;
1645 EXPECT_EQ(MOJO_RESULT_OK, 1641 EXPECT_EQ(MOJO_RESULT_OK,
1646 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); 1642 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1647 EXPECT_EQ(0u, num_bytes); 1643 EXPECT_EQ(0u, num_bytes);
1648 1644
1649 this->ConsumerClose(); 1645 this->ConsumerClose();
1650 } 1646 }
1651 1647
1652 TYPED_TEST(DataPipeImplTest, TwoPhaseAllOrNone) {
1653 const MojoCreateDataPipeOptions options = {
1654 kSizeOfOptions, // |struct_size|.
1655 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
1656 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
1657 10 * sizeof(int32_t) // |capacity_num_bytes|.
1658 };
1659 this->Create(options);
1660 this->DoTransfer();
1661
1662 Waiter waiter;
1663 HandleSignalsState hss;
1664
1665 // Try writing way too much (two-phase).
1666 uint32_t num_bytes = 20u * sizeof(int32_t);
1667 void* write_ptr = nullptr;
1668 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1669 this->ProducerBeginWriteData(MakeUserPointer(&write_ptr),
1670 MakeUserPointer(&num_bytes), true));
1671
1672 // Try writing an amount which isn't a multiple of the element size
1673 // (two-phase).
1674 static_assert(sizeof(int32_t) > 1u, "Wow! int32_t's have size 1");
1675 num_bytes = 1u;
1676 write_ptr = nullptr;
1677 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
1678 this->ProducerBeginWriteData(MakeUserPointer(&write_ptr),
1679 MakeUserPointer(&num_bytes), true));
1680
1681 // Try reading way too much (two-phase).
1682 num_bytes = 20u * sizeof(int32_t);
1683 const void* read_ptr = nullptr;
1684 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1685 this->ConsumerBeginReadData(MakeUserPointer(&read_ptr),
1686 MakeUserPointer(&num_bytes), true));
1687
1688 // Add waiter.
1689 waiter.Init();
1690 ASSERT_EQ(MOJO_RESULT_OK,
1691 this->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 1,
1692 nullptr));
1693
1694 // Write half (two-phase).
1695 num_bytes = 5u * sizeof(int32_t);
1696 write_ptr = nullptr;
1697 EXPECT_EQ(MOJO_RESULT_OK,
1698 this->ProducerBeginWriteData(MakeUserPointer(&write_ptr),
1699 MakeUserPointer(&num_bytes), true));
1700 // May provide more space than requested.
1701 EXPECT_GE(num_bytes, 5u * sizeof(int32_t));
1702 EXPECT_TRUE(write_ptr);
1703 Seq(0, 5, static_cast<int32_t*>(write_ptr));
1704 EXPECT_EQ(MOJO_RESULT_OK, this->ProducerEndWriteData(5u * sizeof(int32_t)));
1705
1706 // Wait for data.
1707 // TODO(vtl): (See corresponding TODO in AllOrNone.)
1708 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(test::TinyDeadline(), nullptr));
1709 hss = HandleSignalsState();
1710 this->ConsumerRemoveAwakable(&waiter, &hss);
1711 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
1712 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
1713 hss.satisfiable_signals);
1714
1715 // Try reading an amount which isn't a multiple of the element size
1716 // (two-phase).
1717 num_bytes = 1u;
1718 read_ptr = nullptr;
1719 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
1720 this->ConsumerBeginReadData(MakeUserPointer(&read_ptr),
1721 MakeUserPointer(&num_bytes), true));
1722
1723 // Read one (two-phase).
1724 num_bytes = 1u * sizeof(int32_t);
1725 read_ptr = nullptr;
1726 EXPECT_EQ(MOJO_RESULT_OK,
1727 this->ConsumerBeginReadData(MakeUserPointer(&read_ptr),
1728 MakeUserPointer(&num_bytes), true));
1729 EXPECT_GE(num_bytes, 1u * sizeof(int32_t));
1730 EXPECT_EQ(0, static_cast<const int32_t*>(read_ptr)[0]);
1731 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(1u * sizeof(int32_t)));
1732
1733 // We should have four left, leaving room for six.
1734 num_bytes = 0u;
1735 EXPECT_EQ(MOJO_RESULT_OK,
1736 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1737 EXPECT_EQ(4u * sizeof(int32_t), num_bytes);
1738
1739 // Assuming a tight circular buffer of the specified capacity, we can't do a
1740 // two-phase write of six now.
1741 num_bytes = 6u * sizeof(int32_t);
1742 write_ptr = nullptr;
1743 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1744 this->ProducerBeginWriteData(MakeUserPointer(&write_ptr),
1745 MakeUserPointer(&num_bytes), true));
1746
1747 // TODO(vtl): Hack (see also the TODO above): We can't currently wait for a
1748 // specified amount of space to be available, so poll.
1749 for (size_t i = 0; i < kMaxPoll; i++) {
1750 // Write six elements (simple), filling the buffer.
1751 num_bytes = 6u * sizeof(int32_t);
1752 int32_t buffer[100];
1753 Seq(100, 6, buffer);
1754 MojoResult result = this->ProducerWriteData(
1755 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true);
1756 if (result == MOJO_RESULT_OK)
1757 break;
1758 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, result);
1759
1760 test::Sleep(test::EpsilonDeadline());
1761 }
1762 EXPECT_EQ(6u * sizeof(int32_t), num_bytes);
1763
1764 // TODO(vtl): Hack: poll again.
1765 for (size_t i = 0; i < kMaxPoll; i++) {
1766 // We have ten.
1767 num_bytes = 0u;
1768 EXPECT_EQ(MOJO_RESULT_OK,
1769 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1770 if (num_bytes >= 10u * sizeof(int32_t))
1771 break;
1772
1773 test::Sleep(test::EpsilonDeadline());
1774 }
1775 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
1776
1777 // Note: Whether a two-phase read of ten would fail here or not is
1778 // implementation-dependent.
1779
1780 // Add waiter.
1781 waiter.Init();
1782 ASSERT_EQ(MOJO_RESULT_OK,
1783 this->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_PEER_CLOSED,
1784 2, nullptr));
1785
1786 // Close the producer.
1787 this->ProducerClose();
1788
1789 // A two-phase read of nine should work.
1790 num_bytes = 9u * sizeof(int32_t);
1791 read_ptr = nullptr;
1792 EXPECT_EQ(MOJO_RESULT_OK,
1793 this->ConsumerBeginReadData(MakeUserPointer(&read_ptr),
1794 MakeUserPointer(&num_bytes), true));
1795 EXPECT_GE(num_bytes, 9u * sizeof(int32_t));
1796 EXPECT_EQ(1, static_cast<const int32_t*>(read_ptr)[0]);
1797 EXPECT_EQ(2, static_cast<const int32_t*>(read_ptr)[1]);
1798 EXPECT_EQ(3, static_cast<const int32_t*>(read_ptr)[2]);
1799 EXPECT_EQ(4, static_cast<const int32_t*>(read_ptr)[3]);
1800 EXPECT_EQ(100, static_cast<const int32_t*>(read_ptr)[4]);
1801 EXPECT_EQ(101, static_cast<const int32_t*>(read_ptr)[5]);
1802 EXPECT_EQ(102, static_cast<const int32_t*>(read_ptr)[6]);
1803 EXPECT_EQ(103, static_cast<const int32_t*>(read_ptr)[7]);
1804 EXPECT_EQ(104, static_cast<const int32_t*>(read_ptr)[8]);
1805 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(9u * sizeof(int32_t)));
1806
1807 // Wait for peer closed.
1808 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(test::TinyDeadline(), nullptr));
1809 hss = HandleSignalsState();
1810 this->ConsumerRemoveAwakable(&waiter, &hss);
1811 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
1812 hss.satisfied_signals);
1813 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
1814 hss.satisfiable_signals);
1815
1816 // A two-phase read of two should fail, with "failed precondition".
1817 num_bytes = 2u * sizeof(int32_t);
1818 read_ptr = nullptr;
1819 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1820 this->ConsumerBeginReadData(MakeUserPointer(&read_ptr),
1821 MakeUserPointer(&num_bytes), true));
1822
1823 this->ConsumerClose();
1824 }
1825
1826 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads, 1648 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads,
1827 // respectively, as much as possible, even if it may have to "wrap around" the 1649 // respectively, as much as possible, even if it may have to "wrap around" the
1828 // internal circular buffer. (Note that the two-phase write and read need not do 1650 // internal circular buffer. (Note that the two-phase write and read need not do
1829 // this.) 1651 // this.)
1830 TYPED_TEST(DataPipeImplTest, WrapAround) { 1652 TYPED_TEST(DataPipeImplTest, WrapAround) {
1831 unsigned char test_data[1000]; 1653 unsigned char test_data[1000];
1832 for (size_t i = 0; i < MOJO_ARRAYSIZE(test_data); i++) 1654 for (size_t i = 0; i < MOJO_ARRAYSIZE(test_data); i++)
1833 test_data[i] = static_cast<unsigned char>(i); 1655 test_data[i] = static_cast<unsigned char>(i);
1834 1656
1835 const MojoCreateDataPipeOptions options = { 1657 const MojoCreateDataPipeOptions options = {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1881 EXPECT_EQ(10u, num_bytes); 1703 EXPECT_EQ(10u, num_bytes);
1882 EXPECT_EQ(0, memcmp(read_buffer, &test_data[0], 10u)); 1704 EXPECT_EQ(0, memcmp(read_buffer, &test_data[0], 10u));
1883 1705
1884 if (this->IsStrictCircularBuffer()) { 1706 if (this->IsStrictCircularBuffer()) {
1885 // Check that a two-phase write can now only write (at most) 80 bytes. (This 1707 // Check that a two-phase write can now only write (at most) 80 bytes. (This
1886 // checks an implementation detail; this behavior is not guaranteed.) 1708 // checks an implementation detail; this behavior is not guaranteed.)
1887 void* write_buffer_ptr = nullptr; 1709 void* write_buffer_ptr = nullptr;
1888 num_bytes = 0u; 1710 num_bytes = 0u;
1889 EXPECT_EQ(MOJO_RESULT_OK, 1711 EXPECT_EQ(MOJO_RESULT_OK,
1890 this->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), 1712 this->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
1891 MakeUserPointer(&num_bytes), false)); 1713 MakeUserPointer(&num_bytes)));
1892 EXPECT_TRUE(write_buffer_ptr); 1714 EXPECT_TRUE(write_buffer_ptr);
1893 EXPECT_EQ(80u, num_bytes); 1715 EXPECT_EQ(80u, num_bytes);
1894 EXPECT_EQ(MOJO_RESULT_OK, this->ProducerEndWriteData(0u)); 1716 EXPECT_EQ(MOJO_RESULT_OK, this->ProducerEndWriteData(0u));
1895 } 1717 }
1896 1718
1897 // TODO(vtl): (See corresponding TODO in TwoPhaseAllOrNone.) 1719 // TODO(vtl): (See corresponding TODO in TwoPhaseAllOrNone.)
1898 size_t total_num_bytes = 0; 1720 size_t total_num_bytes = 0u;
1899 for (size_t i = 0; i < kMaxPoll; i++) { 1721 for (size_t i = 0; i < kMaxPoll; i++) {
1900 // Write as much data as we can (using |ProducerWriteData()|). We should 1722 // Write as much data as we can (using |ProducerWriteData()|). We should
1901 // write 90 bytes (eventually). 1723 // write 90 bytes (eventually).
1902 num_bytes = 200u; 1724 num_bytes = 200u;
1903 MojoResult result = this->ProducerWriteData( 1725 MojoResult result = this->ProducerWriteData(
1904 UserPointer<const void>(&test_data[20 + total_num_bytes]), 1726 UserPointer<const void>(&test_data[20 + total_num_bytes]),
1905 MakeUserPointer(&num_bytes), false); 1727 MakeUserPointer(&num_bytes), false);
1906 if (result == MOJO_RESULT_OK) { 1728 if (result == MOJO_RESULT_OK) {
1907 total_num_bytes += num_bytes; 1729 total_num_bytes += num_bytes;
1908 if (total_num_bytes >= 90u) 1730 if (total_num_bytes >= 90u)
(...skipping 19 matching lines...) Expand all
1928 } 1750 }
1929 EXPECT_EQ(100u, num_bytes); 1751 EXPECT_EQ(100u, num_bytes);
1930 1752
1931 if (this->IsStrictCircularBuffer()) { 1753 if (this->IsStrictCircularBuffer()) {
1932 // Check that a two-phase read can now only read (at most) 90 bytes. (This 1754 // Check that a two-phase read can now only read (at most) 90 bytes. (This
1933 // checks an implementation detail; this behavior is not guaranteed.) 1755 // checks an implementation detail; this behavior is not guaranteed.)
1934 const void* read_buffer_ptr = nullptr; 1756 const void* read_buffer_ptr = nullptr;
1935 num_bytes = 0u; 1757 num_bytes = 0u;
1936 EXPECT_EQ(MOJO_RESULT_OK, 1758 EXPECT_EQ(MOJO_RESULT_OK,
1937 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), 1759 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
1938 MakeUserPointer(&num_bytes), false)); 1760 MakeUserPointer(&num_bytes)));
1939 EXPECT_TRUE(read_buffer_ptr); 1761 EXPECT_TRUE(read_buffer_ptr);
1940 EXPECT_EQ(90u, num_bytes); 1762 EXPECT_EQ(90u, num_bytes);
1941 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(0u)); 1763 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(0u));
1942 } 1764 }
1943 1765
1944 // Read as much as possible (using |ConsumerReadData()|). We should read 100 1766 // Read as much as possible (using |ConsumerReadData()|). We should read 100
1945 // bytes. 1767 // bytes.
1946 num_bytes = static_cast<uint32_t>(MOJO_ARRAYSIZE(read_buffer) * 1768 num_bytes = static_cast<uint32_t>(MOJO_ARRAYSIZE(read_buffer) *
1947 sizeof(read_buffer[0])); 1769 sizeof(read_buffer[0]));
1948 memset(read_buffer, 0, num_bytes); 1770 memset(read_buffer, 0, num_bytes);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1983 EXPECT_EQ(MOJO_RESULT_OK, 1805 EXPECT_EQ(MOJO_RESULT_OK,
1984 this->ProducerWriteData(UserPointer<const void>(kTestData), 1806 this->ProducerWriteData(UserPointer<const void>(kTestData),
1985 MakeUserPointer(&num_bytes), false)); 1807 MakeUserPointer(&num_bytes), false));
1986 EXPECT_EQ(kTestDataSize, num_bytes); 1808 EXPECT_EQ(kTestDataSize, num_bytes);
1987 1809
1988 // Start two-phase write. 1810 // Start two-phase write.
1989 void* write_buffer_ptr = nullptr; 1811 void* write_buffer_ptr = nullptr;
1990 num_bytes = 0u; 1812 num_bytes = 0u;
1991 EXPECT_EQ(MOJO_RESULT_OK, 1813 EXPECT_EQ(MOJO_RESULT_OK,
1992 this->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), 1814 this->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
1993 MakeUserPointer(&num_bytes), false)); 1815 MakeUserPointer(&num_bytes)));
1994 EXPECT_TRUE(write_buffer_ptr); 1816 EXPECT_TRUE(write_buffer_ptr);
1995 EXPECT_GT(num_bytes, 0u); 1817 EXPECT_GT(num_bytes, 0u);
1996 1818
1997 // TODO(vtl): (See corresponding TODO in TwoPhaseAllOrNone.) 1819 // TODO(vtl): (See corresponding TODO in TwoPhaseAllOrNone.)
1998 for (size_t i = 0; i < kMaxPoll; i++) { 1820 for (size_t i = 0; i < kMaxPoll; i++) {
1999 num_bytes = 0u; 1821 num_bytes = 0u;
2000 EXPECT_EQ(MOJO_RESULT_OK, 1822 EXPECT_EQ(MOJO_RESULT_OK,
2001 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); 1823 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
2002 if (num_bytes >= 2u * kTestDataSize) 1824 if (num_bytes >= 2u * kTestDataSize)
2003 break; 1825 break;
2004 1826
2005 test::Sleep(test::EpsilonDeadline()); 1827 test::Sleep(test::EpsilonDeadline());
2006 } 1828 }
2007 EXPECT_EQ(2u * kTestDataSize, num_bytes); 1829 EXPECT_EQ(2u * kTestDataSize, num_bytes);
2008 1830
2009 // Start two-phase read. 1831 // Start two-phase read.
2010 const void* read_buffer_ptr = nullptr; 1832 const void* read_buffer_ptr = nullptr;
2011 num_bytes = 0u; 1833 num_bytes = 0u;
2012 EXPECT_EQ(MOJO_RESULT_OK, 1834 EXPECT_EQ(MOJO_RESULT_OK,
2013 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), 1835 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
2014 MakeUserPointer(&num_bytes), false)); 1836 MakeUserPointer(&num_bytes)));
2015 EXPECT_TRUE(read_buffer_ptr); 1837 EXPECT_TRUE(read_buffer_ptr);
2016 EXPECT_EQ(2u * kTestDataSize, num_bytes); 1838 EXPECT_EQ(2u * kTestDataSize, num_bytes);
2017 1839
2018 // Close the producer. 1840 // Close the producer.
2019 this->ProducerClose(); 1841 this->ProducerClose();
2020 1842
2021 // The consumer can finish its two-phase read. 1843 // The consumer can finish its two-phase read.
2022 EXPECT_EQ(0, memcmp(read_buffer_ptr, kTestData, kTestDataSize)); 1844 EXPECT_EQ(0, memcmp(read_buffer_ptr, kTestData, kTestDataSize));
2023 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(kTestDataSize)); 1845 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(kTestDataSize));
2024 1846
2025 // And start another. 1847 // And start another.
2026 read_buffer_ptr = nullptr; 1848 read_buffer_ptr = nullptr;
2027 num_bytes = 0u; 1849 num_bytes = 0u;
2028 EXPECT_EQ(MOJO_RESULT_OK, 1850 EXPECT_EQ(MOJO_RESULT_OK,
2029 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), 1851 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
2030 MakeUserPointer(&num_bytes), false)); 1852 MakeUserPointer(&num_bytes)));
2031 EXPECT_TRUE(read_buffer_ptr); 1853 EXPECT_TRUE(read_buffer_ptr);
2032 EXPECT_EQ(kTestDataSize, num_bytes); 1854 EXPECT_EQ(kTestDataSize, num_bytes);
2033 1855
2034 // Close the consumer, which cancels the two-phase read. 1856 // Close the consumer, which cancels the two-phase read.
2035 this->ConsumerClose(); 1857 this->ConsumerClose();
2036 } 1858 }
2037 1859
2038 // Tests the behavior of interrupting a two-phase read and write by closing the 1860 // Tests the behavior of interrupting a two-phase read and write by closing the
2039 // consumer. 1861 // consumer.
2040 TYPED_TEST(DataPipeImplTest, TwoPhaseWriteReadCloseConsumer) { 1862 TYPED_TEST(DataPipeImplTest, TwoPhaseWriteReadCloseConsumer) {
(...skipping 23 matching lines...) Expand all
2064 EXPECT_EQ(MOJO_RESULT_OK, 1886 EXPECT_EQ(MOJO_RESULT_OK,
2065 this->ProducerWriteData(UserPointer<const void>(kTestData), 1887 this->ProducerWriteData(UserPointer<const void>(kTestData),
2066 MakeUserPointer(&num_bytes), false)); 1888 MakeUserPointer(&num_bytes), false));
2067 EXPECT_EQ(kTestDataSize, num_bytes); 1889 EXPECT_EQ(kTestDataSize, num_bytes);
2068 1890
2069 // Start two-phase write. 1891 // Start two-phase write.
2070 void* write_buffer_ptr = nullptr; 1892 void* write_buffer_ptr = nullptr;
2071 num_bytes = 0u; 1893 num_bytes = 0u;
2072 EXPECT_EQ(MOJO_RESULT_OK, 1894 EXPECT_EQ(MOJO_RESULT_OK,
2073 this->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), 1895 this->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
2074 MakeUserPointer(&num_bytes), false)); 1896 MakeUserPointer(&num_bytes)));
2075 EXPECT_TRUE(write_buffer_ptr); 1897 EXPECT_TRUE(write_buffer_ptr);
2076 ASSERT_GT(num_bytes, kTestDataSize); 1898 ASSERT_GT(num_bytes, kTestDataSize);
2077 1899
2078 // Wait for data. 1900 // Wait for data.
2079 // TODO(vtl): (See corresponding TODO in AllOrNone.) 1901 // TODO(vtl): (See corresponding TODO in AllOrNone.)
2080 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(test::TinyDeadline(), nullptr)); 1902 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(test::TinyDeadline(), nullptr));
2081 hss = HandleSignalsState(); 1903 hss = HandleSignalsState();
2082 this->ConsumerRemoveAwakable(&waiter, &hss); 1904 this->ConsumerRemoveAwakable(&waiter, &hss);
2083 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); 1905 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
2084 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, 1906 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
2085 hss.satisfiable_signals); 1907 hss.satisfiable_signals);
2086 1908
2087 // Start two-phase read. 1909 // Start two-phase read.
2088 const void* read_buffer_ptr = nullptr; 1910 const void* read_buffer_ptr = nullptr;
2089 num_bytes = 0u; 1911 num_bytes = 0u;
2090 EXPECT_EQ(MOJO_RESULT_OK, 1912 EXPECT_EQ(MOJO_RESULT_OK,
2091 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), 1913 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
2092 MakeUserPointer(&num_bytes), false)); 1914 MakeUserPointer(&num_bytes)));
2093 EXPECT_TRUE(read_buffer_ptr); 1915 EXPECT_TRUE(read_buffer_ptr);
2094 EXPECT_EQ(kTestDataSize, num_bytes); 1916 EXPECT_EQ(kTestDataSize, num_bytes);
2095 1917
2096 // Add waiter. 1918 // Add waiter.
2097 waiter.Init(); 1919 waiter.Init();
2098 ASSERT_EQ(MOJO_RESULT_OK, 1920 ASSERT_EQ(MOJO_RESULT_OK,
2099 this->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_PEER_CLOSED, 1921 this->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_PEER_CLOSED,
2100 1, nullptr)); 1922 1, nullptr));
2101 1923
2102 // Close the consumer. 1924 // Close the consumer.
(...skipping 17 matching lines...) Expand all
2120 num_bytes = kTestDataSize; 1942 num_bytes = kTestDataSize;
2121 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 1943 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
2122 this->ProducerWriteData(UserPointer<const void>(kTestData), 1944 this->ProducerWriteData(UserPointer<const void>(kTestData),
2123 MakeUserPointer(&num_bytes), false)); 1945 MakeUserPointer(&num_bytes), false));
2124 1946
2125 // As will trying to start another two-phase write. 1947 // As will trying to start another two-phase write.
2126 write_buffer_ptr = nullptr; 1948 write_buffer_ptr = nullptr;
2127 num_bytes = 0u; 1949 num_bytes = 0u;
2128 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 1950 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
2129 this->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), 1951 this->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
2130 MakeUserPointer(&num_bytes), false)); 1952 MakeUserPointer(&num_bytes)));
2131 1953
2132 this->ProducerClose(); 1954 this->ProducerClose();
2133 } 1955 }
2134 1956
2135 // Tests the behavior of "interrupting" a two-phase write by closing both the 1957 // Tests the behavior of "interrupting" a two-phase write by closing both the
2136 // producer and the consumer. 1958 // producer and the consumer.
2137 TYPED_TEST(DataPipeImplTest, TwoPhaseWriteCloseBoth) { 1959 TYPED_TEST(DataPipeImplTest, TwoPhaseWriteCloseBoth) {
2138 const uint32_t kTestDataSize = 15u; 1960 const uint32_t kTestDataSize = 15u;
2139 1961
2140 const MojoCreateDataPipeOptions options = { 1962 const MojoCreateDataPipeOptions options = {
2141 kSizeOfOptions, // |struct_size|. 1963 kSizeOfOptions, // |struct_size|.
2142 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. 1964 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
2143 1u, // |element_num_bytes|. 1965 1u, // |element_num_bytes|.
2144 1000u // |capacity_num_bytes|. 1966 1000u // |capacity_num_bytes|.
2145 }; 1967 };
2146 this->Create(options); 1968 this->Create(options);
2147 this->DoTransfer(); 1969 this->DoTransfer();
2148 1970
2149 // Start two-phase write. 1971 // Start two-phase write.
2150 void* write_buffer_ptr = nullptr; 1972 void* write_buffer_ptr = nullptr;
2151 uint32_t num_bytes = 0u; 1973 uint32_t num_bytes = 0u;
2152 EXPECT_EQ(MOJO_RESULT_OK, 1974 EXPECT_EQ(MOJO_RESULT_OK,
2153 this->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), 1975 this->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
2154 MakeUserPointer(&num_bytes), false)); 1976 MakeUserPointer(&num_bytes)));
2155 EXPECT_TRUE(write_buffer_ptr); 1977 EXPECT_TRUE(write_buffer_ptr);
2156 ASSERT_GT(num_bytes, kTestDataSize); 1978 ASSERT_GT(num_bytes, kTestDataSize);
2157 1979
2158 this->ConsumerClose(); 1980 this->ConsumerClose();
2159 this->ProducerClose(); 1981 this->ProducerClose();
2160 } 1982 }
2161 1983
2162 // Tests the behavior of writing, closing the producer, and then reading (with 1984 // Tests the behavior of writing, closing the producer, and then reading (with
2163 // and without data remaining). 1985 // and without data remaining).
2164 TYPED_TEST(DataPipeImplTest, WriteCloseProducerReadNoData) { 1986 TYPED_TEST(DataPipeImplTest, WriteCloseProducerReadNoData) {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
2225 num_bytes = static_cast<uint32_t>(sizeof(buffer)); 2047 num_bytes = static_cast<uint32_t>(sizeof(buffer));
2226 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 2048 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
2227 this->ConsumerReadData(UserPointer<void>(buffer), 2049 this->ConsumerReadData(UserPointer<void>(buffer),
2228 MakeUserPointer(&num_bytes), false, false)); 2050 MakeUserPointer(&num_bytes), false, false));
2229 2051
2230 // A two-phase read should also fail. 2052 // A two-phase read should also fail.
2231 const void* read_buffer_ptr = nullptr; 2053 const void* read_buffer_ptr = nullptr;
2232 num_bytes = 0u; 2054 num_bytes = 0u;
2233 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 2055 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
2234 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), 2056 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
2235 MakeUserPointer(&num_bytes), false)); 2057 MakeUserPointer(&num_bytes)));
2236 2058
2237 // Ditto for discard. 2059 // Ditto for discard.
2238 num_bytes = 10u; 2060 num_bytes = 10u;
2239 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 2061 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
2240 this->ConsumerDiscardData(MakeUserPointer(&num_bytes), false)); 2062 this->ConsumerDiscardData(MakeUserPointer(&num_bytes), false));
2241 2063
2242 this->ConsumerClose(); 2064 this->ConsumerClose();
2243 } 2065 }
2244 2066
2245 // Test that two-phase reads/writes behave correctly when given invalid 2067 // Test that two-phase reads/writes behave correctly when given invalid
(...skipping 25 matching lines...) Expand all
2271 // have time to propagate. 2093 // have time to propagate.
2272 test::Sleep(test::EpsilonDeadline()); 2094 test::Sleep(test::EpsilonDeadline());
2273 2095
2274 // Still no data. 2096 // Still no data.
2275 num_bytes = 1000u; 2097 num_bytes = 1000u;
2276 EXPECT_EQ(MOJO_RESULT_OK, 2098 EXPECT_EQ(MOJO_RESULT_OK,
2277 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); 2099 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
2278 EXPECT_EQ(0u, num_bytes); 2100 EXPECT_EQ(0u, num_bytes);
2279 2101
2280 // Try ending a two-phase write with an invalid amount (too much). 2102 // Try ending a two-phase write with an invalid amount (too much).
2103 void* write_ptr = nullptr;
2281 num_bytes = 0u; 2104 num_bytes = 0u;
2282 void* write_ptr = nullptr;
2283 EXPECT_EQ(MOJO_RESULT_OK, 2105 EXPECT_EQ(MOJO_RESULT_OK,
2284 this->ProducerBeginWriteData(MakeUserPointer(&write_ptr), 2106 this->ProducerBeginWriteData(MakeUserPointer(&write_ptr),
2285 MakeUserPointer(&num_bytes), false)); 2107 MakeUserPointer(&num_bytes)));
2286 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 2108 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
2287 this->ProducerEndWriteData(num_bytes + 2109 this->ProducerEndWriteData(num_bytes +
2288 static_cast<uint32_t>(sizeof(int32_t)))); 2110 static_cast<uint32_t>(sizeof(int32_t))));
2289 2111
2290 // But the two-phase write still ended. 2112 // But the two-phase write still ended.
2291 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, this->ProducerEndWriteData(0u)); 2113 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, this->ProducerEndWriteData(0u));
2292 2114
2293 // Wait a bit (as above). 2115 // Wait a bit (as above).
2294 test::Sleep(test::EpsilonDeadline()); 2116 test::Sleep(test::EpsilonDeadline());
2295 2117
2296 // Still no data. 2118 // Still no data.
2297 num_bytes = 1000u; 2119 num_bytes = 1000u;
2298 EXPECT_EQ(MOJO_RESULT_OK, 2120 EXPECT_EQ(MOJO_RESULT_OK,
2299 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); 2121 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
2300 EXPECT_EQ(0u, num_bytes); 2122 EXPECT_EQ(0u, num_bytes);
2301 2123
2302 // Try ending a two-phase write with an invalid amount (not a multiple of the 2124 // Try ending a two-phase write with an invalid amount (not a multiple of the
2303 // element size). 2125 // element size).
2126 write_ptr = nullptr;
2304 num_bytes = 0u; 2127 num_bytes = 0u;
2305 write_ptr = nullptr;
2306 EXPECT_EQ(MOJO_RESULT_OK, 2128 EXPECT_EQ(MOJO_RESULT_OK,
2307 this->ProducerBeginWriteData(MakeUserPointer(&write_ptr), 2129 this->ProducerBeginWriteData(MakeUserPointer(&write_ptr),
2308 MakeUserPointer(&num_bytes), false)); 2130 MakeUserPointer(&num_bytes)));
2309 EXPECT_GE(num_bytes, 1u); 2131 EXPECT_GE(num_bytes, 1u);
2310 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, this->ProducerEndWriteData(1u)); 2132 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, this->ProducerEndWriteData(1u));
2311 2133
2312 // But the two-phase write still ended. 2134 // But the two-phase write still ended.
2313 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, this->ProducerEndWriteData(0u)); 2135 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, this->ProducerEndWriteData(0u));
2314 2136
2315 // Wait a bit (as above). 2137 // Wait a bit (as above).
2316 test::Sleep(test::EpsilonDeadline()); 2138 test::Sleep(test::EpsilonDeadline());
2317 2139
2318 // Still no data. 2140 // Still no data.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2357 num_bytes = 0u; 2179 num_bytes = 0u;
2358 EXPECT_EQ(MOJO_RESULT_OK, 2180 EXPECT_EQ(MOJO_RESULT_OK,
2359 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); 2181 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
2360 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); 2182 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
2361 2183
2362 // Try ending a two-phase read with an invalid amount (too much). 2184 // Try ending a two-phase read with an invalid amount (too much).
2363 num_bytes = 0u; 2185 num_bytes = 0u;
2364 const void* read_ptr = nullptr; 2186 const void* read_ptr = nullptr;
2365 EXPECT_EQ(MOJO_RESULT_OK, 2187 EXPECT_EQ(MOJO_RESULT_OK,
2366 this->ConsumerBeginReadData(MakeUserPointer(&read_ptr), 2188 this->ConsumerBeginReadData(MakeUserPointer(&read_ptr),
2367 MakeUserPointer(&num_bytes), false)); 2189 MakeUserPointer(&num_bytes)));
2368 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 2190 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
2369 this->ConsumerEndReadData(num_bytes + 2191 this->ConsumerEndReadData(num_bytes +
2370 static_cast<uint32_t>(sizeof(int32_t)))); 2192 static_cast<uint32_t>(sizeof(int32_t))));
2371 2193
2372 // Still one element available. 2194 // Still one element available.
2373 num_bytes = 0u; 2195 num_bytes = 0u;
2374 EXPECT_EQ(MOJO_RESULT_OK, 2196 EXPECT_EQ(MOJO_RESULT_OK,
2375 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); 2197 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
2376 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); 2198 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
2377 2199
2378 // Try ending a two-phase read with an invalid amount (not a multiple of the 2200 // Try ending a two-phase read with an invalid amount (not a multiple of the
2379 // element size). 2201 // element size).
2380 num_bytes = 0u; 2202 num_bytes = 0u;
2381 read_ptr = nullptr; 2203 read_ptr = nullptr;
2382 EXPECT_EQ(MOJO_RESULT_OK, 2204 EXPECT_EQ(MOJO_RESULT_OK,
2383 this->ConsumerBeginReadData(MakeUserPointer(&read_ptr), 2205 this->ConsumerBeginReadData(MakeUserPointer(&read_ptr),
2384 MakeUserPointer(&num_bytes), false)); 2206 MakeUserPointer(&num_bytes)));
2385 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); 2207 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
2386 EXPECT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]); 2208 EXPECT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]);
2387 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, this->ConsumerEndReadData(1u)); 2209 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, this->ConsumerEndReadData(1u));
2388 2210
2389 // Still one element available. 2211 // Still one element available.
2390 num_bytes = 0u; 2212 num_bytes = 0u;
2391 EXPECT_EQ(MOJO_RESULT_OK, 2213 EXPECT_EQ(MOJO_RESULT_OK,
2392 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); 2214 this->ConsumerQueryData(MakeUserPointer(&num_bytes)));
2393 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); 2215 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
2394 2216
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2430 break; 2252 break;
2431 2253
2432 test::Sleep(test::EpsilonDeadline()); 2254 test::Sleep(test::EpsilonDeadline());
2433 } 2255 }
2434 EXPECT_EQ(kTestDataSize, num_bytes); 2256 EXPECT_EQ(kTestDataSize, num_bytes);
2435 2257
2436 const void* read_buffer_ptr = nullptr; 2258 const void* read_buffer_ptr = nullptr;
2437 num_bytes = 0u; 2259 num_bytes = 0u;
2438 EXPECT_EQ(MOJO_RESULT_OK, 2260 EXPECT_EQ(MOJO_RESULT_OK,
2439 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), 2261 this->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
2440 MakeUserPointer(&num_bytes), false)); 2262 MakeUserPointer(&num_bytes)));
2441 EXPECT_EQ(kTestDataSize, num_bytes); 2263 EXPECT_EQ(kTestDataSize, num_bytes);
2442 EXPECT_EQ(0, memcmp(read_buffer_ptr, kTestData, kTestDataSize)); 2264 EXPECT_EQ(0, memcmp(read_buffer_ptr, kTestData, kTestDataSize));
2443 2265
2444 // Close the producer. 2266 // Close the producer.
2445 this->ProducerClose(); 2267 this->ProducerClose();
2446 2268
2447 // Note: This tiny sleep is to allow/encourage a certain race. In particular, 2269 // Note: This tiny sleep is to allow/encourage a certain race. In particular,
2448 // for the remote producer case in 2270 // for the remote producer case in
2449 // |RemoteProducerDataPipeImpl::MarkDataAsConsumed()| (caused by 2271 // |RemoteProducerDataPipeImpl::MarkDataAsConsumed()| (caused by
2450 // |ConsumerEndReadData()| below) we want |producer_open()| to be false but 2272 // |ConsumerEndReadData()| below) we want |producer_open()| to be false but
2451 // the call to |channel_endpoint_->EnqueueMessage()| to fail. (This race can 2273 // the call to |channel_endpoint_->EnqueueMessage()| to fail. (This race can
2452 // occur without the sleep, but is much less likely.) 2274 // occur without the sleep, but is much less likely.)
2453 test::Sleep(10u); 2275 test::Sleep(10u);
2454 2276
2455 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(num_bytes)); 2277 EXPECT_EQ(MOJO_RESULT_OK, this->ConsumerEndReadData(num_bytes));
2456 2278
2457 this->ConsumerClose(); 2279 this->ConsumerClose();
2458 } 2280 }
2459 2281
2460 } // namespace 2282 } // namespace
2461 } // namespace system 2283 } // namespace system
2462 } // namespace mojo 2284 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/data_pipe_impl.h ('k') | mojo/edk/system/data_pipe_producer_dispatcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698