OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "mojo/edk/embedder/embedder.h" | 16 #include "mojo/edk/embedder/embedder.h" |
17 #include "mojo/edk/embedder/platform_channel_pair.h" | 17 #include "mojo/edk/embedder/platform_channel_pair.h" |
18 #include "mojo/edk/system/test_utils.h" | 18 #include "mojo/edk/system/test_utils.h" |
19 #include "mojo/edk/system/waiter.h" | 19 #include "mojo/edk/system/waiter.h" |
20 #include "mojo/edk/test/mojo_test_base.h" | 20 #include "mojo/edk/test/mojo_test_base.h" |
21 #include "mojo/public/c/system/data_pipe.h" | 21 #include "mojo/public/c/system/data_pipe.h" |
22 #include "mojo/public/c/system/functions.h" | 22 #include "mojo/public/c/system/functions.h" |
23 #include "mojo/public/c/system/message_pipe.h" | 23 #include "mojo/public/c/system/message_pipe.h" |
24 #include "mojo/public/cpp/system/watcher.h" | 24 #include "mojo/public/cpp/system/simple_watcher.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 | 26 |
27 namespace mojo { | 27 namespace mojo { |
28 namespace edk { | 28 namespace edk { |
29 namespace { | 29 namespace { |
30 | 30 |
31 const uint32_t kSizeOfOptions = | 31 const uint32_t kSizeOfOptions = |
32 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); | 32 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); |
33 | 33 |
34 // In various places, we have to poll (since, e.g., we can't yet wait for a | 34 // In various places, we have to poll (since, e.g., we can't yet wait for a |
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 | 584 |
585 // Should be never-readable. | 585 // Should be never-readable. |
586 hss = MojoHandleSignalsState(); | 586 hss = MojoHandleSignalsState(); |
587 ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 587 ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
588 MojoWait(consumer_, MOJO_HANDLE_SIGNAL_READABLE, | 588 MojoWait(consumer_, MOJO_HANDLE_SIGNAL_READABLE, |
589 MOJO_DEADLINE_INDEFINITE, &hss)); | 589 MOJO_DEADLINE_INDEFINITE, &hss)); |
590 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 590 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
591 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 591 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
592 } | 592 } |
593 | 593 |
| 594 TEST_F(DataPipeTest, ConsumerMinimumAvailabilitySignaling) { |
| 595 const MojoCreateDataPipeOptions options = { |
| 596 kSizeOfOptions, // |struct_size|. |
| 597 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
| 598 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
| 599 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
| 600 }; |
| 601 EXPECT_EQ(MOJO_RESULT_OK, Create(&options)); |
| 602 |
| 603 int32_t elements[2] = {123, 456}; |
| 604 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
| 605 EXPECT_EQ(MOJO_RESULT_OK, WriteData(elements, &num_bytes, true)); |
| 606 |
| 607 // The consumer handle should appear to be readable. |
| 608 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(consumer_, MOJO_HANDLE_SIGNAL_READABLE, |
| 609 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 610 |
| 611 // Now try to read a minimum of 6 elements. |
| 612 int32_t read_elements[6]; |
| 613 uint32_t num_read_bytes = sizeof(read_elements); |
| 614 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
| 615 MojoReadData(consumer_, read_elements, &num_read_bytes, |
| 616 MOJO_READ_DATA_FLAG_ALL_OR_NONE | |
| 617 MOJO_READ_DATA_FLAG_CLEAR_SIGNAL)); |
| 618 |
| 619 // The consumer should no longer appear to be readable. |
| 620 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, |
| 621 MojoWait(consumer_, MOJO_HANDLE_SIGNAL_READABLE, 0, nullptr)); |
| 622 |
| 623 // Write two more elements. |
| 624 EXPECT_EQ(MOJO_RESULT_OK, WriteData(elements, &num_bytes, true)); |
| 625 |
| 626 // The consumer handle should once again appear to be readable. |
| 627 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(consumer_, MOJO_HANDLE_SIGNAL_READABLE, |
| 628 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 629 |
| 630 // Try again to read a minimum of 6 elements. |
| 631 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
| 632 MojoReadData(consumer_, read_elements, &num_read_bytes, |
| 633 MOJO_READ_DATA_FLAG_ALL_OR_NONE | |
| 634 MOJO_READ_DATA_FLAG_CLEAR_SIGNAL)); |
| 635 |
| 636 // Write two final elements. |
| 637 EXPECT_EQ(MOJO_RESULT_OK, WriteData(elements, &num_bytes, true)); |
| 638 |
| 639 // Read should succeed this time. |
| 640 EXPECT_EQ(MOJO_RESULT_OK, |
| 641 MojoReadData(consumer_, read_elements, &num_read_bytes, |
| 642 MOJO_READ_DATA_FLAG_ALL_OR_NONE | |
| 643 MOJO_READ_DATA_FLAG_CLEAR_SIGNAL)); |
| 644 |
| 645 // And once again the consumer is unreadable. |
| 646 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, |
| 647 MojoWait(consumer_, MOJO_HANDLE_SIGNAL_READABLE, 0, nullptr)); |
| 648 |
| 649 // Write two elements. |
| 650 EXPECT_EQ(MOJO_RESULT_OK, WriteData(elements, &num_bytes, true)); |
| 651 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(consumer_, MOJO_HANDLE_SIGNAL_READABLE, |
| 652 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 653 |
| 654 // Reading without ALL_OR_NONE now should still be able to succeed. i.e., the |
| 655 // previous usage should have no lasting effect beyond a successful read. |
| 656 EXPECT_EQ(MOJO_RESULT_OK, |
| 657 MojoReadData(consumer_, read_elements, &num_read_bytes, |
| 658 MOJO_READ_DATA_FLAG_NONE)); |
| 659 } |
| 660 |
| 661 TEST_F(DataPipeTest, ProducerMinimumCapacitySignaling) { |
| 662 const int32_t kTestElements[6] = {123, 456, 768, 101, 121, 314}; |
| 663 const uint32_t kTestPipeSize = sizeof(kTestElements); |
| 664 |
| 665 const MojoCreateDataPipeOptions options = { |
| 666 kSizeOfOptions, // |struct_size|. |
| 667 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
| 668 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
| 669 kTestPipeSize // |capacity_num_bytes|. |
| 670 }; |
| 671 ASSERT_EQ(MOJO_RESULT_OK, Create(&options)); |
| 672 |
| 673 // Start by filling the pipe. |
| 674 uint32_t num_bytes_written = kTestPipeSize; |
| 675 EXPECT_EQ(MOJO_RESULT_OK, WriteData(kTestElements, &num_bytes_written, true)); |
| 676 |
| 677 // The consumer handle should appear to be readable. |
| 678 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(consumer_, MOJO_HANDLE_SIGNAL_READABLE, |
| 679 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 680 |
| 681 // The producer handle should appear to be unwritable because the pipe is |
| 682 // filled to capacity. |
| 683 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, |
| 684 MojoWait(producer_, MOJO_HANDLE_SIGNAL_WRITABLE, 0, nullptr)); |
| 685 |
| 686 // Make some room. |
| 687 int32_t read_elements[2]; |
| 688 uint32_t num_bytes_read = sizeof(read_elements); |
| 689 EXPECT_EQ(MOJO_RESULT_OK, ReadData(read_elements, &num_bytes_read, true)); |
| 690 EXPECT_EQ(sizeof(read_elements), num_bytes_read); |
| 691 |
| 692 // The producer handle should appear to be writable now. |
| 693 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(producer_, MOJO_HANDLE_SIGNAL_WRITABLE, |
| 694 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 695 |
| 696 // Now attempt to write to the pipe, clearing signals on insufficient space. |
| 697 num_bytes_written = kTestPipeSize; |
| 698 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
| 699 MojoWriteData(producer_, kTestElements, &num_bytes_written, |
| 700 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE | |
| 701 MOJO_WRITE_DATA_FLAG_CLEAR_SIGNAL)); |
| 702 |
| 703 // The producer should no longer appear to be writable even though no new data |
| 704 // was written above. |
| 705 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, |
| 706 MojoWait(producer_, MOJO_HANDLE_SIGNAL_WRITABLE, 0, nullptr)); |
| 707 |
| 708 // Make more room. |
| 709 EXPECT_EQ(MOJO_RESULT_OK, ReadData(read_elements, &num_bytes_read, true)); |
| 710 EXPECT_EQ(sizeof(read_elements), num_bytes_read); |
| 711 |
| 712 // The producer handle should appear to be writable again... |
| 713 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(producer_, MOJO_HANDLE_SIGNAL_WRITABLE, |
| 714 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 715 |
| 716 // ..but there's still not enough capacity. |
| 717 num_bytes_written = kTestPipeSize; |
| 718 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
| 719 MojoWriteData(producer_, kTestElements, &num_bytes_written, |
| 720 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE | |
| 721 MOJO_WRITE_DATA_FLAG_CLEAR_SIGNAL)); |
| 722 |
| 723 // Once again not writable. |
| 724 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, |
| 725 MojoWait(producer_, MOJO_HANDLE_SIGNAL_WRITABLE, 0, nullptr)); |
| 726 |
| 727 // Finally make enough room for the write to succeed. |
| 728 EXPECT_EQ(MOJO_RESULT_OK, ReadData(read_elements, &num_bytes_read, true)); |
| 729 EXPECT_EQ(sizeof(read_elements), num_bytes_read); |
| 730 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(producer_, MOJO_HANDLE_SIGNAL_WRITABLE, |
| 731 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 732 |
| 733 // And now the write should succeed. |
| 734 num_bytes_written = kTestPipeSize; |
| 735 EXPECT_EQ(MOJO_RESULT_OK, |
| 736 MojoWriteData(producer_, kTestElements, &num_bytes_written, |
| 737 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE | |
| 738 MOJO_WRITE_DATA_FLAG_CLEAR_SIGNAL)); |
| 739 |
| 740 // Read two elements. |
| 741 num_bytes_read = sizeof(read_elements); |
| 742 EXPECT_EQ(MOJO_RESULT_OK, ReadData(read_elements, &num_bytes_read, true)); |
| 743 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(producer_, MOJO_HANDLE_SIGNAL_WRITABLE, |
| 744 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 745 |
| 746 // Writing without ALL_OR_NONE now should still be able to succeed, i.e. the |
| 747 // previous usage should have no lasting effect beyond a successful write. |
| 748 num_bytes_written = kTestPipeSize; |
| 749 EXPECT_EQ(MOJO_RESULT_OK, |
| 750 MojoWriteData(producer_, kTestElements, &num_bytes_written, |
| 751 MOJO_READ_DATA_FLAG_NONE)); |
| 752 } |
| 753 |
594 // Test with two-phase APIs and also closing the producer with an active | 754 // Test with two-phase APIs and also closing the producer with an active |
595 // consumer waiter. | 755 // consumer waiter. |
596 TEST_F(DataPipeTest, ConsumerWaitingTwoPhase) { | 756 TEST_F(DataPipeTest, ConsumerWaitingTwoPhase) { |
597 const MojoCreateDataPipeOptions options = { | 757 const MojoCreateDataPipeOptions options = { |
598 kSizeOfOptions, // |struct_size|. | 758 kSizeOfOptions, // |struct_size|. |
599 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 759 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
600 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 760 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
601 1000 * sizeof(int32_t) // |capacity_num_bytes|. | 761 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
602 }; | 762 }; |
603 ASSERT_EQ(MOJO_RESULT_OK, Create(&options)); | 763 ASSERT_EQ(MOJO_RESULT_OK, Create(&options)); |
604 MojoHandleSignalsState hss; | 764 MojoHandleSignalsState hss; |
605 | 765 |
606 // Write two elements. | 766 // Write two elements. |
607 int32_t* elements = nullptr; | 767 int32_t* elements = nullptr; |
608 void* buffer = nullptr; | 768 void* buffer = nullptr; |
609 // Request room for three (but we'll only write two). | 769 // Request room for three (but we'll only write two). |
610 uint32_t num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); | 770 uint32_t num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); |
611 ASSERT_EQ(MOJO_RESULT_OK, BeginWriteData(&buffer, &num_bytes, true)); | 771 ASSERT_EQ(MOJO_RESULT_OK, BeginWriteData(&buffer, &num_bytes, false)); |
612 EXPECT_TRUE(buffer); | 772 EXPECT_TRUE(buffer); |
613 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0]))); | 773 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0]))); |
614 elements = static_cast<int32_t*>(buffer); | 774 elements = static_cast<int32_t*>(buffer); |
615 elements[0] = 123; | 775 elements[0] = 123; |
616 elements[1] = 456; | 776 elements[1] = 456; |
617 ASSERT_EQ(MOJO_RESULT_OK, EndWriteData(2u * sizeof(elements[0]))); | 777 ASSERT_EQ(MOJO_RESULT_OK, EndWriteData(2u * sizeof(elements[0]))); |
618 | 778 |
619 // Wait for readability. | 779 // Wait for readability. |
620 hss = MojoHandleSignalsState(); | 780 hss = MojoHandleSignalsState(); |
621 ASSERT_EQ(MOJO_RESULT_OK, | 781 ASSERT_EQ(MOJO_RESULT_OK, |
(...skipping 1292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1914 MojoWait(producers[0], MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 2074 MojoWait(producers[0], MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
1915 MOJO_DEADLINE_INDEFINITE, nullptr)); | 2075 MOJO_DEADLINE_INDEFINITE, nullptr)); |
1916 | 2076 |
1917 // Wait on consumer 0 using MojoWait. | 2077 // Wait on consumer 0 using MojoWait. |
1918 EXPECT_EQ(MOJO_RESULT_OK, | 2078 EXPECT_EQ(MOJO_RESULT_OK, |
1919 MojoWait(consumers[0], MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 2079 MojoWait(consumers[0], MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
1920 MOJO_DEADLINE_INDEFINITE, nullptr)); | 2080 MOJO_DEADLINE_INDEFINITE, nullptr)); |
1921 | 2081 |
1922 base::MessageLoop message_loop; | 2082 base::MessageLoop message_loop; |
1923 | 2083 |
1924 // Wait on producer 1 and consumer 1 using Watchers. | 2084 // Wait on producer 1 and consumer 1 using SimpleWatchers. |
1925 { | 2085 { |
1926 base::RunLoop run_loop; | 2086 base::RunLoop run_loop; |
1927 int count = 0; | 2087 int count = 0; |
1928 auto callback = base::Bind( | 2088 auto callback = base::Bind( |
1929 [] (base::RunLoop* loop, int* count, MojoResult result) { | 2089 [] (base::RunLoop* loop, int* count, MojoResult result) { |
1930 EXPECT_EQ(MOJO_RESULT_OK, result); | 2090 EXPECT_EQ(MOJO_RESULT_OK, result); |
1931 if (++*count == 2) | 2091 if (++*count == 2) |
1932 loop->Quit(); | 2092 loop->Quit(); |
1933 }, | 2093 }, |
1934 &run_loop, &count); | 2094 &run_loop, &count); |
1935 Watcher producer_watcher(FROM_HERE), consumer_watcher(FROM_HERE); | 2095 SimpleWatcher producer_watcher(FROM_HERE, |
1936 producer_watcher.Start( | 2096 SimpleWatcher::ArmingPolicy::AUTOMATIC); |
1937 Handle(producers[1]), MOJO_HANDLE_SIGNAL_PEER_CLOSED, callback); | 2097 SimpleWatcher consumer_watcher(FROM_HERE, |
1938 consumer_watcher.Start( | 2098 SimpleWatcher::ArmingPolicy::AUTOMATIC); |
1939 Handle(consumers[1]), MOJO_HANDLE_SIGNAL_PEER_CLOSED, callback); | 2099 producer_watcher.Watch(Handle(producers[1]), MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 2100 callback); |
| 2101 consumer_watcher.Watch(Handle(consumers[1]), MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 2102 callback); |
1940 run_loop.Run(); | 2103 run_loop.Run(); |
| 2104 EXPECT_EQ(2, count); |
1941 } | 2105 } |
1942 | 2106 |
1943 // Wait on producer 2 by polling with MojoWriteData. | 2107 // Wait on producer 2 by polling with MojoWriteData. |
1944 MojoResult result; | 2108 MojoResult result; |
1945 do { | 2109 do { |
1946 uint32_t num_bytes = 0; | 2110 uint32_t num_bytes = 0; |
1947 result = MojoWriteData( | 2111 result = MojoWriteData( |
1948 producers[2], nullptr, &num_bytes, MOJO_WRITE_DATA_FLAG_NONE); | 2112 producers[2], nullptr, &num_bytes, MOJO_WRITE_DATA_FLAG_NONE); |
1949 } while (result == MOJO_RESULT_OK); | 2113 } while (result == MOJO_RESULT_OK); |
1950 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); | 2114 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); |
(...skipping 30 matching lines...) Expand all Loading... |
1981 for (size_t i = 3; i < 6; ++i) | 2145 for (size_t i = 3; i < 6; ++i) |
1982 CloseHandle(producers[i]); | 2146 CloseHandle(producers[i]); |
1983 END_CHILD() | 2147 END_CHILD() |
1984 } | 2148 } |
1985 | 2149 |
1986 #endif // !defined(OS_IOS) | 2150 #endif // !defined(OS_IOS) |
1987 | 2151 |
1988 } // namespace | 2152 } // namespace |
1989 } // namespace edk | 2153 } // namespace edk |
1990 } // namespace mojo | 2154 } // namespace mojo |
OLD | NEW |