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/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 base::WrapUnique(new PassThroughFilter()), | 68 base::WrapUnique(new PassThroughFilter()), |
69 false, base::ThreadTaskRunnerHandle::Get()); | 69 false, base::ThreadTaskRunnerHandle::Get()); |
70 ResponseGenerator generator; | 70 ResponseGenerator generator; |
71 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 71 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
72 base::WrapUnique(new PassThroughFilter()), | 72 base::WrapUnique(new PassThroughFilter()), |
73 false, base::ThreadTaskRunnerHandle::Get()); | 73 false, base::ThreadTaskRunnerHandle::Get()); |
74 | 74 |
75 Message request; | 75 Message request; |
76 AllocRequestMessage(1, "hello", &request); | 76 AllocRequestMessage(1, "hello", &request); |
77 | 77 |
| 78 Error error; |
78 MessageQueue message_queue; | 79 MessageQueue message_queue; |
79 base::RunLoop run_loop; | 80 base::RunLoop run_loop; |
80 client0.AcceptWithResponder( | 81 client0.AcceptWithResponder( |
81 &request, | 82 &request, |
82 new MessageAccumulator(&message_queue, run_loop.QuitClosure())); | 83 new MessageAccumulator(&message_queue, run_loop.QuitClosure()), &error); |
83 | 84 |
84 run_loop.Run(); | 85 run_loop.Run(); |
85 | 86 |
86 EXPECT_FALSE(message_queue.IsEmpty()); | 87 EXPECT_FALSE(message_queue.IsEmpty()); |
87 | 88 |
88 Message response; | 89 Message response; |
89 message_queue.Pop(&response); | 90 message_queue.Pop(&response); |
90 | 91 |
91 EXPECT_EQ(std::string("hello world!"), | 92 EXPECT_EQ(std::string("hello world!"), |
92 std::string(reinterpret_cast<const char*>(response.payload()))); | 93 std::string(reinterpret_cast<const char*>(response.payload()))); |
93 | 94 |
94 // Send a second message on the pipe. | 95 // Send a second message on the pipe. |
95 Message request2; | 96 Message request2; |
96 AllocRequestMessage(1, "hello again", &request2); | 97 AllocRequestMessage(1, "hello again", &request2); |
97 | 98 |
98 base::RunLoop run_loop2; | 99 base::RunLoop run_loop2; |
99 client0.AcceptWithResponder( | 100 client0.AcceptWithResponder( |
100 &request2, | 101 &request2, |
101 new MessageAccumulator(&message_queue, run_loop2.QuitClosure())); | 102 new MessageAccumulator(&message_queue, run_loop2.QuitClosure()), &error); |
102 | 103 |
103 run_loop2.Run(); | 104 run_loop2.Run(); |
104 | 105 |
105 EXPECT_FALSE(message_queue.IsEmpty()); | 106 EXPECT_FALSE(message_queue.IsEmpty()); |
106 | 107 |
107 message_queue.Pop(&response); | 108 message_queue.Pop(&response); |
108 | 109 |
109 EXPECT_EQ(std::string("hello again world!"), | 110 EXPECT_EQ(std::string("hello again world!"), |
110 std::string(reinterpret_cast<const char*>(response.payload()))); | 111 std::string(reinterpret_cast<const char*>(response.payload()))); |
111 } | 112 } |
112 | 113 |
113 TEST_F(MultiplexRouterTest, BasicRequestResponse_Synchronous) { | 114 TEST_F(MultiplexRouterTest, BasicRequestResponse_Synchronous) { |
114 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 115 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
115 base::WrapUnique(new PassThroughFilter()), | 116 base::WrapUnique(new PassThroughFilter()), |
116 false, base::ThreadTaskRunnerHandle::Get()); | 117 false, base::ThreadTaskRunnerHandle::Get()); |
117 ResponseGenerator generator; | 118 ResponseGenerator generator; |
118 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 119 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
119 base::WrapUnique(new PassThroughFilter()), | 120 base::WrapUnique(new PassThroughFilter()), |
120 false, base::ThreadTaskRunnerHandle::Get()); | 121 false, base::ThreadTaskRunnerHandle::Get()); |
121 | 122 |
122 Message request; | 123 Message request; |
123 AllocRequestMessage(1, "hello", &request); | 124 AllocRequestMessage(1, "hello", &request); |
124 | 125 |
| 126 Error error; |
125 MessageQueue message_queue; | 127 MessageQueue message_queue; |
126 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 128 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue), |
| 129 &error); |
127 | 130 |
128 router1_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 131 router1_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
129 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 132 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
130 | 133 |
131 EXPECT_FALSE(message_queue.IsEmpty()); | 134 EXPECT_FALSE(message_queue.IsEmpty()); |
132 | 135 |
133 Message response; | 136 Message response; |
134 message_queue.Pop(&response); | 137 message_queue.Pop(&response); |
135 | 138 |
136 EXPECT_EQ(std::string("hello world!"), | 139 EXPECT_EQ(std::string("hello world!"), |
137 std::string(reinterpret_cast<const char*>(response.payload()))); | 140 std::string(reinterpret_cast<const char*>(response.payload()))); |
138 | 141 |
139 // Send a second message on the pipe. | 142 // Send a second message on the pipe. |
140 Message request2; | 143 Message request2; |
141 AllocRequestMessage(1, "hello again", &request2); | 144 AllocRequestMessage(1, "hello again", &request2); |
142 | 145 |
143 client0.AcceptWithResponder(&request2, | 146 client0.AcceptWithResponder(&request2, |
144 new MessageAccumulator(&message_queue)); | 147 new MessageAccumulator(&message_queue), &error); |
145 | 148 |
146 router1_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 149 router1_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
147 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 150 router0_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
148 | 151 |
149 EXPECT_FALSE(message_queue.IsEmpty()); | 152 EXPECT_FALSE(message_queue.IsEmpty()); |
150 | 153 |
151 message_queue.Pop(&response); | 154 message_queue.Pop(&response); |
152 | 155 |
153 EXPECT_EQ(std::string("hello again world!"), | 156 EXPECT_EQ(std::string("hello again world!"), |
154 std::string(reinterpret_cast<const char*>(response.payload()))); | 157 std::string(reinterpret_cast<const char*>(response.payload()))); |
155 } | 158 } |
156 | 159 |
157 TEST_F(MultiplexRouterTest, RequestWithNoReceiver) { | 160 TEST_F(MultiplexRouterTest, RequestWithNoReceiver) { |
158 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 161 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
159 base::WrapUnique(new PassThroughFilter()), | 162 base::WrapUnique(new PassThroughFilter()), |
160 false, base::ThreadTaskRunnerHandle::Get()); | 163 false, base::ThreadTaskRunnerHandle::Get()); |
161 InterfaceEndpointClient client1(std::move(endpoint1_), nullptr, | 164 InterfaceEndpointClient client1(std::move(endpoint1_), nullptr, |
162 base::WrapUnique(new PassThroughFilter()), | 165 base::WrapUnique(new PassThroughFilter()), |
163 false, base::ThreadTaskRunnerHandle::Get()); | 166 false, base::ThreadTaskRunnerHandle::Get()); |
164 | 167 |
165 // Without an incoming receiver set on client1, we expect client0 to observe | 168 // Without an incoming receiver set on client1, we expect client0 to observe |
166 // an error as a result of sending a message. | 169 // an error as a result of sending a message. |
167 | 170 |
168 Message request; | 171 Message request; |
169 AllocRequestMessage(1, "hello", &request); | 172 AllocRequestMessage(1, "hello", &request); |
170 | 173 |
| 174 Error error; |
171 MessageQueue message_queue; | 175 MessageQueue message_queue; |
172 base::RunLoop run_loop, run_loop2; | 176 base::RunLoop run_loop, run_loop2; |
173 client0.set_connection_error_handler(run_loop.QuitClosure()); | 177 client0.set_connection_error_handler(run_loop.QuitClosure()); |
174 client1.set_connection_error_handler(run_loop2.QuitClosure()); | 178 client1.set_connection_error_handler(run_loop2.QuitClosure()); |
175 client0.AcceptWithResponder( | 179 client0.AcceptWithResponder( |
176 &request, new MessageAccumulator(&message_queue, run_loop.QuitClosure())); | 180 &request, new MessageAccumulator(&message_queue, run_loop.QuitClosure()), |
| 181 &error); |
177 | 182 |
178 run_loop.Run(); | 183 run_loop.Run(); |
179 run_loop2.Run(); | 184 run_loop2.Run(); |
180 | 185 |
181 EXPECT_TRUE(client0.encountered_error()); | 186 EXPECT_TRUE(client0.encountered_error()); |
182 EXPECT_TRUE(client1.encountered_error()); | 187 EXPECT_TRUE(client1.encountered_error()); |
183 EXPECT_TRUE(message_queue.IsEmpty()); | 188 EXPECT_TRUE(message_queue.IsEmpty()); |
184 } | 189 } |
185 | 190 |
186 // Tests MultiplexRouter using the LazyResponseGenerator. The responses will not | 191 // Tests MultiplexRouter using the LazyResponseGenerator. The responses will not |
187 // be sent until after the requests have been accepted. | 192 // be sent until after the requests have been accepted. |
188 TEST_F(MultiplexRouterTest, LazyResponses) { | 193 TEST_F(MultiplexRouterTest, LazyResponses) { |
189 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 194 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
190 base::WrapUnique(new PassThroughFilter()), | 195 base::WrapUnique(new PassThroughFilter()), |
191 false, base::ThreadTaskRunnerHandle::Get()); | 196 false, base::ThreadTaskRunnerHandle::Get()); |
192 base::RunLoop run_loop; | 197 base::RunLoop run_loop; |
193 LazyResponseGenerator generator(run_loop.QuitClosure()); | 198 LazyResponseGenerator generator(run_loop.QuitClosure()); |
194 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 199 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
195 base::WrapUnique(new PassThroughFilter()), | 200 base::WrapUnique(new PassThroughFilter()), |
196 false, base::ThreadTaskRunnerHandle::Get()); | 201 false, base::ThreadTaskRunnerHandle::Get()); |
197 | 202 |
198 Message request; | 203 Message request; |
199 AllocRequestMessage(1, "hello", &request); | 204 AllocRequestMessage(1, "hello", &request); |
200 | 205 |
| 206 Error error; |
201 MessageQueue message_queue; | 207 MessageQueue message_queue; |
202 base::RunLoop run_loop2; | 208 base::RunLoop run_loop2; |
203 client0.AcceptWithResponder( | 209 client0.AcceptWithResponder( |
204 &request, | 210 &request, |
205 new MessageAccumulator(&message_queue, run_loop2.QuitClosure())); | 211 new MessageAccumulator(&message_queue, run_loop2.QuitClosure()), &error); |
206 run_loop.Run(); | 212 run_loop.Run(); |
207 | 213 |
208 // The request has been received but the response has not been sent yet. | 214 // The request has been received but the response has not been sent yet. |
209 EXPECT_TRUE(message_queue.IsEmpty()); | 215 EXPECT_TRUE(message_queue.IsEmpty()); |
210 | 216 |
211 // Send the response. | 217 // Send the response. |
212 EXPECT_TRUE(generator.responder_is_valid()); | 218 EXPECT_TRUE(generator.responder_is_valid()); |
213 generator.CompleteWithResponse(); | 219 generator.CompleteWithResponse(); |
214 run_loop2.Run(); | 220 run_loop2.Run(); |
215 | 221 |
216 // Check the response. | 222 // Check the response. |
217 EXPECT_FALSE(message_queue.IsEmpty()); | 223 EXPECT_FALSE(message_queue.IsEmpty()); |
218 Message response; | 224 Message response; |
219 message_queue.Pop(&response); | 225 message_queue.Pop(&response); |
220 EXPECT_EQ(std::string("hello world!"), | 226 EXPECT_EQ(std::string("hello world!"), |
221 std::string(reinterpret_cast<const char*>(response.payload()))); | 227 std::string(reinterpret_cast<const char*>(response.payload()))); |
222 | 228 |
223 // Send a second message on the pipe. | 229 // Send a second message on the pipe. |
224 base::RunLoop run_loop3; | 230 base::RunLoop run_loop3; |
225 generator.set_closure(run_loop3.QuitClosure()); | 231 generator.set_closure(run_loop3.QuitClosure()); |
226 Message request2; | 232 Message request2; |
227 AllocRequestMessage(1, "hello again", &request2); | 233 AllocRequestMessage(1, "hello again", &request2); |
228 | 234 |
229 base::RunLoop run_loop4; | 235 base::RunLoop run_loop4; |
230 client0.AcceptWithResponder( | 236 client0.AcceptWithResponder( |
231 &request2, | 237 &request2, |
232 new MessageAccumulator(&message_queue, run_loop4.QuitClosure())); | 238 new MessageAccumulator(&message_queue, run_loop4.QuitClosure()), &error); |
233 run_loop3.Run(); | 239 run_loop3.Run(); |
234 | 240 |
235 // The request has been received but the response has not been sent yet. | 241 // The request has been received but the response has not been sent yet. |
236 EXPECT_TRUE(message_queue.IsEmpty()); | 242 EXPECT_TRUE(message_queue.IsEmpty()); |
237 | 243 |
238 // Send the second response. | 244 // Send the second response. |
239 EXPECT_TRUE(generator.responder_is_valid()); | 245 EXPECT_TRUE(generator.responder_is_valid()); |
240 generator.CompleteWithResponse(); | 246 generator.CompleteWithResponse(); |
241 run_loop4.Run(); | 247 run_loop4.Run(); |
242 | 248 |
(...skipping 27 matching lines...) Expand all Loading... |
270 bool error_handler_called1 = false; | 276 bool error_handler_called1 = false; |
271 client1.set_connection_error_handler( | 277 client1.set_connection_error_handler( |
272 [&error_handler_called1, &run_loop1]() { | 278 [&error_handler_called1, &run_loop1]() { |
273 error_handler_called1 = true; | 279 error_handler_called1 = true; |
274 run_loop1.Quit(); | 280 run_loop1.Quit(); |
275 }); | 281 }); |
276 | 282 |
277 Message request; | 283 Message request; |
278 AllocRequestMessage(1, "hello", &request); | 284 AllocRequestMessage(1, "hello", &request); |
279 | 285 |
| 286 Error error; |
280 MessageQueue message_queue; | 287 MessageQueue message_queue; |
281 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 288 client0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue), |
| 289 &error); |
282 run_loop3.Run(); | 290 run_loop3.Run(); |
283 | 291 |
284 // The request has been received but no response has been sent. | 292 // The request has been received but no response has been sent. |
285 EXPECT_TRUE(message_queue.IsEmpty()); | 293 EXPECT_TRUE(message_queue.IsEmpty()); |
286 | 294 |
287 // Destroy the responder MessagerReceiver but don't send any response. | 295 // Destroy the responder MessagerReceiver but don't send any response. |
288 generator.CompleteWithoutResponse(); | 296 generator.CompleteWithoutResponse(); |
289 run_loop0.Run(); | 297 run_loop0.Run(); |
290 run_loop1.Run(); | 298 run_loop1.Run(); |
291 | 299 |
(...skipping 24 matching lines...) Expand all Loading... |
316 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, | 324 InterfaceEndpointClient client0(std::move(endpoint0_), nullptr, |
317 base::WrapUnique(new PassThroughFilter()), | 325 base::WrapUnique(new PassThroughFilter()), |
318 false, base::ThreadTaskRunnerHandle::Get()); | 326 false, base::ThreadTaskRunnerHandle::Get()); |
319 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, | 327 InterfaceEndpointClient client1(std::move(endpoint1_), &generator, |
320 base::WrapUnique(new PassThroughFilter()), | 328 base::WrapUnique(new PassThroughFilter()), |
321 false, base::ThreadTaskRunnerHandle::Get()); | 329 false, base::ThreadTaskRunnerHandle::Get()); |
322 | 330 |
323 Message request; | 331 Message request; |
324 AllocRequestMessage(1, "hello", &request); | 332 AllocRequestMessage(1, "hello", &request); |
325 | 333 |
| 334 Error error; |
326 MessageQueue message_queue; | 335 MessageQueue message_queue; |
327 client0.AcceptWithResponder(&request, | 336 client0.AcceptWithResponder(&request, |
328 new MessageAccumulator(&message_queue)); | 337 new MessageAccumulator(&message_queue), &error); |
329 | 338 |
330 run_loop.Run(); | 339 run_loop.Run(); |
331 | 340 |
332 EXPECT_TRUE(generator.has_responder()); | 341 EXPECT_TRUE(generator.has_responder()); |
333 } | 342 } |
334 | 343 |
335 EXPECT_FALSE(generator.responder_is_valid()); | 344 EXPECT_FALSE(generator.responder_is_valid()); |
336 generator.CompleteWithResponse(); // This should end up doing nothing. | 345 generator.CompleteWithResponse(); // This should end up doing nothing. |
337 } | 346 } |
338 | 347 |
339 // TODO(yzshen): add more tests. | 348 // TODO(yzshen): add more tests. |
340 | 349 |
341 } // namespace | 350 } // namespace |
342 } // namespace test | 351 } // namespace test |
343 } // namespace mojo | 352 } // namespace mojo |
OLD | NEW |