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 size_t GetThreadPoolIndexForTraits(const TaskTraits& traits) { | |
139 if (traits.with_file_io()) | |
140 return traits.priority() == TaskPriority::BACKGROUND ? 1U : 3U; | |
robliao
2016/06/23 22:37:03
Should we encourage that the return values of thes
gab
2016/06/27 18:48:23
I would like for implementations of this to look s
fdoray
2016/06/27 19:45:04
I now use an enum. Not sure that an enum class tha
| |
141 return traits.priority() == TaskPriority::BACKGROUND ? 0U : 2U; | |
142 } | |
143 | |
144 class TaskSchedulerImplTest | |
145 : public testing::TestWithParam<TraitsExecutionModePair> { | |
146 protected: | |
147 TaskSchedulerImplTest() = default; | |
148 | |
149 void SetUp() override { | |
150 using IORestriction = SchedulerWorkerPoolImpl::IORestriction; | |
151 | |
152 std::vector<TaskSchedulerImpl::WorkerPoolCreationArgs> | |
robliao
2016/06/23 22:37:03
Since the vector is set up at the get go, we can m
fdoray
2016/06/27 19:45:05
The vector can no longer be const.
robliao
2016/06/28 00:24:24
An alternative is to do a post examination of the
fdoray
2016/06/28 15:23:58
I prefer not to define operator== just for this.
| |
153 worker_pool_creation_args{ | |
gab
2016/06/27 18:48:23
Space before '{' or is no space the style for thes
fdoray
2016/06/27 19:45:05
git cl format removes the space when I add it...
| |
154 {"TaskSchedulerBackground", ThreadPriority::BACKGROUND, | |
155 IORestriction::DISALLOWED, 1U}, | |
156 {"TaskSchedulerBackgroundFileIO", ThreadPriority::BACKGROUND, | |
157 IORestriction::ALLOWED, 3U}, | |
158 {"TaskSchedulerForeground", ThreadPriority::NORMAL, | |
159 IORestriction::DISALLOWED, 4U}, | |
160 {"TaskSchedulerForegroundFileIO", ThreadPriority::NORMAL, | |
161 IORestriction::ALLOWED, 12U}, | |
gab
2016/06/27 18:48:23
Reinforcing my comment above: I think impls should
fdoray
2016/06/27 19:45:05
Done.
| |
162 }; | |
163 | |
164 scheduler_ = TaskSchedulerImpl::Create(worker_pool_creation_args, | |
165 Bind(&GetThreadPoolIndexForTraits)); | |
166 EXPECT_TRUE(scheduler_); | |
robliao
2016/06/23 22:37:03
This should probably be an ASSERT since the guaran
gab
2016/06/27 18:48:23
Agreed, plus the rest of the test is meaningless i
fdoray
2016/06/27 19:45:05
Done.
| |
167 } | |
168 | |
169 void TearDown() override { scheduler_->JoinForTesting(); } | |
170 | |
171 std::unique_ptr<TaskSchedulerImpl> scheduler_; | |
172 | |
173 private: | |
174 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImplTest); | |
175 }; | |
176 | |
155 } // namespace | 177 } // namespace |
156 | 178 |
157 // Verifies that a Task posted via PostTaskWithTraits with parameterized | 179 // Verifies that a Task posted via PostTaskWithTraits with parameterized |
158 // TaskTraits runs on a thread with the expected priority and I/O restrictions. | 180 // TaskTraits runs on a thread with the expected priority and I/O restrictions. |
159 // The ExecutionMode parameter is ignored by this test. | 181 // The ExecutionMode parameter is ignored by this test. |
160 TEST_P(TaskSchedulerImplTest, PostTaskWithTraits) { | 182 TEST_P(TaskSchedulerImplTest, PostTaskWithTraits) { |
161 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 183 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
162 WaitableEvent::InitialState::NOT_SIGNALED); | 184 WaitableEvent::InitialState::NOT_SIGNALED); |
163 scheduler_->PostTaskWithTraits( | 185 scheduler_->PostTaskWithTraits( |
164 FROM_HERE, GetParam().traits, | 186 FROM_HERE, GetParam().traits, |
(...skipping 22 matching lines...) Expand all Loading... | |
187 } | 209 } |
188 | 210 |
189 INSTANTIATE_TEST_CASE_P(OneTraitsExecutionModePair, | 211 INSTANTIATE_TEST_CASE_P(OneTraitsExecutionModePair, |
190 TaskSchedulerImplTest, | 212 TaskSchedulerImplTest, |
191 ::testing::ValuesIn(GetTraitsExecutionModePairs())); | 213 ::testing::ValuesIn(GetTraitsExecutionModePairs())); |
192 | 214 |
193 // Spawns threads that simultaneously post Tasks to TaskRunners with various | 215 // Spawns threads that simultaneously post Tasks to TaskRunners with various |
194 // TaskTraits and ExecutionModes. Verifies that each Task runs on a thread with | 216 // TaskTraits and ExecutionModes. Verifies that each Task runs on a thread with |
195 // the expected priority and I/O restrictions and respects the characteristics | 217 // the expected priority and I/O restrictions and respects the characteristics |
196 // of its ExecutionMode. | 218 // of its ExecutionMode. |
197 TEST(TaskSchedulerImplTest, MultipleTraitsExecutionModePairs) { | 219 TEST_F(TaskSchedulerImplTest, MultipleTraitsExecutionModePairs) { |
198 std::unique_ptr<TaskSchedulerImpl> scheduler = TaskSchedulerImpl::Create(); | |
199 | |
200 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 220 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
201 for (const auto& traits_execution_mode_pair : GetTraitsExecutionModePairs()) { | 221 for (const auto& traits_execution_mode_pair : GetTraitsExecutionModePairs()) { |
202 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( | 222 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
203 scheduler.get(), traits_execution_mode_pair.traits, | 223 scheduler_.get(), traits_execution_mode_pair.traits, |
204 traits_execution_mode_pair.execution_mode))); | 224 traits_execution_mode_pair.execution_mode))); |
205 threads_posting_tasks.back()->Start(); | 225 threads_posting_tasks.back()->Start(); |
206 } | 226 } |
207 | 227 |
208 for (const auto& thread : threads_posting_tasks) { | 228 for (const auto& thread : threads_posting_tasks) { |
209 thread->WaitForAllTasksToRun(); | 229 thread->WaitForAllTasksToRun(); |
210 thread->Join(); | 230 thread->Join(); |
211 } | 231 } |
212 | |
213 scheduler->JoinForTesting(); | |
214 } | 232 } |
215 | 233 |
216 // TODO(fdoray): Add tests with Sequences that move around worker pools once | 234 // TODO(fdoray): Add tests with Sequences that move around worker pools once |
217 // child TaskRunners are supported. | 235 // child TaskRunners are supported. |
218 | 236 |
219 } // namespace internal | 237 } // namespace internal |
220 } // namespace base | 238 } // namespace base |
OLD | NEW |