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 19 matching lines...) Expand all Loading... |
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) { | 156 TEST_F(MultiplexRouterTest, RequestWithNoReceiver) { |
157 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 157 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
158 base::MakeUnique<PassThroughFilter>(), false, | 158 base::MakeUnique<PassThroughFilter>(), false, |
159 base::ThreadTaskRunnerHandle::Get()); | 159 base::ThreadTaskRunnerHandle::Get(), 0u); |
160 InterfaceEndpointClient client1(std::move(endpoint1_), nullptr, | 160 InterfaceEndpointClient client1(std::move(endpoint1_), nullptr, |
161 base::MakeUnique<PassThroughFilter>(), false, | 161 base::MakeUnique<PassThroughFilter>(), false, |
162 base::ThreadTaskRunnerHandle::Get()); | 162 base::ThreadTaskRunnerHandle::Get(), 0u); |
163 | 163 |
164 // Without an incoming receiver set on client1, we expect client0 to observe | 164 // Without an incoming receiver set on client1, we expect client0 to observe |
165 // an error as a result of sending a message. | 165 // an error as a result of sending a message. |
166 | 166 |
167 Message request; | 167 Message request; |
168 AllocRequestMessage(1, "hello", &request); | 168 AllocRequestMessage(1, "hello", &request); |
169 | 169 |
170 MessageQueue message_queue; | 170 MessageQueue message_queue; |
171 base::RunLoop run_loop, run_loop2; | 171 base::RunLoop run_loop, run_loop2; |
172 client0.set_connection_error_handler(run_loop.QuitClosure()); | 172 client0.set_connection_error_handler(run_loop.QuitClosure()); |
173 client1.set_connection_error_handler(run_loop2.QuitClosure()); | 173 client1.set_connection_error_handler(run_loop2.QuitClosure()); |
174 client0.AcceptWithResponder( | 174 client0.AcceptWithResponder( |
175 &request, new MessageAccumulator(&message_queue, run_loop.QuitClosure())); | 175 &request, new MessageAccumulator(&message_queue, run_loop.QuitClosure())); |
176 | 176 |
177 run_loop.Run(); | 177 run_loop.Run(); |
178 run_loop2.Run(); | 178 run_loop2.Run(); |
179 | 179 |
180 EXPECT_TRUE(client0.encountered_error()); | 180 EXPECT_TRUE(client0.encountered_error()); |
181 EXPECT_TRUE(client1.encountered_error()); | 181 EXPECT_TRUE(client1.encountered_error()); |
182 EXPECT_TRUE(message_queue.IsEmpty()); | 182 EXPECT_TRUE(message_queue.IsEmpty()); |
183 } | 183 } |
184 | 184 |
185 // Tests MultiplexRouter using the LazyResponseGenerator. The responses will not | 185 // Tests MultiplexRouter using the LazyResponseGenerator. The responses will not |
186 // be sent until after the requests have been accepted. | 186 // be sent until after the requests have been accepted. |
187 TEST_F(MultiplexRouterTest, LazyResponses) { | 187 TEST_F(MultiplexRouterTest, LazyResponses) { |
188 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 188 InterfaceEndpointClient client0( |
189 base::WrapUnique(new PassThroughFilter()), | 189 std::move(endpoint0_), nullptr, base::WrapUnique(new PassThroughFilter()), |
190 false, base::ThreadTaskRunnerHandle::Get()); | 190 false, base::ThreadTaskRunnerHandle::Get(), 0u); |
191 base::RunLoop run_loop; | 191 base::RunLoop run_loop; |
192 LazyResponseGenerator generator(run_loop.QuitClosure()); | 192 LazyResponseGenerator generator(run_loop.QuitClosure()); |
193 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 193 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
194 base::WrapUnique(new PassThroughFilter()), | 194 base::WrapUnique(new PassThroughFilter()), |
195 false, base::ThreadTaskRunnerHandle::Get()); | 195 false, base::ThreadTaskRunnerHandle::Get(), |
| 196 0u); |
196 | 197 |
197 Message request; | 198 Message request; |
198 AllocRequestMessage(1, "hello", &request); | 199 AllocRequestMessage(1, "hello", &request); |
199 | 200 |
200 MessageQueue message_queue; | 201 MessageQueue message_queue; |
201 base::RunLoop run_loop2; | 202 base::RunLoop run_loop2; |
202 client0.AcceptWithResponder( | 203 client0.AcceptWithResponder( |
203 &request, | 204 &request, |
204 new MessageAccumulator(&message_queue, run_loop2.QuitClosure())); | 205 new MessageAccumulator(&message_queue, run_loop2.QuitClosure())); |
205 run_loop.Run(); | 206 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) { | 250 void ForwardErrorHandler(bool* called, const base::Closure& callback) { |
250 *called = true; | 251 *called = true; |
251 callback.Run(); | 252 callback.Run(); |
252 } | 253 } |
253 | 254 |
254 // Tests that if the receiving application destroys the responder_ without | 255 // Tests that if the receiving application destroys the responder_ without |
255 // sending a response, then we trigger connection error at both sides. Moreover, | 256 // 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. | 257 // both sides still appear to have a valid message pipe handle bound. |
257 TEST_F(MultiplexRouterTest, MissingResponses) { | 258 TEST_F(MultiplexRouterTest, MissingResponses) { |
258 base::RunLoop run_loop0, run_loop1; | 259 base::RunLoop run_loop0, run_loop1; |
259 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 260 InterfaceEndpointClient client0( |
260 base::WrapUnique(new PassThroughFilter()), | 261 std::move(endpoint0_), nullptr, base::WrapUnique(new PassThroughFilter()), |
261 false, base::ThreadTaskRunnerHandle::Get()); | 262 false, base::ThreadTaskRunnerHandle::Get(), 0u); |
262 bool error_handler_called0 = false; | 263 bool error_handler_called0 = false; |
263 client0.set_connection_error_handler( | 264 client0.set_connection_error_handler( |
264 base::Bind(&ForwardErrorHandler, &error_handler_called0, | 265 base::Bind(&ForwardErrorHandler, &error_handler_called0, |
265 run_loop0.QuitClosure())); | 266 run_loop0.QuitClosure())); |
266 | 267 |
267 base::RunLoop run_loop3; | 268 base::RunLoop run_loop3; |
268 LazyResponseGenerator generator(run_loop3.QuitClosure()); | 269 LazyResponseGenerator generator(run_loop3.QuitClosure()); |
269 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 270 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
270 base::WrapUnique(new PassThroughFilter()), | 271 base::WrapUnique(new PassThroughFilter()), |
271 false, base::ThreadTaskRunnerHandle::Get()); | 272 false, base::ThreadTaskRunnerHandle::Get(), |
| 273 0u); |
272 bool error_handler_called1 = false; | 274 bool error_handler_called1 = false; |
273 client1.set_connection_error_handler( | 275 client1.set_connection_error_handler( |
274 base::Bind(&ForwardErrorHandler, &error_handler_called1, | 276 base::Bind(&ForwardErrorHandler, &error_handler_called1, |
275 run_loop1.QuitClosure())); | 277 run_loop1.QuitClosure())); |
276 | 278 |
277 Message request; | 279 Message request; |
278 AllocRequestMessage(1, "hello", &request); | 280 AllocRequestMessage(1, "hello", &request); |
279 | 281 |
280 MessageQueue message_queue; | 282 MessageQueue message_queue; |
281 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 283 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
(...skipping 24 matching lines...) Expand all Loading... |
306 } | 308 } |
307 | 309 |
308 TEST_F(MultiplexRouterTest, LateResponse) { | 310 TEST_F(MultiplexRouterTest, LateResponse) { |
309 // Test that things won't blow up if we try to send a message to a | 311 // 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, | 312 // MessageReceiver, which was given to us via AcceptWithResponder, |
311 // after the router has gone away. | 313 // after the router has gone away. |
312 | 314 |
313 base::RunLoop run_loop; | 315 base::RunLoop run_loop; |
314 LazyResponseGenerator generator(run_loop.QuitClosure()); | 316 LazyResponseGenerator generator(run_loop.QuitClosure()); |
315 { | 317 { |
316 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 318 InterfaceEndpointClient client0( |
317 base::MakeUnique<PassThroughFilter>(), | 319 std::move(endpoint0_), nullptr, base::MakeUnique<PassThroughFilter>(), |
318 false, base::ThreadTaskRunnerHandle::Get()); | 320 false, base::ThreadTaskRunnerHandle::Get(), 0u); |
319 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 321 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
320 base::MakeUnique<PassThroughFilter>(), | 322 base::MakeUnique<PassThroughFilter>(), |
321 false, base::ThreadTaskRunnerHandle::Get()); | 323 false, base::ThreadTaskRunnerHandle::Get(), |
| 324 0u); |
322 | 325 |
323 Message request; | 326 Message request; |
324 AllocRequestMessage(1, "hello", &request); | 327 AllocRequestMessage(1, "hello", &request); |
325 | 328 |
326 MessageQueue message_queue; | 329 MessageQueue message_queue; |
327 client0.AcceptWithResponder(&request, | 330 client0.AcceptWithResponder(&request, |
328 new MessageAccumulator(&message_queue)); | 331 new MessageAccumulator(&message_queue)); |
329 | 332 |
330 run_loop.Run(); | 333 run_loop.Run(); |
331 | 334 |
332 EXPECT_TRUE(generator.has_responder()); | 335 EXPECT_TRUE(generator.has_responder()); |
333 } | 336 } |
334 | 337 |
335 EXPECT_FALSE(generator.responder_is_valid()); | 338 EXPECT_FALSE(generator.responder_is_valid()); |
336 generator.CompleteWithResponse(); // This should end up doing nothing. | 339 generator.CompleteWithResponse(); // This should end up doing nothing. |
337 } | 340 } |
338 | 341 |
339 // TODO(yzshen): add more tests. | 342 // TODO(yzshen): add more tests. |
340 | 343 |
341 } // namespace | 344 } // namespace |
342 } // namespace test | 345 } // namespace test |
343 } // namespace mojo | 346 } // namespace mojo |
OLD | NEW |