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

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

Issue 1906083002: TaskScheduler: Remove base/task_scheduler/utils.h/.cc (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sched_2_stack
Patch Set: typos Created 4 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_thread_pool.h" 5 #include "base/task_scheduler/scheduler_thread_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>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/bind_helpers.h" 14 #include "base/bind_helpers.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
(...skipping 13 matching lines...) Expand all
29 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
30 30
31 namespace base { 31 namespace base {
32 namespace internal { 32 namespace internal {
33 namespace { 33 namespace {
34 34
35 const size_t kNumThreadsInThreadPool = 4; 35 const size_t kNumThreadsInThreadPool = 4;
36 const size_t kNumThreadsPostingTasks = 4; 36 const size_t kNumThreadsPostingTasks = 4;
37 const size_t kNumTasksPostedPerThread = 150; 37 const size_t kNumTasksPostedPerThread = 150;
38 38
39 class TaskSchedulerThreadPoolTest 39 class TestDelayedTaskManager : public DelayedTaskManager {
40 public:
41 TestDelayedTaskManager() : DelayedTaskManager(Bind(&DoNothing)) {}
42
43 void SetCurrentTime(TimeTicks now) { now_ = now; }
44
45 // DelayedTaskManager:
46 TimeTicks Now() const override { return now_; }
47
48 private:
49 TimeTicks now_ = TimeTicks::Now();
50
51 DISALLOW_COPY_AND_ASSIGN(TestDelayedTaskManager);
52 };
53
54 class TaskSchedulerThreadPoolImplTest
40 : public testing::TestWithParam<ExecutionMode> { 55 : public testing::TestWithParam<ExecutionMode> {
41 protected: 56 protected:
42 TaskSchedulerThreadPoolTest() : delayed_task_manager_(Bind(&DoNothing)) {} 57 TaskSchedulerThreadPoolImplTest() = default;
43 58
44 void SetUp() override { 59 void SetUp() override {
45 thread_pool_ = SchedulerThreadPool::CreateThreadPool( 60 thread_pool_ = SchedulerThreadPoolImpl::CreateThreadPool(
46 ThreadPriority::NORMAL, kNumThreadsInThreadPool, 61 ThreadPriority::NORMAL, kNumThreadsInThreadPool,
47 Bind(&TaskSchedulerThreadPoolTest::EnqueueSequenceCallback, 62 Bind(&TaskSchedulerThreadPoolImplTest::ReEnqueueSequenceCallback,
48 Unretained(this)), 63 Unretained(this)),
49 &task_tracker_, &delayed_task_manager_); 64 &task_tracker_, &delayed_task_manager_);
50 ASSERT_TRUE(thread_pool_); 65 ASSERT_TRUE(thread_pool_);
51 } 66 }
52 67
53 void TearDown() override { 68 void TearDown() override {
54 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); 69 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
55 thread_pool_->JoinForTesting(); 70 thread_pool_->JoinForTesting();
56 } 71 }
57 72
58 std::unique_ptr<SchedulerThreadPool> thread_pool_; 73 std::unique_ptr<SchedulerThreadPoolImpl> thread_pool_;
74
75 TaskTracker task_tracker_;
76 TestDelayedTaskManager delayed_task_manager_;
59 77
60 private: 78 private:
61 void EnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { 79 void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) {
62 // In production code, this callback would be implemented by the 80 // In production code, this callback would be implemented by the
63 // TaskScheduler which would first determine which PriorityQueue the 81 // TaskScheduler which would first determine which PriorityQueue the
64 // sequence must be reinserted. 82 // sequence must be re-enqueued.
65 const SequenceSortKey sort_key(sequence->GetSortKey()); 83 const SequenceSortKey sort_key(sequence->GetSortKey());
66 thread_pool_->EnqueueSequence(std::move(sequence), sort_key); 84 thread_pool_->ReEnqueueSequence(std::move(sequence), sort_key);
67 } 85 }
68 86
69 TaskTracker task_tracker_; 87 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolImplTest);
70 DelayedTaskManager delayed_task_manager_;
71
72 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest);
73 }; 88 };
74 89
75 using PostNestedTask = test::TestTaskFactory::PostNestedTask; 90 using PostNestedTask = test::TestTaskFactory::PostNestedTask;
76 91
77 class ThreadPostingTasks : public SimpleThread { 92 class ThreadPostingTasks : public SimpleThread {
78 public: 93 public:
79 enum class WaitBeforePostTask { 94 enum class WaitBeforePostTask {
80 NO_WAIT, 95 NO_WAIT,
81 WAIT_FOR_ALL_THREADS_IDLE, 96 WAIT_FOR_ALL_THREADS_IDLE,
82 }; 97 };
83 98
84 // Constructs a thread that posts tasks to |thread_pool| through an 99 // Constructs a thread that posts tasks to |thread_pool| through an
85 // |execution_mode| task runner. If |wait_before_post_task| is 100 // |execution_mode| task runner. If |wait_before_post_task| is
86 // WAIT_FOR_ALL_THREADS_IDLE, the thread waits until all worker threads in 101 // WAIT_FOR_ALL_THREADS_IDLE, the thread waits until all worker threads in
87 // |thread_pool| are idle before posting a new task. If |post_nested_task| is 102 // |thread_pool| are idle before posting a new task. If |post_nested_task| is
88 // YES, each task posted by this thread posts another task when it runs. 103 // YES, each task posted by this thread posts another task when it runs.
89 ThreadPostingTasks(SchedulerThreadPool* thread_pool, 104 ThreadPostingTasks(SchedulerThreadPoolImpl* thread_pool,
90 ExecutionMode execution_mode, 105 ExecutionMode execution_mode,
91 WaitBeforePostTask wait_before_post_task, 106 WaitBeforePostTask wait_before_post_task,
92 PostNestedTask post_nested_task) 107 PostNestedTask post_nested_task)
93 : SimpleThread("ThreadPostingTasks"), 108 : SimpleThread("ThreadPostingTasks"),
94 thread_pool_(thread_pool), 109 thread_pool_(thread_pool),
95 wait_before_post_task_(wait_before_post_task), 110 wait_before_post_task_(wait_before_post_task),
96 post_nested_task_(post_nested_task), 111 post_nested_task_(post_nested_task),
97 factory_(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), 112 factory_(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(),
98 execution_mode), 113 execution_mode),
99 execution_mode) { 114 execution_mode) {
100 DCHECK(thread_pool_); 115 DCHECK(thread_pool_);
101 } 116 }
102 117
103 const test::TestTaskFactory* factory() const { return &factory_; } 118 const test::TestTaskFactory* factory() const { return &factory_; }
104 119
105 private: 120 private:
106 void Run() override { 121 void Run() override {
107 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); 122 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread());
108 123
109 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { 124 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) {
110 if (wait_before_post_task_ == 125 if (wait_before_post_task_ ==
111 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE) { 126 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE) {
112 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); 127 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
113 } 128 }
114 EXPECT_TRUE(factory_.PostTask(post_nested_task_, nullptr)); 129 EXPECT_TRUE(factory_.PostTask(post_nested_task_, nullptr));
115 } 130 }
116 } 131 }
117 132
118 SchedulerThreadPool* const thread_pool_; 133 SchedulerThreadPoolImpl* const thread_pool_;
119 const scoped_refptr<TaskRunner> task_runner_; 134 const scoped_refptr<TaskRunner> task_runner_;
120 const WaitBeforePostTask wait_before_post_task_; 135 const WaitBeforePostTask wait_before_post_task_;
121 const PostNestedTask post_nested_task_; 136 const PostNestedTask post_nested_task_;
122 test::TestTaskFactory factory_; 137 test::TestTaskFactory factory_;
123 138
124 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); 139 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks);
125 }; 140 };
126 141
127 using WaitBeforePostTask = ThreadPostingTasks::WaitBeforePostTask; 142 using WaitBeforePostTask = ThreadPostingTasks::WaitBeforePostTask;
128 143
129 TEST_P(TaskSchedulerThreadPoolTest, PostTasks) { 144 void ShouldNotRunCallback() {
145 ADD_FAILURE() << "Ran a task that shouldn't run.";
146 }
147
148 void SignalEventCallback(WaitableEvent* event) {
149 DCHECK(event);
150 event->Signal();
151 }
152
153 } // namespace
154
155 TEST_P(TaskSchedulerThreadPoolImplTest, PostTasks) {
130 // Create threads to post tasks. 156 // Create threads to post tasks.
131 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; 157 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
132 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { 158 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) {
133 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( 159 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks(
134 thread_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT, 160 thread_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT,
135 PostNestedTask::NO))); 161 PostNestedTask::NO)));
136 threads_posting_tasks.back()->Start(); 162 threads_posting_tasks.back()->Start();
137 } 163 }
138 164
139 // Wait for all tasks to run. 165 // Wait for all tasks to run.
140 for (const auto& thread_posting_tasks : threads_posting_tasks) { 166 for (const auto& thread_posting_tasks : threads_posting_tasks) {
141 thread_posting_tasks->Join(); 167 thread_posting_tasks->Join();
142 thread_posting_tasks->factory()->WaitForAllTasksToRun(); 168 thread_posting_tasks->factory()->WaitForAllTasksToRun();
143 } 169 }
144 170
145 // Wait until all worker threads are idle to be sure that no task accesses 171 // Wait until all worker threads are idle to be sure that no task accesses
146 // its TestTaskFactory after |thread_posting_tasks| is destroyed. 172 // its TestTaskFactory after |thread_posting_tasks| is destroyed.
147 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); 173 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
148 } 174 }
149 175
150 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) { 176 TEST_P(TaskSchedulerThreadPoolImplTest, PostTasksWaitAllThreadsIdle) {
151 // Create threads to post tasks. To verify that worker threads can sleep and 177 // Create threads to post tasks. To verify that worker threads can sleep and
152 // be woken up when new tasks are posted, wait for all threads to become idle 178 // be woken up when new tasks are posted, wait for all threads to become idle
153 // before posting a new task. 179 // before posting a new task.
154 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; 180 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
155 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { 181 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) {
156 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( 182 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks(
157 thread_pool_.get(), GetParam(), 183 thread_pool_.get(), GetParam(),
158 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE, PostNestedTask::NO))); 184 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE, PostNestedTask::NO)));
159 threads_posting_tasks.back()->Start(); 185 threads_posting_tasks.back()->Start();
160 } 186 }
161 187
162 // Wait for all tasks to run. 188 // Wait for all tasks to run.
163 for (const auto& thread_posting_tasks : threads_posting_tasks) { 189 for (const auto& thread_posting_tasks : threads_posting_tasks) {
164 thread_posting_tasks->Join(); 190 thread_posting_tasks->Join();
165 thread_posting_tasks->factory()->WaitForAllTasksToRun(); 191 thread_posting_tasks->factory()->WaitForAllTasksToRun();
166 } 192 }
167 193
168 // Wait until all worker threads are idle to be sure that no task accesses 194 // Wait until all worker threads are idle to be sure that no task accesses
169 // its TestTaskFactory after |thread_posting_tasks| is destroyed. 195 // its TestTaskFactory after |thread_posting_tasks| is destroyed.
170 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); 196 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
171 } 197 }
172 198
173 TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) { 199 TEST_P(TaskSchedulerThreadPoolImplTest, NestedPostTasks) {
174 // Create threads to post tasks. Each task posted by these threads will post 200 // Create threads to post tasks. Each task posted by these threads will post
175 // another task when it runs. 201 // another task when it runs.
176 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; 202 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
177 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { 203 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) {
178 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( 204 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks(
179 thread_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT, 205 thread_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT,
180 PostNestedTask::YES))); 206 PostNestedTask::YES)));
181 threads_posting_tasks.back()->Start(); 207 threads_posting_tasks.back()->Start();
182 } 208 }
183 209
184 // Wait for all tasks to run. 210 // Wait for all tasks to run.
185 for (const auto& thread_posting_tasks : threads_posting_tasks) { 211 for (const auto& thread_posting_tasks : threads_posting_tasks) {
186 thread_posting_tasks->Join(); 212 thread_posting_tasks->Join();
187 thread_posting_tasks->factory()->WaitForAllTasksToRun(); 213 thread_posting_tasks->factory()->WaitForAllTasksToRun();
188 } 214 }
189 215
190 // Wait until all worker threads are idle to be sure that no task accesses 216 // Wait until all worker threads are idle to be sure that no task accesses
191 // its TestTaskFactory after |thread_posting_tasks| is destroyed. 217 // its TestTaskFactory after |thread_posting_tasks| is destroyed.
192 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); 218 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
193 } 219 }
194 220
195 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) { 221 TEST_P(TaskSchedulerThreadPoolImplTest, PostTasksWithOneAvailableThread) {
196 // Post tasks to keep all threads busy except one until |event| is signaled. 222 // Post tasks to keep all threads busy except one until |event| is signaled.
197 // Use different factories so that tasks are added to different sequences and 223 // Use different factories so that tasks are added to different sequences and
198 // can run simultaneously when the execution mode is SEQUENCED. 224 // can run simultaneously when the execution mode is SEQUENCED.
199 WaitableEvent event(true, false); 225 WaitableEvent event(true, false);
200 std::vector<std::unique_ptr<test::TestTaskFactory>> blocked_task_factories; 226 std::vector<std::unique_ptr<test::TestTaskFactory>> blocked_task_factories;
201 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { 227 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) {
202 blocked_task_factories.push_back(WrapUnique(new test::TestTaskFactory( 228 blocked_task_factories.push_back(WrapUnique(new test::TestTaskFactory(
203 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), 229 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()),
204 GetParam()))); 230 GetParam())));
205 EXPECT_TRUE( 231 EXPECT_TRUE(
(...skipping 11 matching lines...) Expand all
217 short_task_factory.WaitForAllTasksToRun(); 243 short_task_factory.WaitForAllTasksToRun();
218 244
219 // Release tasks waiting on |event|. 245 // Release tasks waiting on |event|.
220 event.Signal(); 246 event.Signal();
221 247
222 // Wait until all worker threads are idle to be sure that no task accesses 248 // Wait until all worker threads are idle to be sure that no task accesses
223 // its TestTaskFactory after it is destroyed. 249 // its TestTaskFactory after it is destroyed.
224 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); 250 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
225 } 251 }
226 252
227 TEST_P(TaskSchedulerThreadPoolTest, Saturate) { 253 TEST_P(TaskSchedulerThreadPoolImplTest, Saturate) {
228 // Verify that it is possible to have |kNumThreadsInThreadPool| 254 // Verify that it is possible to have |kNumThreadsInThreadPool|
229 // tasks/sequences running simultaneously. Use different factories so that 255 // tasks/sequences running simultaneously. Use different factories so that
230 // tasks are added to different sequences and can run simultaneously when the 256 // tasks are added to different sequences and can run simultaneously when the
231 // execution mode is SEQUENCED. 257 // execution mode is SEQUENCED.
232 WaitableEvent event(true, false); 258 WaitableEvent event(true, false);
233 std::vector<std::unique_ptr<test::TestTaskFactory>> factories; 259 std::vector<std::unique_ptr<test::TestTaskFactory>> factories;
234 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { 260 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) {
235 factories.push_back(WrapUnique(new test::TestTaskFactory( 261 factories.push_back(WrapUnique(new test::TestTaskFactory(
236 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), 262 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()),
237 GetParam()))); 263 GetParam())));
238 EXPECT_TRUE(factories.back()->PostTask(PostNestedTask::NO, &event)); 264 EXPECT_TRUE(factories.back()->PostTask(PostNestedTask::NO, &event));
239 factories.back()->WaitForAllTasksToRun(); 265 factories.back()->WaitForAllTasksToRun();
240 } 266 }
241 267
242 // Release tasks waiting on |event|. 268 // Release tasks waiting on |event|.
243 event.Signal(); 269 event.Signal();
244 270
245 // Wait until all worker threads are idle to be sure that no task accesses 271 // Wait until all worker threads are idle to be sure that no task accesses
246 // its TestTaskFactory after it is destroyed. 272 // its TestTaskFactory after it is destroyed.
247 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); 273 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
248 } 274 }
249 275
276 // Verify that a Task can't be posted after shutdown.
277 TEST_P(TaskSchedulerThreadPoolImplTest, PostTaskAfterShutdown) {
278 auto task_runner =
279 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam());
280 task_tracker_.Shutdown();
281 EXPECT_FALSE(task_runner->PostTask(FROM_HERE, Bind(&ShouldNotRunCallback)));
282 }
283
284 // Verify that a Task posted with a delay is added to the DelayedTaskManager and
285 // doesn't run before its delay expires.
286 TEST_P(TaskSchedulerThreadPoolImplTest, PostDelayedTask) {
287 EXPECT_TRUE(delayed_task_manager_.GetDelayedRunTime().is_null());
288
289 // Post a delayed task.
290 WaitableEvent task_ran(true, false);
291 EXPECT_TRUE(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam())
292 ->PostDelayedTask(FROM_HERE, Bind(&SignalEventCallback,
293 Unretained(&task_ran)),
294 TimeDelta::FromSeconds(10)));
295
296 // The task should have been added to the DelayedTaskManager.
297 EXPECT_FALSE(delayed_task_manager_.GetDelayedRunTime().is_null());
298
299 // The task shouldn't run.
300 EXPECT_FALSE(task_ran.IsSignaled());
301
302 // Fast-forward time and post tasks that are ripe for execution.
303 delayed_task_manager_.SetCurrentTime(
304 delayed_task_manager_.GetDelayedRunTime());
305 delayed_task_manager_.PostReadyTasks();
306
307 // The task should run.
308 task_ran.Wait();
309 }
310
250 INSTANTIATE_TEST_CASE_P(Parallel, 311 INSTANTIATE_TEST_CASE_P(Parallel,
251 TaskSchedulerThreadPoolTest, 312 TaskSchedulerThreadPoolImplTest,
252 ::testing::Values(ExecutionMode::PARALLEL)); 313 ::testing::Values(ExecutionMode::PARALLEL));
253 INSTANTIATE_TEST_CASE_P(Sequenced, 314 INSTANTIATE_TEST_CASE_P(Sequenced,
254 TaskSchedulerThreadPoolTest, 315 TaskSchedulerThreadPoolImplTest,
255 ::testing::Values(ExecutionMode::SEQUENCED)); 316 ::testing::Values(ExecutionMode::SEQUENCED));
256 317
257 } // namespace
258 } // namespace internal 318 } // namespace internal
259 } // namespace base 319 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698