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" |
| 6 |
| 7 #include <utility> |
| 8 |
5 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
6 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
7 #include "mojo/message_pump/message_pump_mojo.h" | 11 #include "mojo/message_pump/message_pump_mojo.h" |
8 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" | 12 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" |
9 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" | |
10 #include "mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.h" | 13 #include "mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.h" |
11 #include "mojo/public/cpp/bindings/message.h" | 14 #include "mojo/public/cpp/bindings/message.h" |
12 #include "mojo/public/cpp/bindings/message_filter.h" | 15 #include "mojo/public/cpp/bindings/message_filter.h" |
13 #include "mojo/public/cpp/bindings/tests/message_queue.h" | 16 #include "mojo/public/cpp/bindings/tests/message_queue.h" |
14 #include "mojo/public/cpp/bindings/tests/router_test_util.h" | 17 #include "mojo/public/cpp/bindings/tests/router_test_util.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
16 | 19 |
17 namespace mojo { | 20 namespace mojo { |
18 namespace test { | 21 namespace test { |
19 namespace { | 22 namespace { |
20 | 23 |
21 using mojo::internal::InterfaceEndpointClient; | 24 using mojo::internal::InterfaceEndpointClient; |
22 using mojo::internal::MultiplexRouter; | 25 using mojo::internal::MultiplexRouter; |
23 using mojo::internal::ScopedInterfaceEndpointHandle; | 26 using mojo::internal::ScopedInterfaceEndpointHandle; |
24 | 27 |
25 class MultiplexRouterTest : public testing::Test { | 28 class MultiplexRouterTest : public testing::Test { |
26 public: | 29 public: |
27 MultiplexRouterTest() : loop_(common::MessagePumpMojo::Create()) {} | 30 MultiplexRouterTest() : loop_(common::MessagePumpMojo::Create()) {} |
28 | 31 |
29 void SetUp() override { | 32 void SetUp() override { |
30 MessagePipe pipe; | 33 MessagePipe pipe; |
31 router0_ = new MultiplexRouter(true, pipe.handle0.Pass()); | 34 router0_ = new MultiplexRouter(true, std::move(pipe.handle0)); |
32 router1_ = new MultiplexRouter(true, pipe.handle1.Pass()); | 35 router1_ = new MultiplexRouter(true, std::move(pipe.handle1)); |
33 router0_->CreateEndpointHandlePair(&endpoint0_, &endpoint1_); | 36 router0_->CreateEndpointHandlePair(&endpoint0_, &endpoint1_); |
34 endpoint1_ = | 37 endpoint1_ = |
35 EmulatePassingEndpointHandle(endpoint1_.Pass(), router1_.get()); | 38 EmulatePassingEndpointHandle(std::move(endpoint1_), router1_.get()); |
36 } | 39 } |
37 | 40 |
38 void TearDown() override {} | 41 void TearDown() override {} |
39 | 42 |
40 void PumpMessages() { loop_.RunUntilIdle(); } | 43 void PumpMessages() { loop_.RunUntilIdle(); } |
41 | 44 |
42 ScopedInterfaceEndpointHandle EmulatePassingEndpointHandle( | 45 ScopedInterfaceEndpointHandle EmulatePassingEndpointHandle( |
43 ScopedInterfaceEndpointHandle handle, | 46 ScopedInterfaceEndpointHandle handle, |
44 MultiplexRouter* target) { | 47 MultiplexRouter* target) { |
45 CHECK(!handle.is_local()); | 48 CHECK(!handle.is_local()); |
46 | 49 |
47 return target->CreateLocalEndpointHandle(handle.release()); | 50 return target->CreateLocalEndpointHandle(handle.release()); |
48 } | 51 } |
49 | 52 |
50 protected: | 53 protected: |
51 scoped_refptr<MultiplexRouter> router0_; | 54 scoped_refptr<MultiplexRouter> router0_; |
52 scoped_refptr<MultiplexRouter> router1_; | 55 scoped_refptr<MultiplexRouter> router1_; |
53 ScopedInterfaceEndpointHandle endpoint0_; | 56 ScopedInterfaceEndpointHandle endpoint0_; |
54 ScopedInterfaceEndpointHandle endpoint1_; | 57 ScopedInterfaceEndpointHandle endpoint1_; |
55 | 58 |
56 private: | 59 private: |
57 base::MessageLoop loop_; | 60 base::MessageLoop loop_; |
58 }; | 61 }; |
59 | 62 |
60 TEST_F(MultiplexRouterTest, BasicRequestResponse) { | 63 TEST_F(MultiplexRouterTest, BasicRequestResponse) { |
61 InterfaceEndpointClient client0(endpoint0_.Pass(), nullptr, | 64 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
62 make_scoped_ptr(new PassThroughFilter())); | 65 make_scoped_ptr(new PassThroughFilter())); |
63 ResponseGenerator generator; | 66 ResponseGenerator generator; |
64 InterfaceEndpointClient client1(endpoint1_.Pass(), &generator, | 67 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
65 make_scoped_ptr(new PassThroughFilter())); | 68 make_scoped_ptr(new PassThroughFilter())); |
66 | 69 |
67 Message request; | 70 Message request; |
68 AllocRequestMessage(1, "hello", &request); | 71 AllocRequestMessage(1, "hello", &request); |
69 | 72 |
70 MessageQueue message_queue; | 73 MessageQueue message_queue; |
71 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 74 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
72 | 75 |
73 PumpMessages(); | 76 PumpMessages(); |
74 | 77 |
(...skipping 16 matching lines...) Expand all Loading... |
91 | 94 |
92 EXPECT_FALSE(message_queue.IsEmpty()); | 95 EXPECT_FALSE(message_queue.IsEmpty()); |
93 | 96 |
94 message_queue.Pop(&response); | 97 message_queue.Pop(&response); |
95 | 98 |
96 EXPECT_EQ(std::string("hello again world!"), | 99 EXPECT_EQ(std::string("hello again world!"), |
97 std::string(reinterpret_cast<const char*>(response.payload()))); | 100 std::string(reinterpret_cast<const char*>(response.payload()))); |
98 } | 101 } |
99 | 102 |
100 TEST_F(MultiplexRouterTest, BasicRequestResponse_Synchronous) { | 103 TEST_F(MultiplexRouterTest, BasicRequestResponse_Synchronous) { |
101 InterfaceEndpointClient client0(endpoint0_.Pass(), nullptr, | 104 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
102 make_scoped_ptr(new PassThroughFilter())); | 105 make_scoped_ptr(new PassThroughFilter())); |
103 ResponseGenerator generator; | 106 ResponseGenerator generator; |
104 InterfaceEndpointClient client1(endpoint1_.Pass(), &generator, | 107 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
105 make_scoped_ptr(new PassThroughFilter())); | 108 make_scoped_ptr(new PassThroughFilter())); |
106 | 109 |
107 Message request; | 110 Message request; |
108 AllocRequestMessage(1, "hello", &request); | 111 AllocRequestMessage(1, "hello", &request); |
109 | 112 |
110 MessageQueue message_queue; | 113 MessageQueue message_queue; |
111 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 114 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
112 | 115 |
113 router1_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 116 router1_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
114 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 117 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
(...skipping 18 matching lines...) Expand all Loading... |
133 | 136 |
134 EXPECT_FALSE(message_queue.IsEmpty()); | 137 EXPECT_FALSE(message_queue.IsEmpty()); |
135 | 138 |
136 message_queue.Pop(&response); | 139 message_queue.Pop(&response); |
137 | 140 |
138 EXPECT_EQ(std::string("hello again world!"), | 141 EXPECT_EQ(std::string("hello again world!"), |
139 std::string(reinterpret_cast<const char*>(response.payload()))); | 142 std::string(reinterpret_cast<const char*>(response.payload()))); |
140 } | 143 } |
141 | 144 |
142 TEST_F(MultiplexRouterTest, RequestWithNoReceiver) { | 145 TEST_F(MultiplexRouterTest, RequestWithNoReceiver) { |
143 InterfaceEndpointClient client0(endpoint0_.Pass(), nullptr, | 146 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
144 make_scoped_ptr(new PassThroughFilter())); | 147 make_scoped_ptr(new PassThroughFilter())); |
145 InterfaceEndpointClient client1(endpoint1_.Pass(), nullptr, | 148 InterfaceEndpointClient client1(std::move(endpoint1_), nullptr, |
146 make_scoped_ptr(new PassThroughFilter())); | 149 make_scoped_ptr(new PassThroughFilter())); |
147 | 150 |
148 // Without an incoming receiver set on client1, we expect client0 to observe | 151 // Without an incoming receiver set on client1, we expect client0 to observe |
149 // an error as a result of sending a message. | 152 // an error as a result of sending a message. |
150 | 153 |
151 Message request; | 154 Message request; |
152 AllocRequestMessage(1, "hello", &request); | 155 AllocRequestMessage(1, "hello", &request); |
153 | 156 |
154 MessageQueue message_queue; | 157 MessageQueue message_queue; |
155 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 158 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
156 | 159 |
157 PumpMessages(); | 160 PumpMessages(); |
158 | 161 |
159 EXPECT_TRUE(client0.encountered_error()); | 162 EXPECT_TRUE(client0.encountered_error()); |
160 EXPECT_TRUE(client1.encountered_error()); | 163 EXPECT_TRUE(client1.encountered_error()); |
161 EXPECT_TRUE(message_queue.IsEmpty()); | 164 EXPECT_TRUE(message_queue.IsEmpty()); |
162 } | 165 } |
163 | 166 |
164 // Tests MultiplexRouter using the LazyResponseGenerator. The responses will not | 167 // Tests MultiplexRouter using the LazyResponseGenerator. The responses will not |
165 // be sent until after the requests have been accepted. | 168 // be sent until after the requests have been accepted. |
166 TEST_F(MultiplexRouterTest, LazyResponses) { | 169 TEST_F(MultiplexRouterTest, LazyResponses) { |
167 InterfaceEndpointClient client0(endpoint0_.Pass(), nullptr, | 170 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
168 make_scoped_ptr(new PassThroughFilter())); | 171 make_scoped_ptr(new PassThroughFilter())); |
169 LazyResponseGenerator generator; | 172 LazyResponseGenerator generator; |
170 InterfaceEndpointClient client1(endpoint1_.Pass(), &generator, | 173 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
171 make_scoped_ptr(new PassThroughFilter())); | 174 make_scoped_ptr(new PassThroughFilter())); |
172 | 175 |
173 Message request; | 176 Message request; |
174 AllocRequestMessage(1, "hello", &request); | 177 AllocRequestMessage(1, "hello", &request); |
175 | 178 |
176 MessageQueue message_queue; | 179 MessageQueue message_queue; |
177 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 180 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
178 PumpMessages(); | 181 PumpMessages(); |
179 | 182 |
180 // The request has been received but the response has not been sent yet. | 183 // The request has been received but the response has not been sent yet. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 EXPECT_FALSE(message_queue.IsEmpty()); | 215 EXPECT_FALSE(message_queue.IsEmpty()); |
213 message_queue.Pop(&response); | 216 message_queue.Pop(&response); |
214 EXPECT_EQ(std::string("hello again world!"), | 217 EXPECT_EQ(std::string("hello again world!"), |
215 std::string(reinterpret_cast<const char*>(response.payload()))); | 218 std::string(reinterpret_cast<const char*>(response.payload()))); |
216 } | 219 } |
217 | 220 |
218 // Tests that if the receiving application destroys the responder_ without | 221 // Tests that if the receiving application destroys the responder_ without |
219 // sending a response, then we trigger connection error at both sides. Moreover, | 222 // sending a response, then we trigger connection error at both sides. Moreover, |
220 // both sides still appear to have a valid message pipe handle bound. | 223 // both sides still appear to have a valid message pipe handle bound. |
221 TEST_F(MultiplexRouterTest, MissingResponses) { | 224 TEST_F(MultiplexRouterTest, MissingResponses) { |
222 InterfaceEndpointClient client0(endpoint0_.Pass(), nullptr, | 225 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
223 make_scoped_ptr(new PassThroughFilter())); | 226 make_scoped_ptr(new PassThroughFilter())); |
224 bool error_handler_called0 = false; | 227 bool error_handler_called0 = false; |
225 client0.set_connection_error_handler( | 228 client0.set_connection_error_handler( |
226 [&error_handler_called0]() { error_handler_called0 = true; }); | 229 [&error_handler_called0]() { error_handler_called0 = true; }); |
227 | 230 |
228 LazyResponseGenerator generator; | 231 LazyResponseGenerator generator; |
229 InterfaceEndpointClient client1(endpoint1_.Pass(), &generator, | 232 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
230 make_scoped_ptr(new PassThroughFilter())); | 233 make_scoped_ptr(new PassThroughFilter())); |
231 bool error_handler_called1 = false; | 234 bool error_handler_called1 = false; |
232 client1.set_connection_error_handler( | 235 client1.set_connection_error_handler( |
233 [&error_handler_called1]() { error_handler_called1 = true; }); | 236 [&error_handler_called1]() { error_handler_called1 = true; }); |
234 | 237 |
235 Message request; | 238 Message request; |
236 AllocRequestMessage(1, "hello", &request); | 239 AllocRequestMessage(1, "hello", &request); |
237 | 240 |
238 MessageQueue message_queue; | 241 MessageQueue message_queue; |
239 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 242 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
(...skipping 22 matching lines...) Expand all Loading... |
262 EXPECT_TRUE(router1_->is_valid()); | 265 EXPECT_TRUE(router1_->is_valid()); |
263 } | 266 } |
264 | 267 |
265 TEST_F(MultiplexRouterTest, LateResponse) { | 268 TEST_F(MultiplexRouterTest, LateResponse) { |
266 // Test that things won't blow up if we try to send a message to a | 269 // Test that things won't blow up if we try to send a message to a |
267 // MessageReceiver, which was given to us via AcceptWithResponder, | 270 // MessageReceiver, which was given to us via AcceptWithResponder, |
268 // after the router has gone away. | 271 // after the router has gone away. |
269 | 272 |
270 LazyResponseGenerator generator; | 273 LazyResponseGenerator generator; |
271 { | 274 { |
272 InterfaceEndpointClient client0(endpoint0_.Pass(), nullptr, | 275 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
273 make_scoped_ptr(new PassThroughFilter())); | 276 make_scoped_ptr(new PassThroughFilter())); |
274 InterfaceEndpointClient client1(endpoint1_.Pass(), &generator, | 277 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
275 make_scoped_ptr(new PassThroughFilter())); | 278 make_scoped_ptr(new PassThroughFilter())); |
276 | 279 |
277 Message request; | 280 Message request; |
278 AllocRequestMessage(1, "hello", &request); | 281 AllocRequestMessage(1, "hello", &request); |
279 | 282 |
280 MessageQueue message_queue; | 283 MessageQueue message_queue; |
281 client0.AcceptWithResponder(&request, | 284 client0.AcceptWithResponder(&request, |
282 new MessageAccumulator(&message_queue)); | 285 new MessageAccumulator(&message_queue)); |
283 | 286 |
284 PumpMessages(); | 287 PumpMessages(); |
285 | 288 |
286 EXPECT_TRUE(generator.has_responder()); | 289 EXPECT_TRUE(generator.has_responder()); |
287 } | 290 } |
288 | 291 |
289 EXPECT_FALSE(generator.responder_is_valid()); | 292 EXPECT_FALSE(generator.responder_is_valid()); |
290 generator.CompleteWithResponse(); // This should end up doing nothing. | 293 generator.CompleteWithResponse(); // This should end up doing nothing. |
291 } | 294 } |
292 | 295 |
293 // TODO(yzshen): add more tests. | 296 // TODO(yzshen): add more tests. |
294 | 297 |
295 } // namespace | 298 } // namespace |
296 } // namespace test | 299 } // namespace test |
297 } // namespace mojo | 300 } // namespace mojo |
OLD | NEW |