OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/message_pump/handle_watcher.h" | 5 #include "mojo/message_pump/handle_watcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 | 145 |
146 INSTANTIATE_TEST_CASE_P( | 146 INSTANTIATE_TEST_CASE_P( |
147 MultipleMessageLoopConfigs, HandleWatcherTest, | 147 MultipleMessageLoopConfigs, HandleWatcherTest, |
148 testing::Values(MESSAGE_LOOP_CONFIG_DEFAULT, MESSAGE_LOOP_CONFIG_MOJO)); | 148 testing::Values(MESSAGE_LOOP_CONFIG_DEFAULT, MESSAGE_LOOP_CONFIG_MOJO)); |
149 | 149 |
150 // Trivial test case with a single handle to watch. | 150 // Trivial test case with a single handle to watch. |
151 TEST_P(HandleWatcherTest, SingleHandler) { | 151 TEST_P(HandleWatcherTest, SingleHandler) { |
152 MessagePipe test_pipe; | 152 MessagePipe test_pipe; |
153 ASSERT_TRUE(test_pipe.handle0.is_valid()); | 153 ASSERT_TRUE(test_pipe.handle0.is_valid()); |
154 CallbackHelper callback_helper; | 154 CallbackHelper callback_helper; |
155 HandleWatcher watcher; | 155 HandleWatcher watcher(0); |
156 callback_helper.Start(&watcher, test_pipe.handle0.get()); | 156 callback_helper.Start(&watcher, test_pipe.handle0.get()); |
157 RunUntilIdle(); | 157 RunUntilIdle(); |
158 EXPECT_FALSE(callback_helper.got_callback()); | 158 EXPECT_FALSE(callback_helper.got_callback()); |
159 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle1.get(), | 159 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle1.get(), |
160 std::string())); | 160 std::string())); |
161 callback_helper.RunUntilGotCallback(); | 161 callback_helper.RunUntilGotCallback(); |
162 EXPECT_TRUE(callback_helper.got_callback()); | 162 EXPECT_TRUE(callback_helper.got_callback()); |
163 } | 163 } |
164 | 164 |
165 // Creates three handles and notfies them in reverse order ensuring each one is | 165 // Creates three handles and notfies them in reverse order ensuring each one is |
166 // notified appropriately. | 166 // notified appropriately. |
167 TEST_P(HandleWatcherTest, ThreeHandles) { | 167 TEST_P(HandleWatcherTest, ThreeHandles) { |
168 MessagePipe test_pipe1; | 168 MessagePipe test_pipe1; |
169 MessagePipe test_pipe2; | 169 MessagePipe test_pipe2; |
170 MessagePipe test_pipe3; | 170 MessagePipe test_pipe3; |
171 CallbackHelper callback_helper1; | 171 CallbackHelper callback_helper1; |
172 CallbackHelper callback_helper2; | 172 CallbackHelper callback_helper2; |
173 CallbackHelper callback_helper3; | 173 CallbackHelper callback_helper3; |
174 ASSERT_TRUE(test_pipe1.handle0.is_valid()); | 174 ASSERT_TRUE(test_pipe1.handle0.is_valid()); |
175 ASSERT_TRUE(test_pipe2.handle0.is_valid()); | 175 ASSERT_TRUE(test_pipe2.handle0.is_valid()); |
176 ASSERT_TRUE(test_pipe3.handle0.is_valid()); | 176 ASSERT_TRUE(test_pipe3.handle0.is_valid()); |
177 | 177 |
178 HandleWatcher watcher1; | 178 HandleWatcher watcher1(0); |
179 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); | 179 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); |
180 RunUntilIdle(); | 180 RunUntilIdle(); |
181 EXPECT_FALSE(callback_helper1.got_callback()); | 181 EXPECT_FALSE(callback_helper1.got_callback()); |
182 EXPECT_FALSE(callback_helper2.got_callback()); | 182 EXPECT_FALSE(callback_helper2.got_callback()); |
183 EXPECT_FALSE(callback_helper3.got_callback()); | 183 EXPECT_FALSE(callback_helper3.got_callback()); |
184 | 184 |
185 HandleWatcher watcher2; | 185 HandleWatcher watcher2(0); |
186 callback_helper2.Start(&watcher2, test_pipe2.handle0.get()); | 186 callback_helper2.Start(&watcher2, test_pipe2.handle0.get()); |
187 RunUntilIdle(); | 187 RunUntilIdle(); |
188 EXPECT_FALSE(callback_helper1.got_callback()); | 188 EXPECT_FALSE(callback_helper1.got_callback()); |
189 EXPECT_FALSE(callback_helper2.got_callback()); | 189 EXPECT_FALSE(callback_helper2.got_callback()); |
190 EXPECT_FALSE(callback_helper3.got_callback()); | 190 EXPECT_FALSE(callback_helper3.got_callback()); |
191 | 191 |
192 HandleWatcher watcher3; | 192 HandleWatcher watcher3(0); |
193 callback_helper3.Start(&watcher3, test_pipe3.handle0.get()); | 193 callback_helper3.Start(&watcher3, test_pipe3.handle0.get()); |
194 RunUntilIdle(); | 194 RunUntilIdle(); |
195 EXPECT_FALSE(callback_helper1.got_callback()); | 195 EXPECT_FALSE(callback_helper1.got_callback()); |
196 EXPECT_FALSE(callback_helper2.got_callback()); | 196 EXPECT_FALSE(callback_helper2.got_callback()); |
197 EXPECT_FALSE(callback_helper3.got_callback()); | 197 EXPECT_FALSE(callback_helper3.got_callback()); |
198 | 198 |
199 // Write to 3 and make sure it's notified. | 199 // Write to 3 and make sure it's notified. |
200 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(), | 200 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(), |
201 std::string())); | 201 std::string())); |
202 callback_helper3.RunUntilGotCallback(); | 202 callback_helper3.RunUntilGotCallback(); |
(...skipping 27 matching lines...) Expand all Loading... |
230 | 230 |
231 // Verifies Start() invoked a second time works. | 231 // Verifies Start() invoked a second time works. |
232 TEST_P(HandleWatcherTest, Restart) { | 232 TEST_P(HandleWatcherTest, Restart) { |
233 MessagePipe test_pipe1; | 233 MessagePipe test_pipe1; |
234 MessagePipe test_pipe2; | 234 MessagePipe test_pipe2; |
235 CallbackHelper callback_helper1; | 235 CallbackHelper callback_helper1; |
236 CallbackHelper callback_helper2; | 236 CallbackHelper callback_helper2; |
237 ASSERT_TRUE(test_pipe1.handle0.is_valid()); | 237 ASSERT_TRUE(test_pipe1.handle0.is_valid()); |
238 ASSERT_TRUE(test_pipe2.handle0.is_valid()); | 238 ASSERT_TRUE(test_pipe2.handle0.is_valid()); |
239 | 239 |
240 HandleWatcher watcher1; | 240 HandleWatcher watcher1(0); |
241 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); | 241 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); |
242 RunUntilIdle(); | 242 RunUntilIdle(); |
243 EXPECT_FALSE(callback_helper1.got_callback()); | 243 EXPECT_FALSE(callback_helper1.got_callback()); |
244 EXPECT_FALSE(callback_helper2.got_callback()); | 244 EXPECT_FALSE(callback_helper2.got_callback()); |
245 | 245 |
246 HandleWatcher watcher2; | 246 HandleWatcher watcher2(0); |
247 callback_helper2.Start(&watcher2, test_pipe2.handle0.get()); | 247 callback_helper2.Start(&watcher2, test_pipe2.handle0.get()); |
248 RunUntilIdle(); | 248 RunUntilIdle(); |
249 EXPECT_FALSE(callback_helper1.got_callback()); | 249 EXPECT_FALSE(callback_helper1.got_callback()); |
250 EXPECT_FALSE(callback_helper2.got_callback()); | 250 EXPECT_FALSE(callback_helper2.got_callback()); |
251 | 251 |
252 // Write to 1 and make sure it's notified. | 252 // Write to 1 and make sure it's notified. |
253 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), | 253 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), |
254 std::string())); | 254 std::string())); |
255 callback_helper1.RunUntilGotCallback(); | 255 callback_helper1.RunUntilGotCallback(); |
256 EXPECT_TRUE(callback_helper1.got_callback()); | 256 EXPECT_TRUE(callback_helper1.got_callback()); |
(...skipping 22 matching lines...) Expand all Loading... |
279 EXPECT_TRUE(callback_helper1.got_callback()); | 279 EXPECT_TRUE(callback_helper1.got_callback()); |
280 EXPECT_FALSE(callback_helper2.got_callback()); | 280 EXPECT_FALSE(callback_helper2.got_callback()); |
281 } | 281 } |
282 | 282 |
283 // Verifies Start() invoked a second time on the same handle works. | 283 // Verifies Start() invoked a second time on the same handle works. |
284 TEST_P(HandleWatcherTest, RestartOnSameHandle) { | 284 TEST_P(HandleWatcherTest, RestartOnSameHandle) { |
285 MessagePipe test_pipe; | 285 MessagePipe test_pipe; |
286 CallbackHelper callback_helper; | 286 CallbackHelper callback_helper; |
287 ASSERT_TRUE(test_pipe.handle0.is_valid()); | 287 ASSERT_TRUE(test_pipe.handle0.is_valid()); |
288 | 288 |
289 HandleWatcher watcher; | 289 HandleWatcher watcher(0); |
290 callback_helper.Start(&watcher, test_pipe.handle0.get()); | 290 callback_helper.Start(&watcher, test_pipe.handle0.get()); |
291 RunUntilIdle(); | 291 RunUntilIdle(); |
292 EXPECT_FALSE(callback_helper.got_callback()); | 292 EXPECT_FALSE(callback_helper.got_callback()); |
293 | 293 |
294 callback_helper.Start(&watcher, test_pipe.handle0.get()); | 294 callback_helper.Start(&watcher, test_pipe.handle0.get()); |
295 RunUntilIdle(); | 295 RunUntilIdle(); |
296 EXPECT_FALSE(callback_helper.got_callback()); | 296 EXPECT_FALSE(callback_helper.got_callback()); |
297 } | 297 } |
298 | 298 |
299 // Verifies deadline is honored. | 299 // Verifies deadline is honored. |
300 TEST_P(HandleWatcherTest, Deadline) { | 300 TEST_P(HandleWatcherTest, Deadline) { |
301 InstallTickClock(); | 301 InstallTickClock(); |
302 | 302 |
303 MessagePipe test_pipe1; | 303 MessagePipe test_pipe1; |
304 MessagePipe test_pipe2; | 304 MessagePipe test_pipe2; |
305 MessagePipe test_pipe3; | 305 MessagePipe test_pipe3; |
306 CallbackHelper callback_helper1; | 306 CallbackHelper callback_helper1; |
307 CallbackHelper callback_helper2; | 307 CallbackHelper callback_helper2; |
308 CallbackHelper callback_helper3; | 308 CallbackHelper callback_helper3; |
309 ASSERT_TRUE(test_pipe1.handle0.is_valid()); | 309 ASSERT_TRUE(test_pipe1.handle0.is_valid()); |
310 ASSERT_TRUE(test_pipe2.handle0.is_valid()); | 310 ASSERT_TRUE(test_pipe2.handle0.is_valid()); |
311 ASSERT_TRUE(test_pipe3.handle0.is_valid()); | 311 ASSERT_TRUE(test_pipe3.handle0.is_valid()); |
312 | 312 |
313 // Add a watcher with an infinite timeout. | 313 // Add a watcher with an infinite timeout. |
314 HandleWatcher watcher1; | 314 HandleWatcher watcher1(0); |
315 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); | 315 callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); |
316 RunUntilIdle(); | 316 RunUntilIdle(); |
317 EXPECT_FALSE(callback_helper1.got_callback()); | 317 EXPECT_FALSE(callback_helper1.got_callback()); |
318 EXPECT_FALSE(callback_helper2.got_callback()); | 318 EXPECT_FALSE(callback_helper2.got_callback()); |
319 EXPECT_FALSE(callback_helper3.got_callback()); | 319 EXPECT_FALSE(callback_helper3.got_callback()); |
320 | 320 |
321 // Add another watcher wth a timeout of 500 microseconds. | 321 // Add another watcher wth a timeout of 500 microseconds. |
322 HandleWatcher watcher2; | 322 HandleWatcher watcher2(0); |
323 watcher2.Start(test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, 500, | 323 watcher2.Start(test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, 500, |
324 callback_helper2.GetCallback()); | 324 callback_helper2.GetCallback()); |
325 RunUntilIdle(); | 325 RunUntilIdle(); |
326 EXPECT_FALSE(callback_helper1.got_callback()); | 326 EXPECT_FALSE(callback_helper1.got_callback()); |
327 EXPECT_FALSE(callback_helper2.got_callback()); | 327 EXPECT_FALSE(callback_helper2.got_callback()); |
328 EXPECT_FALSE(callback_helper3.got_callback()); | 328 EXPECT_FALSE(callback_helper3.got_callback()); |
329 | 329 |
330 // Advance the clock passed the deadline. We also have to start another | 330 // Advance the clock passed the deadline. We also have to start another |
331 // watcher to wake up the background thread. | 331 // watcher to wake up the background thread. |
332 tick_clock_.Advance(base::TimeDelta::FromMicroseconds(501)); | 332 tick_clock_.Advance(base::TimeDelta::FromMicroseconds(501)); |
333 | 333 |
334 HandleWatcher watcher3; | 334 HandleWatcher watcher3(0); |
335 callback_helper3.Start(&watcher3, test_pipe3.handle0.get()); | 335 callback_helper3.Start(&watcher3, test_pipe3.handle0.get()); |
336 | 336 |
337 callback_helper2.RunUntilGotCallback(); | 337 callback_helper2.RunUntilGotCallback(); |
338 EXPECT_FALSE(callback_helper1.got_callback()); | 338 EXPECT_FALSE(callback_helper1.got_callback()); |
339 EXPECT_TRUE(callback_helper2.got_callback()); | 339 EXPECT_TRUE(callback_helper2.got_callback()); |
340 EXPECT_FALSE(callback_helper3.got_callback()); | 340 EXPECT_FALSE(callback_helper3.got_callback()); |
341 } | 341 } |
342 | 342 |
343 TEST_P(HandleWatcherTest, DeleteInCallback) { | 343 TEST_P(HandleWatcherTest, DeleteInCallback) { |
344 MessagePipe test_pipe; | 344 MessagePipe test_pipe; |
345 CallbackHelper callback_helper; | 345 CallbackHelper callback_helper; |
346 | 346 |
347 HandleWatcher* watcher = new HandleWatcher(); | 347 HandleWatcher* watcher = new HandleWatcher(0); |
348 callback_helper.StartWithCallback(watcher, test_pipe.handle1.get(), | 348 callback_helper.StartWithCallback(watcher, test_pipe.handle1.get(), |
349 base::Bind(&DeleteWatcherAndForwardResult, | 349 base::Bind(&DeleteWatcherAndForwardResult, |
350 watcher, | 350 watcher, |
351 callback_helper.GetCallback())); | 351 callback_helper.GetCallback())); |
352 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle0.get(), | 352 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle0.get(), |
353 std::string())); | 353 std::string())); |
354 callback_helper.RunUntilGotCallback(); | 354 callback_helper.RunUntilGotCallback(); |
355 EXPECT_TRUE(callback_helper.got_callback()); | 355 EXPECT_TRUE(callback_helper.got_callback()); |
356 } | 356 } |
357 | 357 |
358 TEST_P(HandleWatcherTest, AbortedOnMessageLoopDestruction) { | 358 TEST_P(HandleWatcherTest, AbortedOnMessageLoopDestruction) { |
359 bool was_signaled = false; | 359 bool was_signaled = false; |
360 MojoResult result = MOJO_RESULT_OK; | 360 MojoResult result = MOJO_RESULT_OK; |
361 | 361 |
362 MessagePipe pipe; | 362 MessagePipe pipe; |
363 HandleWatcher watcher; | 363 HandleWatcher watcher(0); |
364 watcher.Start(pipe.handle0.get(), | 364 watcher.Start(pipe.handle0.get(), |
365 MOJO_HANDLE_SIGNAL_READABLE, | 365 MOJO_HANDLE_SIGNAL_READABLE, |
366 MOJO_DEADLINE_INDEFINITE, | 366 MOJO_DEADLINE_INDEFINITE, |
367 base::Bind(&ObserveCallback, &was_signaled, &result)); | 367 base::Bind(&ObserveCallback, &was_signaled, &result)); |
368 | 368 |
369 // Now, let the MessageLoop get torn down. We expect our callback to run. | 369 // Now, let the MessageLoop get torn down. We expect our callback to run. |
370 TearDownMessageLoop(); | 370 TearDownMessageLoop(); |
371 | 371 |
372 EXPECT_TRUE(was_signaled); | 372 EXPECT_TRUE(was_signaled); |
373 EXPECT_EQ(MOJO_RESULT_ABORTED, result); | 373 EXPECT_EQ(MOJO_RESULT_ABORTED, result); |
(...skipping 16 matching lines...) Expand all Loading... |
390 // |count| is the number of HandleWatchers to create. |active_count| is the | 390 // |count| is the number of HandleWatchers to create. |active_count| is the |
391 // number of outstanding threads, |task_runner| the task runner for the main | 391 // number of outstanding threads, |task_runner| the task runner for the main |
392 // thread and |run_loop| the run loop that should be quit when there are no more | 392 // thread and |run_loop| the run loop that should be quit when there are no more |
393 // threads running. When done StressThreadDone() is invoked on the main thread. | 393 // threads running. When done StressThreadDone() is invoked on the main thread. |
394 // |active_count| and |run_loop| should only be used on the main thread. | 394 // |active_count| and |run_loop| should only be used on the main thread. |
395 void RunStressTest(int count, | 395 void RunStressTest(int count, |
396 scoped_refptr<base::TaskRunner> task_runner, | 396 scoped_refptr<base::TaskRunner> task_runner, |
397 base::RunLoop* run_loop, | 397 base::RunLoop* run_loop, |
398 int* active_count) { | 398 int* active_count) { |
399 struct TestData { | 399 struct TestData { |
| 400 TestData() : watcher(0) {} |
| 401 |
400 MessagePipe pipe; | 402 MessagePipe pipe; |
401 HandleWatcher watcher; | 403 HandleWatcher watcher; |
402 }; | 404 }; |
403 ScopedVector<TestData> data_vector; | 405 ScopedVector<TestData> data_vector; |
404 for (int i = 0; i < count; ++i) { | 406 for (int i = 0; i < count; ++i) { |
405 if (i % 20 == 0) { | 407 if (i % 20 == 0) { |
406 // Every so often we wait. This results in some level of thread balancing | 408 // Every so often we wait. This results in some level of thread balancing |
407 // as well as making sure HandleWatcher has time to actually start some | 409 // as well as making sure HandleWatcher has time to actually start some |
408 // watches. | 410 // watches. |
409 MessagePipe test_pipe; | 411 MessagePipe test_pipe; |
410 ASSERT_TRUE(test_pipe.handle0.is_valid()); | 412 ASSERT_TRUE(test_pipe.handle0.is_valid()); |
411 CallbackHelper callback_helper; | 413 CallbackHelper callback_helper; |
412 HandleWatcher watcher; | 414 HandleWatcher watcher(0); |
413 callback_helper.Start(&watcher, test_pipe.handle0.get()); | 415 callback_helper.Start(&watcher, test_pipe.handle0.get()); |
414 RunUntilIdle(); | 416 RunUntilIdle(); |
415 EXPECT_FALSE(callback_helper.got_callback()); | 417 EXPECT_FALSE(callback_helper.got_callback()); |
416 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle1.get(), | 418 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle1.get(), |
417 std::string())); | 419 std::string())); |
418 base::MessageLoop::ScopedNestableTaskAllower scoper( | 420 base::MessageLoop::ScopedNestableTaskAllower scoper( |
419 base::MessageLoop::current()); | 421 base::MessageLoop::current()); |
420 callback_helper.RunUntilGotCallback(); | 422 callback_helper.RunUntilGotCallback(); |
421 EXPECT_TRUE(callback_helper.got_callback()); | 423 EXPECT_TRUE(callback_helper.got_callback()); |
422 } else { | 424 } else { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 message_loop.task_runner(), | 476 message_loop.task_runner(), |
475 &run_loop, &threads_active_counter)); | 477 &run_loop, &threads_active_counter)); |
476 } | 478 } |
477 run_loop.Run(); | 479 run_loop.Run(); |
478 ASSERT_EQ(0, threads_active_counter); | 480 ASSERT_EQ(0, threads_active_counter); |
479 } | 481 } |
480 | 482 |
481 } // namespace test | 483 } // namespace test |
482 } // namespace common | 484 } // namespace common |
483 } // namespace mojo | 485 } // namespace mojo |
OLD | NEW |