| 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/scheduler_worker_pool_impl.h" | 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <unordered_set> | 10 #include <unordered_set> |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 const scoped_refptr<TaskRunner> task_runner_; | 174 const scoped_refptr<TaskRunner> task_runner_; |
| 175 const WaitBeforePostTask wait_before_post_task_; | 175 const WaitBeforePostTask wait_before_post_task_; |
| 176 const PostNestedTask post_nested_task_; | 176 const PostNestedTask post_nested_task_; |
| 177 test::TestTaskFactory factory_; | 177 test::TestTaskFactory factory_; |
| 178 | 178 |
| 179 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); | 179 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); |
| 180 }; | 180 }; |
| 181 | 181 |
| 182 using WaitBeforePostTask = ThreadPostingTasks::WaitBeforePostTask; | 182 using WaitBeforePostTask = ThreadPostingTasks::WaitBeforePostTask; |
| 183 | 183 |
| 184 void ShouldNotRunCallback() { | 184 void ShouldNotRun() { |
| 185 ADD_FAILURE() << "Ran a task that shouldn't run."; | 185 ADD_FAILURE() << "Ran a task that shouldn't run."; |
| 186 } | 186 } |
| 187 | 187 |
| 188 } // namespace | 188 } // namespace |
| 189 | 189 |
| 190 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTasks) { | 190 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTasks) { |
| 191 // Create threads to post tasks. | 191 // Create threads to post tasks. |
| 192 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 192 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 193 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | 193 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
| 194 threads_posting_tasks.push_back(MakeUnique<ThreadPostingTasks>( | 194 threads_posting_tasks.push_back(MakeUnique<ThreadPostingTasks>( |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 // Wait until all workers are idle to be sure that no task accesses | 309 // Wait until all workers are idle to be sure that no task accesses |
| 310 // its TestTaskFactory after it is destroyed. | 310 // its TestTaskFactory after it is destroyed. |
| 311 worker_pool_->WaitForAllWorkersIdleForTesting(); | 311 worker_pool_->WaitForAllWorkersIdleForTesting(); |
| 312 } | 312 } |
| 313 | 313 |
| 314 // Verify that a Task can't be posted after shutdown. | 314 // Verify that a Task can't be posted after shutdown. |
| 315 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTaskAfterShutdown) { | 315 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTaskAfterShutdown) { |
| 316 auto task_runner = | 316 auto task_runner = |
| 317 CreateTaskRunnerWithExecutionMode(worker_pool_.get(), GetParam()); | 317 CreateTaskRunnerWithExecutionMode(worker_pool_.get(), GetParam()); |
| 318 task_tracker_.Shutdown(); | 318 task_tracker_.Shutdown(); |
| 319 EXPECT_FALSE(task_runner->PostTask(FROM_HERE, Bind(&ShouldNotRunCallback))); | 319 EXPECT_FALSE(task_runner->PostTask(FROM_HERE, Bind(&ShouldNotRun))); |
| 320 } | 320 } |
| 321 | 321 |
| 322 // Verify that a Task runs shortly after its delay expires. | 322 // Verify that a Task runs shortly after its delay expires. |
| 323 TEST_P(TaskSchedulerWorkerPoolImplTest, PostDelayedTask) { | 323 TEST_P(TaskSchedulerWorkerPoolImplTest, PostDelayedTask) { |
| 324 TimeTicks start_time = TimeTicks::Now(); | 324 TimeTicks start_time = TimeTicks::Now(); |
| 325 | 325 |
| 326 // Post a task with a short delay. | 326 // Post a task with a short delay. |
| 327 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 327 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
| 328 WaitableEvent::InitialState::NOT_SIGNALED); | 328 WaitableEvent::InitialState::NOT_SIGNALED); |
| 329 EXPECT_TRUE(CreateTaskRunnerWithExecutionMode(worker_pool_.get(), GetParam()) | 329 EXPECT_TRUE(CreateTaskRunnerWithExecutionMode(worker_pool_.get(), GetParam()) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 341 EXPECT_LT(actual_delay, | 341 EXPECT_LT(actual_delay, |
| 342 TimeDelta::FromMilliseconds(250) + TestTimeouts::tiny_timeout()); | 342 TimeDelta::FromMilliseconds(250) + TestTimeouts::tiny_timeout()); |
| 343 } | 343 } |
| 344 | 344 |
| 345 // Verify that the RunsTasksOnCurrentThread() method of a SEQUENCED TaskRunner | 345 // Verify that the RunsTasksOnCurrentThread() method of a SEQUENCED TaskRunner |
| 346 // returns false when called from a task that isn't part of the sequence. Note: | 346 // returns false when called from a task that isn't part of the sequence. Note: |
| 347 // Tests that use TestTaskFactory already verify that RunsTasksOnCurrentThread() | 347 // Tests that use TestTaskFactory already verify that RunsTasksOnCurrentThread() |
| 348 // returns true when appropriate so this method complements it to get full | 348 // returns true when appropriate so this method complements it to get full |
| 349 // coverage of that method. | 349 // coverage of that method. |
| 350 TEST_P(TaskSchedulerWorkerPoolImplTest, SequencedRunsTasksOnCurrentThread) { | 350 TEST_P(TaskSchedulerWorkerPoolImplTest, SequencedRunsTasksOnCurrentThread) { |
| 351 scoped_refptr<TaskRunner> task_runner( | 351 auto task_runner = |
| 352 CreateTaskRunnerWithExecutionMode(worker_pool_.get(), GetParam())); | 352 CreateTaskRunnerWithExecutionMode(worker_pool_.get(), GetParam()); |
| 353 scoped_refptr<SequencedTaskRunner> sequenced_task_runner( | 353 auto sequenced_task_runner = |
| 354 worker_pool_->CreateSequencedTaskRunnerWithTraits(TaskTraits())); | 354 worker_pool_->CreateSequencedTaskRunnerWithTraits(TaskTraits()); |
| 355 | 355 |
| 356 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 356 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
| 357 WaitableEvent::InitialState::NOT_SIGNALED); | 357 WaitableEvent::InitialState::NOT_SIGNALED); |
| 358 task_runner->PostTask( | 358 task_runner->PostTask( |
| 359 FROM_HERE, | 359 FROM_HERE, |
| 360 Bind( | 360 Bind( |
| 361 [](scoped_refptr<TaskRunner> sequenced_task_runner, | 361 [](scoped_refptr<TaskRunner> sequenced_task_runner, |
| 362 WaitableEvent* task_ran) { | 362 WaitableEvent* task_ran) { |
| 363 EXPECT_FALSE(sequenced_task_runner->RunsTasksOnCurrentThread()); | 363 EXPECT_FALSE(sequenced_task_runner->RunsTasksOnCurrentThread()); |
| 364 task_ran->Signal(); | 364 task_ran->Signal(); |
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 StandbyThreadPolicy::ONE, 8U, TimeDelta::Max()), | 797 StandbyThreadPolicy::ONE, 8U, TimeDelta::Max()), |
| 798 Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, | 798 Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, |
| 799 &delayed_task_manager); | 799 &delayed_task_manager); |
| 800 ASSERT_TRUE(worker_pool); | 800 ASSERT_TRUE(worker_pool); |
| 801 EXPECT_EQ(1U, worker_pool->NumberOfAliveWorkersForTesting()); | 801 EXPECT_EQ(1U, worker_pool->NumberOfAliveWorkersForTesting()); |
| 802 worker_pool->JoinForTesting(); | 802 worker_pool->JoinForTesting(); |
| 803 } | 803 } |
| 804 | 804 |
| 805 } // namespace internal | 805 } // namespace internal |
| 806 } // namespace base | 806 } // namespace base |
| OLD | NEW |