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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
51 bool GetIOAllowed() { | 51 bool GetIOAllowed() { |
52 const bool previous_value = ThreadRestrictions::SetIOAllowed(true); | 52 const bool previous_value = ThreadRestrictions::SetIOAllowed(true); |
53 ThreadRestrictions::SetIOAllowed(previous_value); | 53 ThreadRestrictions::SetIOAllowed(previous_value); |
54 return previous_value; | 54 return previous_value; |
55 } | 55 } |
56 #endif | 56 #endif |
57 | 57 |
58 // Verify that the current thread priority and I/O restrictions are appropriate | 58 // Verify that the current thread priority and I/O restrictions are appropriate |
59 // to run a Task with |traits|. | 59 // to run a Task with |traits|. |
60 // Note: ExecutionMode is verified inside TestTaskFactory. | 60 // Note: ExecutionMode is verified inside TestTaskFactory. |
61 void VerifyTaskEnvironment(const TaskTraits& traits) { | 61 void VerifyTaskEnvironment(const TaskTraits* traits) { |
62 const bool supports_background_priority = | 62 const bool supports_background_priority = |
63 Lock::HandlesMultipleThreadPriorities() && | 63 Lock::HandlesMultipleThreadPriorities() && |
64 PlatformThread::CanIncreaseCurrentThreadPriority(); | 64 PlatformThread::CanIncreaseCurrentThreadPriority(); |
65 | 65 |
66 EXPECT_EQ(supports_background_priority && | 66 EXPECT_EQ(supports_background_priority && |
67 traits.priority() == TaskPriority::BACKGROUND | 67 traits->priority() == TaskPriority::BACKGROUND |
68 ? ThreadPriority::BACKGROUND | 68 ? ThreadPriority::BACKGROUND |
69 : ThreadPriority::NORMAL, | 69 : ThreadPriority::NORMAL, |
70 PlatformThread::GetCurrentThreadPriority()); | 70 PlatformThread::GetCurrentThreadPriority()); |
71 | 71 |
72 #if DCHECK_IS_ON() | 72 #if DCHECK_IS_ON() |
73 // The #if above is required because GetIOAllowed() always returns true when | 73 // The #if above is required because GetIOAllowed() always returns true when |
74 // !DCHECK_IS_ON(), even when |traits| don't allow file I/O. | 74 // !DCHECK_IS_ON(), even when |traits| don't allow file I/O. |
75 EXPECT_EQ(traits.may_block(), GetIOAllowed()); | 75 EXPECT_EQ(traits->may_block(), GetIOAllowed()); |
76 #endif | 76 #endif |
77 | 77 |
78 // Verify that the thread the task is running on is named as expected. | 78 // Verify that the thread the task is running on is named as expected. |
79 const std::string current_thread_name(PlatformThread::GetName()); | 79 const std::string current_thread_name(PlatformThread::GetName()); |
80 EXPECT_NE(std::string::npos, current_thread_name.find("TaskScheduler")); | 80 EXPECT_NE(std::string::npos, current_thread_name.find("TaskScheduler")); |
81 EXPECT_NE(std::string::npos, | 81 EXPECT_NE(std::string::npos, |
82 current_thread_name.find( | 82 current_thread_name.find( |
83 traits.priority() == TaskPriority::BACKGROUND ? "Background" | 83 traits->priority() == TaskPriority::BACKGROUND ? "Background" |
84 : "Foreground")); | 84 : "Foreground")); |
85 EXPECT_EQ(traits.may_block(), | 85 EXPECT_EQ(traits->may_block(), |
86 current_thread_name.find("Blocking") != std::string::npos); | 86 current_thread_name.find("Blocking") != std::string::npos); |
87 } | 87 } |
88 | 88 |
89 void VerifyTaskEnvironmentAndSignalEvent(const TaskTraits& traits, | 89 void VerifyTaskEnvironmentAndSignalEvent(const TaskTraits* traits, |
90 WaitableEvent* event) { | 90 WaitableEvent* event) { |
91 DCHECK(event); | 91 DCHECK(event); |
92 VerifyTaskEnvironment(traits); | 92 VerifyTaskEnvironment(traits); |
93 event->Signal(); | 93 event->Signal(); |
94 } | 94 } |
95 | 95 |
96 void VerifyTimeAndTaskEnvironmentAndSignalEvent(const TaskTraits& traits, | 96 void VerifyTimeAndTaskEnvironmentAndSignalEvent(const TaskTraits* traits, |
97 TimeTicks expected_time, | 97 TimeTicks expected_time, |
98 WaitableEvent* event) { | 98 WaitableEvent* event) { |
99 DCHECK(event); | 99 DCHECK(event); |
100 EXPECT_LE(expected_time, TimeTicks::Now()); | 100 EXPECT_LE(expected_time, TimeTicks::Now()); |
101 VerifyTaskEnvironment(traits); | 101 VerifyTaskEnvironment(traits); |
102 event->Signal(); | 102 event->Signal(); |
103 } | 103 } |
104 | 104 |
105 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraitsAndExecutionMode( | 105 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraitsAndExecutionMode( |
106 TaskScheduler* scheduler, | 106 TaskScheduler* scheduler, |
(...skipping 27 matching lines...) Expand all Loading... | |
134 | 134 |
135 void WaitForAllTasksToRun() { factory_.WaitForAllTasksToRun(); } | 135 void WaitForAllTasksToRun() { factory_.WaitForAllTasksToRun(); } |
136 | 136 |
137 private: | 137 private: |
138 void Run() override { | 138 void Run() override { |
139 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); | 139 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); |
140 | 140 |
141 const size_t kNumTasksPerThread = 150; | 141 const size_t kNumTasksPerThread = 150; |
142 for (size_t i = 0; i < kNumTasksPerThread; ++i) { | 142 for (size_t i = 0; i < kNumTasksPerThread; ++i) { |
143 factory_.PostTask(test::TestTaskFactory::PostNestedTask::NO, | 143 factory_.PostTask(test::TestTaskFactory::PostNestedTask::NO, |
144 Bind(&VerifyTaskEnvironment, traits_)); | 144 Bind(&VerifyTaskEnvironment, Unretained(&traits_))); |
145 } | 145 } |
146 } | 146 } |
147 | 147 |
148 const TaskTraits traits_; | 148 const TaskTraits traits_; |
149 test::TestTaskFactory factory_; | 149 test::TestTaskFactory factory_; |
150 | 150 |
151 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); | 151 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); |
152 }; | 152 }; |
153 | 153 |
154 // Returns a vector with a TraitsExecutionModePair for each valid | 154 // Returns a vector with a TraitsExecutionModePair for each valid |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 | 212 |
213 // Verifies that a Task posted via PostDelayedTaskWithTraits with parameterized | 213 // Verifies that a Task posted via PostDelayedTaskWithTraits with parameterized |
214 // TaskTraits and no delay runs on a thread with the expected priority and I/O | 214 // TaskTraits and no delay runs on a thread with the expected priority and I/O |
215 // restrictions. The ExecutionMode parameter is ignored by this test. | 215 // restrictions. The ExecutionMode parameter is ignored by this test. |
216 TEST_P(TaskSchedulerImplTest, PostDelayedTaskWithTraitsNoDelay) { | 216 TEST_P(TaskSchedulerImplTest, PostDelayedTaskWithTraitsNoDelay) { |
217 StartTaskScheduler(); | 217 StartTaskScheduler(); |
218 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 218 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
219 WaitableEvent::InitialState::NOT_SIGNALED); | 219 WaitableEvent::InitialState::NOT_SIGNALED); |
220 scheduler_.PostDelayedTaskWithTraits( | 220 scheduler_.PostDelayedTaskWithTraits( |
221 FROM_HERE, GetParam().traits, | 221 FROM_HERE, GetParam().traits, |
222 BindOnce(&VerifyTaskEnvironmentAndSignalEvent, GetParam().traits, | 222 BindOnce(&VerifyTaskEnvironmentAndSignalEvent, |
223 Unretained(&task_ran)), | 223 Unretained(&GetParam().traits), Unretained(&task_ran)), |
gab
2017/04/28 18:15:42
Bring back old Bind?
fdoray
2017/04/28 18:40:36
Done.
| |
224 TimeDelta()); | 224 TimeDelta()); |
225 task_ran.Wait(); | 225 task_ran.Wait(); |
226 } | 226 } |
227 | 227 |
228 // Verifies that a Task posted via PostDelayedTaskWithTraits with parameterized | 228 // Verifies that a Task posted via PostDelayedTaskWithTraits with parameterized |
229 // TaskTraits and a non-zero delay runs on a thread with the expected priority | 229 // TaskTraits and a non-zero delay runs on a thread with the expected priority |
230 // and I/O restrictions after the delay expires. The ExecutionMode parameter is | 230 // and I/O restrictions after the delay expires. The ExecutionMode parameter is |
231 // ignored by this test. | 231 // ignored by this test. |
232 TEST_P(TaskSchedulerImplTest, PostDelayedTaskWithTraitsWithDelay) { | 232 TEST_P(TaskSchedulerImplTest, PostDelayedTaskWithTraitsWithDelay) { |
233 StartTaskScheduler(); | 233 StartTaskScheduler(); |
234 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 234 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
235 WaitableEvent::InitialState::NOT_SIGNALED); | 235 WaitableEvent::InitialState::NOT_SIGNALED); |
236 scheduler_.PostDelayedTaskWithTraits( | 236 scheduler_.PostDelayedTaskWithTraits( |
237 FROM_HERE, GetParam().traits, | 237 FROM_HERE, GetParam().traits, |
238 BindOnce(&VerifyTimeAndTaskEnvironmentAndSignalEvent, GetParam().traits, | 238 BindOnce(&VerifyTimeAndTaskEnvironmentAndSignalEvent, |
239 Unretained(&GetParam().traits), | |
239 TimeTicks::Now() + TestTimeouts::tiny_timeout(), | 240 TimeTicks::Now() + TestTimeouts::tiny_timeout(), |
240 Unretained(&task_ran)), | 241 Unretained(&task_ran)), |
241 TestTimeouts::tiny_timeout()); | 242 TestTimeouts::tiny_timeout()); |
242 task_ran.Wait(); | 243 task_ran.Wait(); |
243 } | 244 } |
244 | 245 |
245 // Verifies that Tasks posted via a TaskRunner with parameterized TaskTraits and | 246 // Verifies that Tasks posted via a TaskRunner with parameterized TaskTraits and |
246 // ExecutionMode run on a thread with the expected priority and I/O restrictions | 247 // ExecutionMode run on a thread with the expected priority and I/O restrictions |
247 // and respect the characteristics of their ExecutionMode. | 248 // and respect the characteristics of their ExecutionMode. |
248 TEST_P(TaskSchedulerImplTest, PostTasksViaTaskRunner) { | 249 TEST_P(TaskSchedulerImplTest, PostTasksViaTaskRunner) { |
249 StartTaskScheduler(); | 250 StartTaskScheduler(); |
250 test::TestTaskFactory factory( | 251 test::TestTaskFactory factory( |
251 CreateTaskRunnerWithTraitsAndExecutionMode(&scheduler_, GetParam().traits, | 252 CreateTaskRunnerWithTraitsAndExecutionMode(&scheduler_, GetParam().traits, |
252 GetParam().execution_mode), | 253 GetParam().execution_mode), |
253 GetParam().execution_mode); | 254 GetParam().execution_mode); |
254 EXPECT_FALSE(factory.task_runner()->RunsTasksOnCurrentThread()); | 255 EXPECT_FALSE(factory.task_runner()->RunsTasksOnCurrentThread()); |
255 | 256 |
256 const size_t kNumTasksPerTest = 150; | 257 const size_t kNumTasksPerTest = 150; |
257 for (size_t i = 0; i < kNumTasksPerTest; ++i) { | 258 for (size_t i = 0; i < kNumTasksPerTest; ++i) { |
258 factory.PostTask(test::TestTaskFactory::PostNestedTask::NO, | 259 factory.PostTask( |
259 Bind(&VerifyTaskEnvironment, GetParam().traits)); | 260 test::TestTaskFactory::PostNestedTask::NO, |
261 Bind(&VerifyTaskEnvironment, Unretained(&GetParam().traits))); | |
260 } | 262 } |
261 | 263 |
262 factory.WaitForAllTasksToRun(); | 264 factory.WaitForAllTasksToRun(); |
263 } | 265 } |
264 | 266 |
265 // Verifies that a task posted via PostDelayedTaskWithTraits without a delay | 267 // Verifies that a task posted via PostDelayedTaskWithTraits without a delay |
266 // doesn't run before Start() is called. | 268 // doesn't run before Start() is called. |
267 TEST_P(TaskSchedulerImplTest, PostDelayedTaskWithTraitsNoDelayBeforeStart) { | 269 TEST_P(TaskSchedulerImplTest, PostDelayedTaskWithTraitsNoDelayBeforeStart) { |
268 WaitableEvent task_running(WaitableEvent::ResetPolicy::MANUAL, | 270 WaitableEvent task_running(WaitableEvent::ResetPolicy::MANUAL, |
269 WaitableEvent::InitialState::NOT_SIGNALED); | 271 WaitableEvent::InitialState::NOT_SIGNALED); |
270 scheduler_.PostDelayedTaskWithTraits( | 272 scheduler_.PostDelayedTaskWithTraits( |
271 FROM_HERE, GetParam().traits, | 273 FROM_HERE, GetParam().traits, |
272 BindOnce(&VerifyTaskEnvironmentAndSignalEvent, GetParam().traits, | 274 BindOnce(&VerifyTaskEnvironmentAndSignalEvent, |
273 Unretained(&task_running)), | 275 Unretained(&GetParam().traits), Unretained(&task_running)), |
274 TimeDelta()); | 276 TimeDelta()); |
275 | 277 |
276 // Wait a little bit to make sure that the task isn't scheduled before | 278 // Wait a little bit to make sure that the task isn't scheduled before |
277 // Start(). Note: This test won't catch a case where the task runs just after | 279 // Start(). Note: This test won't catch a case where the task runs just after |
278 // the check and before Start(). However, we expect the test to be flaky if | 280 // the check and before Start(). However, we expect the test to be flaky if |
279 // the tested code allows that to happen. | 281 // the tested code allows that to happen. |
280 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 282 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
281 EXPECT_FALSE(task_running.IsSignaled()); | 283 EXPECT_FALSE(task_running.IsSignaled()); |
282 | 284 |
283 StartTaskScheduler(); | 285 StartTaskScheduler(); |
284 task_running.Wait(); | 286 task_running.Wait(); |
285 } | 287 } |
286 | 288 |
287 // Verifies that a task posted via PostDelayedTaskWithTraits with a delay | 289 // Verifies that a task posted via PostDelayedTaskWithTraits with a delay |
288 // doesn't run before Start() is called. | 290 // doesn't run before Start() is called. |
289 TEST_P(TaskSchedulerImplTest, PostDelayedTaskWithTraitsWithDelayBeforeStart) { | 291 TEST_P(TaskSchedulerImplTest, PostDelayedTaskWithTraitsWithDelayBeforeStart) { |
290 WaitableEvent task_running(WaitableEvent::ResetPolicy::MANUAL, | 292 WaitableEvent task_running(WaitableEvent::ResetPolicy::MANUAL, |
291 WaitableEvent::InitialState::NOT_SIGNALED); | 293 WaitableEvent::InitialState::NOT_SIGNALED); |
292 scheduler_.PostDelayedTaskWithTraits( | 294 scheduler_.PostDelayedTaskWithTraits( |
293 FROM_HERE, GetParam().traits, | 295 FROM_HERE, GetParam().traits, |
294 BindOnce(&VerifyTimeAndTaskEnvironmentAndSignalEvent, GetParam().traits, | 296 BindOnce(&VerifyTimeAndTaskEnvironmentAndSignalEvent, |
297 Unretained(&GetParam().traits), | |
295 TimeTicks::Now() + TestTimeouts::tiny_timeout(), | 298 TimeTicks::Now() + TestTimeouts::tiny_timeout(), |
296 Unretained(&task_running)), | 299 Unretained(&task_running)), |
297 TestTimeouts::tiny_timeout()); | 300 TestTimeouts::tiny_timeout()); |
298 | 301 |
299 // Wait a little bit to make sure that the task isn't scheduled before | 302 // Wait a little bit to make sure that the task isn't scheduled before |
300 // Start(). Note: This test won't catch a case where the task runs just after | 303 // Start(). Note: This test won't catch a case where the task runs just after |
301 // the check and before Start(). However, we expect the test to be flaky if | 304 // the check and before Start(). However, we expect the test to be flaky if |
302 // the tested code allows that to happen. | 305 // the tested code allows that to happen. |
303 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 306 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
304 EXPECT_FALSE(task_running.IsSignaled()); | 307 EXPECT_FALSE(task_running.IsSignaled()); |
305 | 308 |
306 StartTaskScheduler(); | 309 StartTaskScheduler(); |
307 task_running.Wait(); | 310 task_running.Wait(); |
308 } | 311 } |
309 | 312 |
310 // Verifies that a task posted via a TaskRunner doesn't run before Start() is | 313 // Verifies that a task posted via a TaskRunner doesn't run before Start() is |
311 // called. | 314 // called. |
312 TEST_P(TaskSchedulerImplTest, PostTaskViaTaskRunnerBeforeStart) { | 315 TEST_P(TaskSchedulerImplTest, PostTaskViaTaskRunnerBeforeStart) { |
313 WaitableEvent task_running(WaitableEvent::ResetPolicy::MANUAL, | 316 WaitableEvent task_running(WaitableEvent::ResetPolicy::MANUAL, |
314 WaitableEvent::InitialState::NOT_SIGNALED); | 317 WaitableEvent::InitialState::NOT_SIGNALED); |
315 CreateTaskRunnerWithTraitsAndExecutionMode(&scheduler_, GetParam().traits, | 318 CreateTaskRunnerWithTraitsAndExecutionMode(&scheduler_, GetParam().traits, |
316 GetParam().execution_mode) | 319 GetParam().execution_mode) |
317 ->PostTask(FROM_HERE, | 320 ->PostTask(FROM_HERE, BindOnce(&VerifyTaskEnvironmentAndSignalEvent, |
318 BindOnce(&VerifyTaskEnvironmentAndSignalEvent, | 321 Unretained(&GetParam().traits), |
319 GetParam().traits, Unretained(&task_running))); | 322 Unretained(&task_running))); |
320 | 323 |
321 // Wait a little bit to make sure that the task isn't scheduled before | 324 // Wait a little bit to make sure that the task isn't scheduled before |
322 // Start(). Note: This test won't catch a case where the task runs just after | 325 // Start(). Note: This test won't catch a case where the task runs just after |
323 // the check and before Start(). However, we expect the test to be flaky if | 326 // the check and before Start(). However, we expect the test to be flaky if |
324 // the tested code allows that to happen. | 327 // the tested code allows that to happen. |
325 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 328 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
326 EXPECT_FALSE(task_running.IsSignaled()); | 329 EXPECT_FALSE(task_running.IsSignaled()); |
327 | 330 |
328 StartTaskScheduler(); | 331 StartTaskScheduler(); |
329 | 332 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
442 } | 445 } |
443 task_ran->Signal(); | 446 task_ran->Signal(); |
444 }, | 447 }, |
445 com_sta_task_runner, Unretained(&task_ran))); | 448 com_sta_task_runner, Unretained(&task_ran))); |
446 task_ran.Wait(); | 449 task_ran.Wait(); |
447 } | 450 } |
448 #endif // defined(OS_WIN) | 451 #endif // defined(OS_WIN) |
449 | 452 |
450 } // namespace internal | 453 } // namespace internal |
451 } // namespace base | 454 } // namespace base |
OLD | NEW |