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 "mojo/public/cpp/bindings/lib/multiplex_router.h" | 5 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 65 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
66 base::MakeUnique<PassThroughFilter>(), false, | 66 base::MakeUnique<PassThroughFilter>(), false, |
67 base::ThreadTaskRunnerHandle::Get(), 0u); | 67 base::ThreadTaskRunnerHandle::Get(), 0u); |
68 | 68 |
69 Message request; | 69 Message request; |
70 AllocRequestMessage(1, "hello", &request); | 70 AllocRequestMessage(1, "hello", &request); |
71 | 71 |
72 MessageQueue message_queue; | 72 MessageQueue message_queue; |
73 base::RunLoop run_loop; | 73 base::RunLoop run_loop; |
74 client0.AcceptWithResponder( | 74 client0.AcceptWithResponder( |
75 &request, | 75 &request, base::MakeUnique<MessageAccumulator>(&message_queue, |
76 new MessageAccumulator(&message_queue, run_loop.QuitClosure())); | 76 run_loop.QuitClosure())); |
77 | 77 |
78 run_loop.Run(); | 78 run_loop.Run(); |
79 | 79 |
80 EXPECT_FALSE(message_queue.IsEmpty()); | 80 EXPECT_FALSE(message_queue.IsEmpty()); |
81 | 81 |
82 Message response; | 82 Message response; |
83 message_queue.Pop(&response); | 83 message_queue.Pop(&response); |
84 | 84 |
85 EXPECT_EQ(std::string("hello world!"), | 85 EXPECT_EQ(std::string("hello world!"), |
86 std::string(reinterpret_cast<const char*>(response.payload()))); | 86 std::string(reinterpret_cast<const char*>(response.payload()))); |
87 | 87 |
88 // Send a second message on the pipe. | 88 // Send a second message on the pipe. |
89 Message request2; | 89 Message request2; |
90 AllocRequestMessage(1, "hello again", &request2); | 90 AllocRequestMessage(1, "hello again", &request2); |
91 | 91 |
92 base::RunLoop run_loop2; | 92 base::RunLoop run_loop2; |
93 client0.AcceptWithResponder( | 93 client0.AcceptWithResponder( |
94 &request2, | 94 &request2, base::MakeUnique<MessageAccumulator>(&message_queue, |
95 new MessageAccumulator(&message_queue, run_loop2.QuitClosure())); | 95 run_loop2.QuitClosure())); |
96 | 96 |
97 run_loop2.Run(); | 97 run_loop2.Run(); |
98 | 98 |
99 EXPECT_FALSE(message_queue.IsEmpty()); | 99 EXPECT_FALSE(message_queue.IsEmpty()); |
100 | 100 |
101 message_queue.Pop(&response); | 101 message_queue.Pop(&response); |
102 | 102 |
103 EXPECT_EQ(std::string("hello again world!"), | 103 EXPECT_EQ(std::string("hello again world!"), |
104 std::string(reinterpret_cast<const char*>(response.payload()))); | 104 std::string(reinterpret_cast<const char*>(response.payload()))); |
105 } | 105 } |
106 | 106 |
107 TEST_F(MultiplexRouterTest, BasicRequestResponse_Synchronous) { | 107 TEST_F(MultiplexRouterTest, BasicRequestResponse_Synchronous) { |
108 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 108 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
109 base::MakeUnique<PassThroughFilter>(), false, | 109 base::MakeUnique<PassThroughFilter>(), false, |
110 base::ThreadTaskRunnerHandle::Get(), 0u); | 110 base::ThreadTaskRunnerHandle::Get(), 0u); |
111 ResponseGenerator generator; | 111 ResponseGenerator generator; |
112 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 112 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
113 base::MakeUnique<PassThroughFilter>(), false, | 113 base::MakeUnique<PassThroughFilter>(), false, |
114 base::ThreadTaskRunnerHandle::Get(), 0u); | 114 base::ThreadTaskRunnerHandle::Get(), 0u); |
115 | 115 |
116 Message request; | 116 Message request; |
117 AllocRequestMessage(1, "hello", &request); | 117 AllocRequestMessage(1, "hello", &request); |
118 | 118 |
119 MessageQueue message_queue; | 119 MessageQueue message_queue; |
120 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 120 client0.AcceptWithResponder( |
| 121 &request, base::MakeUnique<MessageAccumulator>(&message_queue)); |
121 | 122 |
122 router1_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 123 router1_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
123 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 124 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
124 | 125 |
125 EXPECT_FALSE(message_queue.IsEmpty()); | 126 EXPECT_FALSE(message_queue.IsEmpty()); |
126 | 127 |
127 Message response; | 128 Message response; |
128 message_queue.Pop(&response); | 129 message_queue.Pop(&response); |
129 | 130 |
130 EXPECT_EQ(std::string("hello world!"), | 131 EXPECT_EQ(std::string("hello world!"), |
131 std::string(reinterpret_cast<const char*>(response.payload()))); | 132 std::string(reinterpret_cast<const char*>(response.payload()))); |
132 | 133 |
133 // Send a second message on the pipe. | 134 // Send a second message on the pipe. |
134 Message request2; | 135 Message request2; |
135 AllocRequestMessage(1, "hello again", &request2); | 136 AllocRequestMessage(1, "hello again", &request2); |
136 | 137 |
137 client0.AcceptWithResponder(&request2, | 138 client0.AcceptWithResponder( |
138 new MessageAccumulator(&message_queue)); | 139 &request2, base::MakeUnique<MessageAccumulator>(&message_queue)); |
139 | 140 |
140 router1_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 141 router1_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
141 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 142 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
142 | 143 |
143 EXPECT_FALSE(message_queue.IsEmpty()); | 144 EXPECT_FALSE(message_queue.IsEmpty()); |
144 | 145 |
145 message_queue.Pop(&response); | 146 message_queue.Pop(&response); |
146 | 147 |
147 EXPECT_EQ(std::string("hello again world!"), | 148 EXPECT_EQ(std::string("hello again world!"), |
148 std::string(reinterpret_cast<const char*>(response.payload()))); | 149 std::string(reinterpret_cast<const char*>(response.payload()))); |
(...skipping 11 matching lines...) Expand all Loading... |
160 base::WrapUnique(new PassThroughFilter()), | 161 base::WrapUnique(new PassThroughFilter()), |
161 false, base::ThreadTaskRunnerHandle::Get(), | 162 false, base::ThreadTaskRunnerHandle::Get(), |
162 0u); | 163 0u); |
163 | 164 |
164 Message request; | 165 Message request; |
165 AllocRequestMessage(1, "hello", &request); | 166 AllocRequestMessage(1, "hello", &request); |
166 | 167 |
167 MessageQueue message_queue; | 168 MessageQueue message_queue; |
168 base::RunLoop run_loop2; | 169 base::RunLoop run_loop2; |
169 client0.AcceptWithResponder( | 170 client0.AcceptWithResponder( |
170 &request, | 171 &request, base::MakeUnique<MessageAccumulator>(&message_queue, |
171 new MessageAccumulator(&message_queue, run_loop2.QuitClosure())); | 172 run_loop2.QuitClosure())); |
172 run_loop.Run(); | 173 run_loop.Run(); |
173 | 174 |
174 // The request has been received but the response has not been sent yet. | 175 // The request has been received but the response has not been sent yet. |
175 EXPECT_TRUE(message_queue.IsEmpty()); | 176 EXPECT_TRUE(message_queue.IsEmpty()); |
176 | 177 |
177 // Send the response. | 178 // Send the response. |
178 EXPECT_TRUE(generator.responder_is_valid()); | 179 EXPECT_TRUE(generator.responder_is_valid()); |
179 generator.CompleteWithResponse(); | 180 generator.CompleteWithResponse(); |
180 run_loop2.Run(); | 181 run_loop2.Run(); |
181 | 182 |
182 // Check the response. | 183 // Check the response. |
183 EXPECT_FALSE(message_queue.IsEmpty()); | 184 EXPECT_FALSE(message_queue.IsEmpty()); |
184 Message response; | 185 Message response; |
185 message_queue.Pop(&response); | 186 message_queue.Pop(&response); |
186 EXPECT_EQ(std::string("hello world!"), | 187 EXPECT_EQ(std::string("hello world!"), |
187 std::string(reinterpret_cast<const char*>(response.payload()))); | 188 std::string(reinterpret_cast<const char*>(response.payload()))); |
188 | 189 |
189 // Send a second message on the pipe. | 190 // Send a second message on the pipe. |
190 base::RunLoop run_loop3; | 191 base::RunLoop run_loop3; |
191 generator.set_closure(run_loop3.QuitClosure()); | 192 generator.set_closure(run_loop3.QuitClosure()); |
192 Message request2; | 193 Message request2; |
193 AllocRequestMessage(1, "hello again", &request2); | 194 AllocRequestMessage(1, "hello again", &request2); |
194 | 195 |
195 base::RunLoop run_loop4; | 196 base::RunLoop run_loop4; |
196 client0.AcceptWithResponder( | 197 client0.AcceptWithResponder( |
197 &request2, | 198 &request2, base::MakeUnique<MessageAccumulator>(&message_queue, |
198 new MessageAccumulator(&message_queue, run_loop4.QuitClosure())); | 199 run_loop4.QuitClosure())); |
199 run_loop3.Run(); | 200 run_loop3.Run(); |
200 | 201 |
201 // The request has been received but the response has not been sent yet. | 202 // The request has been received but the response has not been sent yet. |
202 EXPECT_TRUE(message_queue.IsEmpty()); | 203 EXPECT_TRUE(message_queue.IsEmpty()); |
203 | 204 |
204 // Send the second response. | 205 // Send the second response. |
205 EXPECT_TRUE(generator.responder_is_valid()); | 206 EXPECT_TRUE(generator.responder_is_valid()); |
206 generator.CompleteWithResponse(); | 207 generator.CompleteWithResponse(); |
207 run_loop4.Run(); | 208 run_loop4.Run(); |
208 | 209 |
(...skipping 30 matching lines...) Expand all Loading... |
239 0u); | 240 0u); |
240 bool error_handler_called1 = false; | 241 bool error_handler_called1 = false; |
241 client1.set_connection_error_handler( | 242 client1.set_connection_error_handler( |
242 base::Bind(&ForwardErrorHandler, &error_handler_called1, | 243 base::Bind(&ForwardErrorHandler, &error_handler_called1, |
243 run_loop1.QuitClosure())); | 244 run_loop1.QuitClosure())); |
244 | 245 |
245 Message request; | 246 Message request; |
246 AllocRequestMessage(1, "hello", &request); | 247 AllocRequestMessage(1, "hello", &request); |
247 | 248 |
248 MessageQueue message_queue; | 249 MessageQueue message_queue; |
249 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 250 client0.AcceptWithResponder( |
| 251 &request, base::MakeUnique<MessageAccumulator>(&message_queue)); |
250 run_loop3.Run(); | 252 run_loop3.Run(); |
251 | 253 |
252 // The request has been received but no response has been sent. | 254 // The request has been received but no response has been sent. |
253 EXPECT_TRUE(message_queue.IsEmpty()); | 255 EXPECT_TRUE(message_queue.IsEmpty()); |
254 | 256 |
255 // Destroy the responder MessagerReceiver but don't send any response. | 257 // Destroy the responder MessagerReceiver but don't send any response. |
256 generator.CompleteWithoutResponse(); | 258 generator.CompleteWithoutResponse(); |
257 run_loop0.Run(); | 259 run_loop0.Run(); |
258 run_loop1.Run(); | 260 run_loop1.Run(); |
259 | 261 |
(...skipping 26 matching lines...) Expand all Loading... |
286 false, base::ThreadTaskRunnerHandle::Get(), 0u); | 288 false, base::ThreadTaskRunnerHandle::Get(), 0u); |
287 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 289 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
288 base::MakeUnique<PassThroughFilter>(), | 290 base::MakeUnique<PassThroughFilter>(), |
289 false, base::ThreadTaskRunnerHandle::Get(), | 291 false, base::ThreadTaskRunnerHandle::Get(), |
290 0u); | 292 0u); |
291 | 293 |
292 Message request; | 294 Message request; |
293 AllocRequestMessage(1, "hello", &request); | 295 AllocRequestMessage(1, "hello", &request); |
294 | 296 |
295 MessageQueue message_queue; | 297 MessageQueue message_queue; |
296 client0.AcceptWithResponder(&request, | 298 client0.AcceptWithResponder( |
297 new MessageAccumulator(&message_queue)); | 299 &request, base::MakeUnique<MessageAccumulator>(&message_queue)); |
298 | 300 |
299 run_loop.Run(); | 301 run_loop.Run(); |
300 | 302 |
301 EXPECT_TRUE(generator.has_responder()); | 303 EXPECT_TRUE(generator.has_responder()); |
302 } | 304 } |
303 | 305 |
304 EXPECT_FALSE(generator.responder_is_valid()); | 306 EXPECT_FALSE(generator.responder_is_valid()); |
305 generator.CompleteWithResponse(); // This should end up doing nothing. | 307 generator.CompleteWithResponse(); // This should end up doing nothing. |
306 } | 308 } |
307 | 309 |
308 // TODO(yzshen): add more tests. | 310 // TODO(yzshen): add more tests. |
309 | 311 |
310 } // namespace | 312 } // namespace |
311 } // namespace test | 313 } // namespace test |
312 } // namespace mojo | 314 } // namespace mojo |
OLD | NEW |