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 "mojo/edk/embedder/embedder.h" | 16 #include "mojo/edk/embedder/embedder.h" |
16 #include "mojo/edk/embedder/platform_channel_pair.h" | 17 #include "mojo/edk/embedder/platform_channel_pair.h" |
17 #include "mojo/edk/system/test_utils.h" | 18 #include "mojo/edk/system/test_utils.h" |
18 #include "mojo/edk/system/waiter.h" | 19 #include "mojo/edk/system/waiter.h" |
19 #include "mojo/edk/test/mojo_test_base.h" | 20 #include "mojo/edk/test/mojo_test_base.h" |
20 #include "mojo/public/c/system/data_pipe.h" | 21 #include "mojo/public/c/system/data_pipe.h" |
21 #include "mojo/public/c/system/functions.h" | 22 #include "mojo/public/c/system/functions.h" |
22 #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" |
23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
24 | 26 |
25 namespace mojo { | 27 namespace mojo { |
26 namespace edk { | 28 namespace edk { |
27 namespace { | 29 namespace { |
28 | 30 |
29 const uint32_t kSizeOfOptions = | 31 const uint32_t kSizeOfOptions = |
30 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); | 32 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); |
31 | 33 |
32 // 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 1858 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1891 EXPECT_EQ(num_bytes, static_cast<uint32_t>(bytes.size())); | 1893 EXPECT_EQ(num_bytes, static_cast<uint32_t>(bytes.size())); |
1892 | 1894 |
1893 std::string message(bytes.data(), bytes.size()); | 1895 std::string message(bytes.data(), bytes.size()); |
1894 EXPECT_EQ(expected_message, message); | 1896 EXPECT_EQ(expected_message, message); |
1895 | 1897 |
1896 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(c)); | 1898 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(c)); |
1897 WriteMessage(child, "quit"); | 1899 WriteMessage(child, "quit"); |
1898 END_CHILD() | 1900 END_CHILD() |
1899 } | 1901 } |
1900 | 1902 |
| 1903 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(DataPipeStatusChangeInTransitClient, |
| 1904 DataPipeTest, parent) { |
| 1905 // This test verifies that peer closure is detectable through various |
| 1906 // mechanisms when it races with handle transfer. |
| 1907 |
| 1908 MojoHandle handles[6]; |
| 1909 EXPECT_EQ("o_O", ReadMessageWithHandles(parent, handles, 6)); |
| 1910 MojoHandle* producers = &handles[0]; |
| 1911 MojoHandle* consumers = &handles[3]; |
| 1912 |
| 1913 // Wait on producer 0 using MojoWait. |
| 1914 EXPECT_EQ(MOJO_RESULT_OK, |
| 1915 MojoWait(producers[0], MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 1916 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 1917 |
| 1918 // Wait on consumer 0 using MojoWait. |
| 1919 EXPECT_EQ(MOJO_RESULT_OK, |
| 1920 MojoWait(consumers[0], MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 1921 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 1922 |
| 1923 base::MessageLoop message_loop; |
| 1924 |
| 1925 // Wait on producer 1 and consumer 1 using Watchers. |
| 1926 { |
| 1927 base::RunLoop run_loop; |
| 1928 int count = 0; |
| 1929 auto callback = base::Bind( |
| 1930 [] (base::RunLoop* loop, int* count, MojoResult result) { |
| 1931 EXPECT_EQ(MOJO_RESULT_OK, result); |
| 1932 if (++*count == 2) |
| 1933 loop->Quit(); |
| 1934 }, |
| 1935 &run_loop, &count); |
| 1936 Watcher producer_watcher, consumer_watcher; |
| 1937 producer_watcher.Start( |
| 1938 Handle(producers[1]), MOJO_HANDLE_SIGNAL_PEER_CLOSED, callback); |
| 1939 consumer_watcher.Start( |
| 1940 Handle(consumers[1]), MOJO_HANDLE_SIGNAL_PEER_CLOSED, callback); |
| 1941 run_loop.Run(); |
| 1942 } |
| 1943 |
| 1944 // Wait on producer 2 by polling with MojoWriteData. |
| 1945 MojoResult result; |
| 1946 do { |
| 1947 uint32_t num_bytes = 0; |
| 1948 result = MojoWriteData( |
| 1949 producers[2], nullptr, &num_bytes, MOJO_WRITE_DATA_FLAG_NONE); |
| 1950 } while (result == MOJO_RESULT_OK); |
| 1951 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); |
| 1952 |
| 1953 // Wait on consumer 2 by polling with MojoReadData. |
| 1954 do { |
| 1955 char byte; |
| 1956 uint32_t num_bytes = 1; |
| 1957 result = MojoReadData( |
| 1958 consumers[2], &byte, &num_bytes, MOJO_READ_DATA_FLAG_NONE); |
| 1959 } while (result == MOJO_RESULT_SHOULD_WAIT); |
| 1960 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); |
| 1961 |
| 1962 for (size_t i = 0; i < 6; ++i) |
| 1963 CloseHandle(handles[i]); |
| 1964 } |
| 1965 |
| 1966 TEST_F(DataPipeTest, StatusChangeInTransit) { |
| 1967 MojoHandle producers[6]; |
| 1968 MojoHandle consumers[6]; |
| 1969 for (size_t i = 0; i < 6; ++i) |
| 1970 CreateDataPipe(&producers[i], &consumers[i], 1); |
| 1971 |
| 1972 RUN_CHILD_ON_PIPE(DataPipeStatusChangeInTransitClient, child) |
| 1973 MojoHandle handles[] = { producers[0], producers[1], producers[2], |
| 1974 consumers[3], consumers[4], consumers[5] }; |
| 1975 |
| 1976 // Send 3 producers and 3 consumers, and let their transfer race with their |
| 1977 // peers' closure. |
| 1978 WriteMessageWithHandles(child, "o_O", handles, 6); |
| 1979 |
| 1980 for (size_t i = 0; i < 3; ++i) |
| 1981 CloseHandle(consumers[i]); |
| 1982 for (size_t i = 3; i < 6; ++i) |
| 1983 CloseHandle(producers[i]); |
| 1984 END_CHILD() |
| 1985 } |
| 1986 |
1901 #endif // !defined(OS_IOS) | 1987 #endif // !defined(OS_IOS) |
1902 | 1988 |
1903 } // namespace | 1989 } // namespace |
1904 } // namespace edk | 1990 } // namespace edk |
1905 } // namespace mojo | 1991 } // namespace mojo |
OLD | NEW |