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
3 // found in the LICENSE file.
5 #include "base/message_loop/message_loop_test.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/run_loop.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "base/threading/thread.h"
18 class Foo
: public RefCounted
<Foo
> {
20 Foo() : test_count_(0) {
27 void Test1ConstRef(const std::string
& a
) {
32 void Test1Ptr(std::string
* a
) {
37 void Test1Int(int a
) {
41 void Test2Ptr(std::string
* a
, std::string
* b
) {
47 void Test2Mixed(const std::string
& a
, std::string
* b
) {
53 int test_count() const { return test_count_
; }
54 const std::string
& result() const { return result_
; }
57 friend class RefCounted
<Foo
>;
64 DISALLOW_COPY_AND_ASSIGN(Foo
);
67 // This function runs slowly to simulate a large amount of work being done.
68 void SlowFunc(TimeDelta pause
, int* quit_counter
) {
69 PlatformThread::Sleep(pause
);
70 if (--(*quit_counter
) == 0)
71 MessageLoop::current()->QuitWhenIdle();
74 // This function records the time when Run was called in a Time object, which is
75 // useful for building a variety of MessageLoop tests.
77 void RecordRunTimeFunc(Time
* run_time
, int* quit_counter
) {
78 *run_time
= Time::Now();
80 // Cause our Run function to take some time to execute. As a result we can
81 // count on subsequent RecordRunTimeFunc()s running at a future time,
82 // without worry about the resolution of our system clock being an issue.
83 SlowFunc(TimeDelta::FromMilliseconds(10), quit_counter
);
88 void RunTest_PostTask(MessagePumpFactory factory
) {
89 scoped_ptr
<MessagePump
> pump(factory());
90 MessageLoop
loop(pump
.Pass());
91 // Add tests to message loop
92 scoped_refptr
<Foo
> foo(new Foo());
93 std::string
a("a"), b("b"), c("c"), d("d");
94 MessageLoop::current()->PostTask(FROM_HERE
, Bind(
95 &Foo::Test0
, foo
.get()));
96 MessageLoop::current()->PostTask(FROM_HERE
, Bind(
97 &Foo::Test1ConstRef
, foo
.get(), a
));
98 MessageLoop::current()->PostTask(FROM_HERE
, Bind(
99 &Foo::Test1Ptr
, foo
.get(), &b
));
100 MessageLoop::current()->PostTask(FROM_HERE
, Bind(
101 &Foo::Test1Int
, foo
.get(), 100));
102 MessageLoop::current()->PostTask(FROM_HERE
, Bind(
103 &Foo::Test2Ptr
, foo
.get(), &a
, &c
));
104 MessageLoop::current()->PostTask(FROM_HERE
, Bind(
105 &Foo::Test2Mixed
, foo
.get(), a
, &d
));
106 // After all tests, post a message that will shut down the message loop
107 MessageLoop::current()->PostTask(FROM_HERE
, Bind(
108 &MessageLoop::Quit
, Unretained(MessageLoop::current())));
110 // Now kick things off
111 MessageLoop::current()->Run();
113 EXPECT_EQ(foo
->test_count(), 105);
114 EXPECT_EQ(foo
->result(), "abacad");
117 void RunTest_PostDelayedTask_Basic(MessagePumpFactory factory
) {
118 scoped_ptr
<MessagePump
> pump(factory());
119 MessageLoop
loop(pump
.Pass());
121 // Test that PostDelayedTask results in a delayed task.
123 const TimeDelta kDelay
= TimeDelta::FromMilliseconds(100);
128 loop
.PostDelayedTask(
129 FROM_HERE
, Bind(&RecordRunTimeFunc
, &run_time
, &num_tasks
),
132 Time time_before_run
= Time::Now();
134 Time time_after_run
= Time::Now();
136 EXPECT_EQ(0, num_tasks
);
137 EXPECT_LT(kDelay
, time_after_run
- time_before_run
);
140 void RunTest_PostDelayedTask_InDelayOrder(MessagePumpFactory factory
) {
141 scoped_ptr
<MessagePump
> pump(factory());
142 MessageLoop
loop(pump
.Pass());
144 // Test that two tasks with different delays run in the right order.
146 Time run_time1
, run_time2
;
148 loop
.PostDelayedTask(
150 Bind(&RecordRunTimeFunc
, &run_time1
, &num_tasks
),
151 TimeDelta::FromMilliseconds(200));
152 // If we get a large pause in execution (due to a context switch) here, this
154 loop
.PostDelayedTask(
156 Bind(&RecordRunTimeFunc
, &run_time2
, &num_tasks
),
157 TimeDelta::FromMilliseconds(10));
160 EXPECT_EQ(0, num_tasks
);
162 EXPECT_TRUE(run_time2
< run_time1
);
165 void RunTest_PostDelayedTask_InPostOrder(MessagePumpFactory factory
) {
166 scoped_ptr
<MessagePump
> pump(factory());
167 MessageLoop
loop(pump
.Pass());
169 // Test that two tasks with the same delay run in the order in which they
172 // NOTE: This is actually an approximate test since the API only takes a
173 // "delay" parameter, so we are not exactly simulating two tasks that get
174 // posted at the exact same time. It would be nice if the API allowed us to
175 // specify the desired run time.
177 const TimeDelta kDelay
= TimeDelta::FromMilliseconds(100);
180 Time run_time1
, run_time2
;
182 loop
.PostDelayedTask(
184 Bind(&RecordRunTimeFunc
, &run_time1
, &num_tasks
), kDelay
);
185 loop
.PostDelayedTask(
187 Bind(&RecordRunTimeFunc
, &run_time2
, &num_tasks
), kDelay
);
190 EXPECT_EQ(0, num_tasks
);
192 EXPECT_TRUE(run_time1
< run_time2
);
195 void RunTest_PostDelayedTask_InPostOrder_2(MessagePumpFactory factory
) {
196 scoped_ptr
<MessagePump
> pump(factory());
197 MessageLoop
loop(pump
.Pass());
199 // Test that a delayed task still runs after a normal tasks even if the
200 // normal tasks take a long time to run.
202 const TimeDelta kPause
= TimeDelta::FromMilliseconds(50);
207 loop
.PostTask(FROM_HERE
, Bind(&SlowFunc
, kPause
, &num_tasks
));
208 loop
.PostDelayedTask(
210 Bind(&RecordRunTimeFunc
, &run_time
, &num_tasks
),
211 TimeDelta::FromMilliseconds(10));
213 Time time_before_run
= Time::Now();
215 Time time_after_run
= Time::Now();
217 EXPECT_EQ(0, num_tasks
);
219 EXPECT_LT(kPause
, time_after_run
- time_before_run
);
222 void RunTest_PostDelayedTask_InPostOrder_3(MessagePumpFactory factory
) {
223 scoped_ptr
<MessagePump
> pump(factory());
224 MessageLoop
loop(pump
.Pass());
226 // Test that a delayed task still runs after a pile of normal tasks. The key
227 // difference between this test and the previous one is that here we return
228 // the MessageLoop a lot so we give the MessageLoop plenty of opportunities
229 // to maybe run the delayed task. It should know not to do so until the
230 // delayed task's delay has passed.
233 Time run_time1
, run_time2
;
235 // Clutter the ML with tasks.
236 for (int i
= 1; i
< num_tasks
; ++i
)
237 loop
.PostTask(FROM_HERE
,
238 Bind(&RecordRunTimeFunc
, &run_time1
, &num_tasks
));
240 loop
.PostDelayedTask(
241 FROM_HERE
, Bind(&RecordRunTimeFunc
, &run_time2
, &num_tasks
),
242 TimeDelta::FromMilliseconds(1));
245 EXPECT_EQ(0, num_tasks
);
247 EXPECT_TRUE(run_time2
> run_time1
);
250 void RunTest_PostDelayedTask_SharedTimer(MessagePumpFactory factory
) {
251 scoped_ptr
<MessagePump
> pump(factory());
252 MessageLoop
loop(pump
.Pass());
254 // Test that the interval of the timer, used to run the next delayed task, is
255 // set to a value corresponding to when the next delayed task should run.
257 // By setting num_tasks to 1, we ensure that the first task to run causes the
260 Time run_time1
, run_time2
;
262 loop
.PostDelayedTask(
264 Bind(&RecordRunTimeFunc
, &run_time1
, &num_tasks
),
265 TimeDelta::FromSeconds(1000));
266 loop
.PostDelayedTask(
268 Bind(&RecordRunTimeFunc
, &run_time2
, &num_tasks
),
269 TimeDelta::FromMilliseconds(10));
271 Time start_time
= Time::Now();
274 EXPECT_EQ(0, num_tasks
);
276 // Ensure that we ran in far less time than the slower timer.
277 TimeDelta total_time
= Time::Now() - start_time
;
278 EXPECT_GT(5000, total_time
.InMilliseconds());
280 // In case both timers somehow run at nearly the same time, sleep a little
281 // and then run all pending to force them both to have run. This is just
282 // encouraging flakiness if there is any.
283 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
284 RunLoop().RunUntilIdle();
286 EXPECT_TRUE(run_time1
.is_null());
287 EXPECT_FALSE(run_time2
.is_null());
290 // This is used to inject a test point for recording the destructor calls for
291 // Closure objects send to MessageLoop::PostTask(). It is awkward usage since we
292 // are trying to hook the actual destruction, which is not a common operation.
293 class RecordDeletionProbe
: public RefCounted
<RecordDeletionProbe
> {
295 RecordDeletionProbe(RecordDeletionProbe
* post_on_delete
, bool* was_deleted
)
296 : post_on_delete_(post_on_delete
), was_deleted_(was_deleted
) {
301 friend class RefCounted
<RecordDeletionProbe
>;
303 ~RecordDeletionProbe() {
304 *was_deleted_
= true;
305 if (post_on_delete_
.get())
306 MessageLoop::current()->PostTask(
307 FROM_HERE
, Bind(&RecordDeletionProbe::Run
, post_on_delete_
.get()));
310 scoped_refptr
<RecordDeletionProbe
> post_on_delete_
;
314 void RunTest_EnsureDeletion(MessagePumpFactory factory
) {
315 bool a_was_deleted
= false;
316 bool b_was_deleted
= false;
318 scoped_ptr
<MessagePump
> pump(factory());
319 MessageLoop
loop(pump
.Pass());
321 FROM_HERE
, Bind(&RecordDeletionProbe::Run
,
322 new RecordDeletionProbe(NULL
, &a_was_deleted
)));
323 // TODO(ajwong): Do we really need 1000ms here?
324 loop
.PostDelayedTask(
325 FROM_HERE
, Bind(&RecordDeletionProbe::Run
,
326 new RecordDeletionProbe(NULL
, &b_was_deleted
)),
327 TimeDelta::FromMilliseconds(1000));
329 EXPECT_TRUE(a_was_deleted
);
330 EXPECT_TRUE(b_was_deleted
);
333 void RunTest_EnsureDeletion_Chain(MessagePumpFactory factory
) {
334 bool a_was_deleted
= false;
335 bool b_was_deleted
= false;
336 bool c_was_deleted
= false;
338 scoped_ptr
<MessagePump
> pump(factory());
339 MessageLoop
loop(pump
.Pass());
340 // The scoped_refptr for each of the below is held either by the chained
341 // RecordDeletionProbe, or the bound RecordDeletionProbe::Run() callback.
342 RecordDeletionProbe
* a
= new RecordDeletionProbe(NULL
, &a_was_deleted
);
343 RecordDeletionProbe
* b
= new RecordDeletionProbe(a
, &b_was_deleted
);
344 RecordDeletionProbe
* c
= new RecordDeletionProbe(b
, &c_was_deleted
);
345 loop
.PostTask(FROM_HERE
, Bind(&RecordDeletionProbe::Run
, c
));
347 EXPECT_TRUE(a_was_deleted
);
348 EXPECT_TRUE(b_was_deleted
);
349 EXPECT_TRUE(c_was_deleted
);
352 void NestingFunc(int* depth
) {
355 MessageLoop::current()->PostTask(FROM_HERE
,
356 Bind(&NestingFunc
, depth
));
358 MessageLoop::current()->SetNestableTasksAllowed(true);
359 MessageLoop::current()->Run();
361 MessageLoop::current()->QuitWhenIdle();
364 void RunTest_Nesting(MessagePumpFactory factory
) {
365 scoped_ptr
<MessagePump
> pump(factory());
366 MessageLoop
loop(pump
.Pass());
369 MessageLoop::current()->PostTask(FROM_HERE
,
370 Bind(&NestingFunc
, &depth
));
371 MessageLoop::current()->Run();
388 TaskItem(TaskType t
, int c
, bool s
)
398 bool operator == (const TaskItem
& other
) const {
399 return type
== other
.type
&& cookie
== other
.cookie
&& start
== other
.start
;
403 std::ostream
& operator <<(std::ostream
& os
, TaskType type
) {
405 case MESSAGEBOX
: os
<< "MESSAGEBOX"; break;
406 case ENDDIALOG
: os
<< "ENDDIALOG"; break;
407 case RECURSIVE
: os
<< "RECURSIVE"; break;
408 case TIMEDMESSAGELOOP
: os
<< "TIMEDMESSAGELOOP"; break;
409 case QUITMESSAGELOOP
: os
<< "QUITMESSAGELOOP"; break;
410 case ORDERED
: os
<< "ORDERED"; break;
411 case PUMPS
: os
<< "PUMPS"; break;
412 case SLEEP
: os
<< "SLEEP"; break;
415 os
<< "Unknown TaskType";
421 std::ostream
& operator <<(std::ostream
& os
, const TaskItem
& item
) {
423 return os
<< item
.type
<< " " << item
.cookie
<< " starts";
425 return os
<< item
.type
<< " " << item
.cookie
<< " ends";
430 void RecordStart(TaskType type
, int cookie
) {
431 TaskItem
item(type
, cookie
, true);
433 task_list_
.push_back(item
);
436 void RecordEnd(TaskType type
, int cookie
) {
437 TaskItem
item(type
, cookie
, false);
439 task_list_
.push_back(item
);
443 return task_list_
.size();
446 TaskItem
Get(int n
) {
447 return task_list_
[n
];
451 std::vector
<TaskItem
> task_list_
;
454 void RecursiveFunc(TaskList
* order
, int cookie
, int depth
,
456 order
->RecordStart(RECURSIVE
, cookie
);
459 MessageLoop::current()->SetNestableTasksAllowed(true);
460 MessageLoop::current()->PostTask(
462 Bind(&RecursiveFunc
, order
, cookie
, depth
- 1, is_reentrant
));
464 order
->RecordEnd(RECURSIVE
, cookie
);
467 void QuitFunc(TaskList
* order
, int cookie
) {
468 order
->RecordStart(QUITMESSAGELOOP
, cookie
);
469 MessageLoop::current()->QuitWhenIdle();
470 order
->RecordEnd(QUITMESSAGELOOP
, cookie
);
472 void RunTest_RecursiveDenial1(MessagePumpFactory factory
) {
473 scoped_ptr
<MessagePump
> pump(factory());
474 MessageLoop
loop(pump
.Pass());
476 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
478 MessageLoop::current()->PostTask(
480 Bind(&RecursiveFunc
, &order
, 1, 2, false));
481 MessageLoop::current()->PostTask(
483 Bind(&RecursiveFunc
, &order
, 2, 2, false));
484 MessageLoop::current()->PostTask(
486 Bind(&QuitFunc
, &order
, 3));
488 MessageLoop::current()->Run();
491 ASSERT_EQ(14U, order
.Size());
492 EXPECT_EQ(order
.Get(0), TaskItem(RECURSIVE
, 1, true));
493 EXPECT_EQ(order
.Get(1), TaskItem(RECURSIVE
, 1, false));
494 EXPECT_EQ(order
.Get(2), TaskItem(RECURSIVE
, 2, true));
495 EXPECT_EQ(order
.Get(3), TaskItem(RECURSIVE
, 2, false));
496 EXPECT_EQ(order
.Get(4), TaskItem(QUITMESSAGELOOP
, 3, true));
497 EXPECT_EQ(order
.Get(5), TaskItem(QUITMESSAGELOOP
, 3, false));
498 EXPECT_EQ(order
.Get(6), TaskItem(RECURSIVE
, 1, true));
499 EXPECT_EQ(order
.Get(7), TaskItem(RECURSIVE
, 1, false));
500 EXPECT_EQ(order
.Get(8), TaskItem(RECURSIVE
, 2, true));
501 EXPECT_EQ(order
.Get(9), TaskItem(RECURSIVE
, 2, false));
502 EXPECT_EQ(order
.Get(10), TaskItem(RECURSIVE
, 1, true));
503 EXPECT_EQ(order
.Get(11), TaskItem(RECURSIVE
, 1, false));
504 EXPECT_EQ(order
.Get(12), TaskItem(RECURSIVE
, 2, true));
505 EXPECT_EQ(order
.Get(13), TaskItem(RECURSIVE
, 2, false));
508 void RecursiveSlowFunc(TaskList
* order
, int cookie
, int depth
,
510 RecursiveFunc(order
, cookie
, depth
, is_reentrant
);
511 PlatformThread::Sleep(TimeDelta::FromMilliseconds(10));
514 void OrderedFunc(TaskList
* order
, int cookie
) {
515 order
->RecordStart(ORDERED
, cookie
);
516 order
->RecordEnd(ORDERED
, cookie
);
519 void RunTest_RecursiveDenial3(MessagePumpFactory factory
) {
520 scoped_ptr
<MessagePump
> pump(factory());
521 MessageLoop
loop(pump
.Pass());
523 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
525 MessageLoop::current()->PostTask(
526 FROM_HERE
, Bind(&RecursiveSlowFunc
, &order
, 1, 2, false));
527 MessageLoop::current()->PostTask(
528 FROM_HERE
, Bind(&RecursiveSlowFunc
, &order
, 2, 2, false));
529 MessageLoop::current()->PostDelayedTask(
531 Bind(&OrderedFunc
, &order
, 3),
532 TimeDelta::FromMilliseconds(5));
533 MessageLoop::current()->PostDelayedTask(
535 Bind(&QuitFunc
, &order
, 4),
536 TimeDelta::FromMilliseconds(5));
538 MessageLoop::current()->Run();
541 ASSERT_EQ(16U, order
.Size());
542 EXPECT_EQ(order
.Get(0), TaskItem(RECURSIVE
, 1, true));
543 EXPECT_EQ(order
.Get(1), TaskItem(RECURSIVE
, 1, false));
544 EXPECT_EQ(order
.Get(2), TaskItem(RECURSIVE
, 2, true));
545 EXPECT_EQ(order
.Get(3), TaskItem(RECURSIVE
, 2, false));
546 EXPECT_EQ(order
.Get(4), TaskItem(RECURSIVE
, 1, true));
547 EXPECT_EQ(order
.Get(5), TaskItem(RECURSIVE
, 1, false));
548 EXPECT_EQ(order
.Get(6), TaskItem(ORDERED
, 3, true));
549 EXPECT_EQ(order
.Get(7), TaskItem(ORDERED
, 3, false));
550 EXPECT_EQ(order
.Get(8), TaskItem(RECURSIVE
, 2, true));
551 EXPECT_EQ(order
.Get(9), TaskItem(RECURSIVE
, 2, false));
552 EXPECT_EQ(order
.Get(10), TaskItem(QUITMESSAGELOOP
, 4, true));
553 EXPECT_EQ(order
.Get(11), TaskItem(QUITMESSAGELOOP
, 4, false));
554 EXPECT_EQ(order
.Get(12), TaskItem(RECURSIVE
, 1, true));
555 EXPECT_EQ(order
.Get(13), TaskItem(RECURSIVE
, 1, false));
556 EXPECT_EQ(order
.Get(14), TaskItem(RECURSIVE
, 2, true));
557 EXPECT_EQ(order
.Get(15), TaskItem(RECURSIVE
, 2, false));
560 void RunTest_RecursiveSupport1(MessagePumpFactory factory
) {
561 scoped_ptr
<MessagePump
> pump(factory());
562 MessageLoop
loop(pump
.Pass());
565 MessageLoop::current()->PostTask(
566 FROM_HERE
, Bind(&RecursiveFunc
, &order
, 1, 2, true));
567 MessageLoop::current()->PostTask(
568 FROM_HERE
, Bind(&RecursiveFunc
, &order
, 2, 2, true));
569 MessageLoop::current()->PostTask(
570 FROM_HERE
, Bind(&QuitFunc
, &order
, 3));
572 MessageLoop::current()->Run();
575 ASSERT_EQ(14U, order
.Size());
576 EXPECT_EQ(order
.Get(0), TaskItem(RECURSIVE
, 1, true));
577 EXPECT_EQ(order
.Get(1), TaskItem(RECURSIVE
, 1, false));
578 EXPECT_EQ(order
.Get(2), TaskItem(RECURSIVE
, 2, true));
579 EXPECT_EQ(order
.Get(3), TaskItem(RECURSIVE
, 2, false));
580 EXPECT_EQ(order
.Get(4), TaskItem(QUITMESSAGELOOP
, 3, true));
581 EXPECT_EQ(order
.Get(5), TaskItem(QUITMESSAGELOOP
, 3, false));
582 EXPECT_EQ(order
.Get(6), TaskItem(RECURSIVE
, 1, true));
583 EXPECT_EQ(order
.Get(7), TaskItem(RECURSIVE
, 1, false));
584 EXPECT_EQ(order
.Get(8), TaskItem(RECURSIVE
, 2, true));
585 EXPECT_EQ(order
.Get(9), TaskItem(RECURSIVE
, 2, false));
586 EXPECT_EQ(order
.Get(10), TaskItem(RECURSIVE
, 1, true));
587 EXPECT_EQ(order
.Get(11), TaskItem(RECURSIVE
, 1, false));
588 EXPECT_EQ(order
.Get(12), TaskItem(RECURSIVE
, 2, true));
589 EXPECT_EQ(order
.Get(13), TaskItem(RECURSIVE
, 2, false));
592 // Tests that non nestable tasks run in FIFO if there are no nested loops.
593 void RunTest_NonNestableWithNoNesting(MessagePumpFactory factory
) {
594 scoped_ptr
<MessagePump
> pump(factory());
595 MessageLoop
loop(pump
.Pass());
599 MessageLoop::current()->PostNonNestableTask(
601 Bind(&OrderedFunc
, &order
, 1));
602 MessageLoop::current()->PostTask(FROM_HERE
,
603 Bind(&OrderedFunc
, &order
, 2));
604 MessageLoop::current()->PostTask(FROM_HERE
,
605 Bind(&QuitFunc
, &order
, 3));
606 MessageLoop::current()->Run();
609 ASSERT_EQ(6U, order
.Size());
610 EXPECT_EQ(order
.Get(0), TaskItem(ORDERED
, 1, true));
611 EXPECT_EQ(order
.Get(1), TaskItem(ORDERED
, 1, false));
612 EXPECT_EQ(order
.Get(2), TaskItem(ORDERED
, 2, true));
613 EXPECT_EQ(order
.Get(3), TaskItem(ORDERED
, 2, false));
614 EXPECT_EQ(order
.Get(4), TaskItem(QUITMESSAGELOOP
, 3, true));
615 EXPECT_EQ(order
.Get(5), TaskItem(QUITMESSAGELOOP
, 3, false));
618 void FuncThatPumps(TaskList
* order
, int cookie
) {
619 order
->RecordStart(PUMPS
, cookie
);
621 MessageLoop::ScopedNestableTaskAllower
allow(MessageLoop::current());
622 RunLoop().RunUntilIdle();
624 order
->RecordEnd(PUMPS
, cookie
);
627 void SleepFunc(TaskList
* order
, int cookie
, TimeDelta delay
) {
628 order
->RecordStart(SLEEP
, cookie
);
629 PlatformThread::Sleep(delay
);
630 order
->RecordEnd(SLEEP
, cookie
);
633 // Tests that non nestable tasks don't run when there's code in the call stack.
634 void RunTest_NonNestableInNestedLoop(MessagePumpFactory factory
,
636 scoped_ptr
<MessagePump
> pump(factory());
637 MessageLoop
loop(pump
.Pass());
641 MessageLoop::current()->PostTask(
643 Bind(&FuncThatPumps
, &order
, 1));
645 MessageLoop::current()->PostNonNestableDelayedTask(
647 Bind(&OrderedFunc
, &order
, 2),
648 TimeDelta::FromMilliseconds(1));
650 MessageLoop::current()->PostNonNestableTask(
652 Bind(&OrderedFunc
, &order
, 2));
654 MessageLoop::current()->PostTask(FROM_HERE
,
655 Bind(&OrderedFunc
, &order
, 3));
656 MessageLoop::current()->PostTask(
658 Bind(&SleepFunc
, &order
, 4, TimeDelta::FromMilliseconds(50)));
659 MessageLoop::current()->PostTask(FROM_HERE
,
660 Bind(&OrderedFunc
, &order
, 5));
662 MessageLoop::current()->PostNonNestableDelayedTask(
664 Bind(&QuitFunc
, &order
, 6),
665 TimeDelta::FromMilliseconds(2));
667 MessageLoop::current()->PostNonNestableTask(
669 Bind(&QuitFunc
, &order
, 6));
672 MessageLoop::current()->Run();
675 ASSERT_EQ(12U, order
.Size());
676 EXPECT_EQ(order
.Get(0), TaskItem(PUMPS
, 1, true));
677 EXPECT_EQ(order
.Get(1), TaskItem(ORDERED
, 3, true));
678 EXPECT_EQ(order
.Get(2), TaskItem(ORDERED
, 3, false));
679 EXPECT_EQ(order
.Get(3), TaskItem(SLEEP
, 4, true));
680 EXPECT_EQ(order
.Get(4), TaskItem(SLEEP
, 4, false));
681 EXPECT_EQ(order
.Get(5), TaskItem(ORDERED
, 5, true));
682 EXPECT_EQ(order
.Get(6), TaskItem(ORDERED
, 5, false));
683 EXPECT_EQ(order
.Get(7), TaskItem(PUMPS
, 1, false));
684 EXPECT_EQ(order
.Get(8), TaskItem(ORDERED
, 2, true));
685 EXPECT_EQ(order
.Get(9), TaskItem(ORDERED
, 2, false));
686 EXPECT_EQ(order
.Get(10), TaskItem(QUITMESSAGELOOP
, 6, true));
687 EXPECT_EQ(order
.Get(11), TaskItem(QUITMESSAGELOOP
, 6, false));
690 void FuncThatRuns(TaskList
* order
, int cookie
, RunLoop
* run_loop
) {
691 order
->RecordStart(RUNS
, cookie
);
693 MessageLoop::ScopedNestableTaskAllower
allow(MessageLoop::current());
696 order
->RecordEnd(RUNS
, cookie
);
699 void FuncThatQuitsNow() {
700 MessageLoop::current()->QuitNow();
702 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
703 void RunTest_QuitNow(MessagePumpFactory factory
) {
704 scoped_ptr
<MessagePump
> pump(factory());
705 MessageLoop
loop(pump
.Pass());
711 MessageLoop::current()->PostTask(FROM_HERE
,
712 Bind(&FuncThatRuns
, &order
, 1, Unretained(&run_loop
)));
713 MessageLoop::current()->PostTask(
714 FROM_HERE
, Bind(&OrderedFunc
, &order
, 2));
715 MessageLoop::current()->PostTask(
716 FROM_HERE
, Bind(&FuncThatQuitsNow
));
717 MessageLoop::current()->PostTask(
718 FROM_HERE
, Bind(&OrderedFunc
, &order
, 3));
719 MessageLoop::current()->PostTask(
720 FROM_HERE
, Bind(&FuncThatQuitsNow
));
721 MessageLoop::current()->PostTask(
722 FROM_HERE
, Bind(&OrderedFunc
, &order
, 4)); // never runs
724 MessageLoop::current()->Run();
726 ASSERT_EQ(6U, order
.Size());
728 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 1, true));
729 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 2, true));
730 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 2, false));
731 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 1, false));
732 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 3, true));
733 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 3, false));
734 EXPECT_EQ(static_cast<size_t>(task_index
), order
.Size());
737 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
738 void RunTest_RunLoopQuitTop(MessagePumpFactory factory
) {
739 scoped_ptr
<MessagePump
> pump(factory());
740 MessageLoop
loop(pump
.Pass());
744 RunLoop outer_run_loop
;
745 RunLoop nested_run_loop
;
747 MessageLoop::current()->PostTask(FROM_HERE
,
748 Bind(&FuncThatRuns
, &order
, 1, Unretained(&nested_run_loop
)));
749 MessageLoop::current()->PostTask(
750 FROM_HERE
, outer_run_loop
.QuitClosure());
751 MessageLoop::current()->PostTask(
752 FROM_HERE
, Bind(&OrderedFunc
, &order
, 2));
753 MessageLoop::current()->PostTask(
754 FROM_HERE
, nested_run_loop
.QuitClosure());
756 outer_run_loop
.Run();
758 ASSERT_EQ(4U, order
.Size());
760 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 1, true));
761 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 2, true));
762 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 2, false));
763 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 1, false));
764 EXPECT_EQ(static_cast<size_t>(task_index
), order
.Size());
767 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
768 void RunTest_RunLoopQuitNested(MessagePumpFactory factory
) {
769 scoped_ptr
<MessagePump
> pump(factory());
770 MessageLoop
loop(pump
.Pass());
774 RunLoop outer_run_loop
;
775 RunLoop nested_run_loop
;
777 MessageLoop::current()->PostTask(FROM_HERE
,
778 Bind(&FuncThatRuns
, &order
, 1, Unretained(&nested_run_loop
)));
779 MessageLoop::current()->PostTask(
780 FROM_HERE
, nested_run_loop
.QuitClosure());
781 MessageLoop::current()->PostTask(
782 FROM_HERE
, Bind(&OrderedFunc
, &order
, 2));
783 MessageLoop::current()->PostTask(
784 FROM_HERE
, outer_run_loop
.QuitClosure());
786 outer_run_loop
.Run();
788 ASSERT_EQ(4U, order
.Size());
790 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 1, true));
791 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 1, false));
792 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 2, true));
793 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 2, false));
794 EXPECT_EQ(static_cast<size_t>(task_index
), order
.Size());
797 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
798 void RunTest_RunLoopQuitBogus(MessagePumpFactory factory
) {
799 scoped_ptr
<MessagePump
> pump(factory());
800 MessageLoop
loop(pump
.Pass());
804 RunLoop outer_run_loop
;
805 RunLoop nested_run_loop
;
806 RunLoop bogus_run_loop
;
808 MessageLoop::current()->PostTask(FROM_HERE
,
809 Bind(&FuncThatRuns
, &order
, 1, Unretained(&nested_run_loop
)));
810 MessageLoop::current()->PostTask(
811 FROM_HERE
, bogus_run_loop
.QuitClosure());
812 MessageLoop::current()->PostTask(
813 FROM_HERE
, Bind(&OrderedFunc
, &order
, 2));
814 MessageLoop::current()->PostTask(
815 FROM_HERE
, outer_run_loop
.QuitClosure());
816 MessageLoop::current()->PostTask(
817 FROM_HERE
, nested_run_loop
.QuitClosure());
819 outer_run_loop
.Run();
821 ASSERT_EQ(4U, order
.Size());
823 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 1, true));
824 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 2, true));
825 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 2, false));
826 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 1, false));
827 EXPECT_EQ(static_cast<size_t>(task_index
), order
.Size());
830 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
831 void RunTest_RunLoopQuitDeep(MessagePumpFactory factory
) {
832 scoped_ptr
<MessagePump
> pump(factory());
833 MessageLoop
loop(pump
.Pass());
837 RunLoop outer_run_loop
;
838 RunLoop nested_loop1
;
839 RunLoop nested_loop2
;
840 RunLoop nested_loop3
;
841 RunLoop nested_loop4
;
843 MessageLoop::current()->PostTask(FROM_HERE
,
844 Bind(&FuncThatRuns
, &order
, 1, Unretained(&nested_loop1
)));
845 MessageLoop::current()->PostTask(FROM_HERE
,
846 Bind(&FuncThatRuns
, &order
, 2, Unretained(&nested_loop2
)));
847 MessageLoop::current()->PostTask(FROM_HERE
,
848 Bind(&FuncThatRuns
, &order
, 3, Unretained(&nested_loop3
)));
849 MessageLoop::current()->PostTask(FROM_HERE
,
850 Bind(&FuncThatRuns
, &order
, 4, Unretained(&nested_loop4
)));
851 MessageLoop::current()->PostTask(
852 FROM_HERE
, Bind(&OrderedFunc
, &order
, 5));
853 MessageLoop::current()->PostTask(
854 FROM_HERE
, outer_run_loop
.QuitClosure());
855 MessageLoop::current()->PostTask(
856 FROM_HERE
, Bind(&OrderedFunc
, &order
, 6));
857 MessageLoop::current()->PostTask(
858 FROM_HERE
, nested_loop1
.QuitClosure());
859 MessageLoop::current()->PostTask(
860 FROM_HERE
, Bind(&OrderedFunc
, &order
, 7));
861 MessageLoop::current()->PostTask(
862 FROM_HERE
, nested_loop2
.QuitClosure());
863 MessageLoop::current()->PostTask(
864 FROM_HERE
, Bind(&OrderedFunc
, &order
, 8));
865 MessageLoop::current()->PostTask(
866 FROM_HERE
, nested_loop3
.QuitClosure());
867 MessageLoop::current()->PostTask(
868 FROM_HERE
, Bind(&OrderedFunc
, &order
, 9));
869 MessageLoop::current()->PostTask(
870 FROM_HERE
, nested_loop4
.QuitClosure());
871 MessageLoop::current()->PostTask(
872 FROM_HERE
, Bind(&OrderedFunc
, &order
, 10));
874 outer_run_loop
.Run();
876 ASSERT_EQ(18U, order
.Size());
878 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 1, true));
879 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 2, true));
880 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 3, true));
881 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 4, true));
882 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 5, true));
883 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 5, false));
884 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 6, true));
885 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 6, false));
886 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 7, true));
887 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 7, false));
888 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 8, true));
889 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 8, false));
890 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 9, true));
891 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 9, false));
892 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 4, false));
893 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 3, false));
894 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 2, false));
895 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 1, false));
896 EXPECT_EQ(static_cast<size_t>(task_index
), order
.Size());
899 // Tests RunLoopQuit works before RunWithID.
900 void RunTest_RunLoopQuitOrderBefore(MessagePumpFactory factory
) {
901 scoped_ptr
<MessagePump
> pump(factory());
902 MessageLoop
loop(pump
.Pass());
910 MessageLoop::current()->PostTask(
911 FROM_HERE
, Bind(&OrderedFunc
, &order
, 1)); // never runs
912 MessageLoop::current()->PostTask(
913 FROM_HERE
, Bind(&FuncThatQuitsNow
)); // never runs
917 ASSERT_EQ(0U, order
.Size());
920 // Tests RunLoopQuit works during RunWithID.
921 void RunTest_RunLoopQuitOrderDuring(MessagePumpFactory factory
) {
922 scoped_ptr
<MessagePump
> pump(factory());
923 MessageLoop
loop(pump
.Pass());
929 MessageLoop::current()->PostTask(
930 FROM_HERE
, Bind(&OrderedFunc
, &order
, 1));
931 MessageLoop::current()->PostTask(
932 FROM_HERE
, run_loop
.QuitClosure());
933 MessageLoop::current()->PostTask(
934 FROM_HERE
, Bind(&OrderedFunc
, &order
, 2)); // never runs
935 MessageLoop::current()->PostTask(
936 FROM_HERE
, Bind(&FuncThatQuitsNow
)); // never runs
940 ASSERT_EQ(2U, order
.Size());
942 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 1, true));
943 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 1, false));
944 EXPECT_EQ(static_cast<size_t>(task_index
), order
.Size());
947 // Tests RunLoopQuit works after RunWithID.
948 void RunTest_RunLoopQuitOrderAfter(MessagePumpFactory factory
) {
949 scoped_ptr
<MessagePump
> pump(factory());
950 MessageLoop
loop(pump
.Pass());
956 MessageLoop::current()->PostTask(FROM_HERE
,
957 Bind(&FuncThatRuns
, &order
, 1, Unretained(&run_loop
)));
958 MessageLoop::current()->PostTask(
959 FROM_HERE
, Bind(&OrderedFunc
, &order
, 2));
960 MessageLoop::current()->PostTask(
961 FROM_HERE
, Bind(&FuncThatQuitsNow
));
962 MessageLoop::current()->PostTask(
963 FROM_HERE
, Bind(&OrderedFunc
, &order
, 3));
964 MessageLoop::current()->PostTask(
965 FROM_HERE
, run_loop
.QuitClosure()); // has no affect
966 MessageLoop::current()->PostTask(
967 FROM_HERE
, Bind(&OrderedFunc
, &order
, 4));
968 MessageLoop::current()->PostTask(
969 FROM_HERE
, Bind(&FuncThatQuitsNow
));
971 RunLoop outer_run_loop
;
972 outer_run_loop
.Run();
974 ASSERT_EQ(8U, order
.Size());
976 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 1, true));
977 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 2, true));
978 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 2, false));
979 EXPECT_EQ(order
.Get(task_index
++), TaskItem(RUNS
, 1, false));
980 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 3, true));
981 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 3, false));
982 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 4, true));
983 EXPECT_EQ(order
.Get(task_index
++), TaskItem(ORDERED
, 4, false));
984 EXPECT_EQ(static_cast<size_t>(task_index
), order
.Size());
987 void PostNTasksThenQuit(int posts_remaining
) {
988 if (posts_remaining
> 1) {
989 MessageLoop::current()->PostTask(
991 Bind(&PostNTasksThenQuit
, posts_remaining
- 1));
993 MessageLoop::current()->QuitWhenIdle();
997 // There was a bug in the MessagePumpGLib where posting tasks recursively
998 // caused the message loop to hang, due to the buffer of the internal pipe
999 // becoming full. Test all MessageLoop types to ensure this issue does not
1000 // exist in other MessagePumps.
1002 // On Linux, the pipe buffer size is 64KiB by default. The bug caused one
1003 // byte accumulated in the pipe per two posts, so we should repeat 128K
1004 // times to reproduce the bug.
1005 void RunTest_RecursivePosts(MessagePumpFactory factory
) {
1006 const int kNumTimes
= 1 << 17;
1007 scoped_ptr
<MessagePump
> pump(factory());
1008 MessageLoop
loop(pump
.Pass());
1009 loop
.PostTask(FROM_HERE
, Bind(&PostNTasksThenQuit
, kNumTimes
));