| 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/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" | 15 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" |
| 15 #include "mojo/public/cpp/bindings/message.h" | 16 #include "mojo/public/cpp/bindings/message.h" |
| 16 #include "mojo/public/cpp/bindings/message_filter.h" | 17 #include "mojo/public/cpp/bindings/message_filter.h" |
| 17 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 18 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
| 18 #include "mojo/public/cpp/bindings/tests/message_queue.h" | 19 #include "mojo/public/cpp/bindings/tests/message_queue.h" |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 generator.CompleteWithResponse(); | 241 generator.CompleteWithResponse(); |
| 241 run_loop4.Run(); | 242 run_loop4.Run(); |
| 242 | 243 |
| 243 // Check the second response. | 244 // Check the second response. |
| 244 EXPECT_FALSE(message_queue.IsEmpty()); | 245 EXPECT_FALSE(message_queue.IsEmpty()); |
| 245 message_queue.Pop(&response); | 246 message_queue.Pop(&response); |
| 246 EXPECT_EQ(std::string("hello again world!"), | 247 EXPECT_EQ(std::string("hello again world!"), |
| 247 std::string(reinterpret_cast<const char*>(response.payload()))); | 248 std::string(reinterpret_cast<const char*>(response.payload()))); |
| 248 } | 249 } |
| 249 | 250 |
| 251 void ForwardErrorHandler(bool* called, const base::Closure& callback) { |
| 252 *called = true; |
| 253 callback.Run(); |
| 254 } |
| 255 |
| 250 // Tests that if the receiving application destroys the responder_ without | 256 // Tests that if the receiving application destroys the responder_ without |
| 251 // sending a response, then we trigger connection error at both sides. Moreover, | 257 // sending a response, then we trigger connection error at both sides. Moreover, |
| 252 // both sides still appear to have a valid message pipe handle bound. | 258 // both sides still appear to have a valid message pipe handle bound. |
| 253 TEST_F(MultiplexRouterTest, MissingResponses) { | 259 TEST_F(MultiplexRouterTest, MissingResponses) { |
| 254 base::RunLoop run_loop0, run_loop1; | 260 base::RunLoop run_loop0, run_loop1; |
| 255 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 261 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
| 256 base::WrapUnique(new PassThroughFilter()), | 262 base::WrapUnique(new PassThroughFilter()), |
| 257 false, base::ThreadTaskRunnerHandle::Get()); | 263 false, base::ThreadTaskRunnerHandle::Get()); |
| 258 bool error_handler_called0 = false; | 264 bool error_handler_called0 = false; |
| 259 client0.set_connection_error_handler( | 265 client0.set_connection_error_handler( |
| 260 [&error_handler_called0, &run_loop0]() { | 266 base::Bind(&ForwardErrorHandler, &error_handler_called0, |
| 261 error_handler_called0 = true; | 267 run_loop0.QuitClosure())); |
| 262 run_loop0.Quit(); | |
| 263 }); | |
| 264 | 268 |
| 265 base::RunLoop run_loop3; | 269 base::RunLoop run_loop3; |
| 266 LazyResponseGenerator generator(run_loop3.QuitClosure()); | 270 LazyResponseGenerator generator(run_loop3.QuitClosure()); |
| 267 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 271 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
| 268 base::WrapUnique(new PassThroughFilter()), | 272 base::WrapUnique(new PassThroughFilter()), |
| 269 false, base::ThreadTaskRunnerHandle::Get()); | 273 false, base::ThreadTaskRunnerHandle::Get()); |
| 270 bool error_handler_called1 = false; | 274 bool error_handler_called1 = false; |
| 271 client1.set_connection_error_handler( | 275 client1.set_connection_error_handler( |
| 272 [&error_handler_called1, &run_loop1]() { | 276 base::Bind(&ForwardErrorHandler, &error_handler_called1, |
| 273 error_handler_called1 = true; | 277 run_loop1.QuitClosure())); |
| 274 run_loop1.Quit(); | |
| 275 }); | |
| 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)); |
| 282 run_loop3.Run(); | 284 run_loop3.Run(); |
| 283 | 285 |
| 284 // The request has been received but no response has been sent. | 286 // The request has been received but no response has been sent. |
| 285 EXPECT_TRUE(message_queue.IsEmpty()); | 287 EXPECT_TRUE(message_queue.IsEmpty()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 | 336 |
| 335 EXPECT_FALSE(generator.responder_is_valid()); | 337 EXPECT_FALSE(generator.responder_is_valid()); |
| 336 generator.CompleteWithResponse(); // This should end up doing nothing. | 338 generator.CompleteWithResponse(); // This should end up doing nothing. |
| 337 } | 339 } |
| 338 | 340 |
| 339 // TODO(yzshen): add more tests. | 341 // TODO(yzshen): add more tests. |
| 340 | 342 |
| 341 } // namespace | 343 } // namespace |
| 342 } // namespace test | 344 } // namespace test |
| 343 } // namespace mojo | 345 } // namespace mojo |
| OLD | NEW |