| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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 Loading... |
| 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 |
| 664 // Verify that Start() is a no-op after JoinForTesting(). |
| 665 TEST(TaskSchedulerWorkerTest, JoinForTestingBeforeStart) { |
| 666 TaskTracker task_tracker; |
| 667 // Will be owned by SchedulerWorker. |
| 668 // No mock here as that's reasonably covered by other tests and the delegate |
| 669 // may destroy on a different thread. Mocks aren't designed with that in mind. |
| 670 std::unique_ptr<ControllableDetachDelegate> delegate = |
| 671 MakeUnique<ControllableDetachDelegate>(&task_tracker); |
| 672 scoped_refptr<ControllableDetachDelegate::Controls> controls = |
| 673 delegate->controls(); |
| 674 controls->set_expect_get_work(false); |
| 675 |
| 676 auto worker = make_scoped_refptr(new SchedulerWorker( |
| 677 ThreadPriority::NORMAL, std::move(delegate), &task_tracker)); |
| 678 |
| 679 worker->JoinForTesting(); |
| 680 |
| 681 worker->Start(); |
| 682 worker->WakeUp(); |
| 683 |
| 684 EXPECT_FALSE(worker->ThreadAliveForTesting()); |
| 685 } |
| 686 |
| 633 namespace { | 687 namespace { |
| 634 | 688 |
| 635 class CallJoinFromDifferentThread : public SimpleThread { | 689 class CallJoinFromDifferentThread : public SimpleThread { |
| 636 public: | 690 public: |
| 637 CallJoinFromDifferentThread(SchedulerWorker* worker_to_join) | 691 CallJoinFromDifferentThread(SchedulerWorker* worker_to_join) |
| 638 : SimpleThread("SchedulerWorkerJoinThread"), | 692 : SimpleThread("SchedulerWorkerJoinThread"), |
| 639 worker_to_join_(worker_to_join), | 693 worker_to_join_(worker_to_join), |
| 640 run_started_event_(WaitableEvent::ResetPolicy::MANUAL, | 694 run_started_event_(WaitableEvent::ResetPolicy::MANUAL, |
| 641 WaitableEvent::InitialState::NOT_SIGNALED) {} | 695 WaitableEvent::InitialState::NOT_SIGNALED) {} |
| 642 | 696 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 663 // No mock here as that's reasonably covered by other tests and the | 717 // 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 | 718 // delegate may destroy on a different thread. Mocks aren't designed with that |
| 665 // in mind. | 719 // in mind. |
| 666 std::unique_ptr<ControllableDetachDelegate> delegate = | 720 std::unique_ptr<ControllableDetachDelegate> delegate = |
| 667 MakeUnique<ControllableDetachDelegate>(&task_tracker); | 721 MakeUnique<ControllableDetachDelegate>(&task_tracker); |
| 668 scoped_refptr<ControllableDetachDelegate::Controls> controls = | 722 scoped_refptr<ControllableDetachDelegate::Controls> controls = |
| 669 delegate->controls(); | 723 delegate->controls(); |
| 670 | 724 |
| 671 controls->HaveWorkBlock(); | 725 controls->HaveWorkBlock(); |
| 672 | 726 |
| 673 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( | 727 auto worker = make_scoped_refptr(new SchedulerWorker( |
| 674 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, | 728 ThreadPriority::NORMAL, std::move(delegate), &task_tracker)); |
| 675 SchedulerWorker::InitialState::ALIVE); | 729 worker->Start(); |
| 676 worker->WakeUp(); | 730 worker->WakeUp(); |
| 677 | 731 |
| 678 controls->WaitForWorkToRun(); | 732 controls->WaitForWorkToRun(); |
| 679 CallJoinFromDifferentThread join_from_different_thread(worker.get()); | 733 CallJoinFromDifferentThread join_from_different_thread(worker.get()); |
| 680 join_from_different_thread.Start(); | 734 join_from_different_thread.Start(); |
| 681 join_from_different_thread.WaitForRunToStart(); | 735 join_from_different_thread.WaitForRunToStart(); |
| 682 // Sleep here to give the other thread a chance to call JoinForTesting(). | 736 // 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 | 737 // Receiving a signal that Run() was called doesn't mean JoinForTesting() was |
| 684 // necessarily called, and we can't signal after JoinForTesting() as | 738 // necessarily called, and we can't signal after JoinForTesting() as |
| 685 // JoinForTesting() blocks until we call UnblockWork(). | 739 // JoinForTesting() blocks until we call UnblockWork(). |
| 686 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 740 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 687 worker->Cleanup(); | 741 worker->Cleanup(); |
| 688 worker = nullptr; | 742 worker = nullptr; |
| 689 controls->UnblockWork(); | 743 controls->UnblockWork(); |
| 690 controls->WaitForDelegateDestroy(); | 744 controls->WaitForDelegateDestroy(); |
| 691 join_from_different_thread.Join(); | 745 join_from_different_thread.Join(); |
| 692 } | 746 } |
| 693 | 747 |
| 694 TEST(TaskSchedulerWorkerTest, WorkerDetachesAndWakes) { | 748 TEST(TaskSchedulerWorkerTest, WorkerDetachesAndWakes) { |
| 695 TaskTracker task_tracker; | 749 TaskTracker task_tracker; |
| 696 // Will be owned by SchedulerWorker. | 750 // Will be owned by SchedulerWorker. |
| 697 MockedControllableDetachDelegate* delegate = | 751 MockedControllableDetachDelegate* delegate = |
| 698 new StrictMock<MockedControllableDetachDelegate>(&task_tracker); | 752 new StrictMock<MockedControllableDetachDelegate>(&task_tracker); |
| 699 scoped_refptr<ControllableDetachDelegate::Controls> controls = | 753 scoped_refptr<ControllableDetachDelegate::Controls> controls = |
| 700 delegate->controls(); | 754 delegate->controls(); |
| 701 | 755 |
| 702 controls->set_can_detach(true); | 756 controls->set_can_detach(true); |
| 703 EXPECT_CALL(*delegate, OnMainEntry(_)); | 757 EXPECT_CALL(*delegate, OnMainEntry(_)); |
| 704 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( | 758 auto worker = make_scoped_refptr(new SchedulerWorker( |
| 705 ThreadPriority::NORMAL, WrapUnique(delegate), &task_tracker, | 759 ThreadPriority::NORMAL, WrapUnique(delegate), &task_tracker)); |
| 706 SchedulerWorker::InitialState::ALIVE); | 760 worker->Start(); |
| 707 worker->WakeUp(); | 761 worker->WakeUp(); |
| 708 controls->WaitForWorkToRun(); | 762 controls->WaitForWorkToRun(); |
| 709 Mock::VerifyAndClear(delegate); | 763 Mock::VerifyAndClear(delegate); |
| 710 controls->WaitForDetachRequest(); | 764 controls->WaitForDetachRequest(); |
| 711 controls->WaitForDetach(); | 765 controls->WaitForDetach(); |
| 712 ASSERT_FALSE(worker->ThreadAliveForTesting()); | 766 ASSERT_FALSE(worker->ThreadAliveForTesting()); |
| 713 | 767 |
| 714 controls->ResetState(); | 768 controls->ResetState(); |
| 715 controls->set_can_detach(false); | 769 controls->set_can_detach(false); |
| 716 // Expect OnMainEntry() to be called when SchedulerWorker recreates its | 770 // Expect OnMainEntry() to be called when SchedulerWorker recreates its |
| 717 // thread. | 771 // thread. |
| 718 EXPECT_CALL(*delegate, OnMainEntry(worker.get())); | 772 EXPECT_CALL(*delegate, OnMainEntry(worker.get())); |
| 719 worker->WakeUp(); | 773 worker->WakeUp(); |
| 720 controls->WaitForWorkToRun(); | 774 controls->WaitForWorkToRun(); |
| 721 Mock::VerifyAndClear(delegate); | 775 Mock::VerifyAndClear(delegate); |
| 722 controls->WaitForDetachRequest(); | 776 controls->WaitForDetachRequest(); |
| 723 controls->WaitForDetach(); | 777 controls->WaitForDetach(); |
| 724 ASSERT_TRUE(worker->ThreadAliveForTesting()); | 778 ASSERT_TRUE(worker->ThreadAliveForTesting()); |
| 725 worker->JoinForTesting(); | 779 worker->JoinForTesting(); |
| 726 } | 780 } |
| 727 | 781 |
| 728 TEST(TaskSchedulerWorkerTest, CreateDetached) { | 782 TEST(TaskSchedulerWorkerTest, StartDetached) { |
| 729 TaskTracker task_tracker; | 783 TaskTracker task_tracker; |
| 730 // Will be owned by SchedulerWorker. | 784 // Will be owned by SchedulerWorker. |
| 731 MockedControllableDetachDelegate* delegate = | 785 MockedControllableDetachDelegate* delegate = |
| 732 new StrictMock<MockedControllableDetachDelegate>(&task_tracker); | 786 new StrictMock<MockedControllableDetachDelegate>(&task_tracker); |
| 733 scoped_refptr<ControllableDetachDelegate::Controls> controls = | 787 scoped_refptr<ControllableDetachDelegate::Controls> controls = |
| 734 delegate->controls(); | 788 delegate->controls(); |
| 735 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( | 789 auto worker = make_scoped_refptr(new SchedulerWorker( |
| 736 ThreadPriority::NORMAL, WrapUnique(delegate), &task_tracker, | 790 ThreadPriority::NORMAL, WrapUnique(delegate), &task_tracker)); |
| 737 SchedulerWorker::InitialState::DETACHED); | 791 worker->Start(SchedulerWorker::InitialState::DETACHED); |
| 738 ASSERT_FALSE(worker->ThreadAliveForTesting()); | 792 ASSERT_FALSE(worker->ThreadAliveForTesting()); |
| 739 EXPECT_CALL(*delegate, OnMainEntry(worker.get())); | 793 EXPECT_CALL(*delegate, OnMainEntry(worker.get())); |
| 740 worker->WakeUp(); | 794 worker->WakeUp(); |
| 741 controls->WaitForWorkToRun(); | 795 controls->WaitForWorkToRun(); |
| 742 Mock::VerifyAndClear(delegate); | 796 Mock::VerifyAndClear(delegate); |
| 743 controls->WaitForDetachRequest(); | 797 controls->WaitForDetachRequest(); |
| 744 ASSERT_TRUE(worker->ThreadAliveForTesting()); | 798 ASSERT_TRUE(worker->ThreadAliveForTesting()); |
| 745 worker->JoinForTesting(); | 799 worker->JoinForTesting(); |
| 746 } | 800 } |
| 747 | 801 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 796 TaskTracker task_tracker; | 850 TaskTracker task_tracker; |
| 797 | 851 |
| 798 std::unique_ptr<ExpectThreadPriorityDelegate> delegate( | 852 std::unique_ptr<ExpectThreadPriorityDelegate> delegate( |
| 799 new ExpectThreadPriorityDelegate); | 853 new ExpectThreadPriorityDelegate); |
| 800 ExpectThreadPriorityDelegate* delegate_raw = delegate.get(); | 854 ExpectThreadPriorityDelegate* delegate_raw = delegate.get(); |
| 801 delegate_raw->SetExpectedThreadPriority( | 855 delegate_raw->SetExpectedThreadPriority( |
| 802 PlatformThread::CanIncreaseCurrentThreadPriority() | 856 PlatformThread::CanIncreaseCurrentThreadPriority() |
| 803 ? ThreadPriority::BACKGROUND | 857 ? ThreadPriority::BACKGROUND |
| 804 : ThreadPriority::NORMAL); | 858 : ThreadPriority::NORMAL); |
| 805 | 859 |
| 806 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( | 860 auto worker = make_scoped_refptr(new SchedulerWorker( |
| 807 ThreadPriority::BACKGROUND, std::move(delegate), &task_tracker, | 861 ThreadPriority::BACKGROUND, std::move(delegate), &task_tracker)); |
| 808 SchedulerWorker::InitialState::ALIVE); | 862 worker->Start(); |
| 809 | 863 |
| 810 // Verify that the initial thread priority is BACKGROUND (or NORMAL if thread | 864 // Verify that the initial thread priority is BACKGROUND (or NORMAL if thread |
| 811 // priority can't be increased). | 865 // priority can't be increased). |
| 812 worker->WakeUp(); | 866 worker->WakeUp(); |
| 813 delegate_raw->WaitForPriorityVerifiedInGetWork(); | 867 delegate_raw->WaitForPriorityVerifiedInGetWork(); |
| 814 | 868 |
| 815 // Verify that the thread priority is bumped to NORMAL during shutdown. | 869 // Verify that the thread priority is bumped to NORMAL during shutdown. |
| 816 delegate_raw->SetExpectedThreadPriority(ThreadPriority::NORMAL); | 870 delegate_raw->SetExpectedThreadPriority(ThreadPriority::NORMAL); |
| 817 task_tracker.SetHasShutdownStartedForTesting(); | 871 task_tracker.SetHasShutdownStartedForTesting(); |
| 818 worker->WakeUp(); | 872 worker->WakeUp(); |
| 819 delegate_raw->WaitForPriorityVerifiedInGetWork(); | 873 delegate_raw->WaitForPriorityVerifiedInGetWork(); |
| 820 | 874 |
| 821 worker->JoinForTesting(); | 875 worker->JoinForTesting(); |
| 822 } | 876 } |
| 823 | 877 |
| 824 TEST(TaskSchedulerWorkerTest, BumpPriorityOfDetachedThreadDuringShutdown) { | 878 TEST(TaskSchedulerWorkerTest, BumpPriorityOfDetachedThreadDuringShutdown) { |
| 825 TaskTracker task_tracker; | 879 TaskTracker task_tracker; |
| 826 | 880 |
| 827 std::unique_ptr<ExpectThreadPriorityDelegate> delegate( | 881 std::unique_ptr<ExpectThreadPriorityDelegate> delegate( |
| 828 new ExpectThreadPriorityDelegate); | 882 new ExpectThreadPriorityDelegate); |
| 829 ExpectThreadPriorityDelegate* delegate_raw = delegate.get(); | 883 ExpectThreadPriorityDelegate* delegate_raw = delegate.get(); |
| 830 delegate_raw->SetExpectedThreadPriority(ThreadPriority::NORMAL); | 884 delegate_raw->SetExpectedThreadPriority(ThreadPriority::NORMAL); |
| 831 | 885 |
| 832 // Create a DETACHED thread. | 886 // Create a DETACHED thread. |
| 833 scoped_refptr<SchedulerWorker> worker = SchedulerWorker::Create( | 887 auto worker = make_scoped_refptr(new SchedulerWorker( |
| 834 ThreadPriority::BACKGROUND, std::move(delegate), &task_tracker, | 888 ThreadPriority::BACKGROUND, std::move(delegate), &task_tracker)); |
| 835 SchedulerWorker::InitialState::DETACHED); | 889 worker->Start(SchedulerWorker::InitialState::DETACHED); |
| 836 | 890 |
| 837 // Pretend that shutdown has started. | 891 // Pretend that shutdown has started. |
| 838 task_tracker.SetHasShutdownStartedForTesting(); | 892 task_tracker.SetHasShutdownStartedForTesting(); |
| 839 | 893 |
| 840 // Wake up the thread and verify that its priority is NORMAL when | 894 // Wake up the thread and verify that its priority is NORMAL when |
| 841 // OnMainEntry() and GetWork() are called. | 895 // OnMainEntry() and GetWork() are called. |
| 842 worker->WakeUp(); | 896 worker->WakeUp(); |
| 843 delegate_raw->WaitForPriorityVerifiedInGetWork(); | 897 delegate_raw->WaitForPriorityVerifiedInGetWork(); |
| 844 | 898 |
| 845 worker->JoinForTesting(); | 899 worker->JoinForTesting(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 880 | 934 |
| 881 } // namespace | 935 } // namespace |
| 882 | 936 |
| 883 TEST(TaskSchedulerWorkerTest, BackwardCompatibilityEnabled) { | 937 TEST(TaskSchedulerWorkerTest, BackwardCompatibilityEnabled) { |
| 884 TaskTracker task_tracker; | 938 TaskTracker task_tracker; |
| 885 auto delegate = MakeUnique<CoInitializeDelegate>(); | 939 auto delegate = MakeUnique<CoInitializeDelegate>(); |
| 886 CoInitializeDelegate* const delegate_raw = delegate.get(); | 940 CoInitializeDelegate* const delegate_raw = delegate.get(); |
| 887 | 941 |
| 888 // Create a worker with backward compatibility ENABLED. Wake it up and wait | 942 // Create a worker with backward compatibility ENABLED. Wake it up and wait |
| 889 // until GetWork() returns. | 943 // until GetWork() returns. |
| 890 auto worker = SchedulerWorker::Create( | 944 auto worker = make_scoped_refptr(new SchedulerWorker( |
| 891 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, | 945 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, |
| 892 SchedulerWorker::InitialState::ALIVE, | 946 SchedulerBackwardCompatibility::INIT_COM_STA)); |
| 893 SchedulerBackwardCompatibility::INIT_COM_STA); | 947 worker->Start(); |
| 894 worker->WakeUp(); | 948 worker->WakeUp(); |
| 895 delegate_raw->WaitUntilGetWorkReturned(); | 949 delegate_raw->WaitUntilGetWorkReturned(); |
| 896 | 950 |
| 897 // The call to CoInitializeEx() should have returned S_FALSE to indicate that | 951 // The call to CoInitializeEx() should have returned S_FALSE to indicate that |
| 898 // the COM library was already initialized on the thread. | 952 // the COM library was already initialized on the thread. |
| 899 EXPECT_EQ(S_FALSE, delegate_raw->coinitialize_hresult()); | 953 EXPECT_EQ(S_FALSE, delegate_raw->coinitialize_hresult()); |
| 900 | 954 |
| 901 worker->JoinForTesting(); | 955 worker->JoinForTesting(); |
| 902 } | 956 } |
| 903 | 957 |
| 904 TEST(TaskSchedulerWorkerTest, BackwardCompatibilityDisabled) { | 958 TEST(TaskSchedulerWorkerTest, BackwardCompatibilityDisabled) { |
| 905 TaskTracker task_tracker; | 959 TaskTracker task_tracker; |
| 906 auto delegate = MakeUnique<CoInitializeDelegate>(); | 960 auto delegate = MakeUnique<CoInitializeDelegate>(); |
| 907 CoInitializeDelegate* const delegate_raw = delegate.get(); | 961 CoInitializeDelegate* const delegate_raw = delegate.get(); |
| 908 | 962 |
| 909 // Create a worker with backward compatibility DISABLED. Wake it up and wait | 963 // Create a worker with backward compatibility DISABLED. Wake it up and wait |
| 910 // until GetWork() returns. | 964 // until GetWork() returns. |
| 911 auto worker = SchedulerWorker::Create( | 965 auto worker = make_scoped_refptr(new SchedulerWorker( |
| 912 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, | 966 ThreadPriority::NORMAL, std::move(delegate), &task_tracker, |
| 913 SchedulerWorker::InitialState::ALIVE, | 967 SchedulerBackwardCompatibility::DISABLED)); |
| 914 SchedulerBackwardCompatibility::DISABLED); | 968 worker->Start(); |
| 915 worker->WakeUp(); | 969 worker->WakeUp(); |
| 916 delegate_raw->WaitUntilGetWorkReturned(); | 970 delegate_raw->WaitUntilGetWorkReturned(); |
| 917 | 971 |
| 918 // The call to CoInitializeEx() should have returned S_OK to indicate that the | 972 // The call to CoInitializeEx() should have returned S_OK to indicate that the |
| 919 // COM library wasn't already initialized on the thread. | 973 // COM library wasn't already initialized on the thread. |
| 920 EXPECT_EQ(S_OK, delegate_raw->coinitialize_hresult()); | 974 EXPECT_EQ(S_OK, delegate_raw->coinitialize_hresult()); |
| 921 | 975 |
| 922 worker->JoinForTesting(); | 976 worker->JoinForTesting(); |
| 923 } | 977 } |
| 924 | 978 |
| 925 #endif // defined(OS_WIN) | 979 #endif // defined(OS_WIN) |
| 926 | 980 |
| 927 } // namespace internal | 981 } // namespace internal |
| 928 } // namespace base | 982 } // namespace base |
| OLD | NEW |