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 <stdlib.h> | 5 #include <stdlib.h> |
6 #include <string.h> | 6 #include <string.h> |
7 | 7 |
8 #include "mojo/public/cpp/bindings/lib/message_builder.h" | 8 #include "mojo/public/cpp/bindings/lib/message_builder.h" |
9 #include "mojo/public/cpp/bindings/lib/message_queue.h" | |
10 #include "mojo/public/cpp/bindings/lib/router.h" | 9 #include "mojo/public/cpp/bindings/lib/router.h" |
| 10 #include "mojo/public/cpp/bindings/tests/message_queue.h" |
11 #include "mojo/public/cpp/environment/environment.h" | 11 #include "mojo/public/cpp/environment/environment.h" |
12 #include "mojo/public/cpp/system/macros.h" | 12 #include "mojo/public/cpp/system/macros.h" |
13 #include "mojo/public/cpp/utility/run_loop.h" | 13 #include "mojo/public/cpp/utility/run_loop.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace mojo { | 16 namespace mojo { |
17 namespace test { | 17 namespace test { |
18 namespace { | 18 namespace { |
19 | 19 |
20 void AllocRequestMessage(uint32_t name, const char* text, Message* message) { | 20 void AllocRequestMessage(uint32_t name, const char* text, Message* message) { |
21 size_t payload_size = strlen(text) + 1; // Plus null terminator. | 21 size_t payload_size = strlen(text) + 1; // Plus null terminator. |
22 internal::RequestMessageBuilder builder(name, payload_size); | 22 internal::RequestMessageBuilder builder(name, payload_size); |
23 memcpy(builder.buffer()->Allocate(payload_size), text, payload_size); | 23 memcpy(builder.buffer()->Allocate(payload_size), text, payload_size); |
24 builder.Finish(message); | 24 builder.Finish(message); |
25 } | 25 } |
26 | 26 |
27 void AllocResponseMessage(uint32_t name, | 27 void AllocResponseMessage(uint32_t name, |
28 const char* text, | 28 const char* text, |
29 uint64_t request_id, | 29 uint64_t request_id, |
30 Message* message) { | 30 Message* message) { |
31 size_t payload_size = strlen(text) + 1; // Plus null terminator. | 31 size_t payload_size = strlen(text) + 1; // Plus null terminator. |
32 internal::ResponseMessageBuilder builder(name, payload_size, request_id); | 32 internal::ResponseMessageBuilder builder(name, payload_size, request_id); |
33 memcpy(builder.buffer()->Allocate(payload_size), text, payload_size); | 33 memcpy(builder.buffer()->Allocate(payload_size), text, payload_size); |
34 builder.Finish(message); | 34 builder.Finish(message); |
35 } | 35 } |
36 | 36 |
37 class MessageAccumulator : public MessageReceiver { | 37 class MessageAccumulator : public MessageReceiver { |
38 public: | 38 public: |
39 explicit MessageAccumulator(internal::MessageQueue* queue) : queue_(queue) {} | 39 explicit MessageAccumulator(MessageQueue* queue) : queue_(queue) {} |
40 | 40 |
41 bool Accept(Message* message) override { | 41 bool Accept(Message* message) override { |
42 queue_->Push(message); | 42 queue_->Push(message); |
43 return true; | 43 return true; |
44 } | 44 } |
45 | 45 |
46 private: | 46 private: |
47 internal::MessageQueue* queue_; | 47 MessageQueue* queue_; |
48 }; | 48 }; |
49 | 49 |
50 class ResponseGenerator : public MessageReceiverWithResponderStatus { | 50 class ResponseGenerator : public MessageReceiverWithResponderStatus { |
51 public: | 51 public: |
52 ResponseGenerator() {} | 52 ResponseGenerator() {} |
53 | 53 |
54 bool Accept(Message* message) override { return false; } | 54 bool Accept(Message* message) override { return false; } |
55 | 55 |
56 bool AcceptWithResponder(Message* message, | 56 bool AcceptWithResponder(Message* message, |
57 MessageReceiverWithStatus* responder) override { | 57 MessageReceiverWithStatus* responder) override { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 TEST_F(RouterTest, BasicRequestResponse) { | 145 TEST_F(RouterTest, BasicRequestResponse) { |
146 internal::Router router0(handle0_.Pass(), internal::FilterChain()); | 146 internal::Router router0(handle0_.Pass(), internal::FilterChain()); |
147 internal::Router router1(handle1_.Pass(), internal::FilterChain()); | 147 internal::Router router1(handle1_.Pass(), internal::FilterChain()); |
148 | 148 |
149 ResponseGenerator generator; | 149 ResponseGenerator generator; |
150 router1.set_incoming_receiver(&generator); | 150 router1.set_incoming_receiver(&generator); |
151 | 151 |
152 Message request; | 152 Message request; |
153 AllocRequestMessage(1, "hello", &request); | 153 AllocRequestMessage(1, "hello", &request); |
154 | 154 |
155 internal::MessageQueue message_queue; | 155 MessageQueue message_queue; |
156 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 156 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
157 | 157 |
158 PumpMessages(); | 158 PumpMessages(); |
159 | 159 |
160 EXPECT_FALSE(message_queue.IsEmpty()); | 160 EXPECT_FALSE(message_queue.IsEmpty()); |
161 | 161 |
162 Message response; | 162 Message response; |
163 message_queue.Pop(&response); | 163 message_queue.Pop(&response); |
164 | 164 |
165 EXPECT_EQ(std::string("hello world!"), | 165 EXPECT_EQ(std::string("hello world!"), |
(...skipping 19 matching lines...) Expand all Loading... |
185 TEST_F(RouterTest, BasicRequestResponse_Synchronous) { | 185 TEST_F(RouterTest, BasicRequestResponse_Synchronous) { |
186 internal::Router router0(handle0_.Pass(), internal::FilterChain()); | 186 internal::Router router0(handle0_.Pass(), internal::FilterChain()); |
187 internal::Router router1(handle1_.Pass(), internal::FilterChain()); | 187 internal::Router router1(handle1_.Pass(), internal::FilterChain()); |
188 | 188 |
189 ResponseGenerator generator; | 189 ResponseGenerator generator; |
190 router1.set_incoming_receiver(&generator); | 190 router1.set_incoming_receiver(&generator); |
191 | 191 |
192 Message request; | 192 Message request; |
193 AllocRequestMessage(1, "hello", &request); | 193 AllocRequestMessage(1, "hello", &request); |
194 | 194 |
195 internal::MessageQueue message_queue; | 195 MessageQueue message_queue; |
196 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 196 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
197 | 197 |
198 router1.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 198 router1.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
199 router0.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 199 router0.WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
200 | 200 |
201 EXPECT_FALSE(message_queue.IsEmpty()); | 201 EXPECT_FALSE(message_queue.IsEmpty()); |
202 | 202 |
203 Message response; | 203 Message response; |
204 message_queue.Pop(&response); | 204 message_queue.Pop(&response); |
205 | 205 |
(...skipping 21 matching lines...) Expand all Loading... |
227 TEST_F(RouterTest, RequestWithNoReceiver) { | 227 TEST_F(RouterTest, RequestWithNoReceiver) { |
228 internal::Router router0(handle0_.Pass(), internal::FilterChain()); | 228 internal::Router router0(handle0_.Pass(), internal::FilterChain()); |
229 internal::Router router1(handle1_.Pass(), internal::FilterChain()); | 229 internal::Router router1(handle1_.Pass(), internal::FilterChain()); |
230 | 230 |
231 // Without an incoming receiver set on router1, we expect router0 to observe | 231 // Without an incoming receiver set on router1, we expect router0 to observe |
232 // an error as a result of sending a message. | 232 // an error as a result of sending a message. |
233 | 233 |
234 Message request; | 234 Message request; |
235 AllocRequestMessage(1, "hello", &request); | 235 AllocRequestMessage(1, "hello", &request); |
236 | 236 |
237 internal::MessageQueue message_queue; | 237 MessageQueue message_queue; |
238 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 238 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
239 | 239 |
240 PumpMessages(); | 240 PumpMessages(); |
241 | 241 |
242 EXPECT_TRUE(router0.encountered_error()); | 242 EXPECT_TRUE(router0.encountered_error()); |
243 EXPECT_TRUE(router1.encountered_error()); | 243 EXPECT_TRUE(router1.encountered_error()); |
244 EXPECT_TRUE(message_queue.IsEmpty()); | 244 EXPECT_TRUE(message_queue.IsEmpty()); |
245 } | 245 } |
246 | 246 |
247 // Tests Router using the LazyResponseGenerator. The responses will not be | 247 // Tests Router using the LazyResponseGenerator. The responses will not be |
248 // sent until after the requests have been accepted. | 248 // sent until after the requests have been accepted. |
249 TEST_F(RouterTest, LazyResponses) { | 249 TEST_F(RouterTest, LazyResponses) { |
250 internal::Router router0(handle0_.Pass(), internal::FilterChain()); | 250 internal::Router router0(handle0_.Pass(), internal::FilterChain()); |
251 internal::Router router1(handle1_.Pass(), internal::FilterChain()); | 251 internal::Router router1(handle1_.Pass(), internal::FilterChain()); |
252 | 252 |
253 LazyResponseGenerator generator; | 253 LazyResponseGenerator generator; |
254 router1.set_incoming_receiver(&generator); | 254 router1.set_incoming_receiver(&generator); |
255 | 255 |
256 Message request; | 256 Message request; |
257 AllocRequestMessage(1, "hello", &request); | 257 AllocRequestMessage(1, "hello", &request); |
258 | 258 |
259 internal::MessageQueue message_queue; | 259 MessageQueue message_queue; |
260 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 260 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
261 PumpMessages(); | 261 PumpMessages(); |
262 | 262 |
263 // The request has been received but the response has not been sent yet. | 263 // The request has been received but the response has not been sent yet. |
264 EXPECT_TRUE(message_queue.IsEmpty()); | 264 EXPECT_TRUE(message_queue.IsEmpty()); |
265 | 265 |
266 // Send the response. | 266 // Send the response. |
267 EXPECT_TRUE(generator.responder_is_valid()); | 267 EXPECT_TRUE(generator.responder_is_valid()); |
268 generator.CompleteWithResponse(); | 268 generator.CompleteWithResponse(); |
269 PumpMessages(); | 269 PumpMessages(); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 TEST_F(RouterTest, MissingResponses) { | 304 TEST_F(RouterTest, MissingResponses) { |
305 internal::Router router0(handle0_.Pass(), internal::FilterChain()); | 305 internal::Router router0(handle0_.Pass(), internal::FilterChain()); |
306 internal::Router router1(handle1_.Pass(), internal::FilterChain()); | 306 internal::Router router1(handle1_.Pass(), internal::FilterChain()); |
307 | 307 |
308 LazyResponseGenerator generator; | 308 LazyResponseGenerator generator; |
309 router1.set_incoming_receiver(&generator); | 309 router1.set_incoming_receiver(&generator); |
310 | 310 |
311 Message request; | 311 Message request; |
312 AllocRequestMessage(1, "hello", &request); | 312 AllocRequestMessage(1, "hello", &request); |
313 | 313 |
314 internal::MessageQueue message_queue; | 314 MessageQueue message_queue; |
315 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 315 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
316 PumpMessages(); | 316 PumpMessages(); |
317 | 317 |
318 // The request has been received but no response has been sent. | 318 // The request has been received but no response has been sent. |
319 EXPECT_TRUE(message_queue.IsEmpty()); | 319 EXPECT_TRUE(message_queue.IsEmpty()); |
320 | 320 |
321 // Destroy the responder MessagerReceiver but don't send any response. | 321 // Destroy the responder MessagerReceiver but don't send any response. |
322 // This should close the pipe. | 322 // This should close the pipe. |
323 generator.CompleteWithoutResponse(); | 323 generator.CompleteWithoutResponse(); |
324 PumpMessages(); | 324 PumpMessages(); |
(...skipping 22 matching lines...) Expand all Loading... |
347 LazyResponseGenerator generator; | 347 LazyResponseGenerator generator; |
348 { | 348 { |
349 internal::Router router0(handle0_.Pass(), internal::FilterChain()); | 349 internal::Router router0(handle0_.Pass(), internal::FilterChain()); |
350 internal::Router router1(handle1_.Pass(), internal::FilterChain()); | 350 internal::Router router1(handle1_.Pass(), internal::FilterChain()); |
351 | 351 |
352 router1.set_incoming_receiver(&generator); | 352 router1.set_incoming_receiver(&generator); |
353 | 353 |
354 Message request; | 354 Message request; |
355 AllocRequestMessage(1, "hello", &request); | 355 AllocRequestMessage(1, "hello", &request); |
356 | 356 |
357 internal::MessageQueue message_queue; | 357 MessageQueue message_queue; |
358 router0.AcceptWithResponder(&request, | 358 router0.AcceptWithResponder(&request, |
359 new MessageAccumulator(&message_queue)); | 359 new MessageAccumulator(&message_queue)); |
360 | 360 |
361 PumpMessages(); | 361 PumpMessages(); |
362 | 362 |
363 EXPECT_TRUE(generator.has_responder()); | 363 EXPECT_TRUE(generator.has_responder()); |
364 } | 364 } |
365 | 365 |
366 EXPECT_FALSE(generator.responder_is_valid()); | 366 EXPECT_FALSE(generator.responder_is_valid()); |
367 generator.CompleteWithResponse(); // This should end up doing nothing. | 367 generator.CompleteWithResponse(); // This should end up doing nothing. |
368 } | 368 } |
369 | 369 |
370 } // namespace | 370 } // namespace |
371 } // namespace test | 371 } // namespace test |
372 } // namespace mojo | 372 } // namespace mojo |
OLD | NEW |