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/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 base::ThreadTaskRunnerHandle::Get()); | 42 base::ThreadTaskRunnerHandle::Get()); |
43 internal::Router router1(std::move(handle1_), internal::FilterChain(), false, | 43 internal::Router router1(std::move(handle1_), internal::FilterChain(), false, |
44 base::ThreadTaskRunnerHandle::Get()); | 44 base::ThreadTaskRunnerHandle::Get()); |
45 | 45 |
46 ResponseGenerator generator; | 46 ResponseGenerator generator; |
47 router1.set_incoming_receiver(&generator); | 47 router1.set_incoming_receiver(&generator); |
48 | 48 |
49 Message request; | 49 Message request; |
50 AllocRequestMessage(1, "hello", &request); | 50 AllocRequestMessage(1, "hello", &request); |
51 | 51 |
| 52 Error error; |
52 MessageQueue message_queue; | 53 MessageQueue message_queue; |
53 base::RunLoop run_loop; | 54 base::RunLoop run_loop; |
54 router0.AcceptWithResponder( | 55 router0.AcceptWithResponder( |
55 &request, new MessageAccumulator(&message_queue, run_loop.QuitClosure())); | 56 &request, new MessageAccumulator(&message_queue, run_loop.QuitClosure()), |
| 57 &error); |
56 | 58 |
57 run_loop.Run(); | 59 run_loop.Run(); |
58 | 60 |
59 EXPECT_FALSE(message_queue.IsEmpty()); | 61 EXPECT_FALSE(message_queue.IsEmpty()); |
60 | 62 |
61 Message response; | 63 Message response; |
62 message_queue.Pop(&response); | 64 message_queue.Pop(&response); |
63 | 65 |
64 EXPECT_EQ(std::string("hello world!"), | 66 EXPECT_EQ(std::string("hello world!"), |
65 std::string(reinterpret_cast<const char*>(response.payload()))); | 67 std::string(reinterpret_cast<const char*>(response.payload()))); |
66 | 68 |
67 // Send a second message on the pipe. | 69 // Send a second message on the pipe. |
68 Message request2; | 70 Message request2; |
69 AllocRequestMessage(1, "hello again", &request2); | 71 AllocRequestMessage(1, "hello again", &request2); |
70 | 72 |
71 base::RunLoop run_loop2; | 73 base::RunLoop run_loop2; |
72 router0.AcceptWithResponder( | 74 router0.AcceptWithResponder( |
73 &request2, | 75 &request2, |
74 new MessageAccumulator(&message_queue, run_loop2.QuitClosure())); | 76 new MessageAccumulator(&message_queue, run_loop2.QuitClosure()), |
| 77 &error); |
75 | 78 |
76 run_loop2.Run(); | 79 run_loop2.Run(); |
77 | 80 |
78 EXPECT_FALSE(message_queue.IsEmpty()); | 81 EXPECT_FALSE(message_queue.IsEmpty()); |
79 | 82 |
80 message_queue.Pop(&response); | 83 message_queue.Pop(&response); |
81 | 84 |
82 EXPECT_EQ(std::string("hello again world!"), | 85 EXPECT_EQ(std::string("hello again world!"), |
83 std::string(reinterpret_cast<const char*>(response.payload()))); | 86 std::string(reinterpret_cast<const char*>(response.payload()))); |
84 } | 87 } |
85 | 88 |
86 TEST_F(RouterTest, BasicRequestResponse_Synchronous) { | 89 TEST_F(RouterTest, BasicRequestResponse_Synchronous) { |
87 internal::Router router0(std::move(handle0_), internal::FilterChain(), false, | 90 internal::Router router0(std::move(handle0_), internal::FilterChain(), false, |
88 base::ThreadTaskRunnerHandle::Get()); | 91 base::ThreadTaskRunnerHandle::Get()); |
89 internal::Router router1(std::move(handle1_), internal::FilterChain(), false, | 92 internal::Router router1(std::move(handle1_), internal::FilterChain(), false, |
90 base::ThreadTaskRunnerHandle::Get()); | 93 base::ThreadTaskRunnerHandle::Get()); |
91 | 94 |
92 ResponseGenerator generator; | 95 ResponseGenerator generator; |
93 router1.set_incoming_receiver(&generator); | 96 router1.set_incoming_receiver(&generator); |
94 | 97 |
95 Message request; | 98 Message request; |
96 AllocRequestMessage(1, "hello", &request); | 99 AllocRequestMessage(1, "hello", &request); |
97 | 100 |
| 101 Error error; |
98 MessageQueue message_queue; | 102 MessageQueue message_queue; |
99 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 103 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue), |
| 104 &error); |
100 | 105 |
101 router1.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 106 router1.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
102 router0.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 107 router0.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
103 | 108 |
104 EXPECT_FALSE(message_queue.IsEmpty()); | 109 EXPECT_FALSE(message_queue.IsEmpty()); |
105 | 110 |
106 Message response; | 111 Message response; |
107 message_queue.Pop(&response); | 112 message_queue.Pop(&response); |
108 | 113 |
109 EXPECT_EQ(std::string("hello world!"), | 114 EXPECT_EQ(std::string("hello world!"), |
110 std::string(reinterpret_cast<const char*>(response.payload()))); | 115 std::string(reinterpret_cast<const char*>(response.payload()))); |
111 | 116 |
112 // Send a second message on the pipe. | 117 // Send a second message on the pipe. |
113 Message request2; | 118 Message request2; |
114 AllocRequestMessage(1, "hello again", &request2); | 119 AllocRequestMessage(1, "hello again", &request2); |
115 | 120 |
116 router0.AcceptWithResponder(&request2, | 121 router0.AcceptWithResponder(&request2, |
117 new MessageAccumulator(&message_queue)); | 122 new MessageAccumulator(&message_queue), &error); |
118 | 123 |
119 router1.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 124 router1.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
120 router0.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 125 router0.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
121 | 126 |
122 EXPECT_FALSE(message_queue.IsEmpty()); | 127 EXPECT_FALSE(message_queue.IsEmpty()); |
123 | 128 |
124 message_queue.Pop(&response); | 129 message_queue.Pop(&response); |
125 | 130 |
126 EXPECT_EQ(std::string("hello again world!"), | 131 EXPECT_EQ(std::string("hello again world!"), |
127 std::string(reinterpret_cast<const char*>(response.payload()))); | 132 std::string(reinterpret_cast<const char*>(response.payload()))); |
128 } | 133 } |
129 | 134 |
130 TEST_F(RouterTest, RequestWithNoReceiver) { | 135 TEST_F(RouterTest, RequestWithNoReceiver) { |
131 internal::Router router0(std::move(handle0_), internal::FilterChain(), false, | 136 internal::Router router0(std::move(handle0_), internal::FilterChain(), false, |
132 base::ThreadTaskRunnerHandle::Get()); | 137 base::ThreadTaskRunnerHandle::Get()); |
133 internal::Router router1(std::move(handle1_), internal::FilterChain(), false, | 138 internal::Router router1(std::move(handle1_), internal::FilterChain(), false, |
134 base::ThreadTaskRunnerHandle::Get()); | 139 base::ThreadTaskRunnerHandle::Get()); |
135 | 140 |
136 // Without an incoming receiver set on router1, we expect router0 to observe | 141 // Without an incoming receiver set on router1, we expect router0 to observe |
137 // an error as a result of sending a message. | 142 // an error as a result of sending a message. |
138 | 143 |
139 Message request; | 144 Message request; |
140 AllocRequestMessage(1, "hello", &request); | 145 AllocRequestMessage(1, "hello", &request); |
141 | 146 |
| 147 Error error; |
142 MessageQueue message_queue; | 148 MessageQueue message_queue; |
143 base::RunLoop run_loop, run_loop2; | 149 base::RunLoop run_loop, run_loop2; |
144 router0.set_connection_error_handler(run_loop.QuitClosure()); | 150 router0.set_connection_error_handler(run_loop.QuitClosure()); |
145 router1.set_connection_error_handler(run_loop2.QuitClosure()); | 151 router1.set_connection_error_handler(run_loop2.QuitClosure()); |
146 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 152 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue), |
| 153 &error); |
147 | 154 |
148 run_loop.Run(); | 155 run_loop.Run(); |
149 run_loop2.Run(); | 156 run_loop2.Run(); |
150 | 157 |
151 EXPECT_TRUE(router0.encountered_error()); | 158 EXPECT_TRUE(router0.encountered_error()); |
152 EXPECT_TRUE(router1.encountered_error()); | 159 EXPECT_TRUE(router1.encountered_error()); |
153 EXPECT_TRUE(message_queue.IsEmpty()); | 160 EXPECT_TRUE(message_queue.IsEmpty()); |
154 } | 161 } |
155 | 162 |
156 // Tests Router using the LazyResponseGenerator. The responses will not be | 163 // Tests Router using the LazyResponseGenerator. The responses will not be |
157 // sent until after the requests have been accepted. | 164 // sent until after the requests have been accepted. |
158 TEST_F(RouterTest, LazyResponses) { | 165 TEST_F(RouterTest, LazyResponses) { |
159 internal::Router router0(std::move(handle0_), internal::FilterChain(), false, | 166 internal::Router router0(std::move(handle0_), internal::FilterChain(), false, |
160 base::ThreadTaskRunnerHandle::Get()); | 167 base::ThreadTaskRunnerHandle::Get()); |
161 internal::Router router1(std::move(handle1_), internal::FilterChain(), false, | 168 internal::Router router1(std::move(handle1_), internal::FilterChain(), false, |
162 base::ThreadTaskRunnerHandle::Get()); | 169 base::ThreadTaskRunnerHandle::Get()); |
163 | 170 |
164 base::RunLoop run_loop; | 171 base::RunLoop run_loop; |
165 LazyResponseGenerator generator(run_loop.QuitClosure()); | 172 LazyResponseGenerator generator(run_loop.QuitClosure()); |
166 router1.set_incoming_receiver(&generator); | 173 router1.set_incoming_receiver(&generator); |
167 | 174 |
168 Message request; | 175 Message request; |
169 AllocRequestMessage(1, "hello", &request); | 176 AllocRequestMessage(1, "hello", &request); |
170 | 177 |
| 178 Error error; |
171 MessageQueue message_queue; | 179 MessageQueue message_queue; |
172 base::RunLoop run_loop2; | 180 base::RunLoop run_loop2; |
173 router0.AcceptWithResponder( | 181 router0.AcceptWithResponder( |
174 &request, | 182 &request, |
175 new MessageAccumulator(&message_queue, run_loop2.QuitClosure())); | 183 new MessageAccumulator(&message_queue, run_loop2.QuitClosure()), &error); |
176 run_loop.Run(); | 184 run_loop.Run(); |
177 | 185 |
178 // The request has been received but the response has not been sent yet. | 186 // The request has been received but the response has not been sent yet. |
179 EXPECT_TRUE(message_queue.IsEmpty()); | 187 EXPECT_TRUE(message_queue.IsEmpty()); |
180 | 188 |
181 // Send the response. | 189 // Send the response. |
182 EXPECT_TRUE(generator.responder_is_valid()); | 190 EXPECT_TRUE(generator.responder_is_valid()); |
183 generator.CompleteWithResponse(); | 191 generator.CompleteWithResponse(); |
184 run_loop2.Run(); | 192 run_loop2.Run(); |
185 | 193 |
186 // Check the response. | 194 // Check the response. |
187 EXPECT_FALSE(message_queue.IsEmpty()); | 195 EXPECT_FALSE(message_queue.IsEmpty()); |
188 Message response; | 196 Message response; |
189 message_queue.Pop(&response); | 197 message_queue.Pop(&response); |
190 EXPECT_EQ(std::string("hello world!"), | 198 EXPECT_EQ(std::string("hello world!"), |
191 std::string(reinterpret_cast<const char*>(response.payload()))); | 199 std::string(reinterpret_cast<const char*>(response.payload()))); |
192 | 200 |
193 // Send a second message on the pipe. | 201 // Send a second message on the pipe. |
194 base::RunLoop run_loop3; | 202 base::RunLoop run_loop3; |
195 LazyResponseGenerator generator2(run_loop3.QuitClosure()); | 203 LazyResponseGenerator generator2(run_loop3.QuitClosure()); |
196 | 204 |
197 router1.set_incoming_receiver(&generator2); | 205 router1.set_incoming_receiver(&generator2); |
198 Message request2; | 206 Message request2; |
199 AllocRequestMessage(1, "hello again", &request2); | 207 AllocRequestMessage(1, "hello again", &request2); |
200 | 208 |
201 base::RunLoop run_loop4; | 209 base::RunLoop run_loop4; |
202 router0.AcceptWithResponder( | 210 router0.AcceptWithResponder( |
203 &request2, | 211 &request2, |
204 new MessageAccumulator(&message_queue, run_loop4.QuitClosure())); | 212 new MessageAccumulator(&message_queue, run_loop4.QuitClosure()), &error); |
205 run_loop3.Run(); | 213 run_loop3.Run(); |
206 | 214 |
207 // The request has been received but the response has not been sent yet. | 215 // The request has been received but the response has not been sent yet. |
208 EXPECT_TRUE(message_queue.IsEmpty()); | 216 EXPECT_TRUE(message_queue.IsEmpty()); |
209 | 217 |
210 // Send the second response. | 218 // Send the second response. |
211 EXPECT_TRUE(generator2.responder_is_valid()); | 219 EXPECT_TRUE(generator2.responder_is_valid()); |
212 generator2.CompleteWithResponse(); | 220 generator2.CompleteWithResponse(); |
213 run_loop4.Run(); | 221 run_loop4.Run(); |
214 | 222 |
(...skipping 28 matching lines...) Expand all Loading... |
243 }); | 251 }); |
244 | 252 |
245 base::RunLoop run_loop3; | 253 base::RunLoop run_loop3; |
246 LazyResponseGenerator generator(run_loop3.QuitClosure()); | 254 LazyResponseGenerator generator(run_loop3.QuitClosure()); |
247 router1.set_incoming_receiver(&generator); | 255 router1.set_incoming_receiver(&generator); |
248 router1.set_incoming_receiver(&generator); | 256 router1.set_incoming_receiver(&generator); |
249 | 257 |
250 Message request; | 258 Message request; |
251 AllocRequestMessage(1, "hello", &request); | 259 AllocRequestMessage(1, "hello", &request); |
252 | 260 |
| 261 Error error; |
253 MessageQueue message_queue; | 262 MessageQueue message_queue; |
254 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 263 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue), |
| 264 &error); |
255 run_loop3.Run(); | 265 run_loop3.Run(); |
256 | 266 |
257 // The request has been received but no response has been sent. | 267 // The request has been received but no response has been sent. |
258 EXPECT_TRUE(message_queue.IsEmpty()); | 268 EXPECT_TRUE(message_queue.IsEmpty()); |
259 | 269 |
260 // Destroy the responder MessagerReceiver but don't send any response. | 270 // Destroy the responder MessagerReceiver but don't send any response. |
261 generator.CompleteWithoutResponse(); | 271 generator.CompleteWithoutResponse(); |
262 run_loop0.Run(); | 272 run_loop0.Run(); |
263 run_loop1.Run(); | 273 run_loop1.Run(); |
264 | 274 |
(...skipping 24 matching lines...) Expand all Loading... |
289 internal::Router router0(std::move(handle0_), internal::FilterChain(), | 299 internal::Router router0(std::move(handle0_), internal::FilterChain(), |
290 false, base::ThreadTaskRunnerHandle::Get()); | 300 false, base::ThreadTaskRunnerHandle::Get()); |
291 internal::Router router1(std::move(handle1_), internal::FilterChain(), | 301 internal::Router router1(std::move(handle1_), internal::FilterChain(), |
292 false, base::ThreadTaskRunnerHandle::Get()); | 302 false, base::ThreadTaskRunnerHandle::Get()); |
293 | 303 |
294 router1.set_incoming_receiver(&generator); | 304 router1.set_incoming_receiver(&generator); |
295 | 305 |
296 Message request; | 306 Message request; |
297 AllocRequestMessage(1, "hello", &request); | 307 AllocRequestMessage(1, "hello", &request); |
298 | 308 |
| 309 Error error; |
299 MessageQueue message_queue; | 310 MessageQueue message_queue; |
300 router0.AcceptWithResponder(&request, | 311 router0.AcceptWithResponder(&request, |
301 new MessageAccumulator(&message_queue)); | 312 new MessageAccumulator(&message_queue), |
| 313 &error); |
302 | 314 |
303 run_loop.Run(); | 315 run_loop.Run(); |
304 | 316 |
305 EXPECT_TRUE(generator.has_responder()); | 317 EXPECT_TRUE(generator.has_responder()); |
306 } | 318 } |
307 | 319 |
308 EXPECT_FALSE(generator.responder_is_valid()); | 320 EXPECT_FALSE(generator.responder_is_valid()); |
309 generator.CompleteWithResponse(); // This should end up doing nothing. | 321 generator.CompleteWithResponse(); // This should end up doing nothing. |
310 } | 322 } |
311 | 323 |
312 } // namespace | 324 } // namespace |
313 } // namespace test | 325 } // namespace test |
314 } // namespace mojo | 326 } // namespace mojo |
OLD | NEW |