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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 ScopedInterfaceEndpointHandle endpoint0_; | 58 ScopedInterfaceEndpointHandle endpoint0_; |
59 ScopedInterfaceEndpointHandle endpoint1_; | 59 ScopedInterfaceEndpointHandle endpoint1_; |
60 | 60 |
61 private: | 61 private: |
62 base::MessageLoop loop_; | 62 base::MessageLoop loop_; |
63 }; | 63 }; |
64 | 64 |
65 TEST_F(MultiplexRouterTest, BasicRequestResponse) { | 65 TEST_F(MultiplexRouterTest, BasicRequestResponse) { |
66 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 66 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
67 base::MakeUnique<PassThroughFilter>(), false, | 67 base::MakeUnique<PassThroughFilter>(), false, |
68 base::ThreadTaskRunnerHandle::Get()); | 68 base::ThreadTaskRunnerHandle::Get(), 0u); |
69 ResponseGenerator generator; | 69 ResponseGenerator generator; |
70 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 70 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
71 base::MakeUnique<PassThroughFilter>(), false, | 71 base::MakeUnique<PassThroughFilter>(), false, |
72 base::ThreadTaskRunnerHandle::Get()); | 72 base::ThreadTaskRunnerHandle::Get(), 0u); |
73 | 73 |
74 Message request; | 74 Message request; |
75 AllocRequestMessage(1, "hello", &request); | 75 AllocRequestMessage(1, "hello", &request); |
76 | 76 |
77 MessageQueue message_queue; | 77 MessageQueue message_queue; |
78 base::RunLoop run_loop; | 78 base::RunLoop run_loop; |
79 client0.AcceptWithResponder( | 79 client0.AcceptWithResponder( |
80 &request, | 80 &request, |
81 new MessageAccumulator(&message_queue, run_loop.QuitClosure())); | 81 new MessageAccumulator(&message_queue, run_loop.QuitClosure())); |
82 | 82 |
(...skipping 22 matching lines...) Expand all Loading... |
105 | 105 |
106 message_queue.Pop(&response); | 106 message_queue.Pop(&response); |
107 | 107 |
108 EXPECT_EQ(std::string("hello again world!"), | 108 EXPECT_EQ(std::string("hello again world!"), |
109 std::string(reinterpret_cast<const char*>(response.payload()))); | 109 std::string(reinterpret_cast<const char*>(response.payload()))); |
110 } | 110 } |
111 | 111 |
112 TEST_F(MultiplexRouterTest, BasicRequestResponse_Synchronous) { | 112 TEST_F(MultiplexRouterTest, BasicRequestResponse_Synchronous) { |
113 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 113 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
114 base::MakeUnique<PassThroughFilter>(), false, | 114 base::MakeUnique<PassThroughFilter>(), false, |
115 base::ThreadTaskRunnerHandle::Get()); | 115 base::ThreadTaskRunnerHandle::Get(), 0u); |
116 ResponseGenerator generator; | 116 ResponseGenerator generator; |
117 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 117 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
118 base::MakeUnique<PassThroughFilter>(), false, | 118 base::MakeUnique<PassThroughFilter>(), false, |
119 base::ThreadTaskRunnerHandle::Get()); | 119 base::ThreadTaskRunnerHandle::Get(), 0u); |
120 | 120 |
121 Message request; | 121 Message request; |
122 AllocRequestMessage(1, "hello", &request); | 122 AllocRequestMessage(1, "hello", &request); |
123 | 123 |
124 MessageQueue message_queue; | 124 MessageQueue message_queue; |
125 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 125 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
126 | 126 |
127 router1_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 127 router1_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
128 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 128 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
129 | 129 |
(...skipping 16 matching lines...) Expand all Loading... |
146 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 146 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
147 | 147 |
148 EXPECT_FALSE(message_queue.IsEmpty()); | 148 EXPECT_FALSE(message_queue.IsEmpty()); |
149 | 149 |
150 message_queue.Pop(&response); | 150 message_queue.Pop(&response); |
151 | 151 |
152 EXPECT_EQ(std::string("hello again world!"), | 152 EXPECT_EQ(std::string("hello again world!"), |
153 std::string(reinterpret_cast<const char*>(response.payload()))); | 153 std::string(reinterpret_cast<const char*>(response.payload()))); |
154 } | 154 } |
155 | 155 |
156 TEST_F(MultiplexRouterTest, RequestWithNoReceiver) { | |
157 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | |
158 base::MakeUnique<PassThroughFilter>(), false, | |
159 base::ThreadTaskRunnerHandle::Get()); | |
160 InterfaceEndpointClient client1(std::move(endpoint1_), nullptr, | |
161 base::MakeUnique<PassThroughFilter>(), false, | |
162 base::ThreadTaskRunnerHandle::Get()); | |
163 | |
164 // Without an incoming receiver set on client1, we expect client0 to observe | |
165 // an error as a result of sending a message. | |
166 | |
167 Message request; | |
168 AllocRequestMessage(1, "hello", &request); | |
169 | |
170 MessageQueue message_queue; | |
171 base::RunLoop run_loop, run_loop2; | |
172 client0.set_connection_error_handler(run_loop.QuitClosure()); | |
173 client1.set_connection_error_handler(run_loop2.QuitClosure()); | |
174 client0.AcceptWithResponder( | |
175 &request, new MessageAccumulator(&message_queue, run_loop.QuitClosure())); | |
176 | |
177 run_loop.Run(); | |
178 run_loop2.Run(); | |
179 | |
180 EXPECT_TRUE(client0.encountered_error()); | |
181 EXPECT_TRUE(client1.encountered_error()); | |
182 EXPECT_TRUE(message_queue.IsEmpty()); | |
183 } | |
184 | |
185 // Tests MultiplexRouter using the LazyResponseGenerator. The responses will not | 156 // Tests MultiplexRouter using the LazyResponseGenerator. The responses will not |
186 // be sent until after the requests have been accepted. | 157 // be sent until after the requests have been accepted. |
187 TEST_F(MultiplexRouterTest, LazyResponses) { | 158 TEST_F(MultiplexRouterTest, LazyResponses) { |
188 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 159 InterfaceEndpointClient client0( |
189 base::WrapUnique(new PassThroughFilter()), | 160 std::move(endpoint0_), nullptr, base::WrapUnique(new PassThroughFilter()), |
190 false, base::ThreadTaskRunnerHandle::Get()); | 161 false, base::ThreadTaskRunnerHandle::Get(), 0u); |
191 base::RunLoop run_loop; | 162 base::RunLoop run_loop; |
192 LazyResponseGenerator generator(run_loop.QuitClosure()); | 163 LazyResponseGenerator generator(run_loop.QuitClosure()); |
193 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 164 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
194 base::WrapUnique(new PassThroughFilter()), | 165 base::WrapUnique(new PassThroughFilter()), |
195 false, base::ThreadTaskRunnerHandle::Get()); | 166 false, base::ThreadTaskRunnerHandle::Get(), |
| 167 0u); |
196 | 168 |
197 Message request; | 169 Message request; |
198 AllocRequestMessage(1, "hello", &request); | 170 AllocRequestMessage(1, "hello", &request); |
199 | 171 |
200 MessageQueue message_queue; | 172 MessageQueue message_queue; |
201 base::RunLoop run_loop2; | 173 base::RunLoop run_loop2; |
202 client0.AcceptWithResponder( | 174 client0.AcceptWithResponder( |
203 &request, | 175 &request, |
204 new MessageAccumulator(&message_queue, run_loop2.QuitClosure())); | 176 new MessageAccumulator(&message_queue, run_loop2.QuitClosure())); |
205 run_loop.Run(); | 177 run_loop.Run(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 void ForwardErrorHandler(bool* called, const base::Closure& callback) { | 221 void ForwardErrorHandler(bool* called, const base::Closure& callback) { |
250 *called = true; | 222 *called = true; |
251 callback.Run(); | 223 callback.Run(); |
252 } | 224 } |
253 | 225 |
254 // Tests that if the receiving application destroys the responder_ without | 226 // Tests that if the receiving application destroys the responder_ without |
255 // sending a response, then we trigger connection error at both sides. Moreover, | 227 // sending a response, then we trigger connection error at both sides. Moreover, |
256 // both sides still appear to have a valid message pipe handle bound. | 228 // both sides still appear to have a valid message pipe handle bound. |
257 TEST_F(MultiplexRouterTest, MissingResponses) { | 229 TEST_F(MultiplexRouterTest, MissingResponses) { |
258 base::RunLoop run_loop0, run_loop1; | 230 base::RunLoop run_loop0, run_loop1; |
259 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 231 InterfaceEndpointClient client0( |
260 base::WrapUnique(new PassThroughFilter()), | 232 std::move(endpoint0_), nullptr, base::WrapUnique(new PassThroughFilter()), |
261 false, base::ThreadTaskRunnerHandle::Get()); | 233 false, base::ThreadTaskRunnerHandle::Get(), 0u); |
262 bool error_handler_called0 = false; | 234 bool error_handler_called0 = false; |
263 client0.set_connection_error_handler( | 235 client0.set_connection_error_handler( |
264 base::Bind(&ForwardErrorHandler, &error_handler_called0, | 236 base::Bind(&ForwardErrorHandler, &error_handler_called0, |
265 run_loop0.QuitClosure())); | 237 run_loop0.QuitClosure())); |
266 | 238 |
267 base::RunLoop run_loop3; | 239 base::RunLoop run_loop3; |
268 LazyResponseGenerator generator(run_loop3.QuitClosure()); | 240 LazyResponseGenerator generator(run_loop3.QuitClosure()); |
269 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 241 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
270 base::WrapUnique(new PassThroughFilter()), | 242 base::WrapUnique(new PassThroughFilter()), |
271 false, base::ThreadTaskRunnerHandle::Get()); | 243 false, base::ThreadTaskRunnerHandle::Get(), |
| 244 0u); |
272 bool error_handler_called1 = false; | 245 bool error_handler_called1 = false; |
273 client1.set_connection_error_handler( | 246 client1.set_connection_error_handler( |
274 base::Bind(&ForwardErrorHandler, &error_handler_called1, | 247 base::Bind(&ForwardErrorHandler, &error_handler_called1, |
275 run_loop1.QuitClosure())); | 248 run_loop1.QuitClosure())); |
276 | 249 |
277 Message request; | 250 Message request; |
278 AllocRequestMessage(1, "hello", &request); | 251 AllocRequestMessage(1, "hello", &request); |
279 | 252 |
280 MessageQueue message_queue; | 253 MessageQueue message_queue; |
281 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 254 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
(...skipping 24 matching lines...) Expand all Loading... |
306 } | 279 } |
307 | 280 |
308 TEST_F(MultiplexRouterTest, LateResponse) { | 281 TEST_F(MultiplexRouterTest, LateResponse) { |
309 // Test that things won't blow up if we try to send a message to a | 282 // Test that things won't blow up if we try to send a message to a |
310 // MessageReceiver, which was given to us via AcceptWithResponder, | 283 // MessageReceiver, which was given to us via AcceptWithResponder, |
311 // after the router has gone away. | 284 // after the router has gone away. |
312 | 285 |
313 base::RunLoop run_loop; | 286 base::RunLoop run_loop; |
314 LazyResponseGenerator generator(run_loop.QuitClosure()); | 287 LazyResponseGenerator generator(run_loop.QuitClosure()); |
315 { | 288 { |
316 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 289 InterfaceEndpointClient client0( |
317 base::MakeUnique<PassThroughFilter>(), | 290 std::move(endpoint0_), nullptr, base::MakeUnique<PassThroughFilter>(), |
318 false, base::ThreadTaskRunnerHandle::Get()); | 291 false, base::ThreadTaskRunnerHandle::Get(), 0u); |
319 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 292 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
320 base::MakeUnique<PassThroughFilter>(), | 293 base::MakeUnique<PassThroughFilter>(), |
321 false, base::ThreadTaskRunnerHandle::Get()); | 294 false, base::ThreadTaskRunnerHandle::Get(), |
| 295 0u); |
322 | 296 |
323 Message request; | 297 Message request; |
324 AllocRequestMessage(1, "hello", &request); | 298 AllocRequestMessage(1, "hello", &request); |
325 | 299 |
326 MessageQueue message_queue; | 300 MessageQueue message_queue; |
327 client0.AcceptWithResponder(&request, | 301 client0.AcceptWithResponder(&request, |
328 new MessageAccumulator(&message_queue)); | 302 new MessageAccumulator(&message_queue)); |
329 | 303 |
330 run_loop.Run(); | 304 run_loop.Run(); |
331 | 305 |
332 EXPECT_TRUE(generator.has_responder()); | 306 EXPECT_TRUE(generator.has_responder()); |
333 } | 307 } |
334 | 308 |
335 EXPECT_FALSE(generator.responder_is_valid()); | 309 EXPECT_FALSE(generator.responder_is_valid()); |
336 generator.CompleteWithResponse(); // This should end up doing nothing. | 310 generator.CompleteWithResponse(); // This should end up doing nothing. |
337 } | 311 } |
338 | 312 |
339 // TODO(yzshen): add more tests. | 313 // TODO(yzshen): add more tests. |
340 | 314 |
341 } // namespace | 315 } // namespace |
342 } // namespace test | 316 } // namespace test |
343 } // namespace mojo | 317 } // namespace mojo |
OLD | NEW |