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

Side by Side Diff: base/message_loop/message_loop_test.cc

Issue 2375663002: Replace MessageLoop::current()->task_runner() with ThreadTaskRunnerHandle::Get(). (Closed)
Patch Set: rebase Created 4 years, 2 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 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 "base/message_loop/message_loop_test.h" 5 #include "base/message_loop/message_loop_test.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
16 #include "base/synchronization/waitable_event.h" 16 #include "base/synchronization/waitable_event.h"
17 #include "base/threading/thread.h" 17 #include "base/threading/thread.h"
18 #include "base/threading/thread_task_runner_handle.h"
18 19
19 namespace base { 20 namespace base {
20 namespace test { 21 namespace test {
21 22
22 namespace { 23 namespace {
23 24
24 class Foo : public RefCounted<Foo> { 25 class Foo : public RefCounted<Foo> {
25 public: 26 public:
26 Foo() : test_count_(0) { 27 Foo() : test_count_(0) {
27 } 28 }
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 } 91 }
91 92
92 } // namespace 93 } // namespace
93 94
94 void RunTest_PostTask(MessagePumpFactory factory) { 95 void RunTest_PostTask(MessagePumpFactory factory) {
95 std::unique_ptr<MessagePump> pump(factory()); 96 std::unique_ptr<MessagePump> pump(factory());
96 MessageLoop loop(std::move(pump)); 97 MessageLoop loop(std::move(pump));
97 // Add tests to message loop 98 // Add tests to message loop
98 scoped_refptr<Foo> foo(new Foo()); 99 scoped_refptr<Foo> foo(new Foo());
99 std::string a("a"), b("b"), c("c"), d("d"); 100 std::string a("a"), b("b"), c("c"), d("d");
100 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 101 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind(&Foo::Test0, foo));
101 Bind(&Foo::Test0, foo)); 102 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
102 MessageLoop::current()->task_runner()->PostTask( 103 Bind(&Foo::Test1ConstRef, foo, a));
103 FROM_HERE, Bind(&Foo::Test1ConstRef, foo, a)); 104 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
104 MessageLoop::current()->task_runner()->PostTask( 105 Bind(&Foo::Test1Ptr, foo, &b));
105 FROM_HERE, Bind(&Foo::Test1Ptr, foo, &b)); 106 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
106 MessageLoop::current()->task_runner()->PostTask( 107 Bind(&Foo::Test1Int, foo, 100));
107 FROM_HERE, Bind(&Foo::Test1Int, foo, 100)); 108 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
108 MessageLoop::current()->task_runner()->PostTask( 109 Bind(&Foo::Test2Ptr, foo, &a, &c));
109 FROM_HERE, Bind(&Foo::Test2Ptr, foo, &a, &c)); 110 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
110 MessageLoop::current()->task_runner()->PostTask( 111 Bind(&Foo::Test2Mixed, foo, a, &d));
111 FROM_HERE, Bind(&Foo::Test2Mixed, foo, a, &d));
112 // After all tests, post a message that will shut down the message loop 112 // After all tests, post a message that will shut down the message loop
113 MessageLoop::current()->task_runner()->PostTask( 113 ThreadTaskRunnerHandle::Get()->PostTask(
114 FROM_HERE, 114 FROM_HERE,
115 Bind(&MessageLoop::QuitWhenIdle, Unretained(MessageLoop::current()))); 115 Bind(&MessageLoop::QuitWhenIdle, Unretained(MessageLoop::current())));
116 116
117 // Now kick things off 117 // Now kick things off
118 RunLoop().Run(); 118 RunLoop().Run();
119 119
120 EXPECT_EQ(foo->test_count(), 105); 120 EXPECT_EQ(foo->test_count(), 105);
121 EXPECT_EQ(foo->result(), "abacad"); 121 EXPECT_EQ(foo->result(), "abacad");
122 } 122 }
123 123
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 : post_on_delete_(post_on_delete), was_deleted_(was_deleted) { 295 : post_on_delete_(post_on_delete), was_deleted_(was_deleted) {
296 } 296 }
297 void Run() {} 297 void Run() {}
298 298
299 private: 299 private:
300 friend class RefCounted<RecordDeletionProbe>; 300 friend class RefCounted<RecordDeletionProbe>;
301 301
302 ~RecordDeletionProbe() { 302 ~RecordDeletionProbe() {
303 *was_deleted_ = true; 303 *was_deleted_ = true;
304 if (post_on_delete_.get()) 304 if (post_on_delete_.get())
305 MessageLoop::current()->task_runner()->PostTask( 305 ThreadTaskRunnerHandle::Get()->PostTask(
306 FROM_HERE, Bind(&RecordDeletionProbe::Run, post_on_delete_)); 306 FROM_HERE, Bind(&RecordDeletionProbe::Run, post_on_delete_));
307 } 307 }
308 308
309 scoped_refptr<RecordDeletionProbe> post_on_delete_; 309 scoped_refptr<RecordDeletionProbe> post_on_delete_;
310 bool* was_deleted_; 310 bool* was_deleted_;
311 }; 311 };
312 312
313 void RunTest_EnsureDeletion(MessagePumpFactory factory) { 313 void RunTest_EnsureDeletion(MessagePumpFactory factory) {
314 bool a_was_deleted = false; 314 bool a_was_deleted = false;
315 bool b_was_deleted = false; 315 bool b_was_deleted = false;
(...skipping 28 matching lines...) Expand all
344 loop.task_runner()->PostTask(FROM_HERE, Bind(&RecordDeletionProbe::Run, c)); 344 loop.task_runner()->PostTask(FROM_HERE, Bind(&RecordDeletionProbe::Run, c));
345 } 345 }
346 EXPECT_TRUE(a_was_deleted); 346 EXPECT_TRUE(a_was_deleted);
347 EXPECT_TRUE(b_was_deleted); 347 EXPECT_TRUE(b_was_deleted);
348 EXPECT_TRUE(c_was_deleted); 348 EXPECT_TRUE(c_was_deleted);
349 } 349 }
350 350
351 void NestingFunc(int* depth) { 351 void NestingFunc(int* depth) {
352 if (*depth > 0) { 352 if (*depth > 0) {
353 *depth -= 1; 353 *depth -= 1;
354 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 354 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
355 Bind(&NestingFunc, depth)); 355 Bind(&NestingFunc, depth));
356 356
357 MessageLoop::current()->SetNestableTasksAllowed(true); 357 MessageLoop::current()->SetNestableTasksAllowed(true);
358 RunLoop().Run(); 358 RunLoop().Run();
359 } 359 }
360 MessageLoop::current()->QuitWhenIdle(); 360 MessageLoop::current()->QuitWhenIdle();
361 } 361 }
362 362
363 void RunTest_Nesting(MessagePumpFactory factory) { 363 void RunTest_Nesting(MessagePumpFactory factory) {
364 std::unique_ptr<MessagePump> pump(factory()); 364 std::unique_ptr<MessagePump> pump(factory());
365 MessageLoop loop(std::move(pump)); 365 MessageLoop loop(std::move(pump));
366 366
367 int depth = 100; 367 int depth = 100;
368 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 368 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
369 Bind(&NestingFunc, &depth)); 369 Bind(&NestingFunc, &depth));
370 RunLoop().Run(); 370 RunLoop().Run();
371 EXPECT_EQ(depth, 0); 371 EXPECT_EQ(depth, 0);
372 } 372 }
373 373
374 // A NestingObserver that tracks the number of nested message loop starts it 374 // A NestingObserver that tracks the number of nested message loop starts it
375 // has seen. 375 // has seen.
376 class TestNestingObserver : public MessageLoop::NestingObserver { 376 class TestNestingObserver : public MessageLoop::NestingObserver {
377 public: 377 public:
378 TestNestingObserver() {} 378 TestNestingObserver() {}
379 ~TestNestingObserver() override {} 379 ~TestNestingObserver() override {}
(...skipping 16 matching lines...) Expand all
396 // Starts a nested message loop. 396 // Starts a nested message loop.
397 void RunNestedLoop(TestNestingObserver* observer, 397 void RunNestedLoop(TestNestingObserver* observer,
398 const Closure& quit_outer_loop) { 398 const Closure& quit_outer_loop) {
399 // The nested loop hasn't started yet. 399 // The nested loop hasn't started yet.
400 EXPECT_EQ(0, observer->begin_nested_loop_count()); 400 EXPECT_EQ(0, observer->begin_nested_loop_count());
401 401
402 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); 402 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
403 RunLoop nested_loop; 403 RunLoop nested_loop;
404 // Verify that by the time the first task is run the observer has seen the 404 // Verify that by the time the first task is run the observer has seen the
405 // message loop begin. 405 // message loop begin.
406 MessageLoop::current()->task_runner()->PostTask( 406 ThreadTaskRunnerHandle::Get()->PostTask(
407 FROM_HERE, Bind(&ExpectOneBeginNestedLoop, observer)); 407 FROM_HERE, Bind(&ExpectOneBeginNestedLoop, observer));
408 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 408 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, nested_loop.QuitClosure());
409 nested_loop.QuitClosure());
410 nested_loop.Run(); 409 nested_loop.Run();
411 410
412 // Quitting message loops doesn't change the begin count. 411 // Quitting message loops doesn't change the begin count.
413 EXPECT_EQ(1, observer->begin_nested_loop_count()); 412 EXPECT_EQ(1, observer->begin_nested_loop_count());
414 413
415 quit_outer_loop.Run(); 414 quit_outer_loop.Run();
416 } 415 }
417 416
418 // Tests that a NestingObserver is notified when a nested message loop begins. 417 // Tests that a NestingObserver is notified when a nested message loop begins.
419 void RunTest_NestingObserver(MessagePumpFactory factory) { 418 void RunTest_NestingObserver(MessagePumpFactory factory) {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 private: 510 private:
512 std::vector<TaskItem> task_list_; 511 std::vector<TaskItem> task_list_;
513 }; 512 };
514 513
515 void RecursiveFunc(TaskList* order, int cookie, int depth, 514 void RecursiveFunc(TaskList* order, int cookie, int depth,
516 bool is_reentrant) { 515 bool is_reentrant) {
517 order->RecordStart(RECURSIVE, cookie); 516 order->RecordStart(RECURSIVE, cookie);
518 if (depth > 0) { 517 if (depth > 0) {
519 if (is_reentrant) 518 if (is_reentrant)
520 MessageLoop::current()->SetNestableTasksAllowed(true); 519 MessageLoop::current()->SetNestableTasksAllowed(true);
521 MessageLoop::current()->task_runner()->PostTask( 520 ThreadTaskRunnerHandle::Get()->PostTask(
522 FROM_HERE, 521 FROM_HERE,
523 Bind(&RecursiveFunc, order, cookie, depth - 1, is_reentrant)); 522 Bind(&RecursiveFunc, order, cookie, depth - 1, is_reentrant));
524 } 523 }
525 order->RecordEnd(RECURSIVE, cookie); 524 order->RecordEnd(RECURSIVE, cookie);
526 } 525 }
527 526
528 void QuitFunc(TaskList* order, int cookie) { 527 void QuitFunc(TaskList* order, int cookie) {
529 order->RecordStart(QUITMESSAGELOOP, cookie); 528 order->RecordStart(QUITMESSAGELOOP, cookie);
530 MessageLoop::current()->QuitWhenIdle(); 529 MessageLoop::current()->QuitWhenIdle();
531 order->RecordEnd(QUITMESSAGELOOP, cookie); 530 order->RecordEnd(QUITMESSAGELOOP, cookie);
532 } 531 }
533 void RunTest_RecursiveDenial1(MessagePumpFactory factory) { 532 void RunTest_RecursiveDenial1(MessagePumpFactory factory) {
534 std::unique_ptr<MessagePump> pump(factory()); 533 std::unique_ptr<MessagePump> pump(factory());
535 MessageLoop loop(std::move(pump)); 534 MessageLoop loop(std::move(pump));
536 535
537 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); 536 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
538 TaskList order; 537 TaskList order;
539 MessageLoop::current()->task_runner()->PostTask( 538 ThreadTaskRunnerHandle::Get()->PostTask(
540 FROM_HERE, Bind(&RecursiveFunc, &order, 1, 2, false)); 539 FROM_HERE, Bind(&RecursiveFunc, &order, 1, 2, false));
541 MessageLoop::current()->task_runner()->PostTask( 540 ThreadTaskRunnerHandle::Get()->PostTask(
542 FROM_HERE, Bind(&RecursiveFunc, &order, 2, 2, false)); 541 FROM_HERE, Bind(&RecursiveFunc, &order, 2, 2, false));
543 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 542 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
544 Bind(&QuitFunc, &order, 3)); 543 Bind(&QuitFunc, &order, 3));
545 544
546 RunLoop().Run(); 545 RunLoop().Run();
547 546
548 // FIFO order. 547 // FIFO order.
549 ASSERT_EQ(14U, order.Size()); 548 ASSERT_EQ(14U, order.Size());
550 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); 549 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
551 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); 550 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
552 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true)); 551 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true));
553 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false)); 552 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false));
554 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); 553 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true));
(...skipping 18 matching lines...) Expand all
573 order->RecordStart(ORDERED, cookie); 572 order->RecordStart(ORDERED, cookie);
574 order->RecordEnd(ORDERED, cookie); 573 order->RecordEnd(ORDERED, cookie);
575 } 574 }
576 575
577 void RunTest_RecursiveDenial3(MessagePumpFactory factory) { 576 void RunTest_RecursiveDenial3(MessagePumpFactory factory) {
578 std::unique_ptr<MessagePump> pump(factory()); 577 std::unique_ptr<MessagePump> pump(factory());
579 MessageLoop loop(std::move(pump)); 578 MessageLoop loop(std::move(pump));
580 579
581 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); 580 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
582 TaskList order; 581 TaskList order;
583 MessageLoop::current()->task_runner()->PostTask( 582 ThreadTaskRunnerHandle::Get()->PostTask(
584 FROM_HERE, Bind(&RecursiveSlowFunc, &order, 1, 2, false)); 583 FROM_HERE, Bind(&RecursiveSlowFunc, &order, 1, 2, false));
585 MessageLoop::current()->task_runner()->PostTask( 584 ThreadTaskRunnerHandle::Get()->PostTask(
586 FROM_HERE, Bind(&RecursiveSlowFunc, &order, 2, 2, false)); 585 FROM_HERE, Bind(&RecursiveSlowFunc, &order, 2, 2, false));
587 MessageLoop::current()->task_runner()->PostDelayedTask( 586 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
588 FROM_HERE, Bind(&OrderedFunc, &order, 3), TimeDelta::FromMilliseconds(5)); 587 FROM_HERE, Bind(&OrderedFunc, &order, 3), TimeDelta::FromMilliseconds(5));
589 MessageLoop::current()->task_runner()->PostDelayedTask( 588 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
590 FROM_HERE, Bind(&QuitFunc, &order, 4), TimeDelta::FromMilliseconds(5)); 589 FROM_HERE, Bind(&QuitFunc, &order, 4), TimeDelta::FromMilliseconds(5));
591 590
592 RunLoop().Run(); 591 RunLoop().Run();
593 592
594 // FIFO order. 593 // FIFO order.
595 ASSERT_EQ(16U, order.Size()); 594 ASSERT_EQ(16U, order.Size());
596 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); 595 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
597 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); 596 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
598 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true)); 597 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true));
599 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false)); 598 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false));
600 EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 1, true)); 599 EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 1, true));
601 EXPECT_EQ(order.Get(5), TaskItem(RECURSIVE, 1, false)); 600 EXPECT_EQ(order.Get(5), TaskItem(RECURSIVE, 1, false));
602 EXPECT_EQ(order.Get(6), TaskItem(ORDERED, 3, true)); 601 EXPECT_EQ(order.Get(6), TaskItem(ORDERED, 3, true));
603 EXPECT_EQ(order.Get(7), TaskItem(ORDERED, 3, false)); 602 EXPECT_EQ(order.Get(7), TaskItem(ORDERED, 3, false));
604 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true)); 603 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true));
605 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false)); 604 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false));
606 EXPECT_EQ(order.Get(10), TaskItem(QUITMESSAGELOOP, 4, true)); 605 EXPECT_EQ(order.Get(10), TaskItem(QUITMESSAGELOOP, 4, true));
607 EXPECT_EQ(order.Get(11), TaskItem(QUITMESSAGELOOP, 4, false)); 606 EXPECT_EQ(order.Get(11), TaskItem(QUITMESSAGELOOP, 4, false));
608 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 1, true)); 607 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 1, true));
609 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, false)); 608 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, false));
610 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 2, true)); 609 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 2, true));
611 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 2, false)); 610 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 2, false));
612 } 611 }
613 612
614 void RunTest_RecursiveSupport1(MessagePumpFactory factory) { 613 void RunTest_RecursiveSupport1(MessagePumpFactory factory) {
615 std::unique_ptr<MessagePump> pump(factory()); 614 std::unique_ptr<MessagePump> pump(factory());
616 MessageLoop loop(std::move(pump)); 615 MessageLoop loop(std::move(pump));
617 616
618 TaskList order; 617 TaskList order;
619 MessageLoop::current()->task_runner()->PostTask( 618 ThreadTaskRunnerHandle::Get()->PostTask(
620 FROM_HERE, Bind(&RecursiveFunc, &order, 1, 2, true)); 619 FROM_HERE, Bind(&RecursiveFunc, &order, 1, 2, true));
621 MessageLoop::current()->task_runner()->PostTask( 620 ThreadTaskRunnerHandle::Get()->PostTask(
622 FROM_HERE, Bind(&RecursiveFunc, &order, 2, 2, true)); 621 FROM_HERE, Bind(&RecursiveFunc, &order, 2, 2, true));
623 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 622 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
624 Bind(&QuitFunc, &order, 3)); 623 Bind(&QuitFunc, &order, 3));
625 624
626 RunLoop().Run(); 625 RunLoop().Run();
627 626
628 // FIFO order. 627 // FIFO order.
629 ASSERT_EQ(14U, order.Size()); 628 ASSERT_EQ(14U, order.Size());
630 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); 629 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
631 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); 630 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
632 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true)); 631 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true));
633 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false)); 632 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false));
634 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); 633 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true));
635 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false)); 634 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false));
636 EXPECT_EQ(order.Get(6), TaskItem(RECURSIVE, 1, true)); 635 EXPECT_EQ(order.Get(6), TaskItem(RECURSIVE, 1, true));
637 EXPECT_EQ(order.Get(7), TaskItem(RECURSIVE, 1, false)); 636 EXPECT_EQ(order.Get(7), TaskItem(RECURSIVE, 1, false));
638 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true)); 637 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true));
639 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false)); 638 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false));
640 EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true)); 639 EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true));
641 EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false)); 640 EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false));
642 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 2, true)); 641 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 2, true));
643 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 2, false)); 642 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 2, false));
644 } 643 }
645 644
646 // Tests that non nestable tasks run in FIFO if there are no nested loops. 645 // Tests that non nestable tasks run in FIFO if there are no nested loops.
647 void RunTest_NonNestableWithNoNesting(MessagePumpFactory factory) { 646 void RunTest_NonNestableWithNoNesting(MessagePumpFactory factory) {
648 std::unique_ptr<MessagePump> pump(factory()); 647 std::unique_ptr<MessagePump> pump(factory());
649 MessageLoop loop(std::move(pump)); 648 MessageLoop loop(std::move(pump));
650 649
651 TaskList order; 650 TaskList order;
652 651
653 MessageLoop::current()->task_runner()->PostNonNestableTask( 652 ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
654 FROM_HERE, 653 FROM_HERE, Bind(&OrderedFunc, &order, 1));
655 Bind(&OrderedFunc, &order, 1)); 654 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
656 MessageLoop::current()->task_runner()->PostTask( 655 Bind(&OrderedFunc, &order, 2));
657 FROM_HERE, 656 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
658 Bind(&OrderedFunc, &order, 2)); 657 Bind(&QuitFunc, &order, 3));
659 MessageLoop::current()->task_runner()->PostTask(FROM_HERE,
660 Bind(&QuitFunc, &order, 3));
661 RunLoop().Run(); 658 RunLoop().Run();
662 659
663 // FIFO order. 660 // FIFO order.
664 ASSERT_EQ(6U, order.Size()); 661 ASSERT_EQ(6U, order.Size());
665 EXPECT_EQ(order.Get(0), TaskItem(ORDERED, 1, true)); 662 EXPECT_EQ(order.Get(0), TaskItem(ORDERED, 1, true));
666 EXPECT_EQ(order.Get(1), TaskItem(ORDERED, 1, false)); 663 EXPECT_EQ(order.Get(1), TaskItem(ORDERED, 1, false));
667 EXPECT_EQ(order.Get(2), TaskItem(ORDERED, 2, true)); 664 EXPECT_EQ(order.Get(2), TaskItem(ORDERED, 2, true));
668 EXPECT_EQ(order.Get(3), TaskItem(ORDERED, 2, false)); 665 EXPECT_EQ(order.Get(3), TaskItem(ORDERED, 2, false));
669 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); 666 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true));
670 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false)); 667 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false));
(...skipping 14 matching lines...) Expand all
685 order->RecordEnd(SLEEP, cookie); 682 order->RecordEnd(SLEEP, cookie);
686 } 683 }
687 684
688 // Tests that non nestable tasks don't run when there's code in the call stack. 685 // Tests that non nestable tasks don't run when there's code in the call stack.
689 void RunTest_NonNestableInNestedLoop(MessagePumpFactory factory) { 686 void RunTest_NonNestableInNestedLoop(MessagePumpFactory factory) {
690 std::unique_ptr<MessagePump> pump(factory()); 687 std::unique_ptr<MessagePump> pump(factory());
691 MessageLoop loop(std::move(pump)); 688 MessageLoop loop(std::move(pump));
692 689
693 TaskList order; 690 TaskList order;
694 691
695 MessageLoop::current()->task_runner()->PostTask( 692 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
696 FROM_HERE, 693 Bind(&FuncThatPumps, &order, 1));
697 Bind(&FuncThatPumps, &order, 1)); 694 ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
698 MessageLoop::current()->task_runner()->PostNonNestableTask( 695 FROM_HERE, Bind(&OrderedFunc, &order, 2));
699 FROM_HERE, 696 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
700 Bind(&OrderedFunc, &order, 2)); 697 Bind(&OrderedFunc, &order, 3));
701 MessageLoop::current()->task_runner()->PostTask( 698 ThreadTaskRunnerHandle::Get()->PostTask(
702 FROM_HERE, 699 FROM_HERE, Bind(&SleepFunc, &order, 4, TimeDelta::FromMilliseconds(50)));
703 Bind(&OrderedFunc, &order, 3)); 700 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
704 MessageLoop::current()->task_runner()->PostTask( 701 Bind(&OrderedFunc, &order, 5));
705 FROM_HERE, 702 ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
706 Bind(&SleepFunc, &order, 4, TimeDelta::FromMilliseconds(50))); 703 FROM_HERE, Bind(&QuitFunc, &order, 6));
707 MessageLoop::current()->task_runner()->PostTask(
708 FROM_HERE,
709 Bind(&OrderedFunc, &order, 5));
710 MessageLoop::current()->task_runner()->PostNonNestableTask(
711 FROM_HERE,
712 Bind(&QuitFunc, &order, 6));
713 704
714 RunLoop().Run(); 705 RunLoop().Run();
715 706
716 // FIFO order. 707 // FIFO order.
717 ASSERT_EQ(12U, order.Size()); 708 ASSERT_EQ(12U, order.Size());
718 EXPECT_EQ(order.Get(0), TaskItem(PUMPS, 1, true)); 709 EXPECT_EQ(order.Get(0), TaskItem(PUMPS, 1, true));
719 EXPECT_EQ(order.Get(1), TaskItem(ORDERED, 3, true)); 710 EXPECT_EQ(order.Get(1), TaskItem(ORDERED, 3, true));
720 EXPECT_EQ(order.Get(2), TaskItem(ORDERED, 3, false)); 711 EXPECT_EQ(order.Get(2), TaskItem(ORDERED, 3, false));
721 EXPECT_EQ(order.Get(3), TaskItem(SLEEP, 4, true)); 712 EXPECT_EQ(order.Get(3), TaskItem(SLEEP, 4, true));
722 EXPECT_EQ(order.Get(4), TaskItem(SLEEP, 4, false)); 713 EXPECT_EQ(order.Get(4), TaskItem(SLEEP, 4, false));
(...skipping 20 matching lines...) Expand all
743 } 734 }
744 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. 735 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
745 void RunTest_QuitNow(MessagePumpFactory factory) { 736 void RunTest_QuitNow(MessagePumpFactory factory) {
746 std::unique_ptr<MessagePump> pump(factory()); 737 std::unique_ptr<MessagePump> pump(factory());
747 MessageLoop loop(std::move(pump)); 738 MessageLoop loop(std::move(pump));
748 739
749 TaskList order; 740 TaskList order;
750 741
751 RunLoop run_loop; 742 RunLoop run_loop;
752 743
753 MessageLoop::current()->task_runner()->PostTask( 744 ThreadTaskRunnerHandle::Get()->PostTask(
754 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&run_loop))); 745 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&run_loop)));
755 MessageLoop::current()->task_runner()->PostTask( 746 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
756 FROM_HERE, Bind(&OrderedFunc, &order, 2)); 747 Bind(&OrderedFunc, &order, 2));
757 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 748 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind(&FuncThatQuitsNow));
758 Bind(&FuncThatQuitsNow)); 749 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
759 MessageLoop::current()->task_runner()->PostTask( 750 Bind(&OrderedFunc, &order, 3));
760 FROM_HERE, Bind(&OrderedFunc, &order, 3)); 751 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind(&FuncThatQuitsNow));
761 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 752 ThreadTaskRunnerHandle::Get()->PostTask(
762 Bind(&FuncThatQuitsNow));
763 MessageLoop::current()->task_runner()->PostTask(
764 FROM_HERE, Bind(&OrderedFunc, &order, 4)); // never runs 753 FROM_HERE, Bind(&OrderedFunc, &order, 4)); // never runs
765 754
766 RunLoop().Run(); 755 RunLoop().Run();
767 756
768 ASSERT_EQ(6U, order.Size()); 757 ASSERT_EQ(6U, order.Size());
769 int task_index = 0; 758 int task_index = 0;
770 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); 759 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true));
771 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); 760 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true));
772 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); 761 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false));
773 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); 762 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false));
774 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, true)); 763 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, true));
775 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, false)); 764 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, false));
776 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); 765 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
777 } 766 }
778 767
779 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. 768 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
780 void RunTest_RunLoopQuitTop(MessagePumpFactory factory) { 769 void RunTest_RunLoopQuitTop(MessagePumpFactory factory) {
781 std::unique_ptr<MessagePump> pump(factory()); 770 std::unique_ptr<MessagePump> pump(factory());
782 MessageLoop loop(std::move(pump)); 771 MessageLoop loop(std::move(pump));
783 772
784 TaskList order; 773 TaskList order;
785 774
786 RunLoop outer_run_loop; 775 RunLoop outer_run_loop;
787 RunLoop nested_run_loop; 776 RunLoop nested_run_loop;
788 777
789 MessageLoop::current()->task_runner()->PostTask( 778 ThreadTaskRunnerHandle::Get()->PostTask(
790 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop))); 779 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop)));
791 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 780 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
792 outer_run_loop.QuitClosure()); 781 outer_run_loop.QuitClosure());
793 MessageLoop::current()->task_runner()->PostTask( 782 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
794 FROM_HERE, Bind(&OrderedFunc, &order, 2)); 783 Bind(&OrderedFunc, &order, 2));
795 MessageLoop::current()->task_runner()->PostTask( 784 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
796 FROM_HERE, nested_run_loop.QuitClosure()); 785 nested_run_loop.QuitClosure());
797 786
798 outer_run_loop.Run(); 787 outer_run_loop.Run();
799 788
800 ASSERT_EQ(4U, order.Size()); 789 ASSERT_EQ(4U, order.Size());
801 int task_index = 0; 790 int task_index = 0;
802 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); 791 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true));
803 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); 792 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true));
804 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); 793 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false));
805 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); 794 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false));
806 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); 795 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
807 } 796 }
808 797
809 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. 798 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
810 void RunTest_RunLoopQuitNested(MessagePumpFactory factory) { 799 void RunTest_RunLoopQuitNested(MessagePumpFactory factory) {
811 std::unique_ptr<MessagePump> pump(factory()); 800 std::unique_ptr<MessagePump> pump(factory());
812 MessageLoop loop(std::move(pump)); 801 MessageLoop loop(std::move(pump));
813 802
814 TaskList order; 803 TaskList order;
815 804
816 RunLoop outer_run_loop; 805 RunLoop outer_run_loop;
817 RunLoop nested_run_loop; 806 RunLoop nested_run_loop;
818 807
819 MessageLoop::current()->task_runner()->PostTask( 808 ThreadTaskRunnerHandle::Get()->PostTask(
820 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop))); 809 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop)));
821 MessageLoop::current()->task_runner()->PostTask( 810 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
822 FROM_HERE, nested_run_loop.QuitClosure()); 811 nested_run_loop.QuitClosure());
823 MessageLoop::current()->task_runner()->PostTask( 812 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
824 FROM_HERE, Bind(&OrderedFunc, &order, 2)); 813 Bind(&OrderedFunc, &order, 2));
825 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 814 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
826 outer_run_loop.QuitClosure()); 815 outer_run_loop.QuitClosure());
827 816
828 outer_run_loop.Run(); 817 outer_run_loop.Run();
829 818
830 ASSERT_EQ(4U, order.Size()); 819 ASSERT_EQ(4U, order.Size());
831 int task_index = 0; 820 int task_index = 0;
832 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); 821 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true));
833 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); 822 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false));
834 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); 823 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true));
835 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); 824 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false));
836 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); 825 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
837 } 826 }
838 827
839 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. 828 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
840 void RunTest_RunLoopQuitBogus(MessagePumpFactory factory) { 829 void RunTest_RunLoopQuitBogus(MessagePumpFactory factory) {
841 std::unique_ptr<MessagePump> pump(factory()); 830 std::unique_ptr<MessagePump> pump(factory());
842 MessageLoop loop(std::move(pump)); 831 MessageLoop loop(std::move(pump));
843 832
844 TaskList order; 833 TaskList order;
845 834
846 RunLoop outer_run_loop; 835 RunLoop outer_run_loop;
847 RunLoop nested_run_loop; 836 RunLoop nested_run_loop;
848 RunLoop bogus_run_loop; 837 RunLoop bogus_run_loop;
849 838
850 MessageLoop::current()->task_runner()->PostTask( 839 ThreadTaskRunnerHandle::Get()->PostTask(
851 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop))); 840 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop)));
852 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 841 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
853 bogus_run_loop.QuitClosure()); 842 bogus_run_loop.QuitClosure());
854 MessageLoop::current()->task_runner()->PostTask( 843 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
855 FROM_HERE, Bind(&OrderedFunc, &order, 2)); 844 Bind(&OrderedFunc, &order, 2));
856 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 845 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
857 outer_run_loop.QuitClosure()); 846 outer_run_loop.QuitClosure());
858 MessageLoop::current()->task_runner()->PostTask( 847 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
859 FROM_HERE, nested_run_loop.QuitClosure()); 848 nested_run_loop.QuitClosure());
860 849
861 outer_run_loop.Run(); 850 outer_run_loop.Run();
862 851
863 ASSERT_EQ(4U, order.Size()); 852 ASSERT_EQ(4U, order.Size());
864 int task_index = 0; 853 int task_index = 0;
865 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); 854 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true));
866 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); 855 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true));
867 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); 856 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false));
868 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); 857 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false));
869 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); 858 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
870 } 859 }
871 860
872 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. 861 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run.
873 void RunTest_RunLoopQuitDeep(MessagePumpFactory factory) { 862 void RunTest_RunLoopQuitDeep(MessagePumpFactory factory) {
874 std::unique_ptr<MessagePump> pump(factory()); 863 std::unique_ptr<MessagePump> pump(factory());
875 MessageLoop loop(std::move(pump)); 864 MessageLoop loop(std::move(pump));
876 865
877 TaskList order; 866 TaskList order;
878 867
879 RunLoop outer_run_loop; 868 RunLoop outer_run_loop;
880 RunLoop nested_loop1; 869 RunLoop nested_loop1;
881 RunLoop nested_loop2; 870 RunLoop nested_loop2;
882 RunLoop nested_loop3; 871 RunLoop nested_loop3;
883 RunLoop nested_loop4; 872 RunLoop nested_loop4;
884 873
885 MessageLoop::current()->task_runner()->PostTask( 874 ThreadTaskRunnerHandle::Get()->PostTask(
886 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&nested_loop1))); 875 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&nested_loop1)));
887 MessageLoop::current()->task_runner()->PostTask( 876 ThreadTaskRunnerHandle::Get()->PostTask(
888 FROM_HERE, Bind(&FuncThatRuns, &order, 2, Unretained(&nested_loop2))); 877 FROM_HERE, Bind(&FuncThatRuns, &order, 2, Unretained(&nested_loop2)));
889 MessageLoop::current()->task_runner()->PostTask( 878 ThreadTaskRunnerHandle::Get()->PostTask(
890 FROM_HERE, Bind(&FuncThatRuns, &order, 3, Unretained(&nested_loop3))); 879 FROM_HERE, Bind(&FuncThatRuns, &order, 3, Unretained(&nested_loop3)));
891 MessageLoop::current()->task_runner()->PostTask( 880 ThreadTaskRunnerHandle::Get()->PostTask(
892 FROM_HERE, Bind(&FuncThatRuns, &order, 4, Unretained(&nested_loop4))); 881 FROM_HERE, Bind(&FuncThatRuns, &order, 4, Unretained(&nested_loop4)));
893 MessageLoop::current()->task_runner()->PostTask( 882 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
894 FROM_HERE, Bind(&OrderedFunc, &order, 5)); 883 Bind(&OrderedFunc, &order, 5));
895 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 884 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
896 outer_run_loop.QuitClosure()); 885 outer_run_loop.QuitClosure());
897 MessageLoop::current()->task_runner()->PostTask( 886 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
898 FROM_HERE, Bind(&OrderedFunc, &order, 6)); 887 Bind(&OrderedFunc, &order, 6));
899 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 888 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
900 nested_loop1.QuitClosure()); 889 nested_loop1.QuitClosure());
901 MessageLoop::current()->task_runner()->PostTask( 890 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
902 FROM_HERE, Bind(&OrderedFunc, &order, 7)); 891 Bind(&OrderedFunc, &order, 7));
903 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 892 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
904 nested_loop2.QuitClosure()); 893 nested_loop2.QuitClosure());
905 MessageLoop::current()->task_runner()->PostTask( 894 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
906 FROM_HERE, Bind(&OrderedFunc, &order, 8)); 895 Bind(&OrderedFunc, &order, 8));
907 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 896 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
908 nested_loop3.QuitClosure()); 897 nested_loop3.QuitClosure());
909 MessageLoop::current()->task_runner()->PostTask( 898 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
910 FROM_HERE, Bind(&OrderedFunc, &order, 9)); 899 Bind(&OrderedFunc, &order, 9));
911 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 900 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
912 nested_loop4.QuitClosure()); 901 nested_loop4.QuitClosure());
913 MessageLoop::current()->task_runner()->PostTask( 902 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
914 FROM_HERE, Bind(&OrderedFunc, &order, 10)); 903 Bind(&OrderedFunc, &order, 10));
915 904
916 outer_run_loop.Run(); 905 outer_run_loop.Run();
917 906
918 ASSERT_EQ(18U, order.Size()); 907 ASSERT_EQ(18U, order.Size());
919 int task_index = 0; 908 int task_index = 0;
920 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); 909 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true));
921 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 2, true)); 910 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 2, true));
922 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 3, true)); 911 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 3, true));
923 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 4, true)); 912 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 4, true));
924 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 5, true)); 913 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 5, true));
(...skipping 17 matching lines...) Expand all
942 void RunTest_RunLoopQuitOrderBefore(MessagePumpFactory factory) { 931 void RunTest_RunLoopQuitOrderBefore(MessagePumpFactory factory) {
943 std::unique_ptr<MessagePump> pump(factory()); 932 std::unique_ptr<MessagePump> pump(factory());
944 MessageLoop loop(std::move(pump)); 933 MessageLoop loop(std::move(pump));
945 934
946 TaskList order; 935 TaskList order;
947 936
948 RunLoop run_loop; 937 RunLoop run_loop;
949 938
950 run_loop.Quit(); 939 run_loop.Quit();
951 940
952 MessageLoop::current()->task_runner()->PostTask( 941 ThreadTaskRunnerHandle::Get()->PostTask(
953 FROM_HERE, Bind(&OrderedFunc, &order, 1)); // never runs 942 FROM_HERE, Bind(&OrderedFunc, &order, 1)); // never runs
954 MessageLoop::current()->task_runner()->PostTask( 943 ThreadTaskRunnerHandle::Get()->PostTask(
955 FROM_HERE, Bind(&FuncThatQuitsNow)); // never runs 944 FROM_HERE, Bind(&FuncThatQuitsNow)); // never runs
956 945
957 run_loop.Run(); 946 run_loop.Run();
958 947
959 ASSERT_EQ(0U, order.Size()); 948 ASSERT_EQ(0U, order.Size());
960 } 949 }
961 950
962 // Tests RunLoopQuit works during RunWithID. 951 // Tests RunLoopQuit works during RunWithID.
963 void RunTest_RunLoopQuitOrderDuring(MessagePumpFactory factory) { 952 void RunTest_RunLoopQuitOrderDuring(MessagePumpFactory factory) {
964 std::unique_ptr<MessagePump> pump(factory()); 953 std::unique_ptr<MessagePump> pump(factory());
965 MessageLoop loop(std::move(pump)); 954 MessageLoop loop(std::move(pump));
966 955
967 TaskList order; 956 TaskList order;
968 957
969 RunLoop run_loop; 958 RunLoop run_loop;
970 959
971 MessageLoop::current()->task_runner()->PostTask( 960 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
972 FROM_HERE, Bind(&OrderedFunc, &order, 1)); 961 Bind(&OrderedFunc, &order, 1));
973 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 962 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_loop.QuitClosure());
974 run_loop.QuitClosure()); 963 ThreadTaskRunnerHandle::Get()->PostTask(
975 MessageLoop::current()->task_runner()->PostTask(
976 FROM_HERE, Bind(&OrderedFunc, &order, 2)); // never runs 964 FROM_HERE, Bind(&OrderedFunc, &order, 2)); // never runs
977 MessageLoop::current()->task_runner()->PostTask( 965 ThreadTaskRunnerHandle::Get()->PostTask(
978 FROM_HERE, Bind(&FuncThatQuitsNow)); // never runs 966 FROM_HERE, Bind(&FuncThatQuitsNow)); // never runs
979 967
980 run_loop.Run(); 968 run_loop.Run();
981 969
982 ASSERT_EQ(2U, order.Size()); 970 ASSERT_EQ(2U, order.Size());
983 int task_index = 0; 971 int task_index = 0;
984 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 1, true)); 972 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 1, true));
985 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 1, false)); 973 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 1, false));
986 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); 974 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
987 } 975 }
988 976
989 // Tests RunLoopQuit works after RunWithID. 977 // Tests RunLoopQuit works after RunWithID.
990 void RunTest_RunLoopQuitOrderAfter(MessagePumpFactory factory) { 978 void RunTest_RunLoopQuitOrderAfter(MessagePumpFactory factory) {
991 std::unique_ptr<MessagePump> pump(factory()); 979 std::unique_ptr<MessagePump> pump(factory());
992 MessageLoop loop(std::move(pump)); 980 MessageLoop loop(std::move(pump));
993 981
994 TaskList order; 982 TaskList order;
995 983
996 RunLoop run_loop; 984 RunLoop run_loop;
997 985
998 MessageLoop::current()->task_runner()->PostTask( 986 ThreadTaskRunnerHandle::Get()->PostTask(
999 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&run_loop))); 987 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&run_loop)));
1000 MessageLoop::current()->task_runner()->PostTask( 988 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
1001 FROM_HERE, Bind(&OrderedFunc, &order, 2)); 989 Bind(&OrderedFunc, &order, 2));
1002 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 990 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind(&FuncThatQuitsNow));
1003 Bind(&FuncThatQuitsNow)); 991 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
1004 MessageLoop::current()->task_runner()->PostTask( 992 Bind(&OrderedFunc, &order, 3));
1005 FROM_HERE, Bind(&OrderedFunc, &order, 3)); 993 ThreadTaskRunnerHandle::Get()->PostTask(
1006 MessageLoop::current()->task_runner()->PostTask(
1007 FROM_HERE, run_loop.QuitClosure()); // has no affect 994 FROM_HERE, run_loop.QuitClosure()); // has no affect
1008 MessageLoop::current()->task_runner()->PostTask( 995 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
1009 FROM_HERE, Bind(&OrderedFunc, &order, 4)); 996 Bind(&OrderedFunc, &order, 4));
1010 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, 997 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind(&FuncThatQuitsNow));
1011 Bind(&FuncThatQuitsNow));
1012 998
1013 RunLoop outer_run_loop; 999 RunLoop outer_run_loop;
1014 outer_run_loop.Run(); 1000 outer_run_loop.Run();
1015 1001
1016 ASSERT_EQ(8U, order.Size()); 1002 ASSERT_EQ(8U, order.Size());
1017 int task_index = 0; 1003 int task_index = 0;
1018 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); 1004 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true));
1019 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); 1005 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true));
1020 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); 1006 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false));
1021 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); 1007 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false));
1022 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, true)); 1008 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, true));
1023 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, false)); 1009 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, false));
1024 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 4, true)); 1010 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 4, true));
1025 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 4, false)); 1011 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 4, false));
1026 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); 1012 EXPECT_EQ(static_cast<size_t>(task_index), order.Size());
1027 } 1013 }
1028 1014
1029 void PostNTasksThenQuit(int posts_remaining) { 1015 void PostNTasksThenQuit(int posts_remaining) {
1030 if (posts_remaining > 1) { 1016 if (posts_remaining > 1) {
1031 MessageLoop::current()->task_runner()->PostTask( 1017 ThreadTaskRunnerHandle::Get()->PostTask(
1032 FROM_HERE, Bind(&PostNTasksThenQuit, posts_remaining - 1)); 1018 FROM_HERE, Bind(&PostNTasksThenQuit, posts_remaining - 1));
1033 } else { 1019 } else {
1034 MessageLoop::current()->QuitWhenIdle(); 1020 MessageLoop::current()->QuitWhenIdle();
1035 } 1021 }
1036 } 1022 }
1037 1023
1038 // There was a bug in the MessagePumpGLib where posting tasks recursively 1024 // There was a bug in the MessagePumpGLib where posting tasks recursively
1039 // caused the message loop to hang, due to the buffer of the internal pipe 1025 // caused the message loop to hang, due to the buffer of the internal pipe
1040 // becoming full. Test all MessageLoop types to ensure this issue does not 1026 // becoming full. Test all MessageLoop types to ensure this issue does not
1041 // exist in other MessagePumps. 1027 // exist in other MessagePumps.
1042 // 1028 //
1043 // On Linux, the pipe buffer size is 64KiB by default. The bug caused one 1029 // On Linux, the pipe buffer size is 64KiB by default. The bug caused one
1044 // byte accumulated in the pipe per two posts, so we should repeat 128K 1030 // byte accumulated in the pipe per two posts, so we should repeat 128K
1045 // times to reproduce the bug. 1031 // times to reproduce the bug.
1046 void RunTest_RecursivePosts(MessagePumpFactory factory) { 1032 void RunTest_RecursivePosts(MessagePumpFactory factory) {
1047 const int kNumTimes = 1 << 17; 1033 const int kNumTimes = 1 << 17;
1048 std::unique_ptr<MessagePump> pump(factory()); 1034 std::unique_ptr<MessagePump> pump(factory());
1049 MessageLoop loop(std::move(pump)); 1035 MessageLoop loop(std::move(pump));
1050 loop.task_runner()->PostTask(FROM_HERE, Bind(&PostNTasksThenQuit, kNumTimes)); 1036 loop.task_runner()->PostTask(FROM_HERE, Bind(&PostNTasksThenQuit, kNumTimes));
1051 RunLoop().Run(); 1037 RunLoop().Run();
1052 } 1038 }
1053 1039
1054 } // namespace test 1040 } // namespace test
1055 } // namespace base 1041 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/ref_counted_delete_on_message_loop.h ('k') | base/message_loop/message_loop_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698