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_pool_impl.h" | 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <unordered_set> | 10 #include <unordered_set> |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 worker_pool_->WaitForAllWorkersIdleForTesting(); | 75 worker_pool_->WaitForAllWorkersIdleForTesting(); |
76 worker_pool_->JoinForTesting(); | 76 worker_pool_->JoinForTesting(); |
77 } | 77 } |
78 | 78 |
79 void CreateWorkerPool() { | 79 void CreateWorkerPool() { |
80 ASSERT_FALSE(worker_pool_); | 80 ASSERT_FALSE(worker_pool_); |
81 service_thread_.Start(); | 81 service_thread_.Start(); |
82 delayed_task_manager_.Start(service_thread_.task_runner()); | 82 delayed_task_manager_.Start(service_thread_.task_runner()); |
83 worker_pool_ = MakeUnique<SchedulerWorkerPoolImpl>( | 83 worker_pool_ = MakeUnique<SchedulerWorkerPoolImpl>( |
84 "TestWorkerPool", ThreadPriority::NORMAL, | 84 "TestWorkerPool", ThreadPriority::NORMAL, |
85 Bind(&TaskSchedulerWorkerPoolImplTest::ReEnqueueSequenceCallback, | |
86 Unretained(this)), | |
87 &task_tracker_, &delayed_task_manager_); | 85 &task_tracker_, &delayed_task_manager_); |
88 ASSERT_TRUE(worker_pool_); | 86 ASSERT_TRUE(worker_pool_); |
89 } | 87 } |
90 | 88 |
91 void StartWorkerPool(TimeDelta suggested_reclaim_time, size_t num_workers) { | 89 void StartWorkerPool(TimeDelta suggested_reclaim_time, size_t num_workers) { |
92 ASSERT_TRUE(worker_pool_); | 90 ASSERT_TRUE(worker_pool_); |
93 worker_pool_->Start(SchedulerWorkerPoolParams( | 91 worker_pool_->Start(SchedulerWorkerPoolParams( |
94 StandbyThreadPolicy::LAZY, num_workers, suggested_reclaim_time)); | 92 StandbyThreadPolicy::LAZY, num_workers, suggested_reclaim_time)); |
95 } | 93 } |
96 | 94 |
97 void CreateAndStartWorkerPool(TimeDelta suggested_reclaim_time, | 95 void CreateAndStartWorkerPool(TimeDelta suggested_reclaim_time, |
98 size_t num_workers) { | 96 size_t num_workers) { |
99 CreateWorkerPool(); | 97 CreateWorkerPool(); |
100 StartWorkerPool(suggested_reclaim_time, num_workers); | 98 StartWorkerPool(suggested_reclaim_time, num_workers); |
101 } | 99 } |
102 | 100 |
103 std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool_; | 101 std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool_; |
104 | 102 |
105 TaskTracker task_tracker_; | 103 TaskTracker task_tracker_; |
106 Thread service_thread_; | 104 Thread service_thread_; |
107 | 105 |
108 private: | 106 private: |
109 void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { | |
110 // In production code, this callback would be implemented by the | |
111 // TaskScheduler which would first determine which PriorityQueue the | |
112 // sequence must be re-enqueued. | |
113 const SequenceSortKey sort_key(sequence->GetSortKey()); | |
114 worker_pool_->ReEnqueueSequence(std::move(sequence), sort_key); | |
115 } | |
116 | |
117 DelayedTaskManager delayed_task_manager_; | 107 DelayedTaskManager delayed_task_manager_; |
118 | 108 |
119 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerPoolImplTest); | 109 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerPoolImplTest); |
120 }; | 110 }; |
121 | 111 |
122 scoped_refptr<TaskRunner> CreateTaskRunnerWithExecutionMode( | 112 scoped_refptr<TaskRunner> CreateTaskRunnerWithExecutionMode( |
123 SchedulerWorkerPoolImpl* worker_pool, | 113 SchedulerWorkerPoolImpl* worker_pool, |
124 test::ExecutionMode execution_mode) { | 114 test::ExecutionMode execution_mode) { |
125 // Allow tasks posted to the returned TaskRunner to wait on a WaitableEvent. | 115 // Allow tasks posted to the returned TaskRunner to wait on a WaitableEvent. |
126 const TaskTraits traits = TaskTraits().WithBaseSyncPrimitives(); | 116 const TaskTraits traits = TaskTraits().WithBaseSyncPrimitives(); |
(...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
773 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); | 763 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); |
774 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(1)); | 764 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(1)); |
775 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(2)); | 765 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(2)); |
776 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); | 766 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); |
777 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(4)); | 767 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(4)); |
778 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(5)); | 768 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(5)); |
779 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(6)); | 769 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(6)); |
780 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); | 770 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); |
781 } | 771 } |
782 | 772 |
783 namespace { | |
784 | |
785 void NotReachedReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { | |
786 ADD_FAILURE() | |
787 << "Unexpected invocation of NotReachedReEnqueueSequenceCallback."; | |
788 } | |
789 | |
790 } // namespace | |
791 | |
792 TEST(TaskSchedulerWorkerPoolStandbyPolicyTest, InitLazy) { | 773 TEST(TaskSchedulerWorkerPoolStandbyPolicyTest, InitLazy) { |
793 TaskTracker task_tracker; | 774 TaskTracker task_tracker; |
794 DelayedTaskManager delayed_task_manager; | 775 DelayedTaskManager delayed_task_manager; |
795 delayed_task_manager.Start(make_scoped_refptr(new TestSimpleTaskRunner)); | 776 delayed_task_manager.Start(make_scoped_refptr(new TestSimpleTaskRunner)); |
796 auto worker_pool = MakeUnique<SchedulerWorkerPoolImpl>( | 777 auto worker_pool = MakeUnique<SchedulerWorkerPoolImpl>( |
797 "LazyPolicyWorkerPool", ThreadPriority::NORMAL, | 778 "LazyPolicyWorkerPool", ThreadPriority::NORMAL, &task_tracker, |
798 Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, | |
799 &delayed_task_manager); | 779 &delayed_task_manager); |
800 worker_pool->Start(SchedulerWorkerPoolParams(StandbyThreadPolicy::LAZY, 8U, | 780 worker_pool->Start(SchedulerWorkerPoolParams(StandbyThreadPolicy::LAZY, 8U, |
801 TimeDelta::Max())); | 781 TimeDelta::Max())); |
802 ASSERT_TRUE(worker_pool); | 782 ASSERT_TRUE(worker_pool); |
803 EXPECT_EQ(0U, worker_pool->NumberOfAliveWorkersForTesting()); | 783 EXPECT_EQ(0U, worker_pool->NumberOfAliveWorkersForTesting()); |
804 worker_pool->JoinForTesting(); | 784 worker_pool->JoinForTesting(); |
805 } | 785 } |
806 | 786 |
807 TEST(TaskSchedulerWorkerPoolStandbyPolicyTest, InitOne) { | 787 TEST(TaskSchedulerWorkerPoolStandbyPolicyTest, InitOne) { |
808 TaskTracker task_tracker; | 788 TaskTracker task_tracker; |
809 DelayedTaskManager delayed_task_manager; | 789 DelayedTaskManager delayed_task_manager; |
810 delayed_task_manager.Start(make_scoped_refptr(new TestSimpleTaskRunner)); | 790 delayed_task_manager.Start(make_scoped_refptr(new TestSimpleTaskRunner)); |
811 auto worker_pool = MakeUnique<SchedulerWorkerPoolImpl>( | 791 auto worker_pool = MakeUnique<SchedulerWorkerPoolImpl>( |
812 "OnePolicyWorkerPool", ThreadPriority::NORMAL, | 792 "OnePolicyWorkerPool", ThreadPriority::NORMAL, &task_tracker, |
813 Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, | |
814 &delayed_task_manager); | 793 &delayed_task_manager); |
815 worker_pool->Start(SchedulerWorkerPoolParams(StandbyThreadPolicy::ONE, 8U, | 794 worker_pool->Start(SchedulerWorkerPoolParams(StandbyThreadPolicy::ONE, 8U, |
816 TimeDelta::Max())); | 795 TimeDelta::Max())); |
817 ASSERT_TRUE(worker_pool); | 796 ASSERT_TRUE(worker_pool); |
818 EXPECT_EQ(1U, worker_pool->NumberOfAliveWorkersForTesting()); | 797 EXPECT_EQ(1U, worker_pool->NumberOfAliveWorkersForTesting()); |
819 worker_pool->JoinForTesting(); | 798 worker_pool->JoinForTesting(); |
820 } | 799 } |
821 | 800 |
822 } // namespace internal | 801 } // namespace internal |
823 } // namespace base | 802 } // namespace base |
OLD | NEW |