| 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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after 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, ConstRef(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 ConstRef(GetParam().traits), Unretained(&task_ran)), |
| 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 ConstRef(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(test::TestTaskFactory::PostNestedTask::NO, |
| 259 Bind(&VerifyTaskEnvironment, GetParam().traits)); | 260 Bind(&VerifyTaskEnvironment, ConstRef(GetParam().traits))); |
| 260 } | 261 } |
| 261 | 262 |
| 262 factory.WaitForAllTasksToRun(); | 263 factory.WaitForAllTasksToRun(); |
| 263 } | 264 } |
| 264 | 265 |
| 265 // Verifies that a task posted via PostDelayedTaskWithTraits without a delay | 266 // Verifies that a task posted via PostDelayedTaskWithTraits without a delay |
| 266 // doesn't run before Start() is called. | 267 // doesn't run before Start() is called. |
| 267 TEST_P(TaskSchedulerImplTest, PostDelayedTaskWithTraitsNoDelayBeforeStart) { | 268 TEST_P(TaskSchedulerImplTest, PostDelayedTaskWithTraitsNoDelayBeforeStart) { |
| 268 WaitableEvent task_running(WaitableEvent::ResetPolicy::MANUAL, | 269 WaitableEvent task_running(WaitableEvent::ResetPolicy::MANUAL, |
| 269 WaitableEvent::InitialState::NOT_SIGNALED); | 270 WaitableEvent::InitialState::NOT_SIGNALED); |
| 270 scheduler_.PostDelayedTaskWithTraits( | 271 scheduler_.PostDelayedTaskWithTraits( |
| 271 FROM_HERE, GetParam().traits, | 272 FROM_HERE, GetParam().traits, |
| 272 BindOnce(&VerifyTaskEnvironmentAndSignalEvent, GetParam().traits, | 273 BindOnce(&VerifyTaskEnvironmentAndSignalEvent, |
| 273 Unretained(&task_running)), | 274 ConstRef(GetParam().traits), Unretained(&task_running)), |
| 274 TimeDelta()); | 275 TimeDelta()); |
| 275 | 276 |
| 276 // Wait a little bit to make sure that the task isn't scheduled before | 277 // 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 | 278 // 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 | 279 // the check and before Start(). However, we expect the test to be flaky if |
| 279 // the tested code allows that to happen. | 280 // the tested code allows that to happen. |
| 280 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 281 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 281 EXPECT_FALSE(task_running.IsSignaled()); | 282 EXPECT_FALSE(task_running.IsSignaled()); |
| 282 | 283 |
| 283 StartTaskScheduler(); | 284 StartTaskScheduler(); |
| 284 task_running.Wait(); | 285 task_running.Wait(); |
| 285 } | 286 } |
| 286 | 287 |
| 287 // Verifies that a task posted via PostDelayedTaskWithTraits with a delay | 288 // Verifies that a task posted via PostDelayedTaskWithTraits with a delay |
| 288 // doesn't run before Start() is called. | 289 // doesn't run before Start() is called. |
| 289 TEST_P(TaskSchedulerImplTest, PostDelayedTaskWithTraitsWithDelayBeforeStart) { | 290 TEST_P(TaskSchedulerImplTest, PostDelayedTaskWithTraitsWithDelayBeforeStart) { |
| 290 WaitableEvent task_running(WaitableEvent::ResetPolicy::MANUAL, | 291 WaitableEvent task_running(WaitableEvent::ResetPolicy::MANUAL, |
| 291 WaitableEvent::InitialState::NOT_SIGNALED); | 292 WaitableEvent::InitialState::NOT_SIGNALED); |
| 292 scheduler_.PostDelayedTaskWithTraits( | 293 scheduler_.PostDelayedTaskWithTraits( |
| 293 FROM_HERE, GetParam().traits, | 294 FROM_HERE, GetParam().traits, |
| 294 BindOnce(&VerifyTimeAndTaskEnvironmentAndSignalEvent, GetParam().traits, | 295 BindOnce(&VerifyTimeAndTaskEnvironmentAndSignalEvent, |
| 296 ConstRef(GetParam().traits), |
| 295 TimeTicks::Now() + TestTimeouts::tiny_timeout(), | 297 TimeTicks::Now() + TestTimeouts::tiny_timeout(), |
| 296 Unretained(&task_running)), | 298 Unretained(&task_running)), |
| 297 TestTimeouts::tiny_timeout()); | 299 TestTimeouts::tiny_timeout()); |
| 298 | 300 |
| 299 // Wait a little bit to make sure that the task isn't scheduled before | 301 // 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 | 302 // 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 | 303 // the check and before Start(). However, we expect the test to be flaky if |
| 302 // the tested code allows that to happen. | 304 // the tested code allows that to happen. |
| 303 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 305 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 304 EXPECT_FALSE(task_running.IsSignaled()); | 306 EXPECT_FALSE(task_running.IsSignaled()); |
| 305 | 307 |
| 306 StartTaskScheduler(); | 308 StartTaskScheduler(); |
| 307 task_running.Wait(); | 309 task_running.Wait(); |
| 308 } | 310 } |
| 309 | 311 |
| 310 // Verifies that a task posted via a TaskRunner doesn't run before Start() is | 312 // Verifies that a task posted via a TaskRunner doesn't run before Start() is |
| 311 // called. | 313 // called. |
| 312 TEST_P(TaskSchedulerImplTest, PostTaskViaTaskRunnerBeforeStart) { | 314 TEST_P(TaskSchedulerImplTest, PostTaskViaTaskRunnerBeforeStart) { |
| 313 WaitableEvent task_running(WaitableEvent::ResetPolicy::MANUAL, | 315 WaitableEvent task_running(WaitableEvent::ResetPolicy::MANUAL, |
| 314 WaitableEvent::InitialState::NOT_SIGNALED); | 316 WaitableEvent::InitialState::NOT_SIGNALED); |
| 315 CreateTaskRunnerWithTraitsAndExecutionMode(&scheduler_, GetParam().traits, | 317 CreateTaskRunnerWithTraitsAndExecutionMode(&scheduler_, GetParam().traits, |
| 316 GetParam().execution_mode) | 318 GetParam().execution_mode) |
| 317 ->PostTask(FROM_HERE, | 319 ->PostTask(FROM_HERE, BindOnce(&VerifyTaskEnvironmentAndSignalEvent, |
| 318 BindOnce(&VerifyTaskEnvironmentAndSignalEvent, | 320 ConstRef(GetParam().traits), |
| 319 GetParam().traits, Unretained(&task_running))); | 321 Unretained(&task_running))); |
| 320 | 322 |
| 321 // Wait a little bit to make sure that the task isn't scheduled before | 323 // 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 | 324 // 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 | 325 // the check and before Start(). However, we expect the test to be flaky if |
| 324 // the tested code allows that to happen. | 326 // the tested code allows that to happen. |
| 325 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 327 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 326 EXPECT_FALSE(task_running.IsSignaled()); | 328 EXPECT_FALSE(task_running.IsSignaled()); |
| 327 | 329 |
| 328 StartTaskScheduler(); | 330 StartTaskScheduler(); |
| 329 | 331 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 } | 444 } |
| 443 task_ran->Signal(); | 445 task_ran->Signal(); |
| 444 }, | 446 }, |
| 445 com_sta_task_runner, Unretained(&task_ran))); | 447 com_sta_task_runner, Unretained(&task_ran))); |
| 446 task_ran.Wait(); | 448 task_ran.Wait(); |
| 447 } | 449 } |
| 448 #endif // defined(OS_WIN) | 450 #endif // defined(OS_WIN) |
| 449 | 451 |
| 450 } // namespace internal | 452 } // namespace internal |
| 451 } // namespace base | 453 } // namespace base |
| OLD | NEW |