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

Side by Side Diff: base/task_scheduler/scheduler_worker_unittest.cc

Issue 2806413002: Separate the create and start phases in SchedulerSingleThreadTaskRunnerManager. (Closed)
Patch Set: self-review Created 3 years, 8 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/task_scheduler/scheduler_worker.h" 5 #include "base/task_scheduler/scheduler_worker.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <vector> 10 #include <vector>
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 class TaskSchedulerWorkerTest : public testing::TestWithParam<size_t> { 70 class TaskSchedulerWorkerTest : public testing::TestWithParam<size_t> {
71 protected: 71 protected:
72 TaskSchedulerWorkerTest() 72 TaskSchedulerWorkerTest()
73 : main_entry_called_(WaitableEvent::ResetPolicy::MANUAL, 73 : main_entry_called_(WaitableEvent::ResetPolicy::MANUAL,
74 WaitableEvent::InitialState::NOT_SIGNALED), 74 WaitableEvent::InitialState::NOT_SIGNALED),
75 num_get_work_cv_(lock_.CreateConditionVariable()), 75 num_get_work_cv_(lock_.CreateConditionVariable()),
76 worker_set_(WaitableEvent::ResetPolicy::MANUAL, 76 worker_set_(WaitableEvent::ResetPolicy::MANUAL,
77 WaitableEvent::InitialState::NOT_SIGNALED) {} 77 WaitableEvent::InitialState::NOT_SIGNALED) {}
78 78
79 void SetUp() override { 79 void SetUp() override {
80 worker_ = SchedulerWorker::Create( 80 worker_ = make_scoped_refptr(new SchedulerWorker(
81 ThreadPriority::NORMAL, MakeUnique<TestSchedulerWorkerDelegate>(this), 81 ThreadPriority::NORMAL, MakeUnique<TestSchedulerWorkerDelegate>(this),
82 &task_tracker_, SchedulerWorker::InitialState::ALIVE); 82 &task_tracker_));
83 ASSERT_TRUE(worker_); 83 ASSERT_TRUE(worker_);
84 worker_->Start();
84 worker_set_.Signal(); 85 worker_set_.Signal();
85 main_entry_called_.Wait(); 86 main_entry_called_.Wait();
86 } 87 }
87 88
88 void TearDown() override { 89 void TearDown() override {
89 worker_->JoinForTesting(); 90 worker_->JoinForTesting();
90 } 91 }
91 92
92 size_t TasksPerSequence() const { return GetParam(); } 93 size_t TasksPerSequence() const { return GetParam(); }
93 94
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 void UnblockCanDetach() { can_detach_block_.Signal(); } 384 void UnblockCanDetach() { can_detach_block_.Signal(); }
384 385
385 void WaitForWorkToRun() { work_processed_.Wait(); } 386 void WaitForWorkToRun() { work_processed_.Wait(); }
386 387
387 void WaitForDetachRequest() { detach_requested_.Wait(); } 388 void WaitForDetachRequest() { detach_requested_.Wait(); }
388 389
389 void WaitForDetach() { detached_.Wait(); } 390 void WaitForDetach() { detached_.Wait(); }
390 391
391 void WaitForDelegateDestroy() { destroyed_.Wait(); } 392 void WaitForDelegateDestroy() { destroyed_.Wait(); }
392 393
394 void set_expect_get_work(bool expect_get_work) {
395 expect_get_work_ = expect_get_work;
396 }
397
393 void ResetState() { 398 void ResetState() {
394 work_running_.Signal(); 399 work_running_.Signal();
395 work_processed_.Reset(); 400 work_processed_.Reset();
396 detach_requested_.Reset(); 401 detach_requested_.Reset();
397 can_detach_block_.Signal(); 402 can_detach_block_.Signal();
398 work_requested_ = false; 403 work_requested_ = false;
399 } 404 }
400 405
401 void set_can_detach(bool can_detach) { can_detach_ = can_detach; } 406 void set_can_detach(bool can_detach) { can_detach_ = can_detach; }
402 407
403 private: 408 private:
404 friend class ControllableDetachDelegate; 409 friend class ControllableDetachDelegate;
405 friend class RefCountedThreadSafe<Controls>; 410 friend class RefCountedThreadSafe<Controls>;
406 ~Controls() = default; 411 ~Controls() = default;
407 412
408 WaitableEvent work_running_; 413 WaitableEvent work_running_;
409 WaitableEvent work_processed_; 414 WaitableEvent work_processed_;
410 WaitableEvent detach_requested_; 415 WaitableEvent detach_requested_;
411 WaitableEvent detached_; 416 WaitableEvent detached_;
412 WaitableEvent can_detach_block_; 417 WaitableEvent can_detach_block_;
413 WaitableEvent destroyed_; 418 WaitableEvent destroyed_;
414 419
420 bool expect_get_work_ = true;
415 bool can_detach_ = false; 421 bool can_detach_ = false;
416 bool work_requested_ = false; 422 bool work_requested_ = false;
417 423
418 DISALLOW_COPY_AND_ASSIGN(Controls); 424 DISALLOW_COPY_AND_ASSIGN(Controls);
419 }; 425 };
420 426
421 ControllableDetachDelegate(TaskTracker* task_tracker) 427 ControllableDetachDelegate(TaskTracker* task_tracker)
422 : task_tracker_(task_tracker), controls_(new Controls()) {} 428 : task_tracker_(task_tracker), controls_(new Controls()) {}
423 429
424 ~ControllableDetachDelegate() override { controls_->destroyed_.Signal(); } 430 ~ControllableDetachDelegate() override { controls_->destroyed_.Signal(); }
425 431
426 scoped_refptr<Sequence> GetWork(SchedulerWorker* worker) 432 scoped_refptr<Sequence> GetWork(SchedulerWorker* worker)
427 override { 433 override {
434 EXPECT_TRUE(controls_->expect_get_work_);
435
428 // Sends one item of work to signal |work_processed_|. On subsequent calls, 436 // Sends one item of work to signal |work_processed_|. On subsequent calls,
429 // sends nullptr to indicate there's no more work to be done. 437 // sends nullptr to indicate there's no more work to be done.
430 if (controls_->work_requested_) 438 if (controls_->work_requested_)
431 return nullptr; 439 return nullptr;
432 440
433 controls_->work_requested_ = true; 441 controls_->work_requested_ = true;
434 scoped_refptr<Sequence> sequence(new Sequence); 442 scoped_refptr<Sequence> sequence(new Sequence);
435 std::unique_ptr<Task> task(new Task( 443 std::unique_ptr<Task> task(new Task(
436 FROM_HERE, 444 FROM_HERE,
437 Bind( 445 Bind(
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 502
495 TEST(TaskSchedulerWorkerTest, WorkerDetaches) { 503 TEST(TaskSchedulerWorkerTest, WorkerDetaches) {
496 TaskTracker task_tracker; 504 TaskTracker task_tracker;
497 // Will be owned by SchedulerWorker. 505 // Will be owned by SchedulerWorker.
498 MockedControllableDetachDelegate* delegate = 506 MockedControllableDetachDelegate* delegate =
499 new StrictMock<MockedControllableDetachDelegate>(&task_tracker); 507 new StrictMock<MockedControllableDetachDelegate>(&task_tracker);
500 scoped_refptr<ControllableDetachDelegate::Controls> controls = 508 scoped_refptr<ControllableDetachDelegate::Controls> controls =
501 delegate->controls(); 509 delegate->controls();
502 controls->set_can_detach(true); 510 controls->set_can_detach(true);
503 EXPECT_CALL(*delegate, OnMainEntry(_)); 511 EXPECT_CALL(*delegate, OnMainEntry(_));
504 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( 512 auto worker = make_scoped_refptr(new SchedulerWorker(
505 ThreadPriority::NORMAL, WrapUnique(delegate), &task_tracker, 513 ThreadPriority::NORMAL, WrapUnique(delegate), &task_tracker));
506 SchedulerWorker::InitialState::ALIVE); 514 worker->Start();
507 worker->WakeUp(); 515 worker->WakeUp();
508 controls->WaitForWorkToRun(); 516 controls->WaitForWorkToRun();
509 Mock::VerifyAndClear(delegate); 517 Mock::VerifyAndClear(delegate);
510 controls->WaitForDetachRequest(); 518 controls->WaitForDetachRequest();
511 controls->WaitForDetach(); 519 controls->WaitForDetach();
512 ASSERT_FALSE(worker->ThreadAliveForTesting()); 520 ASSERT_FALSE(worker->ThreadAliveForTesting());
513 } 521 }
514 522
515 TEST(TaskSchedulerWorkerTest, WorkerCleanupBeforeDetach) { 523 TEST(TaskSchedulerWorkerTest, WorkerCleanupBeforeDetach) {
516 TaskTracker task_tracker; 524 TaskTracker task_tracker;
517 // Will be owned by SchedulerWorker. 525 // Will be owned by SchedulerWorker.
518 // No mock here as that's reasonably covered by other tests and the delegate 526 // No mock here as that's reasonably covered by other tests and the delegate
519 // may destroy on a different thread. Mocks aren't designed with that in mind. 527 // may destroy on a different thread. Mocks aren't designed with that in mind.
520 std::unique_ptr<ControllableDetachDelegate> delegate = 528 std::unique_ptr<ControllableDetachDelegate> delegate =
521 MakeUnique<ControllableDetachDelegate>(&task_tracker); 529 MakeUnique<ControllableDetachDelegate>(&task_tracker);
522 scoped_refptr<ControllableDetachDelegate::Controls> controls = 530 scoped_refptr<ControllableDetachDelegate::Controls> controls =
523 delegate->controls(); 531 delegate->controls();
524 532
525 controls->set_can_detach(true); 533 controls->set_can_detach(true);
526 controls->MakeCanDetachBlock(); 534 controls->MakeCanDetachBlock();
527 535
528 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( 536 auto worker = make_scoped_refptr(new SchedulerWorker(
529 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, 537 ThreadPriority::NORMAL, std::move(delegate), &task_tracker));
530 SchedulerWorker::InitialState::ALIVE); 538 worker->Start();
531 worker->WakeUp(); 539 worker->WakeUp();
532 540
533 controls->WaitForDetachRequest(); 541 controls->WaitForDetachRequest();
534 worker->Cleanup(); 542 worker->Cleanup();
535 worker = nullptr; 543 worker = nullptr;
536 controls->UnblockCanDetach(); 544 controls->UnblockCanDetach();
537 controls->WaitForDelegateDestroy(); 545 controls->WaitForDelegateDestroy();
538 } 546 }
539 547
540 TEST(TaskSchedulerWorkerTest, WorkerCleanupAfterDetach) { 548 TEST(TaskSchedulerWorkerTest, WorkerCleanupAfterDetach) {
541 TaskTracker task_tracker; 549 TaskTracker task_tracker;
542 // Will be owned by SchedulerWorker. 550 // Will be owned by SchedulerWorker.
543 // No mock here as that's reasonably covered by other tests and the delegate 551 // No mock here as that's reasonably covered by other tests and the delegate
544 // may destroy on a different thread. Mocks aren't designed with that in mind. 552 // may destroy on a different thread. Mocks aren't designed with that in mind.
545 std::unique_ptr<ControllableDetachDelegate> delegate = 553 std::unique_ptr<ControllableDetachDelegate> delegate =
546 MakeUnique<ControllableDetachDelegate>(&task_tracker); 554 MakeUnique<ControllableDetachDelegate>(&task_tracker);
547 scoped_refptr<ControllableDetachDelegate::Controls> controls = 555 scoped_refptr<ControllableDetachDelegate::Controls> controls =
548 delegate->controls(); 556 delegate->controls();
549 557
550 controls->set_can_detach(true); 558 controls->set_can_detach(true);
551 559
552 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( 560 auto worker = make_scoped_refptr(new SchedulerWorker(
553 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, 561 ThreadPriority::NORMAL, std::move(delegate), &task_tracker));
554 SchedulerWorker::InitialState::ALIVE); 562 worker->Start();
555 worker->WakeUp(); 563 worker->WakeUp();
556 564
557 controls->WaitForDetach(); 565 controls->WaitForDetach();
558 worker->Cleanup(); 566 worker->Cleanup();
559 worker = nullptr; 567 worker = nullptr;
560 controls->WaitForDelegateDestroy(); 568 controls->WaitForDelegateDestroy();
561 } 569 }
562 570
563 TEST(TaskSchedulerWorkerTest, WorkerCleanupDuringWork) { 571 TEST(TaskSchedulerWorkerTest, WorkerCleanupDuringWork) {
564 TaskTracker task_tracker; 572 TaskTracker task_tracker;
565 // Will be owned by SchedulerWorker. 573 // Will be owned by SchedulerWorker.
566 // No mock here as that's reasonably covered by other tests and the delegate 574 // No mock here as that's reasonably covered by other tests and the delegate
567 // may destroy on a different thread. Mocks aren't designed with that in mind. 575 // may destroy on a different thread. Mocks aren't designed with that in mind.
568 std::unique_ptr<ControllableDetachDelegate> delegate = 576 std::unique_ptr<ControllableDetachDelegate> delegate =
569 MakeUnique<ControllableDetachDelegate>(&task_tracker); 577 MakeUnique<ControllableDetachDelegate>(&task_tracker);
570 scoped_refptr<ControllableDetachDelegate::Controls> controls = 578 scoped_refptr<ControllableDetachDelegate::Controls> controls =
571 delegate->controls(); 579 delegate->controls();
572 580
573 controls->HaveWorkBlock(); 581 controls->HaveWorkBlock();
574 582
575 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( 583 auto worker = make_scoped_refptr(new SchedulerWorker(
576 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, 584 ThreadPriority::NORMAL, std::move(delegate), &task_tracker));
577 SchedulerWorker::InitialState::ALIVE); 585 worker->Start();
578 worker->WakeUp(); 586 worker->WakeUp();
579 587
580 controls->WaitForWorkToRun(); 588 controls->WaitForWorkToRun();
581 worker->Cleanup(); 589 worker->Cleanup();
582 worker = nullptr; 590 worker = nullptr;
583 controls->UnblockWork(); 591 controls->UnblockWork();
584 controls->WaitForDelegateDestroy(); 592 controls->WaitForDelegateDestroy();
585 } 593 }
586 594
587 TEST(TaskSchedulerWorkerTest, WorkerCleanupDuringWait) { 595 TEST(TaskSchedulerWorkerTest, WorkerCleanupDuringWait) {
588 TaskTracker task_tracker; 596 TaskTracker task_tracker;
589 // Will be owned by SchedulerWorker. 597 // Will be owned by SchedulerWorker.
590 // No mock here as that's reasonably covered by other tests and the delegate 598 // No mock here as that's reasonably covered by other tests and the delegate
591 // may destroy on a different thread. Mocks aren't designed with that in mind. 599 // may destroy on a different thread. Mocks aren't designed with that in mind.
592 std::unique_ptr<ControllableDetachDelegate> delegate = 600 std::unique_ptr<ControllableDetachDelegate> delegate =
593 MakeUnique<ControllableDetachDelegate>(&task_tracker); 601 MakeUnique<ControllableDetachDelegate>(&task_tracker);
594 scoped_refptr<ControllableDetachDelegate::Controls> controls = 602 scoped_refptr<ControllableDetachDelegate::Controls> controls =
595 delegate->controls(); 603 delegate->controls();
596 604
597 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( 605 auto worker = make_scoped_refptr(new SchedulerWorker(
598 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, 606 ThreadPriority::NORMAL, std::move(delegate), &task_tracker));
599 SchedulerWorker::InitialState::ALIVE); 607 worker->Start();
600 worker->WakeUp(); 608 worker->WakeUp();
601 609
602 controls->WaitForDetachRequest(); 610 controls->WaitForDetachRequest();
603 worker->Cleanup(); 611 worker->Cleanup();
604 worker = nullptr; 612 worker = nullptr;
605 controls->WaitForDelegateDestroy(); 613 controls->WaitForDelegateDestroy();
606 } 614 }
607 615
608 TEST(TaskSchedulerWorkerTest, WorkerCleanupDuringShutdown) { 616 TEST(TaskSchedulerWorkerTest, WorkerCleanupDuringShutdown) {
609 TaskTracker task_tracker; 617 TaskTracker task_tracker;
610 // Will be owned by SchedulerWorker. 618 // Will be owned by SchedulerWorker.
611 // No mock here as that's reasonably covered by other tests and the delegate 619 // No mock here as that's reasonably covered by other tests and the delegate
612 // may destroy on a different thread. Mocks aren't designed with that in mind. 620 // may destroy on a different thread. Mocks aren't designed with that in mind.
613 std::unique_ptr<ControllableDetachDelegate> delegate = 621 std::unique_ptr<ControllableDetachDelegate> delegate =
614 MakeUnique<ControllableDetachDelegate>(&task_tracker); 622 MakeUnique<ControllableDetachDelegate>(&task_tracker);
615 scoped_refptr<ControllableDetachDelegate::Controls> controls = 623 scoped_refptr<ControllableDetachDelegate::Controls> controls =
616 delegate->controls(); 624 delegate->controls();
617 625
618 controls->HaveWorkBlock(); 626 controls->HaveWorkBlock();
619 627
620 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( 628 auto worker = make_scoped_refptr(new SchedulerWorker(
621 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, 629 ThreadPriority::NORMAL, std::move(delegate), &task_tracker));
622 SchedulerWorker::InitialState::ALIVE); 630 worker->Start();
623 worker->WakeUp(); 631 worker->WakeUp();
624 632
625 controls->WaitForWorkToRun(); 633 controls->WaitForWorkToRun();
626 task_tracker.Shutdown(); 634 task_tracker.Shutdown();
627 worker->Cleanup(); 635 worker->Cleanup();
628 worker = nullptr; 636 worker = nullptr;
629 controls->UnblockWork(); 637 controls->UnblockWork();
630 controls->WaitForDelegateDestroy(); 638 controls->WaitForDelegateDestroy();
631 } 639 }
632 640
641 // Verify that Start() is a no-op after Cleanup().
642 TEST(TaskSchedulerWorkerTest, CleanupBeforeStart) {
643 TaskTracker task_tracker;
644 // Will be owned by SchedulerWorker.
645 // No mock here as that's reasonably covered by other tests and the delegate
646 // may destroy on a different thread. Mocks aren't designed with that in mind.
647 std::unique_ptr<ControllableDetachDelegate> delegate =
648 MakeUnique<ControllableDetachDelegate>(&task_tracker);
649 scoped_refptr<ControllableDetachDelegate::Controls> controls =
650 delegate->controls();
651 controls->set_expect_get_work(false);
652
653 auto worker = make_scoped_refptr(new SchedulerWorker(
654 ThreadPriority::NORMAL, std::move(delegate), &task_tracker));
655
656 worker->Cleanup();
657
658 worker->Start();
659 worker->WakeUp();
660
661 EXPECT_FALSE(worker->ThreadAliveForTesting());
662 }
663
633 namespace { 664 namespace {
634 665
635 class CallJoinFromDifferentThread : public SimpleThread { 666 class CallJoinFromDifferentThread : public SimpleThread {
636 public: 667 public:
637 CallJoinFromDifferentThread(SchedulerWorker* worker_to_join) 668 CallJoinFromDifferentThread(SchedulerWorker* worker_to_join)
638 : SimpleThread("SchedulerWorkerJoinThread"), 669 : SimpleThread("SchedulerWorkerJoinThread"),
639 worker_to_join_(worker_to_join), 670 worker_to_join_(worker_to_join),
640 run_started_event_(WaitableEvent::ResetPolicy::MANUAL, 671 run_started_event_(WaitableEvent::ResetPolicy::MANUAL,
641 WaitableEvent::InitialState::NOT_SIGNALED) {} 672 WaitableEvent::InitialState::NOT_SIGNALED) {}
642 673
(...skipping 20 matching lines...) Expand all
663 // No mock here as that's reasonably covered by other tests and the 694 // No mock here as that's reasonably covered by other tests and the
664 // delegate may destroy on a different thread. Mocks aren't designed with that 695 // delegate may destroy on a different thread. Mocks aren't designed with that
665 // in mind. 696 // in mind.
666 std::unique_ptr<ControllableDetachDelegate> delegate = 697 std::unique_ptr<ControllableDetachDelegate> delegate =
667 MakeUnique<ControllableDetachDelegate>(&task_tracker); 698 MakeUnique<ControllableDetachDelegate>(&task_tracker);
668 scoped_refptr<ControllableDetachDelegate::Controls> controls = 699 scoped_refptr<ControllableDetachDelegate::Controls> controls =
669 delegate->controls(); 700 delegate->controls();
670 701
671 controls->HaveWorkBlock(); 702 controls->HaveWorkBlock();
672 703
673 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( 704 auto worker = make_scoped_refptr(new SchedulerWorker(
674 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, 705 ThreadPriority::NORMAL, std::move(delegate), &task_tracker));
675 SchedulerWorker::InitialState::ALIVE); 706 worker->Start();
676 worker->WakeUp(); 707 worker->WakeUp();
677 708
678 controls->WaitForWorkToRun(); 709 controls->WaitForWorkToRun();
679 CallJoinFromDifferentThread join_from_different_thread(worker.get()); 710 CallJoinFromDifferentThread join_from_different_thread(worker.get());
680 join_from_different_thread.Start(); 711 join_from_different_thread.Start();
681 join_from_different_thread.WaitForRunToStart(); 712 join_from_different_thread.WaitForRunToStart();
682 // Sleep here to give the other thread a chance to call JoinForTesting(). 713 // Sleep here to give the other thread a chance to call JoinForTesting().
683 // Receiving a signal that Run() was called doesn't mean JoinForTesting() was 714 // Receiving a signal that Run() was called doesn't mean JoinForTesting() was
684 // necessarily called, and we can't signal after JoinForTesting() as 715 // necessarily called, and we can't signal after JoinForTesting() as
685 // JoinForTesting() blocks until we call UnblockWork(). 716 // JoinForTesting() blocks until we call UnblockWork().
686 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); 717 PlatformThread::Sleep(TestTimeouts::tiny_timeout());
687 worker->Cleanup(); 718 worker->Cleanup();
688 worker = nullptr; 719 worker = nullptr;
689 controls->UnblockWork(); 720 controls->UnblockWork();
690 controls->WaitForDelegateDestroy(); 721 controls->WaitForDelegateDestroy();
691 join_from_different_thread.Join(); 722 join_from_different_thread.Join();
692 } 723 }
693 724
694 TEST(TaskSchedulerWorkerTest, WorkerDetachesAndWakes) { 725 TEST(TaskSchedulerWorkerTest, WorkerDetachesAndWakes) {
695 TaskTracker task_tracker; 726 TaskTracker task_tracker;
696 // Will be owned by SchedulerWorker. 727 // Will be owned by SchedulerWorker.
697 MockedControllableDetachDelegate* delegate = 728 MockedControllableDetachDelegate* delegate =
698 new StrictMock<MockedControllableDetachDelegate>(&task_tracker); 729 new StrictMock<MockedControllableDetachDelegate>(&task_tracker);
699 scoped_refptr<ControllableDetachDelegate::Controls> controls = 730 scoped_refptr<ControllableDetachDelegate::Controls> controls =
700 delegate->controls(); 731 delegate->controls();
701 732
702 controls->set_can_detach(true); 733 controls->set_can_detach(true);
703 EXPECT_CALL(*delegate, OnMainEntry(_)); 734 EXPECT_CALL(*delegate, OnMainEntry(_));
704 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( 735 auto worker = make_scoped_refptr(new SchedulerWorker(
705 ThreadPriority::NORMAL, WrapUnique(delegate), &task_tracker, 736 ThreadPriority::NORMAL, WrapUnique(delegate), &task_tracker));
706 SchedulerWorker::InitialState::ALIVE); 737 worker->Start();
707 worker->WakeUp(); 738 worker->WakeUp();
708 controls->WaitForWorkToRun(); 739 controls->WaitForWorkToRun();
709 Mock::VerifyAndClear(delegate); 740 Mock::VerifyAndClear(delegate);
710 controls->WaitForDetachRequest(); 741 controls->WaitForDetachRequest();
711 controls->WaitForDetach(); 742 controls->WaitForDetach();
712 ASSERT_FALSE(worker->ThreadAliveForTesting()); 743 ASSERT_FALSE(worker->ThreadAliveForTesting());
713 744
714 controls->ResetState(); 745 controls->ResetState();
715 controls->set_can_detach(false); 746 controls->set_can_detach(false);
716 // Expect OnMainEntry() to be called when SchedulerWorker recreates its 747 // Expect OnMainEntry() to be called when SchedulerWorker recreates its
717 // thread. 748 // thread.
718 EXPECT_CALL(*delegate, OnMainEntry(worker.get())); 749 EXPECT_CALL(*delegate, OnMainEntry(worker.get()));
719 worker->WakeUp(); 750 worker->WakeUp();
720 controls->WaitForWorkToRun(); 751 controls->WaitForWorkToRun();
721 Mock::VerifyAndClear(delegate); 752 Mock::VerifyAndClear(delegate);
722 controls->WaitForDetachRequest(); 753 controls->WaitForDetachRequest();
723 controls->WaitForDetach(); 754 controls->WaitForDetach();
724 ASSERT_TRUE(worker->ThreadAliveForTesting()); 755 ASSERT_TRUE(worker->ThreadAliveForTesting());
725 worker->JoinForTesting(); 756 worker->JoinForTesting();
726 } 757 }
727 758
728 TEST(TaskSchedulerWorkerTest, CreateDetached) { 759 TEST(TaskSchedulerWorkerTest, StartDetached) {
729 TaskTracker task_tracker; 760 TaskTracker task_tracker;
730 // Will be owned by SchedulerWorker. 761 // Will be owned by SchedulerWorker.
731 MockedControllableDetachDelegate* delegate = 762 MockedControllableDetachDelegate* delegate =
732 new StrictMock<MockedControllableDetachDelegate>(&task_tracker); 763 new StrictMock<MockedControllableDetachDelegate>(&task_tracker);
733 scoped_refptr<ControllableDetachDelegate::Controls> controls = 764 scoped_refptr<ControllableDetachDelegate::Controls> controls =
734 delegate->controls(); 765 delegate->controls();
735 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( 766 auto worker = make_scoped_refptr(new SchedulerWorker(
736 ThreadPriority::NORMAL, WrapUnique(delegate), &task_tracker, 767 ThreadPriority::NORMAL, WrapUnique(delegate), &task_tracker));
737 SchedulerWorker::InitialState::DETACHED); 768 worker->Start(SchedulerWorker::InitialState::DETACHED);
738 ASSERT_FALSE(worker->ThreadAliveForTesting()); 769 ASSERT_FALSE(worker->ThreadAliveForTesting());
739 EXPECT_CALL(*delegate, OnMainEntry(worker.get())); 770 EXPECT_CALL(*delegate, OnMainEntry(worker.get()));
740 worker->WakeUp(); 771 worker->WakeUp();
741 controls->WaitForWorkToRun(); 772 controls->WaitForWorkToRun();
742 Mock::VerifyAndClear(delegate); 773 Mock::VerifyAndClear(delegate);
743 controls->WaitForDetachRequest(); 774 controls->WaitForDetachRequest();
744 ASSERT_TRUE(worker->ThreadAliveForTesting()); 775 ASSERT_TRUE(worker->ThreadAliveForTesting());
745 worker->JoinForTesting(); 776 worker->JoinForTesting();
746 } 777 }
747 778
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
796 TaskTracker task_tracker; 827 TaskTracker task_tracker;
797 828
798 std::unique_ptr<ExpectThreadPriorityDelegate> delegate( 829 std::unique_ptr<ExpectThreadPriorityDelegate> delegate(
799 new ExpectThreadPriorityDelegate); 830 new ExpectThreadPriorityDelegate);
800 ExpectThreadPriorityDelegate* delegate_raw = delegate.get(); 831 ExpectThreadPriorityDelegate* delegate_raw = delegate.get();
801 delegate_raw->SetExpectedThreadPriority( 832 delegate_raw->SetExpectedThreadPriority(
802 PlatformThread::CanIncreaseCurrentThreadPriority() 833 PlatformThread::CanIncreaseCurrentThreadPriority()
803 ? ThreadPriority::BACKGROUND 834 ? ThreadPriority::BACKGROUND
804 : ThreadPriority::NORMAL); 835 : ThreadPriority::NORMAL);
805 836
806 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( 837 auto worker = make_scoped_refptr(new SchedulerWorker(
807 ThreadPriority::BACKGROUND, std::move(delegate), &task_tracker, 838 ThreadPriority::BACKGROUND, std::move(delegate), &task_tracker));
808 SchedulerWorker::InitialState::ALIVE); 839 worker->Start();
809 840
810 // Verify that the initial thread priority is BACKGROUND (or NORMAL if thread 841 // Verify that the initial thread priority is BACKGROUND (or NORMAL if thread
811 // priority can't be increased). 842 // priority can't be increased).
812 worker->WakeUp(); 843 worker->WakeUp();
813 delegate_raw->WaitForPriorityVerifiedInGetWork(); 844 delegate_raw->WaitForPriorityVerifiedInGetWork();
814 845
815 // Verify that the thread priority is bumped to NORMAL during shutdown. 846 // Verify that the thread priority is bumped to NORMAL during shutdown.
816 delegate_raw->SetExpectedThreadPriority(ThreadPriority::NORMAL); 847 delegate_raw->SetExpectedThreadPriority(ThreadPriority::NORMAL);
817 task_tracker.SetHasShutdownStartedForTesting(); 848 task_tracker.SetHasShutdownStartedForTesting();
818 worker->WakeUp(); 849 worker->WakeUp();
819 delegate_raw->WaitForPriorityVerifiedInGetWork(); 850 delegate_raw->WaitForPriorityVerifiedInGetWork();
820 851
821 worker->JoinForTesting(); 852 worker->JoinForTesting();
822 } 853 }
823 854
824 TEST(TaskSchedulerWorkerTest, BumpPriorityOfDetachedThreadDuringShutdown) { 855 TEST(TaskSchedulerWorkerTest, BumpPriorityOfDetachedThreadDuringShutdown) {
825 TaskTracker task_tracker; 856 TaskTracker task_tracker;
826 857
827 std::unique_ptr<ExpectThreadPriorityDelegate> delegate( 858 std::unique_ptr<ExpectThreadPriorityDelegate> delegate(
828 new ExpectThreadPriorityDelegate); 859 new ExpectThreadPriorityDelegate);
829 ExpectThreadPriorityDelegate* delegate_raw = delegate.get(); 860 ExpectThreadPriorityDelegate* delegate_raw = delegate.get();
830 delegate_raw->SetExpectedThreadPriority(ThreadPriority::NORMAL); 861 delegate_raw->SetExpectedThreadPriority(ThreadPriority::NORMAL);
831 862
832 // Create a DETACHED thread. 863 // Create a DETACHED thread.
833 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( 864 auto worker = make_scoped_refptr(new SchedulerWorker(
834 ThreadPriority::BACKGROUND, std::move(delegate), &task_tracker, 865 ThreadPriority::BACKGROUND, std::move(delegate), &task_tracker));
835 SchedulerWorker::InitialState::DETACHED); 866 worker->Start(SchedulerWorker::InitialState::DETACHED);
836 867
837 // Pretend that shutdown has started. 868 // Pretend that shutdown has started.
838 task_tracker.SetHasShutdownStartedForTesting(); 869 task_tracker.SetHasShutdownStartedForTesting();
839 870
840 // Wake up the thread and verify that its priority is NORMAL when 871 // Wake up the thread and verify that its priority is NORMAL when
841 // OnMainEntry() and GetWork() are called. 872 // OnMainEntry() and GetWork() are called.
842 worker->WakeUp(); 873 worker->WakeUp();
843 delegate_raw->WaitForPriorityVerifiedInGetWork(); 874 delegate_raw->WaitForPriorityVerifiedInGetWork();
844 875
845 worker->JoinForTesting(); 876 worker->JoinForTesting();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 911
881 } // namespace 912 } // namespace
882 913
883 TEST(TaskSchedulerWorkerTest, BackwardCompatibilityEnabled) { 914 TEST(TaskSchedulerWorkerTest, BackwardCompatibilityEnabled) {
884 TaskTracker task_tracker; 915 TaskTracker task_tracker;
885 auto delegate = MakeUnique<CoInitializeDelegate>(); 916 auto delegate = MakeUnique<CoInitializeDelegate>();
886 CoInitializeDelegate* const delegate_raw = delegate.get(); 917 CoInitializeDelegate* const delegate_raw = delegate.get();
887 918
888 // Create a worker with backward compatibility ENABLED. Wake it up and wait 919 // Create a worker with backward compatibility ENABLED. Wake it up and wait
889 // until GetWork() returns. 920 // until GetWork() returns.
890 auto worker = SchedulerWorker::Create( 921 auto worker = make_scoped_refptr(new SchedulerWorker(
891 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, 922 ThreadPriority::NORMAL, std::move(delegate), &task_tracker,
892 SchedulerWorker::InitialState::ALIVE, 923 SchedulerBackwardCompatibility::INIT_COM_STA));
893 SchedulerBackwardCompatibility::INIT_COM_STA); 924 worker->Start();
894 worker->WakeUp(); 925 worker->WakeUp();
895 delegate_raw->WaitUntilGetWorkReturned(); 926 delegate_raw->WaitUntilGetWorkReturned();
896 927
897 // The call to CoInitializeEx() should have returned S_FALSE to indicate that 928 // The call to CoInitializeEx() should have returned S_FALSE to indicate that
898 // the COM library was already initialized on the thread. 929 // the COM library was already initialized on the thread.
899 EXPECT_EQ(S_FALSE, delegate_raw->coinitialize_hresult()); 930 EXPECT_EQ(S_FALSE, delegate_raw->coinitialize_hresult());
900 931
901 worker->JoinForTesting(); 932 worker->JoinForTesting();
902 } 933 }
903 934
904 TEST(TaskSchedulerWorkerTest, BackwardCompatibilityDisabled) { 935 TEST(TaskSchedulerWorkerTest, BackwardCompatibilityDisabled) {
905 TaskTracker task_tracker; 936 TaskTracker task_tracker;
906 auto delegate = MakeUnique<CoInitializeDelegate>(); 937 auto delegate = MakeUnique<CoInitializeDelegate>();
907 CoInitializeDelegate* const delegate_raw = delegate.get(); 938 CoInitializeDelegate* const delegate_raw = delegate.get();
908 939
909 // Create a worker with backward compatibility DISABLED. Wake it up and wait 940 // Create a worker with backward compatibility DISABLED. Wake it up and wait
910 // until GetWork() returns. 941 // until GetWork() returns.
911 auto worker = SchedulerWorker::Create( 942 auto worker = make_scoped_refptr(new SchedulerWorker(
912 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, 943 ThreadPriority::NORMAL, std::move(delegate), &task_tracker,
913 SchedulerWorker::InitialState::ALIVE, 944 SchedulerBackwardCompatibility::DISABLED));
914 SchedulerBackwardCompatibility::DISABLED); 945 worker->Start();
915 worker->WakeUp(); 946 worker->WakeUp();
916 delegate_raw->WaitUntilGetWorkReturned(); 947 delegate_raw->WaitUntilGetWorkReturned();
917 948
918 // The call to CoInitializeEx() should have returned S_OK to indicate that the 949 // The call to CoInitializeEx() should have returned S_OK to indicate that the
919 // COM library wasn't already initialized on the thread. 950 // COM library wasn't already initialized on the thread.
920 EXPECT_EQ(S_OK, delegate_raw->coinitialize_hresult()); 951 EXPECT_EQ(S_OK, delegate_raw->coinitialize_hresult());
921 952
922 worker->JoinForTesting(); 953 worker->JoinForTesting();
923 } 954 }
924 955
925 #endif // defined(OS_WIN) 956 #endif // defined(OS_WIN)
926 957
927 } // namespace internal 958 } // namespace internal
928 } // namespace base 959 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698