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 "mojo/public/cpp/utility/run_loop.h" | 5 #include "mojo/public/cpp/utility/run_loop.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "mojo/public/cpp/system/core.h" | 9 #include "mojo/public/cpp/system/core.h" |
10 #include "mojo/public/cpp/test_support/test_utils.h" | 10 #include "mojo/public/cpp/test_support/test_utils.h" |
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
406 run_loop.PostDelayedTask(Closure(Task(2, &sequence)), 0); | 406 run_loop.PostDelayedTask(Closure(Task(2, &sequence)), 0); |
407 run_loop.PostDelayedTask(Closure(Task(3, &sequence)), 0); | 407 run_loop.PostDelayedTask(Closure(Task(3, &sequence)), 0); |
408 run_loop.RunUntilIdle(); | 408 run_loop.RunUntilIdle(); |
409 | 409 |
410 ASSERT_EQ(3u, sequence.size()); | 410 ASSERT_EQ(3u, sequence.size()); |
411 EXPECT_EQ(1, sequence[0]); | 411 EXPECT_EQ(1, sequence[0]); |
412 EXPECT_EQ(2, sequence[1]); | 412 EXPECT_EQ(2, sequence[1]); |
413 EXPECT_EQ(3, sequence[2]); | 413 EXPECT_EQ(3, sequence[2]); |
414 } | 414 } |
415 | 415 |
416 struct QuittingTask { | |
417 QuittingTask(RunLoop* run_loop) : run_loop(run_loop) {} | |
418 | |
419 void Run() const { run_loop->Quit(); } | |
420 | |
421 RunLoop* run_loop; | |
422 }; | |
423 | |
424 struct ClosingTask { | |
425 public: | |
426 ClosingTask(ScopedMessagePipeHandle* handle): handle(handle) {} | |
427 | |
428 void Run() const { | |
429 handle->reset(); | |
430 } | |
431 | |
432 ScopedMessagePipeHandle* handle; | |
433 }; | |
434 | |
435 TEST_F(RunLoopTest, QuitFromDelayedTask) { | |
436 TestRunLoopHandler handler; | |
437 MessagePipe test_pipe; | |
438 RunLoop run_loop; | |
439 run_loop.AddHandler(&handler, | |
440 test_pipe.handle0.get(), | |
441 MOJO_HANDLE_SIGNAL_READABLE, | |
442 MOJO_DEADLINE_INDEFINITE); | |
443 run_loop.PostDelayedTask(Closure(QuittingTask(&run_loop)), 0); | |
444 // Ensure the test won't block forever. | |
445 run_loop.PostDelayedTask(Closure(ClosingTask(&test_pipe.handle1)), 1e5); | |
jamesr
2014/09/22 22:30:18
hmm, 1e5 microseconds = 100ms if i'm not mistaken.
| |
446 const MojoTimeTicks before = GetTimeTicksNow(); | |
447 run_loop.Run(); | |
448 const MojoTimeTicks after = GetTimeTicksNow(); | |
449 // Check that the run loop did quit before the closing task was run. | |
450 ASSERT_LE(after-before, 5e4); | |
jamesr
2014/09/22 22:30:18
this is super racy. if you want tests like this we
qsr
2014/09/23 10:40:15
I didn't want to count on timeout because in case
| |
451 } | |
452 | |
416 } // namespace | 453 } // namespace |
417 } // namespace mojo | 454 } // namespace mojo |
OLD | NEW |