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