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

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

Issue 2280483002: Add FlushForTesting to InterfacePtr and Binding. (Closed)
Patch Set: Created 4 years, 3 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/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
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
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 21 matching lines...) Expand all
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) { 131 TEST_F(RouterTest, RequestWithNoReceiver) {
132 internal::Router router0(std::move(handle0_), FilterChain(), false, 132 internal::Router router0(std::move(handle0_), FilterChain(), false,
133 base::ThreadTaskRunnerHandle::Get()); 133 base::ThreadTaskRunnerHandle::Get(), 0u);
134 internal::Router router1(std::move(handle1_), FilterChain(), false, 134 internal::Router router1(std::move(handle1_), FilterChain(), false,
135 base::ThreadTaskRunnerHandle::Get()); 135 base::ThreadTaskRunnerHandle::Get(), 0u);
136 136
137 // Without an incoming receiver set on router1, we expect router0 to observe 137 // Without an incoming receiver set on router1, we expect router0 to observe
138 // an error as a result of sending a message. 138 // an error as a result of sending a message.
139 139
140 Message request; 140 Message request;
141 AllocRequestMessage(1, "hello", &request); 141 AllocRequestMessage(1, "hello", &request);
142 142
143 MessageQueue message_queue; 143 MessageQueue message_queue;
144 base::RunLoop run_loop, run_loop2; 144 base::RunLoop run_loop, run_loop2;
145 router0.set_connection_error_handler(run_loop.QuitClosure()); 145 router0.set_connection_error_handler(run_loop.QuitClosure());
146 router1.set_connection_error_handler(run_loop2.QuitClosure()); 146 router1.set_connection_error_handler(run_loop2.QuitClosure());
147 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); 147 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue));
148 148
149 run_loop.Run(); 149 run_loop.Run();
150 run_loop2.Run(); 150 run_loop2.Run();
151 151
152 EXPECT_TRUE(router0.encountered_error()); 152 EXPECT_TRUE(router0.encountered_error());
153 EXPECT_TRUE(router1.encountered_error()); 153 EXPECT_TRUE(router1.encountered_error());
154 EXPECT_TRUE(message_queue.IsEmpty()); 154 EXPECT_TRUE(message_queue.IsEmpty());
155 } 155 }
156 156
157 // Tests Router using the LazyResponseGenerator. The responses will not be 157 // Tests Router using the LazyResponseGenerator. The responses will not be
158 // sent until after the requests have been accepted. 158 // sent until after the requests have been accepted.
159 TEST_F(RouterTest, LazyResponses) { 159 TEST_F(RouterTest, LazyResponses) {
160 internal::Router router0(std::move(handle0_), FilterChain(), false, 160 internal::Router router0(std::move(handle0_), FilterChain(), false,
161 base::ThreadTaskRunnerHandle::Get()); 161 base::ThreadTaskRunnerHandle::Get(), 0u);
162 internal::Router router1(std::move(handle1_), FilterChain(), false, 162 internal::Router router1(std::move(handle1_), FilterChain(), false,
163 base::ThreadTaskRunnerHandle::Get()); 163 base::ThreadTaskRunnerHandle::Get(), 0u);
164 164
165 base::RunLoop run_loop; 165 base::RunLoop run_loop;
166 LazyResponseGenerator generator(run_loop.QuitClosure()); 166 LazyResponseGenerator generator(run_loop.QuitClosure());
167 router1.set_incoming_receiver(&generator); 167 router1.set_incoming_receiver(&generator);
168 168
169 Message request; 169 Message request;
170 AllocRequestMessage(1, "hello", &request); 170 AllocRequestMessage(1, "hello", &request);
171 171
172 MessageQueue message_queue; 172 MessageQueue message_queue;
173 base::RunLoop run_loop2; 173 base::RunLoop run_loop2;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 *called = true; 224 *called = true;
225 callback.Run(); 225 callback.Run();
226 } 226 }
227 227
228 // Tests that if the receiving application destroys the responder_ without 228 // Tests that if the receiving application destroys the responder_ without
229 // sending a response, then we trigger connection error at both sides. Moreover, 229 // 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. 230 // both sides still appear to have a valid message pipe handle bound.
231 TEST_F(RouterTest, MissingResponses) { 231 TEST_F(RouterTest, MissingResponses) {
232 base::RunLoop run_loop0, run_loop1; 232 base::RunLoop run_loop0, run_loop1;
233 internal::Router router0(std::move(handle0_), FilterChain(), false, 233 internal::Router router0(std::move(handle0_), FilterChain(), false,
234 base::ThreadTaskRunnerHandle::Get()); 234 base::ThreadTaskRunnerHandle::Get(), 0u);
235 bool error_handler_called0 = false; 235 bool error_handler_called0 = false;
236 router0.set_connection_error_handler( 236 router0.set_connection_error_handler(
237 base::Bind(&ForwardErrorHandler, &error_handler_called0, 237 base::Bind(&ForwardErrorHandler, &error_handler_called0,
238 run_loop0.QuitClosure())); 238 run_loop0.QuitClosure()));
239 239
240 internal::Router router1(std::move(handle1_), FilterChain(), false, 240 internal::Router router1(std::move(handle1_), FilterChain(), false,
241 base::ThreadTaskRunnerHandle::Get()); 241 base::ThreadTaskRunnerHandle::Get(), 0u);
242 bool error_handler_called1 = false; 242 bool error_handler_called1 = false;
243 router1.set_connection_error_handler( 243 router1.set_connection_error_handler(
244 base::Bind(&ForwardErrorHandler, &error_handler_called1, 244 base::Bind(&ForwardErrorHandler, &error_handler_called1,
245 run_loop1.QuitClosure())); 245 run_loop1.QuitClosure()));
246 246
247 base::RunLoop run_loop3; 247 base::RunLoop run_loop3;
248 LazyResponseGenerator generator(run_loop3.QuitClosure()); 248 LazyResponseGenerator generator(run_loop3.QuitClosure());
249 router1.set_incoming_receiver(&generator); 249 router1.set_incoming_receiver(&generator);
250 router1.set_incoming_receiver(&generator); 250 router1.set_incoming_receiver(&generator);
251 251
(...skipping 29 matching lines...) Expand all
281 } 281 }
282 282
283 TEST_F(RouterTest, LateResponse) { 283 TEST_F(RouterTest, LateResponse) {
284 // Test that things won't blow up if we try to send a message to a 284 // 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, 285 // MessageReceiver, which was given to us via AcceptWithResponder,
286 // after the router has gone away. 286 // after the router has gone away.
287 287
288 base::RunLoop run_loop; 288 base::RunLoop run_loop;
289 LazyResponseGenerator generator(run_loop.QuitClosure()); 289 LazyResponseGenerator generator(run_loop.QuitClosure());
290 { 290 {
291 internal::Router router0(std::move(handle0_), FilterChain(), 291 internal::Router router0(std::move(handle0_), FilterChain(), false,
292 false, base::ThreadTaskRunnerHandle::Get()); 292 base::ThreadTaskRunnerHandle::Get(), 0u);
293 internal::Router router1(std::move(handle1_), FilterChain(), 293 internal::Router router1(std::move(handle1_), FilterChain(), false,
294 false, base::ThreadTaskRunnerHandle::Get()); 294 base::ThreadTaskRunnerHandle::Get(), 0u);
295 295
296 router1.set_incoming_receiver(&generator); 296 router1.set_incoming_receiver(&generator);
297 297
298 Message request; 298 Message request;
299 AllocRequestMessage(1, "hello", &request); 299 AllocRequestMessage(1, "hello", &request);
300 300
301 MessageQueue message_queue; 301 MessageQueue message_queue;
302 router0.AcceptWithResponder(&request, 302 router0.AcceptWithResponder(&request,
303 new MessageAccumulator(&message_queue)); 303 new MessageAccumulator(&message_queue));
304 304
305 run_loop.Run(); 305 run_loop.Run();
306 306
307 EXPECT_TRUE(generator.has_responder()); 307 EXPECT_TRUE(generator.has_responder());
308 } 308 }
309 309
310 EXPECT_FALSE(generator.responder_is_valid()); 310 EXPECT_FALSE(generator.responder_is_valid());
311 generator.CompleteWithResponse(); // This should end up doing nothing. 311 generator.CompleteWithResponse(); // This should end up doing nothing.
312 } 312 }
313 313
314 } // namespace 314 } // namespace
315 } // namespace test 315 } // namespace test
316 } // namespace mojo 316 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698