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/system/local_data_pipe.h" | 5 #include "mojo/system/local_data_pipe.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 dp->ProducerClose(); | 469 dp->ProducerClose(); |
470 | 470 |
471 // Should be never-readable. | 471 // Should be never-readable. |
472 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000)); | 472 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000)); |
473 dp->ConsumerRemoveWaiter(&waiter); | 473 dp->ConsumerRemoveWaiter(&waiter); |
474 | 474 |
475 dp->ConsumerClose(); | 475 dp->ConsumerClose(); |
476 } | 476 } |
477 } | 477 } |
478 | 478 |
479 // TODO(vtl): More: discard (with/without "all or none"). More "all or none" | 479 void Seq(int32_t start, size_t count, int32_t* out) { |
480 // tests. | 480 for (size_t i = 0; i < count; i++) |
| 481 out[i] = start + static_cast<int32_t>(i); |
| 482 } |
| 483 |
| 484 TEST(LocalDataPipeTest, MayDiscard) { |
| 485 const MojoCreateDataPipeOptions options = { |
| 486 kSizeOfOptions, // |struct_size|. |
| 487 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
| 488 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
| 489 10 * sizeof(int32_t) // |capacity_num_bytes|. |
| 490 }; |
| 491 MojoCreateDataPipeOptions validated_options = { 0 }; |
| 492 EXPECT_EQ(MOJO_RESULT_OK, |
| 493 DataPipe::ValidateOptions(&options, &validated_options)); |
| 494 |
| 495 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
| 496 |
| 497 int32_t buffer[100] = { 0 }; |
| 498 uint32_t num_bytes = 0; |
| 499 |
| 500 num_bytes = 20u * sizeof(int32_t); |
| 501 Seq(0, arraysize(buffer), buffer); |
| 502 // Try writing more than capacity. (This test relies on the implementation |
| 503 // enforcing the capacity strictly.) |
| 504 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, false)); |
| 505 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); |
| 506 |
| 507 // Read half of what we wrote. |
| 508 num_bytes = 5u * sizeof(int32_t); |
| 509 memset(buffer, 0xab, sizeof(buffer)); |
| 510 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, false)); |
| 511 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
| 512 int32_t expected_buffer[100]; |
| 513 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
| 514 Seq(0, 5u, expected_buffer); |
| 515 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
| 516 // Internally, a circular buffer would now look like: |
| 517 // -, -, -, -, -, 5, 6, 7, 8, 9 |
| 518 |
| 519 // Write a bit more than the space that's available. |
| 520 num_bytes = 8u * sizeof(int32_t); |
| 521 Seq(100, arraysize(buffer), buffer); |
| 522 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, false)); |
| 523 EXPECT_EQ(8u * sizeof(int32_t), num_bytes); |
| 524 // Internally, a circular buffer would now look like: |
| 525 // 100, 101, 102, 103, 104, 105, 106, 107, 8, 9 |
| 526 |
| 527 // Read half of what's available. |
| 528 num_bytes = 5u * sizeof(int32_t); |
| 529 memset(buffer, 0xab, sizeof(buffer)); |
| 530 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, false)); |
| 531 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
| 532 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
| 533 expected_buffer[0] = 8; |
| 534 expected_buffer[1] = 9; |
| 535 expected_buffer[2] = 100; |
| 536 expected_buffer[3] = 101; |
| 537 expected_buffer[4] = 102; |
| 538 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
| 539 // Internally, a circular buffer would now look like: |
| 540 // -, -, -, 103, 104, 105, 106, 107, -, - |
| 541 |
| 542 // Write one integer. |
| 543 num_bytes = 1u * sizeof(int32_t); |
| 544 Seq(200, arraysize(buffer), buffer); |
| 545 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, false)); |
| 546 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
| 547 // Internally, a circular buffer would now look like: |
| 548 // -, -, -, 103, 104, 105, 106, 107, 200, - |
| 549 |
| 550 // Write five more. |
| 551 num_bytes = 5u * sizeof(int32_t); |
| 552 Seq(300, arraysize(buffer), buffer); |
| 553 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, false)); |
| 554 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
| 555 // Internally, a circular buffer would now look like: |
| 556 // 301, 302, 303, 304, 104, 105, 106, 107, 200, 300 |
| 557 |
| 558 // Read it all. |
| 559 num_bytes = sizeof(buffer); |
| 560 memset(buffer, 0xab, sizeof(buffer)); |
| 561 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, false)); |
| 562 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); |
| 563 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
| 564 expected_buffer[0] = 104; |
| 565 expected_buffer[1] = 105; |
| 566 expected_buffer[2] = 106; |
| 567 expected_buffer[3] = 107; |
| 568 expected_buffer[4] = 200; |
| 569 expected_buffer[5] = 300; |
| 570 expected_buffer[6] = 301; |
| 571 expected_buffer[7] = 302; |
| 572 expected_buffer[8] = 303; |
| 573 expected_buffer[9] = 304; |
| 574 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
| 575 |
| 576 // TODO(vtl): Test two-phase write when it supports "may discard". |
| 577 |
| 578 dp->ProducerClose(); |
| 579 dp->ConsumerClose(); |
| 580 } |
| 581 |
| 582 // TODO(vtl): More "all or none" tests (without and with "may discard"). |
481 | 583 |
482 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads, | 584 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads, |
483 // respectively, as much as possible, even if it has to "wrap around" the | 585 // respectively, as much as possible, even if it has to "wrap around" the |
484 // internal circular buffer. (Note that the two-phase write and read do not do | 586 // internal circular buffer. (Note that the two-phase write and read do not do |
485 // this.) | 587 // this.) |
486 TEST(LocalDataPipeTest, WrapAround) { | 588 TEST(LocalDataPipeTest, WrapAround) { |
487 unsigned char test_data[1000]; | 589 unsigned char test_data[1000]; |
488 for (size_t i = 0; i < arraysize(test_data); i++) | 590 for (size_t i = 0; i < arraysize(test_data); i++) |
489 test_data[i] = static_cast<unsigned char>(i); | 591 test_data[i] = static_cast<unsigned char>(i); |
490 | 592 |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
733 | 835 |
734 dp->ConsumerClose(); | 836 dp->ConsumerClose(); |
735 } | 837 } |
736 } | 838 } |
737 | 839 |
738 // TODO(vtl): More. | 840 // TODO(vtl): More. |
739 | 841 |
740 } // namespace | 842 } // namespace |
741 } // namespace system | 843 } // namespace system |
742 } // namespace mojo | 844 } // namespace mojo |
OLD | NEW |