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 TestTaskSchedulerImplDelegate : public TaskSchedulerImpl::Delegate { |
| 42 public: |
| 43 using ThreadPoolCreationArgs = |
| 44 TaskSchedulerImpl::Delegate::ThreadPoolCreationArgs; |
| 45 |
| 46 TestTaskSchedulerImplDelegate() = default; |
| 47 ~TestTaskSchedulerImplDelegate() = default; |
| 48 |
| 49 size_t GetNumThreadPools() override { return 4; } |
| 50 |
| 51 ThreadPoolCreationArgs GetCreationArgsForThreadPool(size_t pool_index) { |
| 52 switch (pool_index) { |
| 53 case 0: |
| 54 return ThreadPoolCreationArgs{ |
| 55 std::string("Background"), ThreadPriority::BACKGROUND, |
| 56 SchedulerThreadPoolImpl::IORestriction::DISALLOWED, 1U}; |
| 57 case 1: |
| 58 return ThreadPoolCreationArgs{ |
| 59 "BackgroundFileIO", ThreadPriority::BACKGROUND, |
| 60 SchedulerThreadPoolImpl::IORestriction::ALLOWED, 3U}; |
| 61 case 2: |
| 62 return ThreadPoolCreationArgs{ |
| 63 "Foreground", ThreadPriority::NORMAL, |
| 64 SchedulerThreadPoolImpl::IORestriction::DISALLOWED, 4U}; |
| 65 case 3: |
| 66 return ThreadPoolCreationArgs{ |
| 67 "ForegroundFileIO", ThreadPriority::NORMAL, |
| 68 SchedulerThreadPoolImpl::IORestriction::ALLOWED, 12U}; |
| 69 default: |
| 70 ADD_FAILURE() << "Requested creation args for invalid pool index."; |
| 71 return ThreadPoolCreationArgs{}; |
| 72 } |
| 73 } |
| 74 |
| 75 size_t GetThreadPoolIndexForTraits(const TaskTraits& traits) override { |
| 76 if (traits.with_file_io()) |
| 77 return traits.priority() == TaskPriority::BACKGROUND ? 1U : 3U; |
| 78 return traits.priority() == TaskPriority::BACKGROUND ? 0U : 2U; |
| 79 } |
| 80 |
| 81 private: |
| 82 DISALLOW_COPY_AND_ASSIGN(TestTaskSchedulerImplDelegate); |
| 83 }; |
| 84 |
41 class TaskSchedulerImplTest | 85 class TaskSchedulerImplTest |
42 : public testing::TestWithParam<TraitsExecutionModePair> { | 86 : public testing::TestWithParam<TraitsExecutionModePair> { |
43 protected: | 87 protected: |
44 TaskSchedulerImplTest() = default; | 88 TaskSchedulerImplTest() = default; |
45 | 89 |
46 void SetUp() override { | 90 void SetUp() override { |
47 scheduler_ = TaskSchedulerImpl::Create(); | 91 scheduler_ = TaskSchedulerImpl::Create( |
| 92 WrapUnique(new TestTaskSchedulerImplDelegate)); |
48 EXPECT_TRUE(scheduler_); | 93 EXPECT_TRUE(scheduler_); |
49 } | 94 } |
50 void TearDown() override { scheduler_->JoinForTesting(); } | 95 void TearDown() override { scheduler_->JoinForTesting(); } |
51 | 96 |
52 std::unique_ptr<TaskSchedulerImpl> scheduler_; | 97 std::unique_ptr<TaskSchedulerImpl> scheduler_; |
53 | 98 |
54 private: | 99 private: |
55 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImplTest); | 100 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImplTest); |
56 }; | 101 }; |
57 | 102 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 | 233 |
189 INSTANTIATE_TEST_CASE_P(OneTraitsExecutionModePair, | 234 INSTANTIATE_TEST_CASE_P(OneTraitsExecutionModePair, |
190 TaskSchedulerImplTest, | 235 TaskSchedulerImplTest, |
191 ::testing::ValuesIn(GetTraitsExecutionModePairs())); | 236 ::testing::ValuesIn(GetTraitsExecutionModePairs())); |
192 | 237 |
193 // Spawns threads that simultaneously post Tasks to TaskRunners with various | 238 // Spawns threads that simultaneously post Tasks to TaskRunners with various |
194 // TaskTraits and ExecutionModes. Verifies that each Task runs on a thread with | 239 // TaskTraits and ExecutionModes. Verifies that each Task runs on a thread with |
195 // the expected priority and I/O restrictions and respects the characteristics | 240 // the expected priority and I/O restrictions and respects the characteristics |
196 // of its ExecutionMode. | 241 // of its ExecutionMode. |
197 TEST(TaskSchedulerImplTest, MultipleTraitsExecutionModePairs) { | 242 TEST(TaskSchedulerImplTest, MultipleTraitsExecutionModePairs) { |
198 std::unique_ptr<TaskSchedulerImpl> scheduler = TaskSchedulerImpl::Create(); | 243 std::unique_ptr<TaskSchedulerImpl> scheduler = |
| 244 TaskSchedulerImpl::Create(WrapUnique(new TestTaskSchedulerImplDelegate)); |
199 | 245 |
200 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 246 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
201 for (const auto& traits_execution_mode_pair : GetTraitsExecutionModePairs()) { | 247 for (const auto& traits_execution_mode_pair : GetTraitsExecutionModePairs()) { |
202 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( | 248 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
203 scheduler.get(), traits_execution_mode_pair.traits, | 249 scheduler.get(), traits_execution_mode_pair.traits, |
204 traits_execution_mode_pair.execution_mode))); | 250 traits_execution_mode_pair.execution_mode))); |
205 threads_posting_tasks.back()->Start(); | 251 threads_posting_tasks.back()->Start(); |
206 } | 252 } |
207 | 253 |
208 for (const auto& thread : threads_posting_tasks) { | 254 for (const auto& thread : threads_posting_tasks) { |
209 thread->WaitForAllTasksToRun(); | 255 thread->WaitForAllTasksToRun(); |
210 thread->Join(); | 256 thread->Join(); |
211 } | 257 } |
212 | 258 |
213 scheduler->JoinForTesting(); | 259 scheduler->JoinForTesting(); |
214 } | 260 } |
215 | 261 |
216 // TODO(fdoray): Add tests with Sequences that move around thread pools once | 262 // TODO(fdoray): Add tests with Sequences that move around thread pools once |
217 // child TaskRunners are supported. | 263 // child TaskRunners are supported. |
218 | 264 |
219 } // namespace internal | 265 } // namespace internal |
220 } // namespace base | 266 } // namespace base |
OLD | NEW |