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

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

Issue 1552983003: Fix a bunch of mojo_public_*_unittests with the new EDK. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments Created 4 years, 11 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 "mojo/message_pump/message_pump_mojo.h" 11 #include "mojo/message_pump/message_pump_mojo.h"
11 #include "mojo/public/cpp/bindings/tests/message_queue.h" 12 #include "mojo/public/cpp/bindings/tests/message_queue.h"
12 #include "mojo/public/cpp/bindings/tests/router_test_util.h" 13 #include "mojo/public/cpp/bindings/tests/router_test_util.h"
13 #include "mojo/public/cpp/system/macros.h" 14 #include "mojo/public/cpp/system/macros.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 namespace mojo { 17 namespace mojo {
17 namespace test { 18 namespace test {
18 namespace { 19 namespace {
19 20
(...skipping 21 matching lines...) Expand all
41 internal::Router router0(std::move(handle0_), internal::FilterChain()); 42 internal::Router router0(std::move(handle0_), internal::FilterChain());
42 internal::Router router1(std::move(handle1_), internal::FilterChain()); 43 internal::Router router1(std::move(handle1_), internal::FilterChain());
43 44
44 ResponseGenerator generator; 45 ResponseGenerator generator;
45 router1.set_incoming_receiver(&generator); 46 router1.set_incoming_receiver(&generator);
46 47
47 Message request; 48 Message request;
48 AllocRequestMessage(1, "hello", &request); 49 AllocRequestMessage(1, "hello", &request);
49 50
50 MessageQueue message_queue; 51 MessageQueue message_queue;
51 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); 52 base::RunLoop run_loop;
53 router0.AcceptWithResponder(
54 &request, new MessageAccumulator(&message_queue, run_loop.QuitClosure()));
52 55
53 PumpMessages(); 56 run_loop.Run();
54 57
55 EXPECT_FALSE(message_queue.IsEmpty()); 58 EXPECT_FALSE(message_queue.IsEmpty());
56 59
57 Message response; 60 Message response;
58 message_queue.Pop(&response); 61 message_queue.Pop(&response);
59 62
60 EXPECT_EQ(std::string("hello world!"), 63 EXPECT_EQ(std::string("hello world!"),
61 std::string(reinterpret_cast<const char*>(response.payload()))); 64 std::string(reinterpret_cast<const char*>(response.payload())));
62 65
63 // Send a second message on the pipe. 66 // Send a second message on the pipe.
64 Message request2; 67 Message request2;
65 AllocRequestMessage(1, "hello again", &request2); 68 AllocRequestMessage(1, "hello again", &request2);
66 69
67 router0.AcceptWithResponder(&request2, 70 base::RunLoop run_loop2;
68 new MessageAccumulator(&message_queue)); 71 router0.AcceptWithResponder(
72 &request2,
73 new MessageAccumulator(&message_queue, run_loop2.QuitClosure()));
69 74
70 PumpMessages(); 75 run_loop2.Run();
71 76
72 EXPECT_FALSE(message_queue.IsEmpty()); 77 EXPECT_FALSE(message_queue.IsEmpty());
73 78
74 message_queue.Pop(&response); 79 message_queue.Pop(&response);
75 80
76 EXPECT_EQ(std::string("hello again world!"), 81 EXPECT_EQ(std::string("hello again world!"),
77 std::string(reinterpret_cast<const char*>(response.payload()))); 82 std::string(reinterpret_cast<const char*>(response.payload())));
78 } 83 }
79 84
80 TEST_F(RouterTest, BasicRequestResponse_Synchronous) { 85 TEST_F(RouterTest, BasicRequestResponse_Synchronous) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 internal::Router router0(std::move(handle0_), internal::FilterChain()); 128 internal::Router router0(std::move(handle0_), internal::FilterChain());
124 internal::Router router1(std::move(handle1_), internal::FilterChain()); 129 internal::Router router1(std::move(handle1_), internal::FilterChain());
125 130
126 // 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
127 // an error as a result of sending a message. 132 // an error as a result of sending a message.
128 133
129 Message request; 134 Message request;
130 AllocRequestMessage(1, "hello", &request); 135 AllocRequestMessage(1, "hello", &request);
131 136
132 MessageQueue message_queue; 137 MessageQueue message_queue;
138 base::RunLoop run_loop, run_loop2;
139 router0.set_connection_error_handler(run_loop.QuitClosure());
140 router1.set_connection_error_handler(run_loop2.QuitClosure());
133 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); 141 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue));
134 142
135 PumpMessages(); 143 run_loop.Run();
144 run_loop2.Run();
136 145
137 EXPECT_TRUE(router0.encountered_error()); 146 EXPECT_TRUE(router0.encountered_error());
138 EXPECT_TRUE(router1.encountered_error()); 147 EXPECT_TRUE(router1.encountered_error());
139 EXPECT_TRUE(message_queue.IsEmpty()); 148 EXPECT_TRUE(message_queue.IsEmpty());
140 } 149 }
141 150
142 // Tests Router using the LazyResponseGenerator. The responses will not be 151 // Tests Router using the LazyResponseGenerator. The responses will not be
143 // sent until after the requests have been accepted. 152 // sent until after the requests have been accepted.
144 TEST_F(RouterTest, LazyResponses) { 153 TEST_F(RouterTest, LazyResponses) {
145 internal::Router router0(std::move(handle0_), internal::FilterChain()); 154 internal::Router router0(std::move(handle0_), internal::FilterChain());
146 internal::Router router1(std::move(handle1_), internal::FilterChain()); 155 internal::Router router1(std::move(handle1_), internal::FilterChain());
147 156
148 LazyResponseGenerator generator; 157 base::RunLoop run_loop;
158 LazyResponseGenerator generator(run_loop.QuitClosure());
149 router1.set_incoming_receiver(&generator); 159 router1.set_incoming_receiver(&generator);
150 160
151 Message request; 161 Message request;
152 AllocRequestMessage(1, "hello", &request); 162 AllocRequestMessage(1, "hello", &request);
153 163
154 MessageQueue message_queue; 164 MessageQueue message_queue;
155 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); 165 base::RunLoop run_loop2;
156 PumpMessages(); 166 router0.AcceptWithResponder(
167 &request,
168 new MessageAccumulator(&message_queue, run_loop2.QuitClosure()));
169 run_loop.Run();
157 170
158 // The request has been received but the response has not been sent yet. 171 // The request has been received but the response has not been sent yet.
159 EXPECT_TRUE(message_queue.IsEmpty()); 172 EXPECT_TRUE(message_queue.IsEmpty());
160 173
161 // Send the response. 174 // Send the response.
162 EXPECT_TRUE(generator.responder_is_valid()); 175 EXPECT_TRUE(generator.responder_is_valid());
163 generator.CompleteWithResponse(); 176 generator.CompleteWithResponse();
164 PumpMessages(); 177 run_loop2.Run();
165 178
166 // Check the response. 179 // Check the response.
167 EXPECT_FALSE(message_queue.IsEmpty()); 180 EXPECT_FALSE(message_queue.IsEmpty());
168 Message response; 181 Message response;
169 message_queue.Pop(&response); 182 message_queue.Pop(&response);
170 EXPECT_EQ(std::string("hello world!"), 183 EXPECT_EQ(std::string("hello world!"),
171 std::string(reinterpret_cast<const char*>(response.payload()))); 184 std::string(reinterpret_cast<const char*>(response.payload())));
172 185
173 // Send a second message on the pipe. 186 // Send a second message on the pipe.
187 base::RunLoop run_loop3;
188 LazyResponseGenerator generator2(run_loop3.QuitClosure());
189
190 router1.set_incoming_receiver(&generator2);
174 Message request2; 191 Message request2;
175 AllocRequestMessage(1, "hello again", &request2); 192 AllocRequestMessage(1, "hello again", &request2);
176 193
177 router0.AcceptWithResponder(&request2, 194 base::RunLoop run_loop4;
178 new MessageAccumulator(&message_queue)); 195 router0.AcceptWithResponder(
179 PumpMessages(); 196 &request2,
197 new MessageAccumulator(&message_queue, run_loop4.QuitClosure()));
198 run_loop3.Run();
180 199
181 // The request has been received but the response has not been sent yet. 200 // The request has been received but the response has not been sent yet.
182 EXPECT_TRUE(message_queue.IsEmpty()); 201 EXPECT_TRUE(message_queue.IsEmpty());
183 202
184 // Send the second response. 203 // Send the second response.
185 EXPECT_TRUE(generator.responder_is_valid()); 204 EXPECT_TRUE(generator2.responder_is_valid());
186 generator.CompleteWithResponse(); 205 generator2.CompleteWithResponse();
187 PumpMessages(); 206 run_loop4.Run();
188 207
189 // Check the second response. 208 // Check the second response.
190 EXPECT_FALSE(message_queue.IsEmpty()); 209 EXPECT_FALSE(message_queue.IsEmpty());
191 message_queue.Pop(&response); 210 message_queue.Pop(&response);
192 EXPECT_EQ(std::string("hello again world!"), 211 EXPECT_EQ(std::string("hello again world!"),
193 std::string(reinterpret_cast<const char*>(response.payload()))); 212 std::string(reinterpret_cast<const char*>(response.payload())));
194 } 213 }
195 214
196 // Tests that if the receiving application destroys the responder_ without 215 // Tests that if the receiving application destroys the responder_ without
197 // 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,
198 // 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.
199 TEST_F(RouterTest, MissingResponses) { 218 TEST_F(RouterTest, MissingResponses) {
219 base::RunLoop run_loop0, run_loop1;
200 internal::Router router0(std::move(handle0_), internal::FilterChain()); 220 internal::Router router0(std::move(handle0_), internal::FilterChain());
201 bool error_handler_called0 = false; 221 bool error_handler_called0 = false;
202 router0.set_connection_error_handler( 222 router0.set_connection_error_handler(
203 [&error_handler_called0]() { error_handler_called0 = true; }); 223 [&error_handler_called0, &run_loop0]() {
224 error_handler_called0 = true;
225 run_loop0.Quit();
226 });
204 227
205 internal::Router router1(std::move(handle1_), internal::FilterChain()); 228 internal::Router router1(std::move(handle1_), internal::FilterChain());
206 bool error_handler_called1 = false; 229 bool error_handler_called1 = false;
207 router1.set_connection_error_handler( 230 router1.set_connection_error_handler(
208 [&error_handler_called1]() { error_handler_called1 = true; }); 231 [&error_handler_called1, &run_loop1]() {
232 error_handler_called1 = true;
233 run_loop1.Quit();
234 });
209 235
210 LazyResponseGenerator generator; 236 base::RunLoop run_loop3;
237 LazyResponseGenerator generator(run_loop3.QuitClosure());
238 router1.set_incoming_receiver(&generator);
211 router1.set_incoming_receiver(&generator); 239 router1.set_incoming_receiver(&generator);
212 240
213 Message request; 241 Message request;
214 AllocRequestMessage(1, "hello", &request); 242 AllocRequestMessage(1, "hello", &request);
215 243
216 MessageQueue message_queue; 244 MessageQueue message_queue;
217 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); 245 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue));
218 PumpMessages(); 246 run_loop3.Run();
219 247
220 // The request has been received but no response has been sent. 248 // The request has been received but no response has been sent.
221 EXPECT_TRUE(message_queue.IsEmpty()); 249 EXPECT_TRUE(message_queue.IsEmpty());
222 250
223 // Destroy the responder MessagerReceiver but don't send any response. 251 // Destroy the responder MessagerReceiver but don't send any response.
224 generator.CompleteWithoutResponse(); 252 generator.CompleteWithoutResponse();
225 PumpMessages(); 253 run_loop0.Run();
254 run_loop1.Run();
226 255
227 // Check that no response was received. 256 // Check that no response was received.
228 EXPECT_TRUE(message_queue.IsEmpty()); 257 EXPECT_TRUE(message_queue.IsEmpty());
229 258
230 // Connection error handler is called at both sides. 259 // Connection error handler is called at both sides.
231 EXPECT_TRUE(error_handler_called0); 260 EXPECT_TRUE(error_handler_called0);
232 EXPECT_TRUE(error_handler_called1); 261 EXPECT_TRUE(error_handler_called1);
233 262
234 // The error flag is set at both sides. 263 // The error flag is set at both sides.
235 EXPECT_TRUE(router0.encountered_error()); 264 EXPECT_TRUE(router0.encountered_error());
236 EXPECT_TRUE(router1.encountered_error()); 265 EXPECT_TRUE(router1.encountered_error());
237 266
238 // The message pipe handle is valid at both sides. 267 // The message pipe handle is valid at both sides.
239 EXPECT_TRUE(router0.is_valid()); 268 EXPECT_TRUE(router0.is_valid());
240 EXPECT_TRUE(router1.is_valid()); 269 EXPECT_TRUE(router1.is_valid());
241 } 270 }
242 271
243 TEST_F(RouterTest, LateResponse) { 272 TEST_F(RouterTest, LateResponse) {
244 // 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
245 // MessageReceiver, which was given to us via AcceptWithResponder, 274 // MessageReceiver, which was given to us via AcceptWithResponder,
246 // after the router has gone away. 275 // after the router has gone away.
247 276
248 LazyResponseGenerator generator; 277 base::RunLoop run_loop;
278 LazyResponseGenerator generator(run_loop.QuitClosure());
249 { 279 {
250 internal::Router router0(std::move(handle0_), internal::FilterChain()); 280 internal::Router router0(std::move(handle0_), internal::FilterChain());
251 internal::Router router1(std::move(handle1_), internal::FilterChain()); 281 internal::Router router1(std::move(handle1_), internal::FilterChain());
252 282
253 router1.set_incoming_receiver(&generator); 283 router1.set_incoming_receiver(&generator);
254 284
255 Message request; 285 Message request;
256 AllocRequestMessage(1, "hello", &request); 286 AllocRequestMessage(1, "hello", &request);
257 287
258 MessageQueue message_queue; 288 MessageQueue message_queue;
259 router0.AcceptWithResponder(&request, 289 router0.AcceptWithResponder(&request,
260 new MessageAccumulator(&message_queue)); 290 new MessageAccumulator(&message_queue));
261 291
262 PumpMessages(); 292 run_loop.Run();
263 293
264 EXPECT_TRUE(generator.has_responder()); 294 EXPECT_TRUE(generator.has_responder());
265 } 295 }
266 296
267 EXPECT_FALSE(generator.responder_is_valid()); 297 EXPECT_FALSE(generator.responder_is_valid());
268 generator.CompleteWithResponse(); // This should end up doing nothing. 298 generator.CompleteWithResponse(); // This should end up doing nothing.
269 } 299 }
270 300
271 } // namespace 301 } // namespace
272 } // namespace test 302 } // namespace test
273 } // namespace mojo 303 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/router_test_util.cc ('k') | mojo/public/cpp/bindings/tests/union_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698