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

Side by Side Diff: base/task_scheduler/scheduler_thread_pool_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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/task_scheduler/scheduler_thread_pool.h"
6
7 #include <stddef.h>
8
9 #include <memory>
10 #include <unordered_set>
11 #include <vector>
12
13 #include "base/bind.h"
14 #include "base/bind_helpers.h"
15 #include "base/macros.h"
16 #include "base/memory/ptr_util.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/synchronization/condition_variable.h"
19 #include "base/synchronization/lock.h"
20 #include "base/synchronization/waitable_event.h"
21 #include "base/task_runner.h"
22 #include "base/task_scheduler/delayed_task_manager.h"
23 #include "base/task_scheduler/sequence.h"
24 #include "base/task_scheduler/sequence_sort_key.h"
25 #include "base/task_scheduler/task_tracker.h"
26 #include "base/task_scheduler/test_task_factory.h"
27 #include "base/threading/platform_thread.h"
28 #include "base/threading/simple_thread.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30
31 namespace base {
32 namespace internal {
33 namespace {
34
35 const size_t kNumThreadsInThreadPool = 4;
36 const size_t kNumThreadsPostingTasks = 4;
37 const size_t kNumTasksPostedPerThread = 150;
38
39 class TaskSchedulerThreadPoolTest
40 : public testing::TestWithParam<ExecutionMode> {
41 protected:
42 TaskSchedulerThreadPoolTest() : delayed_task_manager_(Bind(&DoNothing)) {}
43
44 void SetUp() override {
45 thread_pool_ = SchedulerThreadPool::CreateThreadPool(
46 ThreadPriority::NORMAL, kNumThreadsInThreadPool,
47 Bind(&TaskSchedulerThreadPoolTest::EnqueueSequenceCallback,
48 Unretained(this)),
49 &task_tracker_, &delayed_task_manager_);
50 ASSERT_TRUE(thread_pool_);
51 }
52
53 void TearDown() override {
54 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
55 thread_pool_->JoinForTesting();
56 }
57
58 std::unique_ptr<SchedulerThreadPool> thread_pool_;
59
60 private:
61 void EnqueueSequenceCallback(scoped_refptr<Sequence> sequence) {
62 // In production code, this callback would be implemented by the
63 // TaskScheduler which would first determine which PriorityQueue the
64 // sequence must be reinserted.
65 const SequenceSortKey sort_key(sequence->GetSortKey());
66 thread_pool_->EnqueueSequence(std::move(sequence), sort_key);
67 }
68
69 TaskTracker task_tracker_;
70 DelayedTaskManager delayed_task_manager_;
71
72 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest);
73 };
74
75 using PostNestedTask = test::TestTaskFactory::PostNestedTask;
76
77 class ThreadPostingTasks : public SimpleThread {
78 public:
79 enum class WaitBeforePostTask {
80 NO_WAIT,
81 WAIT_FOR_ALL_THREADS_IDLE,
82 };
83
84 // Constructs a thread that posts tasks to |thread_pool| through an
85 // |execution_mode| task runner. If |wait_before_post_task| is
86 // 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
88 // YES, each task posted by this thread posts another task when it runs.
89 ThreadPostingTasks(SchedulerThreadPool* thread_pool,
90 ExecutionMode execution_mode,
91 WaitBeforePostTask wait_before_post_task,
92 PostNestedTask post_nested_task)
93 : SimpleThread("ThreadPostingTasks"),
94 thread_pool_(thread_pool),
95 wait_before_post_task_(wait_before_post_task),
96 post_nested_task_(post_nested_task),
97 factory_(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(),
98 execution_mode),
99 execution_mode) {
100 DCHECK(thread_pool_);
101 }
102
103 const test::TestTaskFactory* factory() const { return &factory_; }
104
105 private:
106 void Run() override {
107 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread());
108
109 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) {
110 if (wait_before_post_task_ ==
111 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE) {
112 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
113 }
114 EXPECT_TRUE(factory_.PostTask(post_nested_task_, nullptr));
115 }
116 }
117
118 SchedulerThreadPool* const thread_pool_;
119 const scoped_refptr<TaskRunner> task_runner_;
120 const WaitBeforePostTask wait_before_post_task_;
121 const PostNestedTask post_nested_task_;
122 test::TestTaskFactory factory_;
123
124 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks);
125 };
126
127 using WaitBeforePostTask = ThreadPostingTasks::WaitBeforePostTask;
128
129 TEST_P(TaskSchedulerThreadPoolTest, PostTasks) {
130 // Create threads to post tasks.
131 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
132 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) {
133 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks(
134 thread_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT,
135 PostNestedTask::NO)));
136 threads_posting_tasks.back()->Start();
137 }
138
139 // Wait for all tasks to run.
140 for (const auto& thread_posting_tasks : threads_posting_tasks) {
141 thread_posting_tasks->Join();
142 thread_posting_tasks->factory()->WaitForAllTasksToRun();
143 }
144
145 // Wait until all worker threads are idle to be sure that no task accesses
146 // its TestTaskFactory after |thread_posting_tasks| is destroyed.
147 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
148 }
149
150 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) {
151 // 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
153 // before posting a new task.
154 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
155 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) {
156 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks(
157 thread_pool_.get(), GetParam(),
158 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE, PostNestedTask::NO)));
159 threads_posting_tasks.back()->Start();
160 }
161
162 // Wait for all tasks to run.
163 for (const auto& thread_posting_tasks : threads_posting_tasks) {
164 thread_posting_tasks->Join();
165 thread_posting_tasks->factory()->WaitForAllTasksToRun();
166 }
167
168 // Wait until all worker threads are idle to be sure that no task accesses
169 // its TestTaskFactory after |thread_posting_tasks| is destroyed.
170 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
171 }
172
173 TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) {
174 // Create threads to post tasks. Each task posted by these threads will post
175 // another task when it runs.
176 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
177 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) {
178 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks(
179 thread_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT,
180 PostNestedTask::YES)));
181 threads_posting_tasks.back()->Start();
182 }
183
184 // Wait for all tasks to run.
185 for (const auto& thread_posting_tasks : threads_posting_tasks) {
186 thread_posting_tasks->Join();
187 thread_posting_tasks->factory()->WaitForAllTasksToRun();
188 }
189
190 // Wait until all worker threads are idle to be sure that no task accesses
191 // its TestTaskFactory after |thread_posting_tasks| is destroyed.
192 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
193 }
194
195 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) {
196 // 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
198 // can run simultaneously when the execution mode is SEQUENCED.
199 WaitableEvent event(true, false);
200 std::vector<std::unique_ptr<test::TestTaskFactory>> blocked_task_factories;
201 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) {
202 blocked_task_factories.push_back(WrapUnique(new test::TestTaskFactory(
203 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()),
204 GetParam())));
205 EXPECT_TRUE(
206 blocked_task_factories.back()->PostTask(PostNestedTask::NO, &event));
207 blocked_task_factories.back()->WaitForAllTasksToRun();
208 }
209
210 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact
211 // that only one thread in |thread_pool_| isn't busy.
212 test::TestTaskFactory short_task_factory(
213 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()),
214 GetParam());
215 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i)
216 EXPECT_TRUE(short_task_factory.PostTask(PostNestedTask::NO, nullptr));
217 short_task_factory.WaitForAllTasksToRun();
218
219 // Release tasks waiting on |event|.
220 event.Signal();
221
222 // Wait until all worker threads are idle to be sure that no task accesses
223 // its TestTaskFactory after it is destroyed.
224 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
225 }
226
227 TEST_P(TaskSchedulerThreadPoolTest, Saturate) {
228 // Verify that it is possible to have |kNumThreadsInThreadPool|
229 // tasks/sequences running simultaneously. Use different factories so that
230 // tasks are added to different sequences and can run simultaneously when the
231 // execution mode is SEQUENCED.
232 WaitableEvent event(true, false);
233 std::vector<std::unique_ptr<test::TestTaskFactory>> factories;
234 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) {
235 factories.push_back(WrapUnique(new test::TestTaskFactory(
236 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()),
237 GetParam())));
238 EXPECT_TRUE(factories.back()->PostTask(PostNestedTask::NO, &event));
239 factories.back()->WaitForAllTasksToRun();
240 }
241
242 // Release tasks waiting on |event|.
243 event.Signal();
244
245 // Wait until all worker threads are idle to be sure that no task accesses
246 // its TestTaskFactory after it is destroyed.
247 thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
248 }
249
250 INSTANTIATE_TEST_CASE_P(Parallel,
251 TaskSchedulerThreadPoolTest,
252 ::testing::Values(ExecutionMode::PARALLEL));
253 INSTANTIATE_TEST_CASE_P(Sequenced,
254 TaskSchedulerThreadPoolTest,
255 ::testing::Values(ExecutionMode::SEQUENCED));
256
257 } // namespace
258 } // namespace internal
259 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698