OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/router.h" | 5 #include "mojo/public/cpp/bindings/lib/router.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 protected: | 33 protected: |
34 ScopedMessagePipeHandle handle0_; | 34 ScopedMessagePipeHandle handle0_; |
35 ScopedMessagePipeHandle handle1_; | 35 ScopedMessagePipeHandle handle1_; |
36 | 36 |
37 private: | 37 private: |
38 base::MessageLoop loop_; | 38 base::MessageLoop loop_; |
39 }; | 39 }; |
40 | 40 |
41 TEST_F(RouterTest, BasicRequestResponse) { | 41 TEST_F(RouterTest, BasicRequestResponse) { |
42 internal::Router router0(std::move(handle0_), FilterChain(), false, | 42 internal::Router router0(std::move(handle0_), FilterChain(), false, |
43 base::ThreadTaskRunnerHandle::Get()); | 43 base::ThreadTaskRunnerHandle::Get(), 0u); |
44 internal::Router router1(std::move(handle1_), FilterChain(), false, | 44 internal::Router router1(std::move(handle1_), FilterChain(), false, |
45 base::ThreadTaskRunnerHandle::Get()); | 45 base::ThreadTaskRunnerHandle::Get(), 0u); |
46 | 46 |
47 ResponseGenerator generator; | 47 ResponseGenerator generator; |
48 router1.set_incoming_receiver(&generator); | 48 router1.set_incoming_receiver(&generator); |
49 | 49 |
50 Message request; | 50 Message request; |
51 AllocRequestMessage(1, "hello", &request); | 51 AllocRequestMessage(1, "hello", &request); |
52 | 52 |
53 MessageQueue message_queue; | 53 MessageQueue message_queue; |
54 base::RunLoop run_loop; | 54 base::RunLoop run_loop; |
55 router0.AcceptWithResponder( | 55 router0.AcceptWithResponder( |
(...skipping 23 matching lines...) Expand all Loading... |
79 EXPECT_FALSE(message_queue.IsEmpty()); | 79 EXPECT_FALSE(message_queue.IsEmpty()); |
80 | 80 |
81 message_queue.Pop(&response); | 81 message_queue.Pop(&response); |
82 | 82 |
83 EXPECT_EQ(std::string("hello again world!"), | 83 EXPECT_EQ(std::string("hello again world!"), |
84 std::string(reinterpret_cast<const char*>(response.payload()))); | 84 std::string(reinterpret_cast<const char*>(response.payload()))); |
85 } | 85 } |
86 | 86 |
87 TEST_F(RouterTest, BasicRequestResponse_Synchronous) { | 87 TEST_F(RouterTest, BasicRequestResponse_Synchronous) { |
88 internal::Router router0(std::move(handle0_), FilterChain(), false, | 88 internal::Router router0(std::move(handle0_), FilterChain(), false, |
89 base::ThreadTaskRunnerHandle::Get()); | 89 base::ThreadTaskRunnerHandle::Get(), 0u); |
90 internal::Router router1(std::move(handle1_), FilterChain(), false, | 90 internal::Router router1(std::move(handle1_), FilterChain(), false, |
91 base::ThreadTaskRunnerHandle::Get()); | 91 base::ThreadTaskRunnerHandle::Get(), 0u); |
92 | 92 |
93 ResponseGenerator generator; | 93 ResponseGenerator generator; |
94 router1.set_incoming_receiver(&generator); | 94 router1.set_incoming_receiver(&generator); |
95 | 95 |
96 Message request; | 96 Message request; |
97 AllocRequestMessage(1, "hello", &request); | 97 AllocRequestMessage(1, "hello", &request); |
98 | 98 |
99 MessageQueue message_queue; | 99 MessageQueue message_queue; |
100 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 100 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
101 | 101 |
(...skipping 19 matching lines...) Expand all Loading... |
121 router0.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 121 router0.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
122 | 122 |
123 EXPECT_FALSE(message_queue.IsEmpty()); | 123 EXPECT_FALSE(message_queue.IsEmpty()); |
124 | 124 |
125 message_queue.Pop(&response); | 125 message_queue.Pop(&response); |
126 | 126 |
127 EXPECT_EQ(std::string("hello again world!"), | 127 EXPECT_EQ(std::string("hello again world!"), |
128 std::string(reinterpret_cast<const char*>(response.payload()))); | 128 std::string(reinterpret_cast<const char*>(response.payload()))); |
129 } | 129 } |
130 | 130 |
131 TEST_F(RouterTest, RequestWithNoReceiver) { | |
132 internal::Router router0(std::move(handle0_), FilterChain(), false, | |
133 base::ThreadTaskRunnerHandle::Get()); | |
134 internal::Router router1(std::move(handle1_), FilterChain(), false, | |
135 base::ThreadTaskRunnerHandle::Get()); | |
136 | |
137 // Without an incoming receiver set on router1, we expect router0 to observe | |
138 // an error as a result of sending a message. | |
139 | |
140 Message request; | |
141 AllocRequestMessage(1, "hello", &request); | |
142 | |
143 MessageQueue message_queue; | |
144 base::RunLoop run_loop, run_loop2; | |
145 router0.set_connection_error_handler(run_loop.QuitClosure()); | |
146 router1.set_connection_error_handler(run_loop2.QuitClosure()); | |
147 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | |
148 | |
149 run_loop.Run(); | |
150 run_loop2.Run(); | |
151 | |
152 EXPECT_TRUE(router0.encountered_error()); | |
153 EXPECT_TRUE(router1.encountered_error()); | |
154 EXPECT_TRUE(message_queue.IsEmpty()); | |
155 } | |
156 | |
157 // Tests Router using the LazyResponseGenerator. The responses will not be | 131 // Tests Router using the LazyResponseGenerator. The responses will not be |
158 // sent until after the requests have been accepted. | 132 // sent until after the requests have been accepted. |
159 TEST_F(RouterTest, LazyResponses) { | 133 TEST_F(RouterTest, LazyResponses) { |
160 internal::Router router0(std::move(handle0_), FilterChain(), false, | 134 internal::Router router0(std::move(handle0_), FilterChain(), false, |
161 base::ThreadTaskRunnerHandle::Get()); | 135 base::ThreadTaskRunnerHandle::Get(), 0u); |
162 internal::Router router1(std::move(handle1_), FilterChain(), false, | 136 internal::Router router1(std::move(handle1_), FilterChain(), false, |
163 base::ThreadTaskRunnerHandle::Get()); | 137 base::ThreadTaskRunnerHandle::Get(), 0u); |
164 | 138 |
165 base::RunLoop run_loop; | 139 base::RunLoop run_loop; |
166 LazyResponseGenerator generator(run_loop.QuitClosure()); | 140 LazyResponseGenerator generator(run_loop.QuitClosure()); |
167 router1.set_incoming_receiver(&generator); | 141 router1.set_incoming_receiver(&generator); |
168 | 142 |
169 Message request; | 143 Message request; |
170 AllocRequestMessage(1, "hello", &request); | 144 AllocRequestMessage(1, "hello", &request); |
171 | 145 |
172 MessageQueue message_queue; | 146 MessageQueue message_queue; |
173 base::RunLoop run_loop2; | 147 base::RunLoop run_loop2; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 *called = true; | 198 *called = true; |
225 callback.Run(); | 199 callback.Run(); |
226 } | 200 } |
227 | 201 |
228 // Tests that if the receiving application destroys the responder_ without | 202 // Tests that if the receiving application destroys the responder_ without |
229 // sending a response, then we trigger connection error at both sides. Moreover, | 203 // sending a response, then we trigger connection error at both sides. Moreover, |
230 // both sides still appear to have a valid message pipe handle bound. | 204 // both sides still appear to have a valid message pipe handle bound. |
231 TEST_F(RouterTest, MissingResponses) { | 205 TEST_F(RouterTest, MissingResponses) { |
232 base::RunLoop run_loop0, run_loop1; | 206 base::RunLoop run_loop0, run_loop1; |
233 internal::Router router0(std::move(handle0_), FilterChain(), false, | 207 internal::Router router0(std::move(handle0_), FilterChain(), false, |
234 base::ThreadTaskRunnerHandle::Get()); | 208 base::ThreadTaskRunnerHandle::Get(), 0u); |
235 bool error_handler_called0 = false; | 209 bool error_handler_called0 = false; |
236 router0.set_connection_error_handler( | 210 router0.set_connection_error_handler( |
237 base::Bind(&ForwardErrorHandler, &error_handler_called0, | 211 base::Bind(&ForwardErrorHandler, &error_handler_called0, |
238 run_loop0.QuitClosure())); | 212 run_loop0.QuitClosure())); |
239 | 213 |
240 internal::Router router1(std::move(handle1_), FilterChain(), false, | 214 internal::Router router1(std::move(handle1_), FilterChain(), false, |
241 base::ThreadTaskRunnerHandle::Get()); | 215 base::ThreadTaskRunnerHandle::Get(), 0u); |
242 bool error_handler_called1 = false; | 216 bool error_handler_called1 = false; |
243 router1.set_connection_error_handler( | 217 router1.set_connection_error_handler( |
244 base::Bind(&ForwardErrorHandler, &error_handler_called1, | 218 base::Bind(&ForwardErrorHandler, &error_handler_called1, |
245 run_loop1.QuitClosure())); | 219 run_loop1.QuitClosure())); |
246 | 220 |
247 base::RunLoop run_loop3; | 221 base::RunLoop run_loop3; |
248 LazyResponseGenerator generator(run_loop3.QuitClosure()); | 222 LazyResponseGenerator generator(run_loop3.QuitClosure()); |
249 router1.set_incoming_receiver(&generator); | 223 router1.set_incoming_receiver(&generator); |
250 router1.set_incoming_receiver(&generator); | 224 router1.set_incoming_receiver(&generator); |
251 | 225 |
(...skipping 29 matching lines...) Expand all Loading... |
281 } | 255 } |
282 | 256 |
283 TEST_F(RouterTest, LateResponse) { | 257 TEST_F(RouterTest, LateResponse) { |
284 // Test that things won't blow up if we try to send a message to a | 258 // Test that things won't blow up if we try to send a message to a |
285 // MessageReceiver, which was given to us via AcceptWithResponder, | 259 // MessageReceiver, which was given to us via AcceptWithResponder, |
286 // after the router has gone away. | 260 // after the router has gone away. |
287 | 261 |
288 base::RunLoop run_loop; | 262 base::RunLoop run_loop; |
289 LazyResponseGenerator generator(run_loop.QuitClosure()); | 263 LazyResponseGenerator generator(run_loop.QuitClosure()); |
290 { | 264 { |
291 internal::Router router0(std::move(handle0_), FilterChain(), | 265 internal::Router router0(std::move(handle0_), FilterChain(), false, |
292 false, base::ThreadTaskRunnerHandle::Get()); | 266 base::ThreadTaskRunnerHandle::Get(), 0u); |
293 internal::Router router1(std::move(handle1_), FilterChain(), | 267 internal::Router router1(std::move(handle1_), FilterChain(), false, |
294 false, base::ThreadTaskRunnerHandle::Get()); | 268 base::ThreadTaskRunnerHandle::Get(), 0u); |
295 | 269 |
296 router1.set_incoming_receiver(&generator); | 270 router1.set_incoming_receiver(&generator); |
297 | 271 |
298 Message request; | 272 Message request; |
299 AllocRequestMessage(1, "hello", &request); | 273 AllocRequestMessage(1, "hello", &request); |
300 | 274 |
301 MessageQueue message_queue; | 275 MessageQueue message_queue; |
302 router0.AcceptWithResponder(&request, | 276 router0.AcceptWithResponder(&request, |
303 new MessageAccumulator(&message_queue)); | 277 new MessageAccumulator(&message_queue)); |
304 | 278 |
305 run_loop.Run(); | 279 run_loop.Run(); |
306 | 280 |
307 EXPECT_TRUE(generator.has_responder()); | 281 EXPECT_TRUE(generator.has_responder()); |
308 } | 282 } |
309 | 283 |
310 EXPECT_FALSE(generator.responder_is_valid()); | 284 EXPECT_FALSE(generator.responder_is_valid()); |
311 generator.CompleteWithResponse(); // This should end up doing nothing. | 285 generator.CompleteWithResponse(); // This should end up doing nothing. |
312 } | 286 } |
313 | 287 |
314 } // namespace | 288 } // namespace |
315 } // namespace test | 289 } // namespace test |
316 } // namespace mojo | 290 } // namespace mojo |
OLD | NEW |