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/task_scheduler_impl.h" | 5 #include "base/task_scheduler/task_scheduler_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 20 matching lines...) Expand all Loading... | |
31 | 31 |
32 struct TraitsExecutionModePair { | 32 struct TraitsExecutionModePair { |
33 TraitsExecutionModePair(const TaskTraits& traits, | 33 TraitsExecutionModePair(const TaskTraits& traits, |
34 ExecutionMode execution_mode) | 34 ExecutionMode execution_mode) |
35 : traits(traits), execution_mode(execution_mode) {} | 35 : traits(traits), execution_mode(execution_mode) {} |
36 | 36 |
37 TaskTraits traits; | 37 TaskTraits traits; |
38 ExecutionMode execution_mode; | 38 ExecutionMode execution_mode; |
39 }; | 39 }; |
40 | 40 |
41 class TaskSchedulerImplTest | |
42 : public testing::TestWithParam<TraitsExecutionModePair> { | |
43 protected: | |
44 TaskSchedulerImplTest() = default; | |
45 | |
46 void SetUp() override { | |
47 scheduler_ = TaskSchedulerImpl::Create(); | |
48 EXPECT_TRUE(scheduler_); | |
49 } | |
50 void TearDown() override { scheduler_->JoinForTesting(); } | |
51 | |
52 std::unique_ptr<TaskSchedulerImpl> scheduler_; | |
53 | |
54 private: | |
55 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImplTest); | |
56 }; | |
57 | |
58 #if ENABLE_THREAD_RESTRICTIONS | 41 #if ENABLE_THREAD_RESTRICTIONS |
59 // Returns whether I/O calls are allowed on the current thread. | 42 // Returns whether I/O calls are allowed on the current thread. |
60 bool GetIOAllowed() { | 43 bool GetIOAllowed() { |
61 const bool previous_value = ThreadRestrictions::SetIOAllowed(true); | 44 const bool previous_value = ThreadRestrictions::SetIOAllowed(true); |
62 ThreadRestrictions::SetIOAllowed(previous_value); | 45 ThreadRestrictions::SetIOAllowed(previous_value); |
63 return previous_value; | 46 return previous_value; |
64 } | 47 } |
65 #endif | 48 #endif |
66 | 49 |
67 // Verify that the current thread priority and I/O restrictions are appropriate | 50 // Verify that the current thread priority and I/O restrictions are appropriate |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 params.push_back(TraitsExecutionModePair( | 128 params.push_back(TraitsExecutionModePair( |
146 TaskTraits().WithPriority(priority), execution_mode)); | 129 TaskTraits().WithPriority(priority), execution_mode)); |
147 params.push_back(TraitsExecutionModePair( | 130 params.push_back(TraitsExecutionModePair( |
148 TaskTraits().WithPriority(priority).WithFileIO(), execution_mode)); | 131 TaskTraits().WithPriority(priority).WithFileIO(), execution_mode)); |
149 } | 132 } |
150 } | 133 } |
151 | 134 |
152 return params; | 135 return params; |
153 } | 136 } |
154 | 137 |
138 enum WorkerPoolType { | |
139 BACKGROUND_WORKER_POOL = 0, | |
140 BACKGROUND_FILE_IO_WORKER_POOL, | |
141 FOREGROUND_WORKER_POOL, | |
142 FOREGROUND_FILE_IO_WORKER_POOL, | |
143 }; | |
144 | |
145 size_t GetThreadPoolIndexForTraits(const TaskTraits& traits) { | |
146 if (traits.with_file_io()) { | |
147 return traits.priority() == TaskPriority::BACKGROUND | |
148 ? BACKGROUND_FILE_IO_WORKER_POOL | |
149 : FOREGROUND_FILE_IO_WORKER_POOL; | |
150 } | |
151 return traits.priority() == TaskPriority::BACKGROUND ? BACKGROUND_WORKER_POOL | |
152 : FOREGROUND_WORKER_POOL; | |
153 } | |
154 | |
155 class TaskSchedulerImplTest | |
156 : public testing::TestWithParam<TraitsExecutionModePair> { | |
157 protected: | |
158 TaskSchedulerImplTest() = default; | |
159 | |
160 void SetUp() override { | |
161 using IORestriction = SchedulerWorkerPoolImpl::IORestriction; | |
162 | |
163 std::vector<TaskSchedulerImpl::WorkerPoolCreationArgs> worker_pools; | |
164 | |
165 EXPECT_EQ(BACKGROUND_WORKER_POOL, worker_pools.size()); | |
robliao
2016/06/28 00:24:24
These should probably be ASSERTS. If an index mism
fdoray
2016/06/28 15:23:58
Done.
| |
166 worker_pools.push_back({"TaskSchedulerBackground", | |
167 ThreadPriority::BACKGROUND, | |
168 IORestriction::DISALLOWED, 1U}); | |
169 | |
170 EXPECT_EQ(BACKGROUND_FILE_IO_WORKER_POOL, worker_pools.size()); | |
171 worker_pools.push_back({"TaskSchedulerBackgroundFileIO", | |
172 ThreadPriority::BACKGROUND, IORestriction::ALLOWED, | |
173 3U}); | |
174 | |
175 EXPECT_EQ(FOREGROUND_WORKER_POOL, worker_pools.size()); | |
176 worker_pools.push_back({"TaskSchedulerForeground", ThreadPriority::NORMAL, | |
177 IORestriction::DISALLOWED, 4U}); | |
178 | |
179 DCHECK_EQ(FOREGROUND_FILE_IO_WORKER_POOL, worker_pools.size()); | |
robliao
2016/06/28 00:24:24
Typo?
fdoray
2016/06/28 15:23:58
Done.
| |
180 worker_pools.push_back({"TaskSchedulerForegroundFileIO", | |
181 ThreadPriority::NORMAL, IORestriction::ALLOWED, | |
182 12U}); | |
183 | |
184 scheduler_ = TaskSchedulerImpl::Create(worker_pools, | |
185 Bind(&GetThreadPoolIndexForTraits)); | |
186 ASSERT_TRUE(scheduler_); | |
187 } | |
188 | |
189 void TearDown() override { scheduler_->JoinForTesting(); } | |
190 | |
191 std::unique_ptr<TaskSchedulerImpl> scheduler_; | |
192 | |
193 private: | |
194 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImplTest); | |
195 }; | |
196 | |
155 } // namespace | 197 } // namespace |
156 | 198 |
157 // Verifies that a Task posted via PostTaskWithTraits with parameterized | 199 // Verifies that a Task posted via PostTaskWithTraits with parameterized |
158 // TaskTraits runs on a thread with the expected priority and I/O restrictions. | 200 // TaskTraits runs on a thread with the expected priority and I/O restrictions. |
159 // The ExecutionMode parameter is ignored by this test. | 201 // The ExecutionMode parameter is ignored by this test. |
160 TEST_P(TaskSchedulerImplTest, PostTaskWithTraits) { | 202 TEST_P(TaskSchedulerImplTest, PostTaskWithTraits) { |
161 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 203 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
162 WaitableEvent::InitialState::NOT_SIGNALED); | 204 WaitableEvent::InitialState::NOT_SIGNALED); |
163 scheduler_->PostTaskWithTraits( | 205 scheduler_->PostTaskWithTraits( |
164 FROM_HERE, GetParam().traits, | 206 FROM_HERE, GetParam().traits, |
(...skipping 22 matching lines...) Expand all Loading... | |
187 } | 229 } |
188 | 230 |
189 INSTANTIATE_TEST_CASE_P(OneTraitsExecutionModePair, | 231 INSTANTIATE_TEST_CASE_P(OneTraitsExecutionModePair, |
190 TaskSchedulerImplTest, | 232 TaskSchedulerImplTest, |
191 ::testing::ValuesIn(GetTraitsExecutionModePairs())); | 233 ::testing::ValuesIn(GetTraitsExecutionModePairs())); |
192 | 234 |
193 // Spawns threads that simultaneously post Tasks to TaskRunners with various | 235 // Spawns threads that simultaneously post Tasks to TaskRunners with various |
194 // TaskTraits and ExecutionModes. Verifies that each Task runs on a thread with | 236 // TaskTraits and ExecutionModes. Verifies that each Task runs on a thread with |
195 // the expected priority and I/O restrictions and respects the characteristics | 237 // the expected priority and I/O restrictions and respects the characteristics |
196 // of its ExecutionMode. | 238 // of its ExecutionMode. |
197 TEST(TaskSchedulerImplTest, MultipleTraitsExecutionModePairs) { | 239 TEST_F(TaskSchedulerImplTest, MultipleTraitsExecutionModePairs) { |
198 std::unique_ptr<TaskSchedulerImpl> scheduler = TaskSchedulerImpl::Create(); | |
199 | |
200 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 240 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
201 for (const auto& traits_execution_mode_pair : GetTraitsExecutionModePairs()) { | 241 for (const auto& traits_execution_mode_pair : GetTraitsExecutionModePairs()) { |
202 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( | 242 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
203 scheduler.get(), traits_execution_mode_pair.traits, | 243 scheduler_.get(), traits_execution_mode_pair.traits, |
204 traits_execution_mode_pair.execution_mode))); | 244 traits_execution_mode_pair.execution_mode))); |
205 threads_posting_tasks.back()->Start(); | 245 threads_posting_tasks.back()->Start(); |
206 } | 246 } |
207 | 247 |
208 for (const auto& thread : threads_posting_tasks) { | 248 for (const auto& thread : threads_posting_tasks) { |
209 thread->WaitForAllTasksToRun(); | 249 thread->WaitForAllTasksToRun(); |
210 thread->Join(); | 250 thread->Join(); |
211 } | 251 } |
212 | |
213 scheduler->JoinForTesting(); | |
214 } | 252 } |
215 | 253 |
216 // TODO(fdoray): Add tests with Sequences that move around worker pools once | 254 // TODO(fdoray): Add tests with Sequences that move around worker pools once |
217 // child TaskRunners are supported. | 255 // child TaskRunners are supported. |
218 | 256 |
219 } // namespace internal | 257 } // namespace internal |
220 } // namespace base | 258 } // namespace base |
OLD | NEW |