OLD | NEW |
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 #include "mojo/edk/system/local_data_pipe.h" | 5 #include "mojo/edk/system/local_data_pipe.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 230 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
231 Waiter waiter; | 231 Waiter waiter; |
232 uint32_t context = 0; | 232 uint32_t context = 0; |
233 HandleSignalsState hss; | 233 HandleSignalsState hss; |
234 | 234 |
235 // Never readable. | 235 // Never readable. |
236 waiter.Init(); | 236 waiter.Init(); |
237 hss = HandleSignalsState(); | 237 hss = HandleSignalsState(); |
238 EXPECT_EQ( | 238 EXPECT_EQ( |
239 MOJO_RESULT_FAILED_PRECONDITION, | 239 MOJO_RESULT_FAILED_PRECONDITION, |
240 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss)); | 240 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss)); |
241 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 241 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
242 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 242 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
243 hss.satisfiable_signals); | 243 hss.satisfiable_signals); |
244 | 244 |
245 // Already writable. | 245 // Already writable. |
246 waiter.Init(); | 246 waiter.Init(); |
247 hss = HandleSignalsState(); | 247 hss = HandleSignalsState(); |
248 EXPECT_EQ( | 248 EXPECT_EQ( |
249 MOJO_RESULT_ALREADY_EXISTS, | 249 MOJO_RESULT_ALREADY_EXISTS, |
250 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 34, &hss)); | 250 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 34, &hss)); |
251 | 251 |
252 // Write two elements. | 252 // Write two elements. |
253 int32_t elements[2] = {123, 456}; | 253 int32_t elements[2] = {123, 456}; |
254 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); | 254 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
255 EXPECT_EQ(MOJO_RESULT_OK, | 255 EXPECT_EQ(MOJO_RESULT_OK, |
256 dp->ProducerWriteData(UserPointer<const void>(elements), | 256 dp->ProducerWriteData(UserPointer<const void>(elements), |
257 MakeUserPointer(&num_bytes), true)); | 257 MakeUserPointer(&num_bytes), true)); |
258 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); | 258 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); |
259 | 259 |
260 // Adding a waiter should now succeed. | 260 // Adding a waiter should now succeed. |
261 waiter.Init(); | 261 waiter.Init(); |
262 ASSERT_EQ( | 262 ASSERT_EQ(MOJO_RESULT_OK, |
263 MOJO_RESULT_OK, | 263 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 56, |
264 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 56, nullptr)); | 264 nullptr)); |
265 // And it shouldn't be writable yet. | 265 // And it shouldn't be writable yet. |
266 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 266 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
267 hss = HandleSignalsState(); | 267 hss = HandleSignalsState(); |
268 dp->ProducerRemoveWaiter(&waiter, &hss); | 268 dp->ProducerRemoveAwakable(&waiter, &hss); |
269 EXPECT_EQ(0u, hss.satisfied_signals); | 269 EXPECT_EQ(0u, hss.satisfied_signals); |
270 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 270 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
271 hss.satisfiable_signals); | 271 hss.satisfiable_signals); |
272 | 272 |
273 // Peek one element. | 273 // Peek one element. |
274 elements[0] = -1; | 274 elements[0] = -1; |
275 elements[1] = -1; | 275 elements[1] = -1; |
276 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 276 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
277 EXPECT_EQ(MOJO_RESULT_OK, | 277 EXPECT_EQ(MOJO_RESULT_OK, |
278 dp->ConsumerReadData(UserPointer<void>(elements), | 278 dp->ConsumerReadData(UserPointer<void>(elements), |
279 MakeUserPointer(&num_bytes), true, true)); | 279 MakeUserPointer(&num_bytes), true, true)); |
280 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 280 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
281 EXPECT_EQ(123, elements[0]); | 281 EXPECT_EQ(123, elements[0]); |
282 EXPECT_EQ(-1, elements[1]); | 282 EXPECT_EQ(-1, elements[1]); |
283 | 283 |
284 // Add a waiter. | 284 // Add a waiter. |
285 waiter.Init(); | 285 waiter.Init(); |
286 ASSERT_EQ( | 286 ASSERT_EQ(MOJO_RESULT_OK, |
287 MOJO_RESULT_OK, | 287 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 56, |
288 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 56, nullptr)); | 288 nullptr)); |
289 // And it still shouldn't be writable yet. | 289 // And it still shouldn't be writable yet. |
290 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 290 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
291 hss = HandleSignalsState(); | 291 hss = HandleSignalsState(); |
292 dp->ProducerRemoveWaiter(&waiter, &hss); | 292 dp->ProducerRemoveAwakable(&waiter, &hss); |
293 EXPECT_EQ(0u, hss.satisfied_signals); | 293 EXPECT_EQ(0u, hss.satisfied_signals); |
294 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 294 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
295 hss.satisfiable_signals); | 295 hss.satisfiable_signals); |
296 | 296 |
297 // Do it again. | 297 // Do it again. |
298 waiter.Init(); | 298 waiter.Init(); |
299 ASSERT_EQ( | 299 ASSERT_EQ(MOJO_RESULT_OK, |
300 MOJO_RESULT_OK, | 300 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 78, |
301 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 78, nullptr)); | 301 nullptr)); |
302 | 302 |
303 // Read one element. | 303 // Read one element. |
304 elements[0] = -1; | 304 elements[0] = -1; |
305 elements[1] = -1; | 305 elements[1] = -1; |
306 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 306 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
307 EXPECT_EQ(MOJO_RESULT_OK, | 307 EXPECT_EQ(MOJO_RESULT_OK, |
308 dp->ConsumerReadData(UserPointer<void>(elements), | 308 dp->ConsumerReadData(UserPointer<void>(elements), |
309 MakeUserPointer(&num_bytes), true, false)); | 309 MakeUserPointer(&num_bytes), true, false)); |
310 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 310 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
311 EXPECT_EQ(123, elements[0]); | 311 EXPECT_EQ(123, elements[0]); |
312 EXPECT_EQ(-1, elements[1]); | 312 EXPECT_EQ(-1, elements[1]); |
313 | 313 |
314 // Waiting should now succeed. | 314 // Waiting should now succeed. |
315 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); | 315 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); |
316 EXPECT_EQ(78u, context); | 316 EXPECT_EQ(78u, context); |
317 hss = HandleSignalsState(); | 317 hss = HandleSignalsState(); |
318 dp->ProducerRemoveWaiter(&waiter, &hss); | 318 dp->ProducerRemoveAwakable(&waiter, &hss); |
319 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 319 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
320 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 320 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
321 hss.satisfiable_signals); | 321 hss.satisfiable_signals); |
322 | 322 |
323 // Try writing, using a two-phase write. | 323 // Try writing, using a two-phase write. |
324 void* buffer = nullptr; | 324 void* buffer = nullptr; |
325 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); | 325 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); |
326 EXPECT_EQ(MOJO_RESULT_OK, | 326 EXPECT_EQ(MOJO_RESULT_OK, |
327 dp->ProducerBeginWriteData(MakeUserPointer(&buffer), | 327 dp->ProducerBeginWriteData(MakeUserPointer(&buffer), |
328 MakeUserPointer(&num_bytes), false)); | 328 MakeUserPointer(&num_bytes), false)); |
329 EXPECT_TRUE(buffer); | 329 EXPECT_TRUE(buffer); |
330 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 330 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
331 | 331 |
332 static_cast<int32_t*>(buffer)[0] = 789; | 332 static_cast<int32_t*>(buffer)[0] = 789; |
333 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(static_cast<uint32_t>( | 333 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(static_cast<uint32_t>( |
334 1u * sizeof(elements[0])))); | 334 1u * sizeof(elements[0])))); |
335 | 335 |
336 // Add a waiter. | 336 // Add a waiter. |
337 waiter.Init(); | 337 waiter.Init(); |
338 ASSERT_EQ( | 338 ASSERT_EQ(MOJO_RESULT_OK, |
339 MOJO_RESULT_OK, | 339 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 90, |
340 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 90, nullptr)); | 340 nullptr)); |
341 | 341 |
342 // Read one element, using a two-phase read. | 342 // Read one element, using a two-phase read. |
343 const void* read_buffer = nullptr; | 343 const void* read_buffer = nullptr; |
344 num_bytes = 0u; | 344 num_bytes = 0u; |
345 EXPECT_EQ(MOJO_RESULT_OK, | 345 EXPECT_EQ(MOJO_RESULT_OK, |
346 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer), | 346 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer), |
347 MakeUserPointer(&num_bytes), false)); | 347 MakeUserPointer(&num_bytes), false)); |
348 EXPECT_TRUE(read_buffer); | 348 EXPECT_TRUE(read_buffer); |
349 // Since we only read one element (after having written three in all), the | 349 // Since we only read one element (after having written three in all), the |
350 // two-phase read should only allow us to read one. This checks an | 350 // two-phase read should only allow us to read one. This checks an |
351 // implementation detail! | 351 // implementation detail! |
352 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 352 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
353 EXPECT_EQ(456, static_cast<const int32_t*>(read_buffer)[0]); | 353 EXPECT_EQ(456, static_cast<const int32_t*>(read_buffer)[0]); |
354 EXPECT_EQ( | 354 EXPECT_EQ( |
355 MOJO_RESULT_OK, | 355 MOJO_RESULT_OK, |
356 dp->ConsumerEndReadData(static_cast<uint32_t>(1u * sizeof(elements[0])))); | 356 dp->ConsumerEndReadData(static_cast<uint32_t>(1u * sizeof(elements[0])))); |
357 | 357 |
358 // Waiting should succeed. | 358 // Waiting should succeed. |
359 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); | 359 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); |
360 EXPECT_EQ(90u, context); | 360 EXPECT_EQ(90u, context); |
361 hss = HandleSignalsState(); | 361 hss = HandleSignalsState(); |
362 dp->ProducerRemoveWaiter(&waiter, &hss); | 362 dp->ProducerRemoveAwakable(&waiter, &hss); |
363 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 363 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
364 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 364 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
365 hss.satisfiable_signals); | 365 hss.satisfiable_signals); |
366 | 366 |
367 // Write one element. | 367 // Write one element. |
368 elements[0] = 123; | 368 elements[0] = 123; |
369 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 369 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
370 EXPECT_EQ(MOJO_RESULT_OK, | 370 EXPECT_EQ(MOJO_RESULT_OK, |
371 dp->ProducerWriteData(UserPointer<const void>(elements), | 371 dp->ProducerWriteData(UserPointer<const void>(elements), |
372 MakeUserPointer(&num_bytes), false)); | 372 MakeUserPointer(&num_bytes), false)); |
373 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 373 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
374 | 374 |
375 // Add a waiter. | 375 // Add a waiter. |
376 waiter.Init(); | 376 waiter.Init(); |
377 ASSERT_EQ( | 377 ASSERT_EQ(MOJO_RESULT_OK, |
378 MOJO_RESULT_OK, | 378 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, |
379 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, nullptr)); | 379 nullptr)); |
380 | 380 |
381 // Close the consumer. | 381 // Close the consumer. |
382 dp->ConsumerClose(); | 382 dp->ConsumerClose(); |
383 | 383 |
384 // It should now be never-writable. | 384 // It should now be never-writable. |
385 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000, &context)); | 385 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000, &context)); |
386 EXPECT_EQ(12u, context); | 386 EXPECT_EQ(12u, context); |
387 hss = HandleSignalsState(); | 387 hss = HandleSignalsState(); |
388 dp->ProducerRemoveWaiter(&waiter, &hss); | 388 dp->ProducerRemoveAwakable(&waiter, &hss); |
389 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 389 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
390 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 390 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
391 | 391 |
392 dp->ProducerClose(); | 392 dp->ProducerClose(); |
393 } | 393 } |
394 | 394 |
395 TEST(LocalDataPipeTest, PeerClosedWaiting) { | 395 TEST(LocalDataPipeTest, PeerClosedWaiting) { |
396 const MojoCreateDataPipeOptions options = { | 396 const MojoCreateDataPipeOptions options = { |
397 kSizeOfOptions, // |struct_size|. | 397 kSizeOfOptions, // |struct_size|. |
398 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 398 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
399 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 399 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
400 2 * sizeof(int32_t) // |capacity_num_bytes|. | 400 2 * sizeof(int32_t) // |capacity_num_bytes|. |
401 }; | 401 }; |
402 MojoCreateDataPipeOptions validated_options = {0}; | 402 MojoCreateDataPipeOptions validated_options = {0}; |
403 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 403 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
404 MakeUserPointer(&options), &validated_options)); | 404 MakeUserPointer(&options), &validated_options)); |
405 | 405 |
406 Waiter waiter; | 406 Waiter waiter; |
407 HandleSignalsState hss; | 407 HandleSignalsState hss; |
408 | 408 |
409 // Check MOJO_HANDLE_SIGNAL_PEER_CLOSED on producer. | 409 // Check MOJO_HANDLE_SIGNAL_PEER_CLOSED on producer. |
410 { | 410 { |
411 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 411 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
412 // Add a waiter. | 412 // Add a waiter. |
413 waiter.Init(); | 413 waiter.Init(); |
414 ASSERT_EQ(MOJO_RESULT_OK, | 414 ASSERT_EQ(MOJO_RESULT_OK, |
415 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_PEER_CLOSED, 12, | 415 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
416 nullptr)); | 416 12, nullptr)); |
417 | 417 |
418 // Close the consumer. | 418 // Close the consumer. |
419 dp->ConsumerClose(); | 419 dp->ConsumerClose(); |
420 | 420 |
421 // It should be signaled. | 421 // It should be signaled. |
422 uint32_t context = 0; | 422 uint32_t context = 0; |
423 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); | 423 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); |
424 EXPECT_EQ(12u, context); | 424 EXPECT_EQ(12u, context); |
425 hss = HandleSignalsState(); | 425 hss = HandleSignalsState(); |
426 dp->ProducerRemoveWaiter(&waiter, &hss); | 426 dp->ProducerRemoveAwakable(&waiter, &hss); |
427 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 427 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
428 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 428 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
429 | 429 |
430 dp->ProducerClose(); | 430 dp->ProducerClose(); |
431 } | 431 } |
432 | 432 |
433 // Check MOJO_HANDLE_SIGNAL_PEER_CLOSED on consumer. | 433 // Check MOJO_HANDLE_SIGNAL_PEER_CLOSED on consumer. |
434 { | 434 { |
435 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 435 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
436 // Add a waiter. | 436 // Add a waiter. |
437 waiter.Init(); | 437 waiter.Init(); |
438 ASSERT_EQ(MOJO_RESULT_OK, | 438 ASSERT_EQ(MOJO_RESULT_OK, |
439 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_PEER_CLOSED, 12, | 439 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
440 nullptr)); | 440 12, nullptr)); |
441 | 441 |
442 // Close the producer. | 442 // Close the producer. |
443 dp->ProducerClose(); | 443 dp->ProducerClose(); |
444 | 444 |
445 // It should be signaled. | 445 // It should be signaled. |
446 uint32_t context = 0; | 446 uint32_t context = 0; |
447 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); | 447 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); |
448 EXPECT_EQ(12u, context); | 448 EXPECT_EQ(12u, context); |
449 hss = HandleSignalsState(); | 449 hss = HandleSignalsState(); |
450 dp->ConsumerRemoveWaiter(&waiter, &hss); | 450 dp->ConsumerRemoveAwakable(&waiter, &hss); |
451 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 451 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
452 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 452 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
453 | 453 |
454 dp->ConsumerClose(); | 454 dp->ConsumerClose(); |
455 } | 455 } |
456 } | 456 } |
457 | 457 |
458 TEST(LocalDataPipeTest, BasicConsumerWaiting) { | 458 TEST(LocalDataPipeTest, BasicConsumerWaiting) { |
459 const MojoCreateDataPipeOptions options = { | 459 const MojoCreateDataPipeOptions options = { |
460 kSizeOfOptions, // |struct_size|. | 460 kSizeOfOptions, // |struct_size|. |
461 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 461 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
462 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 462 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
463 1000 * sizeof(int32_t) // |capacity_num_bytes|. | 463 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
464 }; | 464 }; |
465 MojoCreateDataPipeOptions validated_options = {0}; | 465 MojoCreateDataPipeOptions validated_options = {0}; |
466 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 466 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
467 MakeUserPointer(&options), &validated_options)); | 467 MakeUserPointer(&options), &validated_options)); |
468 | 468 |
469 { | 469 { |
470 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 470 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
471 Waiter waiter; | 471 Waiter waiter; |
472 uint32_t context = 0; | 472 uint32_t context = 0; |
473 HandleSignalsState hss; | 473 HandleSignalsState hss; |
474 | 474 |
475 // Never writable. | 475 // Never writable. |
476 waiter.Init(); | 476 waiter.Init(); |
477 hss = HandleSignalsState(); | 477 hss = HandleSignalsState(); |
478 EXPECT_EQ( | 478 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
479 MOJO_RESULT_FAILED_PRECONDITION, | 479 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, |
480 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, &hss)); | 480 &hss)); |
481 EXPECT_EQ(0u, hss.satisfied_signals); | 481 EXPECT_EQ(0u, hss.satisfied_signals); |
482 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 482 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
483 hss.satisfiable_signals); | 483 hss.satisfiable_signals); |
484 | 484 |
485 // Not yet readable. | 485 // Not yet readable. |
486 waiter.Init(); | 486 waiter.Init(); |
487 ASSERT_EQ(MOJO_RESULT_OK, | 487 ASSERT_EQ(MOJO_RESULT_OK, |
488 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, | 488 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, |
489 nullptr)); | 489 nullptr)); |
490 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 490 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
491 hss = HandleSignalsState(); | 491 hss = HandleSignalsState(); |
492 dp->ConsumerRemoveWaiter(&waiter, &hss); | 492 dp->ConsumerRemoveAwakable(&waiter, &hss); |
493 EXPECT_EQ(0u, hss.satisfied_signals); | 493 EXPECT_EQ(0u, hss.satisfied_signals); |
494 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 494 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
495 hss.satisfiable_signals); | 495 hss.satisfiable_signals); |
496 | 496 |
497 // Write two elements. | 497 // Write two elements. |
498 int32_t elements[2] = {123, 456}; | 498 int32_t elements[2] = {123, 456}; |
499 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); | 499 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
500 EXPECT_EQ(MOJO_RESULT_OK, | 500 EXPECT_EQ(MOJO_RESULT_OK, |
501 dp->ProducerWriteData(UserPointer<const void>(elements), | 501 dp->ProducerWriteData(UserPointer<const void>(elements), |
502 MakeUserPointer(&num_bytes), true)); | 502 MakeUserPointer(&num_bytes), true)); |
503 | 503 |
504 // Should already be readable. | 504 // Should already be readable. |
505 waiter.Init(); | 505 waiter.Init(); |
506 hss = HandleSignalsState(); | 506 hss = HandleSignalsState(); |
507 EXPECT_EQ( | 507 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
508 MOJO_RESULT_ALREADY_EXISTS, | 508 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 56, |
509 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 56, &hss)); | 509 &hss)); |
510 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 510 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
511 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 511 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
512 hss.satisfiable_signals); | 512 hss.satisfiable_signals); |
513 | 513 |
514 // Discard one element. | 514 // Discard one element. |
515 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 515 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
516 EXPECT_EQ(MOJO_RESULT_OK, | 516 EXPECT_EQ(MOJO_RESULT_OK, |
517 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); | 517 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); |
518 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 518 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
519 | 519 |
520 // Should still be readable. | 520 // Should still be readable. |
521 waiter.Init(); | 521 waiter.Init(); |
522 hss = HandleSignalsState(); | 522 hss = HandleSignalsState(); |
523 EXPECT_EQ( | 523 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
524 MOJO_RESULT_ALREADY_EXISTS, | 524 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 78, |
525 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 78, &hss)); | 525 &hss)); |
526 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 526 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
527 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 527 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
528 hss.satisfiable_signals); | 528 hss.satisfiable_signals); |
529 | 529 |
530 // Peek one element. | 530 // Peek one element. |
531 elements[0] = -1; | 531 elements[0] = -1; |
532 elements[1] = -1; | 532 elements[1] = -1; |
533 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 533 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
534 EXPECT_EQ(MOJO_RESULT_OK, | 534 EXPECT_EQ(MOJO_RESULT_OK, |
535 dp->ConsumerReadData(UserPointer<void>(elements), | 535 dp->ConsumerReadData(UserPointer<void>(elements), |
536 MakeUserPointer(&num_bytes), true, true)); | 536 MakeUserPointer(&num_bytes), true, true)); |
537 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 537 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
538 EXPECT_EQ(456, elements[0]); | 538 EXPECT_EQ(456, elements[0]); |
539 EXPECT_EQ(-1, elements[1]); | 539 EXPECT_EQ(-1, elements[1]); |
540 | 540 |
541 // Should still be readable. | 541 // Should still be readable. |
542 waiter.Init(); | 542 waiter.Init(); |
543 hss = HandleSignalsState(); | 543 hss = HandleSignalsState(); |
544 EXPECT_EQ( | 544 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
545 MOJO_RESULT_ALREADY_EXISTS, | 545 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 78, |
546 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 78, &hss)); | 546 &hss)); |
547 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 547 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
548 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 548 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
549 hss.satisfiable_signals); | 549 hss.satisfiable_signals); |
550 | 550 |
551 // Read one element. | 551 // Read one element. |
552 elements[0] = -1; | 552 elements[0] = -1; |
553 elements[1] = -1; | 553 elements[1] = -1; |
554 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 554 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
555 EXPECT_EQ(MOJO_RESULT_OK, | 555 EXPECT_EQ(MOJO_RESULT_OK, |
556 dp->ConsumerReadData(UserPointer<void>(elements), | 556 dp->ConsumerReadData(UserPointer<void>(elements), |
557 MakeUserPointer(&num_bytes), true, false)); | 557 MakeUserPointer(&num_bytes), true, false)); |
558 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 558 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
559 EXPECT_EQ(456, elements[0]); | 559 EXPECT_EQ(456, elements[0]); |
560 EXPECT_EQ(-1, elements[1]); | 560 EXPECT_EQ(-1, elements[1]); |
561 | 561 |
562 // Adding a waiter should now succeed. | 562 // Adding a waiter should now succeed. |
563 waiter.Init(); | 563 waiter.Init(); |
564 ASSERT_EQ(MOJO_RESULT_OK, | 564 ASSERT_EQ(MOJO_RESULT_OK, |
565 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 90, | 565 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 90, |
566 nullptr)); | 566 nullptr)); |
567 | 567 |
568 // Write one element. | 568 // Write one element. |
569 elements[0] = 789; | 569 elements[0] = 789; |
570 elements[1] = -1; | 570 elements[1] = -1; |
571 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 571 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
572 EXPECT_EQ(MOJO_RESULT_OK, | 572 EXPECT_EQ(MOJO_RESULT_OK, |
573 dp->ProducerWriteData(UserPointer<const void>(elements), | 573 dp->ProducerWriteData(UserPointer<const void>(elements), |
574 MakeUserPointer(&num_bytes), true)); | 574 MakeUserPointer(&num_bytes), true)); |
575 | 575 |
576 // Waiting should now succeed. | 576 // Waiting should now succeed. |
577 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); | 577 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); |
578 EXPECT_EQ(90u, context); | 578 EXPECT_EQ(90u, context); |
579 hss = HandleSignalsState(); | 579 hss = HandleSignalsState(); |
580 dp->ConsumerRemoveWaiter(&waiter, &hss); | 580 dp->ConsumerRemoveAwakable(&waiter, &hss); |
581 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 581 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
582 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 582 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
583 hss.satisfiable_signals); | 583 hss.satisfiable_signals); |
584 | 584 |
585 // Close the producer. | 585 // Close the producer. |
586 dp->ProducerClose(); | 586 dp->ProducerClose(); |
587 | 587 |
588 // Should still be readable. | 588 // Should still be readable. |
589 waiter.Init(); | 589 waiter.Init(); |
590 hss = HandleSignalsState(); | 590 hss = HandleSignalsState(); |
591 EXPECT_EQ( | 591 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
592 MOJO_RESULT_ALREADY_EXISTS, | 592 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, |
593 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss)); | 593 &hss)); |
594 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 594 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
595 hss.satisfied_signals); | 595 hss.satisfied_signals); |
596 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 596 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
597 hss.satisfiable_signals); | 597 hss.satisfiable_signals); |
598 | 598 |
599 // Read one element. | 599 // Read one element. |
600 elements[0] = -1; | 600 elements[0] = -1; |
601 elements[1] = -1; | 601 elements[1] = -1; |
602 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 602 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
603 EXPECT_EQ(MOJO_RESULT_OK, | 603 EXPECT_EQ(MOJO_RESULT_OK, |
604 dp->ConsumerReadData(UserPointer<void>(elements), | 604 dp->ConsumerReadData(UserPointer<void>(elements), |
605 MakeUserPointer(&num_bytes), true, false)); | 605 MakeUserPointer(&num_bytes), true, false)); |
606 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 606 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
607 EXPECT_EQ(789, elements[0]); | 607 EXPECT_EQ(789, elements[0]); |
608 EXPECT_EQ(-1, elements[1]); | 608 EXPECT_EQ(-1, elements[1]); |
609 | 609 |
610 // Should be never-readable. | 610 // Should be never-readable. |
611 waiter.Init(); | 611 waiter.Init(); |
612 hss = HandleSignalsState(); | 612 hss = HandleSignalsState(); |
613 EXPECT_EQ( | 613 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
614 MOJO_RESULT_FAILED_PRECONDITION, | 614 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, |
615 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, &hss)); | 615 &hss)); |
616 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 616 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
617 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 617 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
618 | 618 |
619 dp->ConsumerClose(); | 619 dp->ConsumerClose(); |
620 } | 620 } |
621 | 621 |
622 // Test with two-phase APIs and closing the producer with an active consumer | 622 // Test with two-phase APIs and closing the producer with an active consumer |
623 // waiter. | 623 // waiter. |
624 { | 624 { |
625 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 625 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
(...skipping 13 matching lines...) Expand all Loading... |
639 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0]))); | 639 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0]))); |
640 elements = static_cast<int32_t*>(buffer); | 640 elements = static_cast<int32_t*>(buffer); |
641 elements[0] = 123; | 641 elements[0] = 123; |
642 elements[1] = 456; | 642 elements[1] = 456; |
643 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(static_cast<uint32_t>( | 643 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(static_cast<uint32_t>( |
644 2u * sizeof(elements[0])))); | 644 2u * sizeof(elements[0])))); |
645 | 645 |
646 // Should already be readable. | 646 // Should already be readable. |
647 waiter.Init(); | 647 waiter.Init(); |
648 hss = HandleSignalsState(); | 648 hss = HandleSignalsState(); |
649 EXPECT_EQ( | 649 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
650 MOJO_RESULT_ALREADY_EXISTS, | 650 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, |
651 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss)); | 651 &hss)); |
652 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 652 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
653 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 653 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
654 hss.satisfiable_signals); | 654 hss.satisfiable_signals); |
655 | 655 |
656 // Read one element. | 656 // Read one element. |
657 // Request two in all-or-none mode, but only read one. | 657 // Request two in all-or-none mode, but only read one. |
658 const void* read_buffer = nullptr; | 658 const void* read_buffer = nullptr; |
659 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); | 659 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
660 EXPECT_EQ(MOJO_RESULT_OK, | 660 EXPECT_EQ(MOJO_RESULT_OK, |
661 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer), | 661 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer), |
662 MakeUserPointer(&num_bytes), true)); | 662 MakeUserPointer(&num_bytes), true)); |
663 EXPECT_TRUE(read_buffer); | 663 EXPECT_TRUE(read_buffer); |
664 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); | 664 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); |
665 const int32_t* read_elements = static_cast<const int32_t*>(read_buffer); | 665 const int32_t* read_elements = static_cast<const int32_t*>(read_buffer); |
666 EXPECT_EQ(123, read_elements[0]); | 666 EXPECT_EQ(123, read_elements[0]); |
667 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(static_cast<uint32_t>( | 667 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(static_cast<uint32_t>( |
668 1u * sizeof(elements[0])))); | 668 1u * sizeof(elements[0])))); |
669 | 669 |
670 // Should still be readable. | 670 // Should still be readable. |
671 waiter.Init(); | 671 waiter.Init(); |
672 hss = HandleSignalsState(); | 672 hss = HandleSignalsState(); |
673 EXPECT_EQ( | 673 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
674 MOJO_RESULT_ALREADY_EXISTS, | 674 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, |
675 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, &hss)); | 675 &hss)); |
676 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 676 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
677 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 677 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
678 hss.satisfiable_signals); | 678 hss.satisfiable_signals); |
679 | 679 |
680 // Read one element. | 680 // Read one element. |
681 // Request three, but not in all-or-none mode. | 681 // Request three, but not in all-or-none mode. |
682 read_buffer = nullptr; | 682 read_buffer = nullptr; |
683 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); | 683 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); |
684 EXPECT_EQ(MOJO_RESULT_OK, | 684 EXPECT_EQ(MOJO_RESULT_OK, |
685 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer), | 685 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer), |
686 MakeUserPointer(&num_bytes), false)); | 686 MakeUserPointer(&num_bytes), false)); |
687 EXPECT_TRUE(read_buffer); | 687 EXPECT_TRUE(read_buffer); |
688 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 688 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
689 read_elements = static_cast<const int32_t*>(read_buffer); | 689 read_elements = static_cast<const int32_t*>(read_buffer); |
690 EXPECT_EQ(456, read_elements[0]); | 690 EXPECT_EQ(456, read_elements[0]); |
691 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(static_cast<uint32_t>( | 691 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(static_cast<uint32_t>( |
692 1u * sizeof(elements[0])))); | 692 1u * sizeof(elements[0])))); |
693 | 693 |
694 // Adding a waiter should now succeed. | 694 // Adding a waiter should now succeed. |
695 waiter.Init(); | 695 waiter.Init(); |
696 ASSERT_EQ(MOJO_RESULT_OK, | 696 ASSERT_EQ(MOJO_RESULT_OK, |
697 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 56, | 697 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 56, |
698 nullptr)); | 698 nullptr)); |
699 | 699 |
700 // Close the producer. | 700 // Close the producer. |
701 dp->ProducerClose(); | 701 dp->ProducerClose(); |
702 | 702 |
703 // Should be never-readable. | 703 // Should be never-readable. |
704 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000, &context)); | 704 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000, &context)); |
705 EXPECT_EQ(56u, context); | 705 EXPECT_EQ(56u, context); |
706 hss = HandleSignalsState(); | 706 hss = HandleSignalsState(); |
707 dp->ConsumerRemoveWaiter(&waiter, &hss); | 707 dp->ConsumerRemoveAwakable(&waiter, &hss); |
708 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 708 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
709 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 709 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
710 | 710 |
711 dp->ConsumerClose(); | 711 dp->ConsumerClose(); |
712 } | 712 } |
713 } | 713 } |
714 | 714 |
715 // Tests that data pipes aren't writable/readable during two-phase writes/reads. | 715 // Tests that data pipes aren't writable/readable during two-phase writes/reads. |
716 TEST(LocalDataPipeTest, BasicTwoPhaseWaiting) { | 716 TEST(LocalDataPipeTest, BasicTwoPhaseWaiting) { |
717 const MojoCreateDataPipeOptions options = { | 717 const MojoCreateDataPipeOptions options = { |
718 kSizeOfOptions, // |struct_size|. | 718 kSizeOfOptions, // |struct_size|. |
719 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 719 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
720 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 720 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
721 1000 * sizeof(int32_t) // |capacity_num_bytes|. | 721 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
722 }; | 722 }; |
723 MojoCreateDataPipeOptions validated_options = {0}; | 723 MojoCreateDataPipeOptions validated_options = {0}; |
724 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 724 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
725 MakeUserPointer(&options), &validated_options)); | 725 MakeUserPointer(&options), &validated_options)); |
726 | 726 |
727 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 727 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
728 Waiter waiter; | 728 Waiter waiter; |
729 HandleSignalsState hss; | 729 HandleSignalsState hss; |
730 | 730 |
731 // It should be writable. | 731 // It should be writable. |
732 waiter.Init(); | 732 waiter.Init(); |
733 hss = HandleSignalsState(); | 733 hss = HandleSignalsState(); |
734 EXPECT_EQ( | 734 EXPECT_EQ( |
735 MOJO_RESULT_ALREADY_EXISTS, | 735 MOJO_RESULT_ALREADY_EXISTS, |
736 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); | 736 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); |
737 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 737 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
738 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 738 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
739 hss.satisfiable_signals); | 739 hss.satisfiable_signals); |
740 | 740 |
741 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); | 741 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); |
742 void* write_ptr = nullptr; | 742 void* write_ptr = nullptr; |
743 EXPECT_EQ(MOJO_RESULT_OK, | 743 EXPECT_EQ(MOJO_RESULT_OK, |
744 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 744 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
745 MakeUserPointer(&num_bytes), false)); | 745 MakeUserPointer(&num_bytes), false)); |
746 EXPECT_TRUE(write_ptr); | 746 EXPECT_TRUE(write_ptr); |
747 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); | 747 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); |
748 | 748 |
749 // At this point, it shouldn't be writable. | 749 // At this point, it shouldn't be writable. |
750 waiter.Init(); | 750 waiter.Init(); |
751 ASSERT_EQ( | 751 ASSERT_EQ(MOJO_RESULT_OK, |
752 MOJO_RESULT_OK, | 752 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 1, |
753 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 1, nullptr)); | 753 nullptr)); |
754 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 754 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
755 hss = HandleSignalsState(); | 755 hss = HandleSignalsState(); |
756 dp->ProducerRemoveWaiter(&waiter, &hss); | 756 dp->ProducerRemoveAwakable(&waiter, &hss); |
757 EXPECT_EQ(0u, hss.satisfied_signals); | 757 EXPECT_EQ(0u, hss.satisfied_signals); |
758 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 758 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
759 hss.satisfiable_signals); | 759 hss.satisfiable_signals); |
760 | 760 |
761 // It shouldn't be readable yet either. | 761 // It shouldn't be readable yet either. |
762 waiter.Init(); | 762 waiter.Init(); |
763 ASSERT_EQ( | 763 ASSERT_EQ(MOJO_RESULT_OK, |
764 MOJO_RESULT_OK, | 764 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 2, |
765 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 2, nullptr)); | 765 nullptr)); |
766 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 766 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
767 hss = HandleSignalsState(); | 767 hss = HandleSignalsState(); |
768 dp->ConsumerRemoveWaiter(&waiter, &hss); | 768 dp->ConsumerRemoveAwakable(&waiter, &hss); |
769 EXPECT_EQ(0u, hss.satisfied_signals); | 769 EXPECT_EQ(0u, hss.satisfied_signals); |
770 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 770 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
771 hss.satisfiable_signals); | 771 hss.satisfiable_signals); |
772 | 772 |
773 static_cast<int32_t*>(write_ptr)[0] = 123; | 773 static_cast<int32_t*>(write_ptr)[0] = 123; |
774 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData( | 774 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData( |
775 static_cast<uint32_t>(1u * sizeof(int32_t)))); | 775 static_cast<uint32_t>(1u * sizeof(int32_t)))); |
776 | 776 |
777 // It should be writable again. | 777 // It should be writable again. |
778 waiter.Init(); | 778 waiter.Init(); |
779 hss = HandleSignalsState(); | 779 hss = HandleSignalsState(); |
780 EXPECT_EQ( | 780 EXPECT_EQ( |
781 MOJO_RESULT_ALREADY_EXISTS, | 781 MOJO_RESULT_ALREADY_EXISTS, |
782 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 3, &hss)); | 782 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 3, &hss)); |
783 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 783 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
784 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 784 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
785 hss.satisfiable_signals); | 785 hss.satisfiable_signals); |
786 | 786 |
787 // And readable. | 787 // And readable. |
788 waiter.Init(); | 788 waiter.Init(); |
789 hss = HandleSignalsState(); | 789 hss = HandleSignalsState(); |
790 EXPECT_EQ( | 790 EXPECT_EQ( |
791 MOJO_RESULT_ALREADY_EXISTS, | 791 MOJO_RESULT_ALREADY_EXISTS, |
792 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 4, &hss)); | 792 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 4, &hss)); |
793 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 793 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
794 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 794 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
795 hss.satisfiable_signals); | 795 hss.satisfiable_signals); |
796 | 796 |
797 // Start another two-phase write and check that it's readable even in the | 797 // Start another two-phase write and check that it's readable even in the |
798 // middle of it. | 798 // middle of it. |
799 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); | 799 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); |
800 write_ptr = nullptr; | 800 write_ptr = nullptr; |
801 EXPECT_EQ(MOJO_RESULT_OK, | 801 EXPECT_EQ(MOJO_RESULT_OK, |
802 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 802 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
803 MakeUserPointer(&num_bytes), false)); | 803 MakeUserPointer(&num_bytes), false)); |
804 EXPECT_TRUE(write_ptr); | 804 EXPECT_TRUE(write_ptr); |
805 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); | 805 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); |
806 | 806 |
807 // It should be readable. | 807 // It should be readable. |
808 waiter.Init(); | 808 waiter.Init(); |
809 hss = HandleSignalsState(); | 809 hss = HandleSignalsState(); |
810 EXPECT_EQ( | 810 EXPECT_EQ( |
811 MOJO_RESULT_ALREADY_EXISTS, | 811 MOJO_RESULT_ALREADY_EXISTS, |
812 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5, &hss)); | 812 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5, &hss)); |
813 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 813 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
814 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 814 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
815 hss.satisfiable_signals); | 815 hss.satisfiable_signals); |
816 | 816 |
817 // End the two-phase write without writing anything. | 817 // End the two-phase write without writing anything. |
818 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); | 818 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); |
819 | 819 |
820 // Start a two-phase read. | 820 // Start a two-phase read. |
821 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); | 821 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); |
822 const void* read_ptr = nullptr; | 822 const void* read_ptr = nullptr; |
823 EXPECT_EQ(MOJO_RESULT_OK, | 823 EXPECT_EQ(MOJO_RESULT_OK, |
824 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 824 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
825 MakeUserPointer(&num_bytes), false)); | 825 MakeUserPointer(&num_bytes), false)); |
826 EXPECT_TRUE(read_ptr); | 826 EXPECT_TRUE(read_ptr); |
827 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(int32_t)), num_bytes); | 827 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(int32_t)), num_bytes); |
828 | 828 |
829 // At this point, it should still be writable. | 829 // At this point, it should still be writable. |
830 waiter.Init(); | 830 waiter.Init(); |
831 hss = HandleSignalsState(); | 831 hss = HandleSignalsState(); |
832 EXPECT_EQ( | 832 EXPECT_EQ( |
833 MOJO_RESULT_ALREADY_EXISTS, | 833 MOJO_RESULT_ALREADY_EXISTS, |
834 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6, &hss)); | 834 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6, &hss)); |
835 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 835 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
836 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 836 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
837 hss.satisfiable_signals); | 837 hss.satisfiable_signals); |
838 | 838 |
839 // But not readable. | 839 // But not readable. |
840 waiter.Init(); | 840 waiter.Init(); |
841 ASSERT_EQ( | 841 ASSERT_EQ(MOJO_RESULT_OK, |
842 MOJO_RESULT_OK, | 842 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 7, |
843 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 7, nullptr)); | 843 nullptr)); |
844 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 844 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
845 hss = HandleSignalsState(); | 845 hss = HandleSignalsState(); |
846 dp->ConsumerRemoveWaiter(&waiter, &hss); | 846 dp->ConsumerRemoveAwakable(&waiter, &hss); |
847 EXPECT_EQ(0u, hss.satisfied_signals); | 847 EXPECT_EQ(0u, hss.satisfied_signals); |
848 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 848 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
849 hss.satisfiable_signals); | 849 hss.satisfiable_signals); |
850 | 850 |
851 // End the two-phase read without reading anything. | 851 // End the two-phase read without reading anything. |
852 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u)); | 852 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u)); |
853 | 853 |
854 // It should be readable again. | 854 // It should be readable again. |
855 waiter.Init(); | 855 waiter.Init(); |
856 hss = HandleSignalsState(); | 856 hss = HandleSignalsState(); |
857 EXPECT_EQ( | 857 EXPECT_EQ( |
858 MOJO_RESULT_ALREADY_EXISTS, | 858 MOJO_RESULT_ALREADY_EXISTS, |
859 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 8, &hss)); | 859 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 8, &hss)); |
860 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 860 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
861 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 861 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
862 hss.satisfiable_signals); | 862 hss.satisfiable_signals); |
863 | 863 |
864 dp->ProducerClose(); | 864 dp->ProducerClose(); |
865 dp->ConsumerClose(); | 865 dp->ConsumerClose(); |
866 } | 866 } |
867 | 867 |
868 // Test that a "may discard" data pipe is writable even when it's full. | 868 // Test that a "may discard" data pipe is writable even when it's full. |
869 TEST(LocalDataPipeTest, BasicMayDiscardWaiting) { | 869 TEST(LocalDataPipeTest, BasicMayDiscardWaiting) { |
870 const MojoCreateDataPipeOptions options = { | 870 const MojoCreateDataPipeOptions options = { |
871 kSizeOfOptions, // |struct_size|. | 871 kSizeOfOptions, // |struct_size|. |
872 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. | 872 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
873 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 873 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
874 1 * sizeof(int32_t) // |capacity_num_bytes|. | 874 1 * sizeof(int32_t) // |capacity_num_bytes|. |
875 }; | 875 }; |
876 MojoCreateDataPipeOptions validated_options = {0}; | 876 MojoCreateDataPipeOptions validated_options = {0}; |
877 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 877 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
878 MakeUserPointer(&options), &validated_options)); | 878 MakeUserPointer(&options), &validated_options)); |
879 | 879 |
880 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 880 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
881 Waiter waiter; | 881 Waiter waiter; |
882 HandleSignalsState hss; | 882 HandleSignalsState hss; |
883 | 883 |
884 // Writable. | 884 // Writable. |
885 waiter.Init(); | 885 waiter.Init(); |
886 hss = HandleSignalsState(); | 886 hss = HandleSignalsState(); |
887 EXPECT_EQ( | 887 EXPECT_EQ( |
888 MOJO_RESULT_ALREADY_EXISTS, | 888 MOJO_RESULT_ALREADY_EXISTS, |
889 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); | 889 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); |
890 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 890 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
891 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 891 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
892 hss.satisfiable_signals); | 892 hss.satisfiable_signals); |
893 | 893 |
894 // Not readable. | 894 // Not readable. |
895 waiter.Init(); | 895 waiter.Init(); |
896 ASSERT_EQ( | 896 ASSERT_EQ(MOJO_RESULT_OK, |
897 MOJO_RESULT_OK, | 897 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 1, |
898 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 1, nullptr)); | 898 nullptr)); |
899 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 899 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
900 hss = HandleSignalsState(); | 900 hss = HandleSignalsState(); |
901 dp->ConsumerRemoveWaiter(&waiter, &hss); | 901 dp->ConsumerRemoveAwakable(&waiter, &hss); |
902 EXPECT_EQ(0u, hss.satisfied_signals); | 902 EXPECT_EQ(0u, hss.satisfied_signals); |
903 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 903 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
904 hss.satisfiable_signals); | 904 hss.satisfiable_signals); |
905 | 905 |
906 uint32_t num_bytes = static_cast<uint32_t>(sizeof(int32_t)); | 906 uint32_t num_bytes = static_cast<uint32_t>(sizeof(int32_t)); |
907 int32_t element = 123; | 907 int32_t element = 123; |
908 EXPECT_EQ(MOJO_RESULT_OK, | 908 EXPECT_EQ(MOJO_RESULT_OK, |
909 dp->ProducerWriteData(UserPointer<const void>(&element), | 909 dp->ProducerWriteData(UserPointer<const void>(&element), |
910 MakeUserPointer(&num_bytes), false)); | 910 MakeUserPointer(&num_bytes), false)); |
911 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); | 911 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); |
912 | 912 |
913 // Still writable (even though it's full). | 913 // Still writable (even though it's full). |
914 waiter.Init(); | 914 waiter.Init(); |
915 hss = HandleSignalsState(); | 915 hss = HandleSignalsState(); |
916 EXPECT_EQ( | 916 EXPECT_EQ( |
917 MOJO_RESULT_ALREADY_EXISTS, | 917 MOJO_RESULT_ALREADY_EXISTS, |
918 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 2, &hss)); | 918 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 2, &hss)); |
919 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 919 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
920 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 920 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
921 hss.satisfiable_signals); | 921 hss.satisfiable_signals); |
922 | 922 |
923 // Now readable. | 923 // Now readable. |
924 waiter.Init(); | 924 waiter.Init(); |
925 hss = HandleSignalsState(); | 925 hss = HandleSignalsState(); |
926 EXPECT_EQ( | 926 EXPECT_EQ( |
927 MOJO_RESULT_ALREADY_EXISTS, | 927 MOJO_RESULT_ALREADY_EXISTS, |
928 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 3, &hss)); | 928 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 3, &hss)); |
929 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 929 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
930 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 930 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
931 hss.satisfiable_signals); | 931 hss.satisfiable_signals); |
932 | 932 |
933 // Overwrite that element. | 933 // Overwrite that element. |
934 num_bytes = static_cast<uint32_t>(sizeof(int32_t)); | 934 num_bytes = static_cast<uint32_t>(sizeof(int32_t)); |
935 element = 456; | 935 element = 456; |
936 EXPECT_EQ(MOJO_RESULT_OK, | 936 EXPECT_EQ(MOJO_RESULT_OK, |
937 dp->ProducerWriteData(UserPointer<const void>(&element), | 937 dp->ProducerWriteData(UserPointer<const void>(&element), |
938 MakeUserPointer(&num_bytes), false)); | 938 MakeUserPointer(&num_bytes), false)); |
939 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); | 939 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); |
940 | 940 |
941 // Still writable. | 941 // Still writable. |
942 waiter.Init(); | 942 waiter.Init(); |
943 hss = HandleSignalsState(); | 943 hss = HandleSignalsState(); |
944 EXPECT_EQ( | 944 EXPECT_EQ( |
945 MOJO_RESULT_ALREADY_EXISTS, | 945 MOJO_RESULT_ALREADY_EXISTS, |
946 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 4, &hss)); | 946 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 4, &hss)); |
947 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 947 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
948 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 948 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
949 hss.satisfiable_signals); | 949 hss.satisfiable_signals); |
950 | 950 |
951 // And still readable. | 951 // And still readable. |
952 waiter.Init(); | 952 waiter.Init(); |
953 hss = HandleSignalsState(); | 953 hss = HandleSignalsState(); |
954 EXPECT_EQ( | 954 EXPECT_EQ( |
955 MOJO_RESULT_ALREADY_EXISTS, | 955 MOJO_RESULT_ALREADY_EXISTS, |
956 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5, &hss)); | 956 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5, &hss)); |
957 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 957 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
958 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 958 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
959 hss.satisfiable_signals); | 959 hss.satisfiable_signals); |
960 | 960 |
961 // Read that element. | 961 // Read that element. |
962 num_bytes = static_cast<uint32_t>(sizeof(int32_t)); | 962 num_bytes = static_cast<uint32_t>(sizeof(int32_t)); |
963 element = 0; | 963 element = 0; |
964 EXPECT_EQ(MOJO_RESULT_OK, | 964 EXPECT_EQ(MOJO_RESULT_OK, |
965 dp->ConsumerReadData(UserPointer<void>(&element), | 965 dp->ConsumerReadData(UserPointer<void>(&element), |
966 MakeUserPointer(&num_bytes), false, false)); | 966 MakeUserPointer(&num_bytes), false, false)); |
967 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); | 967 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); |
968 EXPECT_EQ(456, element); | 968 EXPECT_EQ(456, element); |
969 | 969 |
970 // Still writable. | 970 // Still writable. |
971 waiter.Init(); | 971 waiter.Init(); |
972 hss = HandleSignalsState(); | 972 hss = HandleSignalsState(); |
973 EXPECT_EQ( | 973 EXPECT_EQ( |
974 MOJO_RESULT_ALREADY_EXISTS, | 974 MOJO_RESULT_ALREADY_EXISTS, |
975 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6, &hss)); | 975 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6, &hss)); |
976 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 976 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
977 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 977 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
978 hss.satisfiable_signals); | 978 hss.satisfiable_signals); |
979 | 979 |
980 // No longer readable. | 980 // No longer readable. |
981 waiter.Init(); | 981 waiter.Init(); |
982 ASSERT_EQ( | 982 ASSERT_EQ(MOJO_RESULT_OK, |
983 MOJO_RESULT_OK, | 983 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 7, |
984 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 7, nullptr)); | 984 nullptr)); |
985 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 985 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
986 hss = HandleSignalsState(); | 986 hss = HandleSignalsState(); |
987 dp->ConsumerRemoveWaiter(&waiter, &hss); | 987 dp->ConsumerRemoveAwakable(&waiter, &hss); |
988 EXPECT_EQ(0u, hss.satisfied_signals); | 988 EXPECT_EQ(0u, hss.satisfied_signals); |
989 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 989 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
990 hss.satisfiable_signals); | 990 hss.satisfiable_signals); |
991 | 991 |
992 dp->ProducerClose(); | 992 dp->ProducerClose(); |
993 dp->ConsumerClose(); | 993 dp->ConsumerClose(); |
994 } | 994 } |
995 | 995 |
996 void Seq(int32_t start, size_t count, int32_t* out) { | 996 void Seq(int32_t start, size_t count, int32_t* out) { |
997 for (size_t i = 0; i < count; i++) | 997 for (size_t i = 0; i < count; i++) |
(...skipping 1064 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2062 // End reading. | 2062 // End reading. |
2063 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); | 2063 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); |
2064 | 2064 |
2065 dp->ProducerClose(); | 2065 dp->ProducerClose(); |
2066 dp->ConsumerClose(); | 2066 dp->ConsumerClose(); |
2067 } | 2067 } |
2068 | 2068 |
2069 } // namespace | 2069 } // namespace |
2070 } // namespace system | 2070 } // namespace system |
2071 } // namespace mojo | 2071 } // namespace mojo |
OLD | NEW |