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

Side by Side Diff: mojo/system/message_pipe_dispatcher_unittest.cc

Issue 140503005: Mojo: foo_[0-9] -> foo[0-9]. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « mojo/system/message_pipe.cc ('k') | mojo/system/remote_message_pipe_posix_unittest.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 2013 The Chromium Authors. All rights reserved. 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 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 // NOTE(vtl): Some of these tests are inherently flaky (e.g., if run on a 5 // NOTE(vtl): Some of these tests are inherently flaky (e.g., if run on a
6 // heavily-loaded system). Sorry. |kEpsilonMicros| may be increased to increase 6 // heavily-loaded system). Sorry. |kEpsilonMicros| may be increased to increase
7 // tolerance and reduce observed flakiness. 7 // tolerance and reduce observed flakiness.
8 8
9 #include "mojo/system/message_pipe_dispatcher.h" 9 #include "mojo/system/message_pipe_dispatcher.h"
10 10
(...skipping 20 matching lines...) Expand all
31 const int64_t kMicrosPerMs = 1000; 31 const int64_t kMicrosPerMs = 1000;
32 const int64_t kEpsilonMicros = 15 * kMicrosPerMs; // 15 ms. 32 const int64_t kEpsilonMicros = 15 * kMicrosPerMs; // 15 ms.
33 33
34 TEST(MessagePipeDispatcherTest, Basic) { 34 TEST(MessagePipeDispatcherTest, Basic) {
35 test::Stopwatch stopwatch; 35 test::Stopwatch stopwatch;
36 int32_t buffer[1]; 36 int32_t buffer[1];
37 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); 37 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer));
38 uint32_t buffer_size; 38 uint32_t buffer_size;
39 int64_t elapsed_micros; 39 int64_t elapsed_micros;
40 40
41 // Run this test both with |d_0| as port 0, |d_1| as port 1 and vice versa. 41 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa.
42 for (unsigned i = 0; i < 2; i++) { 42 for (unsigned i = 0; i < 2; i++) {
43 scoped_refptr<MessagePipeDispatcher> d_0(new MessagePipeDispatcher()); 43 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher());
44 scoped_refptr<MessagePipeDispatcher> d_1(new MessagePipeDispatcher()); 44 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher());
45 { 45 {
46 scoped_refptr<MessagePipe> mp(new MessagePipe()); 46 scoped_refptr<MessagePipe> mp(new MessagePipe());
47 d_0->Init(mp, i); // 0, 1. 47 d0->Init(mp, i); // 0, 1.
48 d_1->Init(mp, i ^ 1); // 1, 0. 48 d1->Init(mp, i ^ 1); // 1, 0.
49 } 49 }
50 Waiter w; 50 Waiter w;
51 51
52 // Try adding a writable waiter when already writable. 52 // Try adding a writable waiter when already writable.
53 w.Init(); 53 w.Init();
54 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 54 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
55 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 0)); 55 d0->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 0));
56 // Shouldn't need to remove the waiter (it was not added). 56 // Shouldn't need to remove the waiter (it was not added).
57 57
58 // Add a readable waiter to |d_0|, then make it readable (by writing to 58 // Add a readable waiter to |d0|, then make it readable (by writing to
59 // |d_1|), then wait. 59 // |d1|), then wait.
60 w.Init(); 60 w.Init();
61 EXPECT_EQ(MOJO_RESULT_OK, 61 EXPECT_EQ(MOJO_RESULT_OK,
62 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 1)); 62 d0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 1));
63 buffer[0] = 123456789; 63 buffer[0] = 123456789;
64 EXPECT_EQ(MOJO_RESULT_OK, 64 EXPECT_EQ(MOJO_RESULT_OK,
65 d_1->WriteMessage(buffer, kBufferSize, 65 d1->WriteMessage(buffer, kBufferSize,
66 NULL, 66 NULL,
67 MOJO_WRITE_MESSAGE_FLAG_NONE)); 67 MOJO_WRITE_MESSAGE_FLAG_NONE));
68 stopwatch.Start(); 68 stopwatch.Start();
69 EXPECT_EQ(1, w.Wait(MOJO_DEADLINE_INDEFINITE)); 69 EXPECT_EQ(1, w.Wait(MOJO_DEADLINE_INDEFINITE));
70 elapsed_micros = stopwatch.Elapsed(); 70 elapsed_micros = stopwatch.Elapsed();
71 EXPECT_LT(elapsed_micros, kEpsilonMicros); 71 EXPECT_LT(elapsed_micros, kEpsilonMicros);
72 d_0->RemoveWaiter(&w); 72 d0->RemoveWaiter(&w);
73 73
74 // Try adding a readable waiter when already readable (from above). 74 // Try adding a readable waiter when already readable (from above).
75 w.Init(); 75 w.Init();
76 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 76 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
77 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 2)); 77 d0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 2));
78 // Shouldn't need to remove the waiter (it was not added). 78 // Shouldn't need to remove the waiter (it was not added).
79 79
80 // Make |d_0| no longer readable (by reading from it). 80 // Make |d0| no longer readable (by reading from it).
81 buffer[0] = 0; 81 buffer[0] = 0;
82 buffer_size = kBufferSize; 82 buffer_size = kBufferSize;
83 EXPECT_EQ(MOJO_RESULT_OK, 83 EXPECT_EQ(MOJO_RESULT_OK,
84 d_0->ReadMessage(buffer, &buffer_size, 84 d0->ReadMessage(buffer, &buffer_size,
85 0, NULL, 85 0, NULL,
86 MOJO_READ_MESSAGE_FLAG_NONE)); 86 MOJO_READ_MESSAGE_FLAG_NONE));
87 EXPECT_EQ(kBufferSize, buffer_size); 87 EXPECT_EQ(kBufferSize, buffer_size);
88 EXPECT_EQ(123456789, buffer[0]); 88 EXPECT_EQ(123456789, buffer[0]);
89 89
90 // Wait for zero time for readability on |d_0| (will time out). 90 // Wait for zero time for readability on |d0| (will time out).
91 w.Init(); 91 w.Init();
92 EXPECT_EQ(MOJO_RESULT_OK, 92 EXPECT_EQ(MOJO_RESULT_OK,
93 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 3)); 93 d0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 3));
94 stopwatch.Start(); 94 stopwatch.Start();
95 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0)); 95 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0));
96 elapsed_micros = stopwatch.Elapsed(); 96 elapsed_micros = stopwatch.Elapsed();
97 EXPECT_LT(elapsed_micros, kEpsilonMicros); 97 EXPECT_LT(elapsed_micros, kEpsilonMicros);
98 d_0->RemoveWaiter(&w); 98 d0->RemoveWaiter(&w);
99 99
100 // Wait for non-zero, finite time for readability on |d_0| (will time out). 100 // Wait for non-zero, finite time for readability on |d0| (will time out).
101 w.Init(); 101 w.Init();
102 EXPECT_EQ(MOJO_RESULT_OK, 102 EXPECT_EQ(MOJO_RESULT_OK,
103 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 3)); 103 d0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 3));
104 stopwatch.Start(); 104 stopwatch.Start();
105 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(2 * kEpsilonMicros)); 105 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(2 * kEpsilonMicros));
106 elapsed_micros = stopwatch.Elapsed(); 106 elapsed_micros = stopwatch.Elapsed();
107 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); 107 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros);
108 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); 108 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros);
109 d_0->RemoveWaiter(&w); 109 d0->RemoveWaiter(&w);
110 110
111 EXPECT_EQ(MOJO_RESULT_OK, d_0->Close()); 111 EXPECT_EQ(MOJO_RESULT_OK, d0->Close());
112 EXPECT_EQ(MOJO_RESULT_OK, d_1->Close()); 112 EXPECT_EQ(MOJO_RESULT_OK, d1->Close());
113 } 113 }
114 } 114 }
115 115
116 TEST(MessagePipeDispatcherTest, InvalidParams) { 116 TEST(MessagePipeDispatcherTest, InvalidParams) {
117 char buffer[1]; 117 char buffer[1];
118 118
119 scoped_refptr<MessagePipeDispatcher> d_0(new MessagePipeDispatcher()); 119 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher());
120 scoped_refptr<MessagePipeDispatcher> d_1(new MessagePipeDispatcher()); 120 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher());
121 { 121 {
122 scoped_refptr<MessagePipe> mp(new MessagePipe()); 122 scoped_refptr<MessagePipe> mp(new MessagePipe());
123 d_0->Init(mp, 0); 123 d0->Init(mp, 0);
124 d_1->Init(mp, 1); 124 d1->Init(mp, 1);
125 } 125 }
126 126
127 // |WriteMessage|: 127 // |WriteMessage|:
128 // Null buffer with nonzero buffer size. 128 // Null buffer with nonzero buffer size.
129 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 129 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
130 d_0->WriteMessage(NULL, 1, 130 d0->WriteMessage(NULL, 1,
131 NULL, 131 NULL,
132 MOJO_WRITE_MESSAGE_FLAG_NONE)); 132 MOJO_WRITE_MESSAGE_FLAG_NONE));
133 // Huge buffer size. 133 // Huge buffer size.
134 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, 134 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
135 d_0->WriteMessage(buffer, std::numeric_limits<uint32_t>::max(), 135 d0->WriteMessage(buffer, std::numeric_limits<uint32_t>::max(),
136 NULL, 136 NULL,
137 MOJO_WRITE_MESSAGE_FLAG_NONE)); 137 MOJO_WRITE_MESSAGE_FLAG_NONE));
138 138
139 // |ReadMessage|: 139 // |ReadMessage|:
140 // Null buffer with nonzero buffer size. 140 // Null buffer with nonzero buffer size.
141 uint32_t buffer_size = 1; 141 uint32_t buffer_size = 1;
142 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 142 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
143 d_0->ReadMessage(NULL, &buffer_size, 143 d0->ReadMessage(NULL, &buffer_size,
144 0, NULL, 144 0, NULL,
145 MOJO_READ_MESSAGE_FLAG_NONE)); 145 MOJO_READ_MESSAGE_FLAG_NONE));
146 146
147 EXPECT_EQ(MOJO_RESULT_OK, d_0->Close()); 147 EXPECT_EQ(MOJO_RESULT_OK, d0->Close());
148 EXPECT_EQ(MOJO_RESULT_OK, d_1->Close()); 148 EXPECT_EQ(MOJO_RESULT_OK, d1->Close());
149 } 149 }
150 150
151 // Test what happens when one end is closed (single-threaded test). 151 // Test what happens when one end is closed (single-threaded test).
152 TEST(MessagePipeDispatcherTest, BasicClosed) { 152 TEST(MessagePipeDispatcherTest, BasicClosed) {
153 int32_t buffer[1]; 153 int32_t buffer[1];
154 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); 154 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer));
155 uint32_t buffer_size; 155 uint32_t buffer_size;
156 156
157 // Run this test both with |d_0| as port 0, |d_1| as port 1 and vice versa. 157 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa.
158 for (unsigned i = 0; i < 2; i++) { 158 for (unsigned i = 0; i < 2; i++) {
159 scoped_refptr<MessagePipeDispatcher> d_0(new MessagePipeDispatcher()); 159 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher());
160 scoped_refptr<MessagePipeDispatcher> d_1(new MessagePipeDispatcher()); 160 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher());
161 { 161 {
162 scoped_refptr<MessagePipe> mp(new MessagePipe()); 162 scoped_refptr<MessagePipe> mp(new MessagePipe());
163 d_0->Init(mp, i); // 0, 1. 163 d0->Init(mp, i); // 0, 1.
164 d_1->Init(mp, i ^ 1); // 1, 0. 164 d1->Init(mp, i ^ 1); // 1, 0.
165 } 165 }
166 Waiter w; 166 Waiter w;
167 167
168 // Write (twice) to |d_1|. 168 // Write (twice) to |d1|.
169 buffer[0] = 123456789; 169 buffer[0] = 123456789;
170 EXPECT_EQ(MOJO_RESULT_OK, 170 EXPECT_EQ(MOJO_RESULT_OK,
171 d_1->WriteMessage(buffer, kBufferSize, 171 d1->WriteMessage(buffer, kBufferSize,
172 NULL, 172 NULL,
173 MOJO_WRITE_MESSAGE_FLAG_NONE)); 173 MOJO_WRITE_MESSAGE_FLAG_NONE));
174 buffer[0] = 234567890; 174 buffer[0] = 234567890;
175 EXPECT_EQ(MOJO_RESULT_OK, 175 EXPECT_EQ(MOJO_RESULT_OK,
176 d_1->WriteMessage(buffer, kBufferSize, 176 d1->WriteMessage(buffer, kBufferSize,
177 NULL, 177 NULL,
178 MOJO_WRITE_MESSAGE_FLAG_NONE)); 178 MOJO_WRITE_MESSAGE_FLAG_NONE));
179 179
180 // Try waiting for readable on |d_0|; should fail (already satisfied). 180 // Try waiting for readable on |d0|; should fail (already satisfied).
181 w.Init(); 181 w.Init();
182 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 182 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
183 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 0)); 183 d0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 0));
184 184
185 // Try reading from |d_1|; should fail (nothing to read). 185 // Try reading from |d1|; should fail (nothing to read).
186 buffer[0] = 0; 186 buffer[0] = 0;
187 buffer_size = kBufferSize; 187 buffer_size = kBufferSize;
188 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, 188 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
189 d_1->ReadMessage(buffer, &buffer_size, 189 d1->ReadMessage(buffer, &buffer_size,
190 0, NULL, 190 0, NULL,
191 MOJO_READ_MESSAGE_FLAG_NONE)); 191 MOJO_READ_MESSAGE_FLAG_NONE));
192 192
193 // Close |d_1|. 193 // Close |d1|.
194 EXPECT_EQ(MOJO_RESULT_OK, d_1->Close()); 194 EXPECT_EQ(MOJO_RESULT_OK, d1->Close());
195 195
196 // Try waiting for readable on |d_0|; should fail (already satisfied). 196 // Try waiting for readable on |d0|; should fail (already satisfied).
197 w.Init(); 197 w.Init();
198 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 198 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
199 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 1)); 199 d0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 1));
200 200
201 // Read from |d_0|. 201 // Read from |d0|.
202 buffer[0] = 0; 202 buffer[0] = 0;
203 buffer_size = kBufferSize; 203 buffer_size = kBufferSize;
204 EXPECT_EQ(MOJO_RESULT_OK, 204 EXPECT_EQ(MOJO_RESULT_OK,
205 d_0->ReadMessage(buffer, &buffer_size, 205 d0->ReadMessage(buffer, &buffer_size,
206 0, NULL, 206 0, NULL,
207 MOJO_READ_MESSAGE_FLAG_NONE)); 207 MOJO_READ_MESSAGE_FLAG_NONE));
208 EXPECT_EQ(kBufferSize, buffer_size); 208 EXPECT_EQ(kBufferSize, buffer_size);
209 EXPECT_EQ(123456789, buffer[0]); 209 EXPECT_EQ(123456789, buffer[0]);
210 210
211 // Try waiting for readable on |d_0|; should fail (already satisfied). 211 // Try waiting for readable on |d0|; should fail (already satisfied).
212 w.Init(); 212 w.Init();
213 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 213 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
214 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 2)); 214 d0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 2));
215 215
216 // Read again from |d_0|. 216 // Read again from |d0|.
217 buffer[0] = 0; 217 buffer[0] = 0;
218 buffer_size = kBufferSize; 218 buffer_size = kBufferSize;
219 EXPECT_EQ(MOJO_RESULT_OK, 219 EXPECT_EQ(MOJO_RESULT_OK,
220 d_0->ReadMessage(buffer, &buffer_size, 220 d0->ReadMessage(buffer, &buffer_size,
221 0, NULL, 221 0, NULL,
222 MOJO_READ_MESSAGE_FLAG_NONE)); 222 MOJO_READ_MESSAGE_FLAG_NONE));
223 EXPECT_EQ(kBufferSize, buffer_size); 223 EXPECT_EQ(kBufferSize, buffer_size);
224 EXPECT_EQ(234567890, buffer[0]); 224 EXPECT_EQ(234567890, buffer[0]);
225 225
226 // Try waiting for readable on |d_0|; should fail (unsatisfiable). 226 // Try waiting for readable on |d0|; should fail (unsatisfiable).
227 w.Init(); 227 w.Init();
228 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 228 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
229 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 3)); 229 d0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 3));
230 230
231 // Try waiting for writable on |d_0|; should fail (unsatisfiable). 231 // Try waiting for writable on |d0|; should fail (unsatisfiable).
232 w.Init(); 232 w.Init();
233 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 233 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
234 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 4)); 234 d0->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 4));
235 235
236 // Try reading from |d_0|; should fail (nothing to read and other end 236 // Try reading from |d0|; should fail (nothing to read and other end
237 // closed). 237 // closed).
238 buffer[0] = 0; 238 buffer[0] = 0;
239 buffer_size = kBufferSize; 239 buffer_size = kBufferSize;
240 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 240 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
241 d_0->ReadMessage(buffer, &buffer_size, 241 d0->ReadMessage(buffer, &buffer_size,
242 0, NULL, 242 0, NULL,
243 MOJO_READ_MESSAGE_FLAG_NONE)); 243 MOJO_READ_MESSAGE_FLAG_NONE));
244 244
245 // Try writing to |d_0|; should fail (other end closed). 245 // Try writing to |d0|; should fail (other end closed).
246 buffer[0] = 345678901; 246 buffer[0] = 345678901;
247 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 247 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
248 d_0->WriteMessage(buffer, kBufferSize, 248 d0->WriteMessage(buffer, kBufferSize,
249 NULL, 249 NULL,
250 MOJO_WRITE_MESSAGE_FLAG_NONE)); 250 MOJO_WRITE_MESSAGE_FLAG_NONE));
251 251
252 EXPECT_EQ(MOJO_RESULT_OK, d_0->Close()); 252 EXPECT_EQ(MOJO_RESULT_OK, d0->Close());
253 } 253 }
254 } 254 }
255 255
256 TEST(MessagePipeDispatcherTest, BasicThreaded) { 256 TEST(MessagePipeDispatcherTest, BasicThreaded) {
257 test::Stopwatch stopwatch; 257 test::Stopwatch stopwatch;
258 int32_t buffer[1]; 258 int32_t buffer[1];
259 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); 259 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer));
260 uint32_t buffer_size; 260 uint32_t buffer_size;
261 bool did_wait; 261 bool did_wait;
262 MojoResult result; 262 MojoResult result;
263 int64_t elapsed_micros; 263 int64_t elapsed_micros;
264 264
265 // Run this test both with |d_0| as port 0, |d_1| as port 1 and vice versa. 265 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa.
266 for (unsigned i = 0; i < 2; i++) { 266 for (unsigned i = 0; i < 2; i++) {
267 scoped_refptr<MessagePipeDispatcher> d_0(new MessagePipeDispatcher()); 267 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher());
268 scoped_refptr<MessagePipeDispatcher> d_1(new MessagePipeDispatcher()); 268 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher());
269 { 269 {
270 scoped_refptr<MessagePipe> mp(new MessagePipe()); 270 scoped_refptr<MessagePipe> mp(new MessagePipe());
271 d_0->Init(mp, i); // 0, 1. 271 d0->Init(mp, i); // 0, 1.
272 d_1->Init(mp, i ^ 1); // 1, 0. 272 d1->Init(mp, i ^ 1); // 1, 0.
273 } 273 }
274 274
275 // Wait for readable on |d_1|, which will become readable after some time. 275 // Wait for readable on |d1|, which will become readable after some time.
276 { 276 {
277 test::WaiterThread thread(d_1, 277 test::WaiterThread thread(d1,
278 MOJO_WAIT_FLAG_READABLE, 278 MOJO_WAIT_FLAG_READABLE,
279 MOJO_DEADLINE_INDEFINITE, 279 MOJO_DEADLINE_INDEFINITE,
280 0, 280 0,
281 &did_wait, &result); 281 &did_wait, &result);
282 stopwatch.Start(); 282 stopwatch.Start();
283 thread.Start(); 283 thread.Start();
284 base::PlatformThread::Sleep( 284 base::PlatformThread::Sleep(
285 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); 285 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros));
286 // Wake it up by writing to |d_0|. 286 // Wake it up by writing to |d0|.
287 buffer[0] = 123456789; 287 buffer[0] = 123456789;
288 EXPECT_EQ(MOJO_RESULT_OK, 288 EXPECT_EQ(MOJO_RESULT_OK,
289 d_0->WriteMessage(buffer, kBufferSize, 289 d0->WriteMessage(buffer, kBufferSize,
290 NULL, 290 NULL,
291 MOJO_WRITE_MESSAGE_FLAG_NONE)); 291 MOJO_WRITE_MESSAGE_FLAG_NONE));
292 } // Joins the thread. 292 } // Joins the thread.
293 elapsed_micros = stopwatch.Elapsed(); 293 elapsed_micros = stopwatch.Elapsed();
294 EXPECT_TRUE(did_wait); 294 EXPECT_TRUE(did_wait);
295 EXPECT_EQ(0, result); 295 EXPECT_EQ(0, result);
296 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); 296 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros);
297 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); 297 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros);
298 298
299 // Now |d_1| is already readable. Try waiting for it again. 299 // Now |d1| is already readable. Try waiting for it again.
300 { 300 {
301 test::WaiterThread thread(d_1, 301 test::WaiterThread thread(d1,
302 MOJO_WAIT_FLAG_READABLE, 302 MOJO_WAIT_FLAG_READABLE,
303 MOJO_DEADLINE_INDEFINITE, 303 MOJO_DEADLINE_INDEFINITE,
304 1, 304 1,
305 &did_wait, &result); 305 &did_wait, &result);
306 stopwatch.Start(); 306 stopwatch.Start();
307 thread.Start(); 307 thread.Start();
308 } // Joins the thread. 308 } // Joins the thread.
309 elapsed_micros = stopwatch.Elapsed(); 309 elapsed_micros = stopwatch.Elapsed();
310 EXPECT_FALSE(did_wait); 310 EXPECT_FALSE(did_wait);
311 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, result); 311 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, result);
312 EXPECT_LT(elapsed_micros, kEpsilonMicros); 312 EXPECT_LT(elapsed_micros, kEpsilonMicros);
313 313
314 // Consume what we wrote to |d_0|. 314 // Consume what we wrote to |d0|.
315 buffer[0] = 0; 315 buffer[0] = 0;
316 buffer_size = kBufferSize; 316 buffer_size = kBufferSize;
317 EXPECT_EQ(MOJO_RESULT_OK, 317 EXPECT_EQ(MOJO_RESULT_OK,
318 d_1->ReadMessage(buffer, &buffer_size, 318 d1->ReadMessage(buffer, &buffer_size,
319 0, NULL, 319 0, NULL,
320 MOJO_READ_MESSAGE_FLAG_NONE)); 320 MOJO_READ_MESSAGE_FLAG_NONE));
321 EXPECT_EQ(kBufferSize, buffer_size); 321 EXPECT_EQ(kBufferSize, buffer_size);
322 EXPECT_EQ(123456789, buffer[0]); 322 EXPECT_EQ(123456789, buffer[0]);
323 323
324 // Wait for readable on |d_1| and close |d_0| after some time, which should 324 // Wait for readable on |d1| and close |d0| after some time, which should
325 // cancel that wait. 325 // cancel that wait.
326 { 326 {
327 test::WaiterThread thread(d_1, 327 test::WaiterThread thread(d1,
328 MOJO_WAIT_FLAG_READABLE, 328 MOJO_WAIT_FLAG_READABLE,
329 MOJO_DEADLINE_INDEFINITE, 329 MOJO_DEADLINE_INDEFINITE,
330 0, 330 0,
331 &did_wait, &result); 331 &did_wait, &result);
332 stopwatch.Start(); 332 stopwatch.Start();
333 thread.Start(); 333 thread.Start();
334 base::PlatformThread::Sleep( 334 base::PlatformThread::Sleep(
335 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); 335 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros));
336 EXPECT_EQ(MOJO_RESULT_OK, d_0->Close()); 336 EXPECT_EQ(MOJO_RESULT_OK, d0->Close());
337 } // Joins the thread. 337 } // Joins the thread.
338 elapsed_micros = stopwatch.Elapsed(); 338 elapsed_micros = stopwatch.Elapsed();
339 EXPECT_TRUE(did_wait); 339 EXPECT_TRUE(did_wait);
340 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); 340 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result);
341 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); 341 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros);
342 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); 342 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros);
343 343
344 EXPECT_EQ(MOJO_RESULT_OK, d_1->Close()); 344 EXPECT_EQ(MOJO_RESULT_OK, d1->Close());
345 } 345 }
346 346
347 for (unsigned i = 0; i < 2; i++) { 347 for (unsigned i = 0; i < 2; i++) {
348 scoped_refptr<MessagePipeDispatcher> d_0(new MessagePipeDispatcher()); 348 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher());
349 scoped_refptr<MessagePipeDispatcher> d_1(new MessagePipeDispatcher()); 349 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher());
350 { 350 {
351 scoped_refptr<MessagePipe> mp(new MessagePipe()); 351 scoped_refptr<MessagePipe> mp(new MessagePipe());
352 d_0->Init(mp, i); // 0, 1. 352 d0->Init(mp, i); // 0, 1.
353 d_1->Init(mp, i ^ 1); // 1, 0. 353 d1->Init(mp, i ^ 1); // 1, 0.
354 } 354 }
355 355
356 // Wait for readable on |d_1| and close |d_1| after some time, which should 356 // Wait for readable on |d1| and close |d1| after some time, which should
357 // cancel that wait. 357 // cancel that wait.
358 { 358 {
359 test::WaiterThread thread(d_1, 359 test::WaiterThread thread(d1,
360 MOJO_WAIT_FLAG_READABLE, 360 MOJO_WAIT_FLAG_READABLE,
361 MOJO_DEADLINE_INDEFINITE, 361 MOJO_DEADLINE_INDEFINITE,
362 0, 362 0,
363 &did_wait, &result); 363 &did_wait, &result);
364 stopwatch.Start(); 364 stopwatch.Start();
365 thread.Start(); 365 thread.Start();
366 base::PlatformThread::Sleep( 366 base::PlatformThread::Sleep(
367 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); 367 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros));
368 EXPECT_EQ(MOJO_RESULT_OK, d_1->Close()); 368 EXPECT_EQ(MOJO_RESULT_OK, d1->Close());
369 } // Joins the thread. 369 } // Joins the thread.
370 elapsed_micros = stopwatch.Elapsed(); 370 elapsed_micros = stopwatch.Elapsed();
371 EXPECT_TRUE(did_wait); 371 EXPECT_TRUE(did_wait);
372 EXPECT_EQ(MOJO_RESULT_CANCELLED, result); 372 EXPECT_EQ(MOJO_RESULT_CANCELLED, result);
373 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); 373 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros);
374 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); 374 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros);
375 375
376 EXPECT_EQ(MOJO_RESULT_OK, d_0->Close()); 376 EXPECT_EQ(MOJO_RESULT_OK, d0->Close());
377 } 377 }
378 } 378 }
379 379
380 // Stress test ----------------------------------------------------------------- 380 // Stress test -----------------------------------------------------------------
381 381
382 const size_t kMaxMessageSize = 2000; 382 const size_t kMaxMessageSize = 2000;
383 383
384 class WriterThread : public base::SimpleThread { 384 class WriterThread : public base::SimpleThread {
385 public: 385 public:
386 // |*messages_written| and |*bytes_written| belong to the thread while it's 386 // |*messages_written| and |*bytes_written| belong to the thread while it's
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 EXPECT_EQ(total_messages_written, total_messages_read); 578 EXPECT_EQ(total_messages_written, total_messages_read);
579 EXPECT_EQ(total_bytes_written, total_bytes_read); 579 EXPECT_EQ(total_bytes_written, total_bytes_read);
580 580
581 EXPECT_EQ(MOJO_RESULT_OK, d_write->Close()); 581 EXPECT_EQ(MOJO_RESULT_OK, d_write->Close());
582 EXPECT_EQ(MOJO_RESULT_OK, d_read->Close()); 582 EXPECT_EQ(MOJO_RESULT_OK, d_read->Close());
583 } 583 }
584 584
585 } // namespace 585 } // namespace
586 } // namespace system 586 } // namespace system
587 } // namespace mojo 587 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/system/message_pipe.cc ('k') | mojo/system/remote_message_pipe_posix_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698