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