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

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

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

Powered by Google App Engine
This is Rietveld 408576698