Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: mojo/public/cpp/bindings/tests/router_unittest.cc

Issue 1723673002: Reland "Mojo C++ bindings: support sync methods - part 2" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 21 matching lines...) Expand all
32 32
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_), internal::FilterChain()); 42 internal::Router router0(std::move(handle0_), internal::FilterChain(), false);
43 internal::Router router1(std::move(handle1_), internal::FilterChain()); 43 internal::Router router1(std::move(handle1_), internal::FilterChain(), false);
44 44
45 ResponseGenerator generator; 45 ResponseGenerator generator;
46 router1.set_incoming_receiver(&generator); 46 router1.set_incoming_receiver(&generator);
47 47
48 Message request; 48 Message request;
49 AllocRequestMessage(1, "hello", &request); 49 AllocRequestMessage(1, "hello", &request);
50 50
51 MessageQueue message_queue; 51 MessageQueue message_queue;
52 base::RunLoop run_loop; 52 base::RunLoop run_loop;
53 router0.AcceptWithResponder( 53 router0.AcceptWithResponder(
(...skipping 22 matching lines...) Expand all
76 76
77 EXPECT_FALSE(message_queue.IsEmpty()); 77 EXPECT_FALSE(message_queue.IsEmpty());
78 78
79 message_queue.Pop(&response); 79 message_queue.Pop(&response);
80 80
81 EXPECT_EQ(std::string("hello again world!"), 81 EXPECT_EQ(std::string("hello again world!"),
82 std::string(reinterpret_cast<const char*>(response.payload()))); 82 std::string(reinterpret_cast<const char*>(response.payload())));
83 } 83 }
84 84
85 TEST_F(RouterTest, BasicRequestResponse_Synchronous) { 85 TEST_F(RouterTest, BasicRequestResponse_Synchronous) {
86 internal::Router router0(std::move(handle0_), internal::FilterChain()); 86 internal::Router router0(std::move(handle0_), internal::FilterChain(), false);
87 internal::Router router1(std::move(handle1_), internal::FilterChain()); 87 internal::Router router1(std::move(handle1_), internal::FilterChain(), false);
88 88
89 ResponseGenerator generator; 89 ResponseGenerator generator;
90 router1.set_incoming_receiver(&generator); 90 router1.set_incoming_receiver(&generator);
91 91
92 Message request; 92 Message request;
93 AllocRequestMessage(1, "hello", &request); 93 AllocRequestMessage(1, "hello", &request);
94 94
95 MessageQueue message_queue; 95 MessageQueue message_queue;
96 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); 96 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue));
97 97
(...skipping 20 matching lines...) Expand all
118 118
119 EXPECT_FALSE(message_queue.IsEmpty()); 119 EXPECT_FALSE(message_queue.IsEmpty());
120 120
121 message_queue.Pop(&response); 121 message_queue.Pop(&response);
122 122
123 EXPECT_EQ(std::string("hello again world!"), 123 EXPECT_EQ(std::string("hello again world!"),
124 std::string(reinterpret_cast<const char*>(response.payload()))); 124 std::string(reinterpret_cast<const char*>(response.payload())));
125 } 125 }
126 126
127 TEST_F(RouterTest, RequestWithNoReceiver) { 127 TEST_F(RouterTest, RequestWithNoReceiver) {
128 internal::Router router0(std::move(handle0_), internal::FilterChain()); 128 internal::Router router0(std::move(handle0_), internal::FilterChain(), false);
129 internal::Router router1(std::move(handle1_), internal::FilterChain()); 129 internal::Router router1(std::move(handle1_), internal::FilterChain(), false);
130 130
131 // Without an incoming receiver set on router1, we expect router0 to observe 131 // Without an incoming receiver set on router1, we expect router0 to observe
132 // an error as a result of sending a message. 132 // an error as a result of sending a message.
133 133
134 Message request; 134 Message request;
135 AllocRequestMessage(1, "hello", &request); 135 AllocRequestMessage(1, "hello", &request);
136 136
137 MessageQueue message_queue; 137 MessageQueue message_queue;
138 base::RunLoop run_loop, run_loop2; 138 base::RunLoop run_loop, run_loop2;
139 router0.set_connection_error_handler(run_loop.QuitClosure()); 139 router0.set_connection_error_handler(run_loop.QuitClosure());
140 router1.set_connection_error_handler(run_loop2.QuitClosure()); 140 router1.set_connection_error_handler(run_loop2.QuitClosure());
141 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); 141 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue));
142 142
143 run_loop.Run(); 143 run_loop.Run();
144 run_loop2.Run(); 144 run_loop2.Run();
145 145
146 EXPECT_TRUE(router0.encountered_error()); 146 EXPECT_TRUE(router0.encountered_error());
147 EXPECT_TRUE(router1.encountered_error()); 147 EXPECT_TRUE(router1.encountered_error());
148 EXPECT_TRUE(message_queue.IsEmpty()); 148 EXPECT_TRUE(message_queue.IsEmpty());
149 } 149 }
150 150
151 // Tests Router using the LazyResponseGenerator. The responses will not be 151 // Tests Router using the LazyResponseGenerator. The responses will not be
152 // sent until after the requests have been accepted. 152 // sent until after the requests have been accepted.
153 TEST_F(RouterTest, LazyResponses) { 153 TEST_F(RouterTest, LazyResponses) {
154 internal::Router router0(std::move(handle0_), internal::FilterChain()); 154 internal::Router router0(std::move(handle0_), internal::FilterChain(), false);
155 internal::Router router1(std::move(handle1_), internal::FilterChain()); 155 internal::Router router1(std::move(handle1_), internal::FilterChain(), false);
156 156
157 base::RunLoop run_loop; 157 base::RunLoop run_loop;
158 LazyResponseGenerator generator(run_loop.QuitClosure()); 158 LazyResponseGenerator generator(run_loop.QuitClosure());
159 router1.set_incoming_receiver(&generator); 159 router1.set_incoming_receiver(&generator);
160 160
161 Message request; 161 Message request;
162 AllocRequestMessage(1, "hello", &request); 162 AllocRequestMessage(1, "hello", &request);
163 163
164 MessageQueue message_queue; 164 MessageQueue message_queue;
165 base::RunLoop run_loop2; 165 base::RunLoop run_loop2;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 message_queue.Pop(&response); 210 message_queue.Pop(&response);
211 EXPECT_EQ(std::string("hello again world!"), 211 EXPECT_EQ(std::string("hello again world!"),
212 std::string(reinterpret_cast<const char*>(response.payload()))); 212 std::string(reinterpret_cast<const char*>(response.payload())));
213 } 213 }
214 214
215 // Tests that if the receiving application destroys the responder_ without 215 // Tests that if the receiving application destroys the responder_ without
216 // sending a response, then we trigger connection error at both sides. Moreover, 216 // sending a response, then we trigger connection error at both sides. Moreover,
217 // both sides still appear to have a valid message pipe handle bound. 217 // both sides still appear to have a valid message pipe handle bound.
218 TEST_F(RouterTest, MissingResponses) { 218 TEST_F(RouterTest, MissingResponses) {
219 base::RunLoop run_loop0, run_loop1; 219 base::RunLoop run_loop0, run_loop1;
220 internal::Router router0(std::move(handle0_), internal::FilterChain()); 220 internal::Router router0(std::move(handle0_), internal::FilterChain(), false);
221 bool error_handler_called0 = false; 221 bool error_handler_called0 = false;
222 router0.set_connection_error_handler( 222 router0.set_connection_error_handler(
223 [&error_handler_called0, &run_loop0]() { 223 [&error_handler_called0, &run_loop0]() {
224 error_handler_called0 = true; 224 error_handler_called0 = true;
225 run_loop0.Quit(); 225 run_loop0.Quit();
226 }); 226 });
227 227
228 internal::Router router1(std::move(handle1_), internal::FilterChain()); 228 internal::Router router1(std::move(handle1_), internal::FilterChain(), false);
229 bool error_handler_called1 = false; 229 bool error_handler_called1 = false;
230 router1.set_connection_error_handler( 230 router1.set_connection_error_handler(
231 [&error_handler_called1, &run_loop1]() { 231 [&error_handler_called1, &run_loop1]() {
232 error_handler_called1 = true; 232 error_handler_called1 = true;
233 run_loop1.Quit(); 233 run_loop1.Quit();
234 }); 234 });
235 235
236 base::RunLoop run_loop3; 236 base::RunLoop run_loop3;
237 LazyResponseGenerator generator(run_loop3.QuitClosure()); 237 LazyResponseGenerator generator(run_loop3.QuitClosure());
238 router1.set_incoming_receiver(&generator); 238 router1.set_incoming_receiver(&generator);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 } 270 }
271 271
272 TEST_F(RouterTest, LateResponse) { 272 TEST_F(RouterTest, LateResponse) {
273 // Test that things won't blow up if we try to send a message to a 273 // Test that things won't blow up if we try to send a message to a
274 // MessageReceiver, which was given to us via AcceptWithResponder, 274 // MessageReceiver, which was given to us via AcceptWithResponder,
275 // after the router has gone away. 275 // after the router has gone away.
276 276
277 base::RunLoop run_loop; 277 base::RunLoop run_loop;
278 LazyResponseGenerator generator(run_loop.QuitClosure()); 278 LazyResponseGenerator generator(run_loop.QuitClosure());
279 { 279 {
280 internal::Router router0(std::move(handle0_), internal::FilterChain()); 280 internal::Router router0(std::move(handle0_), internal::FilterChain(),
281 internal::Router router1(std::move(handle1_), internal::FilterChain()); 281 false);
282 internal::Router router1(std::move(handle1_), internal::FilterChain(),
283 false);
282 284
283 router1.set_incoming_receiver(&generator); 285 router1.set_incoming_receiver(&generator);
284 286
285 Message request; 287 Message request;
286 AllocRequestMessage(1, "hello", &request); 288 AllocRequestMessage(1, "hello", &request);
287 289
288 MessageQueue message_queue; 290 MessageQueue message_queue;
289 router0.AcceptWithResponder(&request, 291 router0.AcceptWithResponder(&request,
290 new MessageAccumulator(&message_queue)); 292 new MessageAccumulator(&message_queue));
291 293
292 run_loop.Run(); 294 run_loop.Run();
293 295
294 EXPECT_TRUE(generator.has_responder()); 296 EXPECT_TRUE(generator.has_responder());
295 } 297 }
296 298
297 EXPECT_FALSE(generator.responder_is_valid()); 299 EXPECT_FALSE(generator.responder_is_valid());
298 generator.CompleteWithResponse(); // This should end up doing nothing. 300 generator.CompleteWithResponse(); // This should end up doing nothing.
299 } 301 }
300 302
301 } // namespace 303 } // namespace
302 } // namespace test 304 } // namespace test
303 } // namespace mojo 305 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/message.h ('k') | mojo/public/cpp/bindings/tests/sync_method_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698