OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This file tests the C++ wrappers in mojo/public/cpp/system/data_pipe.h. | |
6 | |
7 #include "mojo/public/cpp/system/data_pipe.h" | |
8 | |
9 #include "gtest/gtest.h" | |
10 #include "mojo/public/cpp/system/handle.h" | |
11 #include "mojo/public/cpp/system/macros.h" | |
12 #include "mojo/public/cpp/system/wait.h" | |
13 | |
14 namespace mojo { | |
15 namespace { | |
16 | |
17 TEST(DataPipe, Basic) { | |
18 // Cursory compilation tests of |MakeScopedHandle()| with data pipe handles. | |
19 EXPECT_FALSE(MakeScopedHandle(DataPipeProducerHandle()).is_valid()); | |
20 EXPECT_FALSE(MakeScopedHandle(DataPipeConsumerHandle()).is_valid()); | |
21 | |
22 ScopedDataPipeProducerHandle ph; | |
23 ScopedDataPipeConsumerHandle ch; | |
24 | |
25 ASSERT_EQ(MOJO_RESULT_OK, CreateDataPipe(nullptr, &ph, &ch)); | |
26 ASSERT_TRUE(ph.get().is_valid()); | |
27 ASSERT_TRUE(ch.get().is_valid()); | |
28 | |
29 uint32_t read_threshold = 123u; | |
30 EXPECT_EQ(MOJO_RESULT_OK, | |
31 GetDataPipeConsumerOptions(ch.get(), &read_threshold)); | |
32 EXPECT_EQ(0u, read_threshold); | |
33 | |
34 EXPECT_EQ(MOJO_RESULT_OK, SetDataPipeConsumerOptions(ch.get(), 2u)); | |
35 | |
36 EXPECT_EQ(MOJO_RESULT_OK, | |
37 GetDataPipeConsumerOptions(ch.get(), &read_threshold)); | |
38 EXPECT_EQ(2u, read_threshold); | |
39 | |
40 // Write a byte. | |
41 static const char kA = 'A'; | |
42 uint32_t num_bytes = 1u; | |
43 EXPECT_EQ(MOJO_RESULT_OK, | |
44 WriteDataRaw(ph.get(), &kA, &num_bytes, MOJO_WRITE_DATA_FLAG_NONE)); | |
45 | |
46 // Waiting for "read threshold" should fail. (Wait a nonzero amount, in case | |
47 // there's some latency.) | |
48 MojoHandleSignalsState state; | |
49 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, | |
50 Wait(ch.get(), MOJO_HANDLE_SIGNAL_READ_THRESHOLD, 1000, &state)); | |
51 // ... but it should be readable. | |
52 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, state.satisfied_signals); | |
53 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED | | |
54 MOJO_HANDLE_SIGNAL_READ_THRESHOLD, | |
55 state.satisfiable_signals); | |
56 | |
57 // Do a two-phase write of another byte. | |
58 void* write_buffer = nullptr; | |
59 num_bytes = 0u; | |
60 ASSERT_EQ(MOJO_RESULT_OK, | |
61 BeginWriteDataRaw(ph.get(), &write_buffer, &num_bytes, | |
62 MOJO_WRITE_DATA_FLAG_NONE)); | |
63 ASSERT_TRUE(write_buffer); | |
64 ASSERT_GT(num_bytes, 0u); | |
65 static_cast<char*>(write_buffer)[0] = 'B'; | |
66 EXPECT_EQ(MOJO_RESULT_OK, EndWriteDataRaw(ph.get(), 1u)); | |
67 | |
68 // Now waiting for "read threshold" should succeed. (Wait a nonzero amount, in | |
69 // case there's some latency.) | |
70 state = MojoHandleSignalsState(); | |
71 EXPECT_EQ(MOJO_RESULT_OK, | |
72 Wait(ch.get(), MOJO_HANDLE_SIGNAL_READ_THRESHOLD, 1000, &state)); | |
73 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_READ_THRESHOLD, | |
74 state.satisfied_signals); | |
75 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED | | |
76 MOJO_HANDLE_SIGNAL_READ_THRESHOLD, | |
77 state.satisfiable_signals); | |
78 | |
79 // Read a byte. | |
80 char read_byte = 'x'; | |
81 num_bytes = 1u; | |
82 EXPECT_EQ(MOJO_RESULT_OK, ReadDataRaw(ch.get(), &read_byte, &num_bytes, | |
83 MOJO_READ_DATA_FLAG_NONE)); | |
84 EXPECT_EQ(1u, num_bytes); | |
85 EXPECT_EQ('A', read_byte); | |
86 | |
87 // Waiting for "read threshold" should now fail. | |
88 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, | |
89 Wait(ch.get(), MOJO_HANDLE_SIGNAL_READ_THRESHOLD, 0, nullptr)); | |
90 | |
91 // Reset the read threshold/options. | |
92 EXPECT_EQ(MOJO_RESULT_OK, SetDataPipeConsumerOptionsToDefault(ch.get())); | |
93 | |
94 // Waiting for "read threshold" should now succeed. | |
95 EXPECT_EQ(MOJO_RESULT_OK, | |
96 Wait(ch.get(), MOJO_HANDLE_SIGNAL_READ_THRESHOLD, 0, nullptr)); | |
97 | |
98 // Do a two-phase read. | |
99 const void* read_buffer = nullptr; | |
100 num_bytes = 0u; | |
101 ASSERT_EQ(MOJO_RESULT_OK, BeginReadDataRaw(ch.get(), &read_buffer, &num_bytes, | |
102 MOJO_READ_DATA_FLAG_NONE)); | |
103 ASSERT_TRUE(read_buffer); | |
104 ASSERT_EQ(1u, num_bytes); | |
105 EXPECT_EQ('B', static_cast<const char*>(read_buffer)[0]); | |
106 EXPECT_EQ(MOJO_RESULT_OK, EndReadDataRaw(ch.get(), 1u)); | |
107 | |
108 // Waiting for "read" should now fail (time out). | |
109 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, | |
110 Wait(ch.get(), MOJO_HANDLE_SIGNAL_READ_THRESHOLD, 1000, nullptr)); | |
111 | |
112 // Close the producer. | |
113 ph.reset(); | |
114 | |
115 // Waiting for "read" should now fail. | |
116 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | |
117 Wait(ch.get(), MOJO_HANDLE_SIGNAL_READ_THRESHOLD, 1000, nullptr)); | |
118 } | |
119 | |
120 } // namespace mojo | |
121 } // namespace | |
OLD | NEW |