| 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_thread_pool.h" | 5 #include "base/task_scheduler/scheduler_thread_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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 29 |
| 30 namespace base { | 30 namespace base { |
| 31 namespace internal { | 31 namespace internal { |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 const size_t kNumThreadsInThreadPool = 4; | 34 const size_t kNumThreadsInThreadPool = 4; |
| 35 const size_t kNumThreadsPostingTasks = 4; | 35 const size_t kNumThreadsPostingTasks = 4; |
| 36 const size_t kNumTasksPostedPerThread = 150; | 36 const size_t kNumTasksPostedPerThread = 150; |
| 37 | 37 |
| 38 class TaskSchedulerThreadPoolTest | 38 class TestDelayedTaskManager : public DelayedTaskManager { |
| 39 public: |
| 40 TestDelayedTaskManager() : DelayedTaskManager(Bind(&DoNothing)) {} |
| 41 |
| 42 void SetCurrentTime(TimeTicks now) { now_ = now; } |
| 43 |
| 44 // DelayedTaskManager: |
| 45 TimeTicks Now() const override { return now_; } |
| 46 |
| 47 private: |
| 48 TimeTicks now_ = TimeTicks::Now(); |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(TestDelayedTaskManager); |
| 51 }; |
| 52 |
| 53 class TaskSchedulerThreadPoolImplTest |
| 39 : public testing::TestWithParam<ExecutionMode> { | 54 : public testing::TestWithParam<ExecutionMode> { |
| 40 protected: | 55 protected: |
| 41 TaskSchedulerThreadPoolTest() : delayed_task_manager_(Bind(&DoNothing)) {} | 56 TaskSchedulerThreadPoolImplTest() = default; |
| 42 | 57 |
| 43 void SetUp() override { | 58 void SetUp() override { |
| 44 thread_pool_ = SchedulerThreadPool::CreateThreadPool( | 59 thread_pool_ = SchedulerThreadPoolImpl::CreateThreadPool( |
| 45 ThreadPriority::NORMAL, kNumThreadsInThreadPool, | 60 ThreadPriority::NORMAL, kNumThreadsInThreadPool, |
| 46 Bind(&TaskSchedulerThreadPoolTest::EnqueueSequenceCallback, | 61 Bind(&TaskSchedulerThreadPoolImplTest::ReEnqueueSequenceCallback, |
| 47 Unretained(this)), | 62 Unretained(this)), |
| 48 &task_tracker_, &delayed_task_manager_); | 63 &task_tracker_, &delayed_task_manager_); |
| 49 ASSERT_TRUE(thread_pool_); | 64 ASSERT_TRUE(thread_pool_); |
| 50 } | 65 } |
| 51 | 66 |
| 52 void TearDown() override { | 67 void TearDown() override { |
| 53 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 68 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 54 thread_pool_->JoinForTesting(); | 69 thread_pool_->JoinForTesting(); |
| 55 } | 70 } |
| 56 | 71 |
| 57 std::unique_ptr<SchedulerThreadPool> thread_pool_; | 72 std::unique_ptr<SchedulerThreadPoolImpl> thread_pool_; |
| 73 |
| 74 TaskTracker task_tracker_; |
| 75 TestDelayedTaskManager delayed_task_manager_; |
| 58 | 76 |
| 59 private: | 77 private: |
| 60 void EnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { | 78 void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { |
| 61 // In production code, this callback would be implemented by the | 79 // In production code, this callback would be implemented by the |
| 62 // TaskScheduler which would first determine which PriorityQueue the | 80 // TaskScheduler which would first determine which PriorityQueue the |
| 63 // sequence must be reinserted. | 81 // sequence must be re-enqueued. |
| 64 const SequenceSortKey sort_key(sequence->GetSortKey()); | 82 const SequenceSortKey sort_key(sequence->GetSortKey()); |
| 65 thread_pool_->EnqueueSequence(std::move(sequence), sort_key); | 83 thread_pool_->ReEnqueueSequence(std::move(sequence), sort_key); |
| 66 } | 84 } |
| 67 | 85 |
| 68 TaskTracker task_tracker_; | 86 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolImplTest); |
| 69 DelayedTaskManager delayed_task_manager_; | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest); | |
| 72 }; | 87 }; |
| 73 | 88 |
| 74 class TaskFactory { | 89 class TaskFactory { |
| 75 public: | 90 public: |
| 76 // Constructs a TaskFactory that posts tasks with |execution_mode| to | 91 // Constructs a TaskFactory that posts tasks with |execution_mode| to |
| 77 // |thread_pool|. | 92 // |thread_pool|. |
| 78 TaskFactory(SchedulerThreadPool* thread_pool, ExecutionMode execution_mode) | 93 TaskFactory(SchedulerThreadPoolImpl* thread_pool, |
| 94 ExecutionMode execution_mode) |
| 79 : cv_(&lock_), | 95 : cv_(&lock_), |
| 80 task_runner_(thread_pool->CreateTaskRunnerWithTraits(TaskTraits(), | 96 task_runner_(thread_pool->CreateTaskRunnerWithTraits(TaskTraits(), |
| 81 execution_mode)), | 97 execution_mode)), |
| 82 execution_mode_(execution_mode) {} | 98 execution_mode_(execution_mode) {} |
| 83 | 99 |
| 84 // Posts a task through |task_runner_|. If |post_nested_task| is true, the | 100 // Posts a task through |task_runner_|. If |post_nested_task| is true, the |
| 85 // task will post a new task when it runs. If |event| is set, the task will | 101 // task will post a new task when it runs. If |event| is set, the task will |
| 86 // block until it is signaled. | 102 // block until it is signaled. |
| 87 void PostTestTask(bool post_nested_task, WaitableEvent* event) { | 103 void PostTestTask(bool post_nested_task, WaitableEvent* event) { |
| 88 AutoLock auto_lock(lock_); | 104 AutoLock auto_lock(lock_); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 DISALLOW_COPY_AND_ASSIGN(TaskFactory); | 172 DISALLOW_COPY_AND_ASSIGN(TaskFactory); |
| 157 }; | 173 }; |
| 158 | 174 |
| 159 class ThreadPostingTasks : public SimpleThread { | 175 class ThreadPostingTasks : public SimpleThread { |
| 160 public: | 176 public: |
| 161 // Constructs a thread that posts tasks to |thread_pool| through an | 177 // Constructs a thread that posts tasks to |thread_pool| through an |
| 162 // |execution_mode| task runner. If |wait_for_all_threads_idle| is true, the | 178 // |execution_mode| task runner. If |wait_for_all_threads_idle| is true, the |
| 163 // thread wait until all worker threads in |thread_pool| are idle before | 179 // thread wait until all worker threads in |thread_pool| are idle before |
| 164 // posting a new task. If |post_nested_task| is true, each task posted by this | 180 // posting a new task. If |post_nested_task| is true, each task posted by this |
| 165 // thread posts another task when it runs. | 181 // thread posts another task when it runs. |
| 166 ThreadPostingTasks(SchedulerThreadPool* thread_pool, | 182 ThreadPostingTasks(SchedulerThreadPoolImpl* thread_pool, |
| 167 ExecutionMode execution_mode, | 183 ExecutionMode execution_mode, |
| 168 bool wait_for_all_threads_idle, | 184 bool wait_for_all_threads_idle, |
| 169 bool post_nested_task) | 185 bool post_nested_task) |
| 170 : SimpleThread("ThreadPostingTasks"), | 186 : SimpleThread("ThreadPostingTasks"), |
| 171 thread_pool_(thread_pool), | 187 thread_pool_(thread_pool), |
| 172 wait_for_all_threads_idle_(wait_for_all_threads_idle), | 188 wait_for_all_threads_idle_(wait_for_all_threads_idle), |
| 173 post_nested_task_(post_nested_task), | 189 post_nested_task_(post_nested_task), |
| 174 factory_(thread_pool_, execution_mode) { | 190 factory_(thread_pool_, execution_mode) { |
| 175 DCHECK(thread_pool_); | 191 DCHECK(thread_pool_); |
| 176 } | 192 } |
| 177 | 193 |
| 178 const TaskFactory* factory() const { return &factory_; } | 194 const TaskFactory* factory() const { return &factory_; } |
| 179 | 195 |
| 180 private: | 196 private: |
| 181 void Run() override { | 197 void Run() override { |
| 182 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); | 198 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); |
| 183 | 199 |
| 184 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { | 200 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { |
| 185 if (wait_for_all_threads_idle_) | 201 if (wait_for_all_threads_idle_) |
| 186 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 202 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 187 factory_.PostTestTask(post_nested_task_, nullptr); | 203 factory_.PostTestTask(post_nested_task_, nullptr); |
| 188 } | 204 } |
| 189 } | 205 } |
| 190 | 206 |
| 191 SchedulerThreadPool* const thread_pool_; | 207 SchedulerThreadPoolImpl* const thread_pool_; |
| 192 const scoped_refptr<TaskRunner> task_runner_; | 208 const scoped_refptr<TaskRunner> task_runner_; |
| 193 const bool wait_for_all_threads_idle_; | 209 const bool wait_for_all_threads_idle_; |
| 194 const bool post_nested_task_; | 210 const bool post_nested_task_; |
| 195 TaskFactory factory_; | 211 TaskFactory factory_; |
| 196 | 212 |
| 197 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); | 213 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); |
| 198 }; | 214 }; |
| 199 | 215 |
| 200 TEST_P(TaskSchedulerThreadPoolTest, PostTasks) { | 216 void ShouldNotRunCallback() { |
| 217 ADD_FAILURE() << "Ran a task that shouldn't run."; |
| 218 } |
| 219 |
| 220 void SignalEventCallback(WaitableEvent* event) { |
| 221 DCHECK(event); |
| 222 event->Signal(); |
| 223 } |
| 224 |
| 225 } // namespace |
| 226 |
| 227 TEST_P(TaskSchedulerThreadPoolImplTest, PostTasks) { |
| 201 // Create threads to post tasks. | 228 // Create threads to post tasks. |
| 202 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 229 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 203 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | 230 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
| 204 const bool kWaitForAllThreadIdle = false; | 231 const bool kWaitForAllThreadIdle = false; |
| 205 const bool kPostNestedTasks = false; | 232 const bool kPostNestedTasks = false; |
| 206 threads_posting_tasks.push_back(WrapUnique( | 233 threads_posting_tasks.push_back(WrapUnique( |
| 207 new ThreadPostingTasks(thread_pool_.get(), GetParam(), | 234 new ThreadPostingTasks(thread_pool_.get(), GetParam(), |
| 208 kWaitForAllThreadIdle, kPostNestedTasks))); | 235 kWaitForAllThreadIdle, kPostNestedTasks))); |
| 209 threads_posting_tasks.back()->Start(); | 236 threads_posting_tasks.back()->Start(); |
| 210 } | 237 } |
| 211 | 238 |
| 212 // Wait for all tasks to run. | 239 // Wait for all tasks to run. |
| 213 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 240 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 214 thread_posting_tasks->Join(); | 241 thread_posting_tasks->Join(); |
| 215 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 242 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 216 EXPECT_EQ(kNumTasksPostedPerThread, | 243 EXPECT_EQ(kNumTasksPostedPerThread, |
| 217 thread_posting_tasks->factory()->NumRunTasks()); | 244 thread_posting_tasks->factory()->NumRunTasks()); |
| 218 } | 245 } |
| 219 | 246 |
| 220 // Wait until all worker threads are idle to be sure that no task accesses | 247 // Wait until all worker threads are idle to be sure that no task accesses |
| 221 // its TaskFactory after |thread_posting_tasks| is destroyed. | 248 // its TaskFactory after |thread_posting_tasks| is destroyed. |
| 222 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 249 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 223 } | 250 } |
| 224 | 251 |
| 225 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) { | 252 TEST_P(TaskSchedulerThreadPoolImplTest, PostTasksWaitAllThreadsIdle) { |
| 226 // Create threads to post tasks. To verify that worker threads can sleep and | 253 // Create threads to post tasks. To verify that worker threads can sleep and |
| 227 // be woken up when new tasks are posted, wait for all threads to become idle | 254 // be woken up when new tasks are posted, wait for all threads to become idle |
| 228 // before posting a new task. | 255 // before posting a new task. |
| 229 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 256 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 230 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | 257 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
| 231 const bool kWaitForAllThreadIdle = true; | 258 const bool kWaitForAllThreadIdle = true; |
| 232 const bool kPostNestedTasks = false; | 259 const bool kPostNestedTasks = false; |
| 233 threads_posting_tasks.push_back(WrapUnique( | 260 threads_posting_tasks.push_back(WrapUnique( |
| 234 new ThreadPostingTasks(thread_pool_.get(), GetParam(), | 261 new ThreadPostingTasks(thread_pool_.get(), GetParam(), |
| 235 kWaitForAllThreadIdle, kPostNestedTasks))); | 262 kWaitForAllThreadIdle, kPostNestedTasks))); |
| 236 threads_posting_tasks.back()->Start(); | 263 threads_posting_tasks.back()->Start(); |
| 237 } | 264 } |
| 238 | 265 |
| 239 // Wait for all tasks to run. | 266 // Wait for all tasks to run. |
| 240 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 267 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 241 thread_posting_tasks->Join(); | 268 thread_posting_tasks->Join(); |
| 242 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 269 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 243 EXPECT_EQ(kNumTasksPostedPerThread, | 270 EXPECT_EQ(kNumTasksPostedPerThread, |
| 244 thread_posting_tasks->factory()->NumRunTasks()); | 271 thread_posting_tasks->factory()->NumRunTasks()); |
| 245 } | 272 } |
| 246 | 273 |
| 247 // Wait until all worker threads are idle to be sure that no task accesses | 274 // Wait until all worker threads are idle to be sure that no task accesses |
| 248 // its TaskFactory after |thread_posting_tasks| is destroyed. | 275 // its TaskFactory after |thread_posting_tasks| is destroyed. |
| 249 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 276 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 250 } | 277 } |
| 251 | 278 |
| 252 TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) { | 279 TEST_P(TaskSchedulerThreadPoolImplTest, NestedPostTasks) { |
| 253 // Create threads to post tasks. Each task posted by these threads will post | 280 // Create threads to post tasks. Each task posted by these threads will post |
| 254 // another task when it runs. | 281 // another task when it runs. |
| 255 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 282 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 256 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | 283 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
| 257 const bool kWaitForAllThreadIdle = false; | 284 const bool kWaitForAllThreadIdle = false; |
| 258 const bool kPostNestedTasks = true; | 285 const bool kPostNestedTasks = true; |
| 259 threads_posting_tasks.push_back(WrapUnique( | 286 threads_posting_tasks.push_back(WrapUnique( |
| 260 new ThreadPostingTasks(thread_pool_.get(), GetParam(), | 287 new ThreadPostingTasks(thread_pool_.get(), GetParam(), |
| 261 kWaitForAllThreadIdle, kPostNestedTasks))); | 288 kWaitForAllThreadIdle, kPostNestedTasks))); |
| 262 threads_posting_tasks.back()->Start(); | 289 threads_posting_tasks.back()->Start(); |
| 263 } | 290 } |
| 264 | 291 |
| 265 // Wait for all tasks to run. | 292 // Wait for all tasks to run. |
| 266 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 293 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 267 thread_posting_tasks->Join(); | 294 thread_posting_tasks->Join(); |
| 268 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 295 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 269 EXPECT_EQ(2 * kNumTasksPostedPerThread, | 296 EXPECT_EQ(2 * kNumTasksPostedPerThread, |
| 270 thread_posting_tasks->factory()->NumRunTasks()); | 297 thread_posting_tasks->factory()->NumRunTasks()); |
| 271 } | 298 } |
| 272 | 299 |
| 273 // Wait until all worker threads are idle to be sure that no task accesses | 300 // Wait until all worker threads are idle to be sure that no task accesses |
| 274 // its TaskFactory after |thread_posting_tasks| is destroyed. | 301 // its TaskFactory after |thread_posting_tasks| is destroyed. |
| 275 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 302 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 276 } | 303 } |
| 277 | 304 |
| 278 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) { | 305 TEST_P(TaskSchedulerThreadPoolImplTest, PostTasksWithOneAvailableThread) { |
| 279 // Post tasks to keep all threads busy except one until |event| is signaled. | 306 // Post tasks to keep all threads busy except one until |event| is signaled. |
| 280 // Use different factories so that tasks are added to different sequences and | 307 // Use different factories so that tasks are added to different sequences and |
| 281 // can run simultaneously when the execution mode is SEQUENCED. | 308 // can run simultaneously when the execution mode is SEQUENCED. |
| 282 WaitableEvent event(true, false); | 309 WaitableEvent event(true, false); |
| 283 std::vector<std::unique_ptr<TaskFactory>> blocked_task_factories; | 310 std::vector<std::unique_ptr<TaskFactory>> blocked_task_factories; |
| 284 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { | 311 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { |
| 285 blocked_task_factories.push_back( | 312 blocked_task_factories.push_back( |
| 286 WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); | 313 WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); |
| 287 blocked_task_factories.back()->PostTestTask(false, &event); | 314 blocked_task_factories.back()->PostTestTask(false, &event); |
| 288 blocked_task_factories.back()->WaitForAllTasksToRun(); | 315 blocked_task_factories.back()->WaitForAllTasksToRun(); |
| 289 } | 316 } |
| 290 | 317 |
| 291 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact | 318 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact |
| 292 // that only one thread in |thread_pool_| isn't busy. | 319 // that only one thread in |thread_pool_| isn't busy. |
| 293 TaskFactory short_task_factory(thread_pool_.get(), GetParam()); | 320 TaskFactory short_task_factory(thread_pool_.get(), GetParam()); |
| 294 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) | 321 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) |
| 295 short_task_factory.PostTestTask(false, nullptr); | 322 short_task_factory.PostTestTask(false, nullptr); |
| 296 short_task_factory.WaitForAllTasksToRun(); | 323 short_task_factory.WaitForAllTasksToRun(); |
| 297 | 324 |
| 298 // Release tasks waiting on |event|. | 325 // Release tasks waiting on |event|. |
| 299 event.Signal(); | 326 event.Signal(); |
| 300 | 327 |
| 301 // Wait until all worker threads are idle to be sure that no task accesses | 328 // Wait until all worker threads are idle to be sure that no task accesses |
| 302 // its TaskFactory after it is destroyed. | 329 // its TaskFactory after it is destroyed. |
| 303 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 330 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 304 } | 331 } |
| 305 | 332 |
| 306 TEST_P(TaskSchedulerThreadPoolTest, Saturate) { | 333 TEST_P(TaskSchedulerThreadPoolImplTest, Saturate) { |
| 307 // Verify that it is possible to have |kNumThreadsInThreadPool| | 334 // Verify that it is possible to have |kNumThreadsInThreadPool| |
| 308 // tasks/sequences running simultaneously. Use different factories so that | 335 // tasks/sequences running simultaneously. Use different factories so that |
| 309 // tasks are added to different sequences and can run simultaneously when the | 336 // tasks are added to different sequences and can run simultaneously when the |
| 310 // execution mode is SEQUENCED. | 337 // execution mode is SEQUENCED. |
| 311 WaitableEvent event(true, false); | 338 WaitableEvent event(true, false); |
| 312 std::vector<std::unique_ptr<TaskFactory>> factories; | 339 std::vector<std::unique_ptr<TaskFactory>> factories; |
| 313 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { | 340 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { |
| 314 factories.push_back( | 341 factories.push_back( |
| 315 WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); | 342 WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); |
| 316 factories.back()->PostTestTask(false, &event); | 343 factories.back()->PostTestTask(false, &event); |
| 317 factories.back()->WaitForAllTasksToRun(); | 344 factories.back()->WaitForAllTasksToRun(); |
| 318 } | 345 } |
| 319 | 346 |
| 320 // Release tasks waiting on |event|. | 347 // Release tasks waiting on |event|. |
| 321 event.Signal(); | 348 event.Signal(); |
| 322 | 349 |
| 323 // Wait until all worker threads are idle to be sure that no task accesses | 350 // Wait until all worker threads are idle to be sure that no task accesses |
| 324 // its TaskFactory after it is destroyed. | 351 // its TaskFactory after it is destroyed. |
| 325 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 352 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 326 } | 353 } |
| 327 | 354 |
| 355 // Verify that a Task can't be posted after shutdown. |
| 356 TEST_P(TaskSchedulerThreadPoolImplTest, PostTaskAfterShutdown) { |
| 357 auto task_runner = |
| 358 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()); |
| 359 task_tracker_.Shutdown(); |
| 360 EXPECT_FALSE(task_runner->PostTask(FROM_HERE, Bind(&ShouldNotRunCallback))); |
| 361 } |
| 362 |
| 363 // Verify that a Task posted with a delay is added to the DelayedTaskManager and |
| 364 // doesn't run before its delay expires. |
| 365 TEST_P(TaskSchedulerThreadPoolImplTest, PostDelayedTask) { |
| 366 EXPECT_TRUE(delayed_task_manager_.GetDelayedRunTime().is_null()); |
| 367 |
| 368 // Post a delayed task. |
| 369 WaitableEvent task_ran(true, false); |
| 370 EXPECT_TRUE(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()) |
| 371 ->PostDelayedTask(FROM_HERE, Bind(&SignalEventCallback, |
| 372 Unretained(&task_ran)), |
| 373 TimeDelta::FromSeconds(10))); |
| 374 |
| 375 // The task should have been added to the DelayedTaskManager. |
| 376 EXPECT_FALSE(delayed_task_manager_.GetDelayedRunTime().is_null()); |
| 377 |
| 378 // The task shouldn't run. |
| 379 EXPECT_FALSE(task_ran.IsSignaled()); |
| 380 |
| 381 // Fast-forward time and post tasks that are ripe for execution. |
| 382 delayed_task_manager_.SetCurrentTime( |
| 383 delayed_task_manager_.GetDelayedRunTime()); |
| 384 delayed_task_manager_.PostReadyTasks(); |
| 385 |
| 386 // The task should run. |
| 387 task_ran.Wait(); |
| 388 } |
| 389 |
| 328 INSTANTIATE_TEST_CASE_P(Parallel, | 390 INSTANTIATE_TEST_CASE_P(Parallel, |
| 329 TaskSchedulerThreadPoolTest, | 391 TaskSchedulerThreadPoolImplTest, |
| 330 ::testing::Values(ExecutionMode::PARALLEL)); | 392 ::testing::Values(ExecutionMode::PARALLEL)); |
| 331 INSTANTIATE_TEST_CASE_P(Sequenced, | 393 INSTANTIATE_TEST_CASE_P(Sequenced, |
| 332 TaskSchedulerThreadPoolTest, | 394 TaskSchedulerThreadPoolImplTest, |
| 333 ::testing::Values(ExecutionMode::SEQUENCED)); | 395 ::testing::Values(ExecutionMode::SEQUENCED)); |
| 334 | 396 |
| 335 } // namespace | |
| 336 } // namespace internal | 397 } // namespace internal |
| 337 } // namespace base | 398 } // namespace base |
| OLD | NEW |