fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 5 | #include "base/task_scheduler/scheduler_thread_pool_impl.h" |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 6 | |
| 7 | #include <stddef.h> |
| 8 | |
| 9 | #include <memory> |
| 10 | #include <unordered_set> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/bind.h" |
| 14 | #include "base/bind_helpers.h" |
fdoray | dace22d | 2016-04-29 04:35:47 +0900 | [diff] [blame^] | 15 | #include "base/callback.h" |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 16 | #include "base/macros.h" |
| 17 | #include "base/memory/ptr_util.h" |
| 18 | #include "base/memory/ref_counted.h" |
| 19 | #include "base/synchronization/condition_variable.h" |
| 20 | #include "base/synchronization/lock.h" |
| 21 | #include "base/synchronization/waitable_event.h" |
| 22 | #include "base/task_runner.h" |
fdoray | c2c7499 | 2016-04-20 10:39:21 +0900 | [diff] [blame] | 23 | #include "base/task_scheduler/delayed_task_manager.h" |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 24 | #include "base/task_scheduler/sequence.h" |
| 25 | #include "base/task_scheduler/sequence_sort_key.h" |
| 26 | #include "base/task_scheduler/task_tracker.h" |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 27 | #include "base/task_scheduler/test_task_factory.h" |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 28 | #include "base/threading/platform_thread.h" |
| 29 | #include "base/threading/simple_thread.h" |
| 30 | #include "testing/gtest/include/gtest/gtest.h" |
| 31 | |
| 32 | namespace base { |
| 33 | namespace internal { |
| 34 | namespace { |
| 35 | |
| 36 | const size_t kNumThreadsInThreadPool = 4; |
| 37 | const size_t kNumThreadsPostingTasks = 4; |
| 38 | const size_t kNumTasksPostedPerThread = 150; |
| 39 | |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 40 | class TestDelayedTaskManager : public DelayedTaskManager { |
| 41 | public: |
| 42 | TestDelayedTaskManager() : DelayedTaskManager(Bind(&DoNothing)) {} |
| 43 | |
| 44 | void SetCurrentTime(TimeTicks now) { now_ = now; } |
| 45 | |
| 46 | // DelayedTaskManager: |
| 47 | TimeTicks Now() const override { return now_; } |
| 48 | |
| 49 | private: |
| 50 | TimeTicks now_ = TimeTicks::Now(); |
| 51 | |
| 52 | DISALLOW_COPY_AND_ASSIGN(TestDelayedTaskManager); |
| 53 | }; |
| 54 | |
| 55 | class TaskSchedulerThreadPoolImplTest |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 56 | : public testing::TestWithParam<ExecutionMode> { |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 57 | protected: |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 58 | TaskSchedulerThreadPoolImplTest() = default; |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 59 | |
| 60 | void SetUp() override { |
fdoray | de70fef | 2016-04-26 10:52:07 +0900 | [diff] [blame] | 61 | thread_pool_ = SchedulerThreadPoolImpl::Create( |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 62 | ThreadPriority::NORMAL, kNumThreadsInThreadPool, |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 63 | Bind(&TaskSchedulerThreadPoolImplTest::ReEnqueueSequenceCallback, |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 64 | Unretained(this)), |
fdoray | c2c7499 | 2016-04-20 10:39:21 +0900 | [diff] [blame] | 65 | &task_tracker_, &delayed_task_manager_); |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 66 | ASSERT_TRUE(thread_pool_); |
| 67 | } |
| 68 | |
| 69 | void TearDown() override { |
| 70 | thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 71 | thread_pool_->JoinForTesting(); |
| 72 | } |
| 73 | |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 74 | std::unique_ptr<SchedulerThreadPoolImpl> thread_pool_; |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 75 | |
| 76 | TaskTracker task_tracker_; |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 77 | TestDelayedTaskManager delayed_task_manager_; |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 78 | |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 79 | private: |
| 80 | void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { |
| 81 | // In production code, this callback would be implemented by the |
| 82 | // TaskScheduler which would first determine which PriorityQueue the |
| 83 | // sequence must be re-enqueued. |
| 84 | const SequenceSortKey sort_key(sequence->GetSortKey()); |
| 85 | thread_pool_->ReEnqueueSequence(std::move(sequence), sort_key); |
| 86 | } |
| 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolImplTest); |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 89 | }; |
| 90 | |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 91 | using PostNestedTask = test::TestTaskFactory::PostNestedTask; |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 92 | |
| 93 | class ThreadPostingTasks : public SimpleThread { |
| 94 | public: |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 95 | enum class WaitBeforePostTask { |
| 96 | NO_WAIT, |
| 97 | WAIT_FOR_ALL_THREADS_IDLE, |
| 98 | }; |
| 99 | |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 100 | // Constructs a thread that posts tasks to |thread_pool| through an |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 101 | // |execution_mode| task runner. If |wait_before_post_task| is |
| 102 | // WAIT_FOR_ALL_THREADS_IDLE, the thread waits until all worker threads in |
| 103 | // |thread_pool| are idle before posting a new task. If |post_nested_task| is |
| 104 | // YES, each task posted by this thread posts another task when it runs. |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 105 | ThreadPostingTasks(SchedulerThreadPoolImpl* thread_pool, |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 106 | ExecutionMode execution_mode, |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 107 | WaitBeforePostTask wait_before_post_task, |
| 108 | PostNestedTask post_nested_task) |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 109 | : SimpleThread("ThreadPostingTasks"), |
| 110 | thread_pool_(thread_pool), |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 111 | wait_before_post_task_(wait_before_post_task), |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 112 | post_nested_task_(post_nested_task), |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 113 | factory_(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), |
| 114 | execution_mode), |
| 115 | execution_mode) { |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 116 | DCHECK(thread_pool_); |
| 117 | } |
| 118 | |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 119 | const test::TestTaskFactory* factory() const { return &factory_; } |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | void Run() override { |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 123 | EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 124 | |
| 125 | for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 126 | if (wait_before_post_task_ == |
| 127 | WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE) { |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 128 | thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 129 | } |
fdoray | dace22d | 2016-04-29 04:35:47 +0900 | [diff] [blame^] | 130 | EXPECT_TRUE(factory_.PostTask(post_nested_task_, Closure())); |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 134 | SchedulerThreadPoolImpl* const thread_pool_; |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 135 | const scoped_refptr<TaskRunner> task_runner_; |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 136 | const WaitBeforePostTask wait_before_post_task_; |
| 137 | const PostNestedTask post_nested_task_; |
| 138 | test::TestTaskFactory factory_; |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 139 | |
| 140 | DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); |
| 141 | }; |
| 142 | |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 143 | using WaitBeforePostTask = ThreadPostingTasks::WaitBeforePostTask; |
| 144 | |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 145 | void ShouldNotRunCallback() { |
| 146 | ADD_FAILURE() << "Ran a task that shouldn't run."; |
| 147 | } |
| 148 | |
| 149 | void SignalEventCallback(WaitableEvent* event) { |
| 150 | DCHECK(event); |
| 151 | event->Signal(); |
| 152 | } |
| 153 | |
| 154 | } // namespace |
| 155 | |
| 156 | TEST_P(TaskSchedulerThreadPoolImplTest, PostTasks) { |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 157 | // Create threads to post tasks. |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 158 | std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 159 | for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 160 | threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
| 161 | thread_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT, |
| 162 | PostNestedTask::NO))); |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 163 | threads_posting_tasks.back()->Start(); |
| 164 | } |
| 165 | |
| 166 | // Wait for all tasks to run. |
| 167 | for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 168 | thread_posting_tasks->Join(); |
| 169 | thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // Wait until all worker threads are idle to be sure that no task accesses |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 173 | // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 174 | thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 175 | } |
| 176 | |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 177 | TEST_P(TaskSchedulerThreadPoolImplTest, PostTasksWaitAllThreadsIdle) { |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 178 | // Create threads to post tasks. To verify that worker threads can sleep and |
| 179 | // be woken up when new tasks are posted, wait for all threads to become idle |
| 180 | // before posting a new task. |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 181 | std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 182 | for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 183 | threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
| 184 | thread_pool_.get(), GetParam(), |
| 185 | WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE, PostNestedTask::NO))); |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 186 | threads_posting_tasks.back()->Start(); |
| 187 | } |
| 188 | |
| 189 | // Wait for all tasks to run. |
| 190 | for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 191 | thread_posting_tasks->Join(); |
| 192 | thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Wait until all worker threads are idle to be sure that no task accesses |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 196 | // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 197 | thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 198 | } |
| 199 | |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 200 | TEST_P(TaskSchedulerThreadPoolImplTest, NestedPostTasks) { |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 201 | // Create threads to post tasks. Each task posted by these threads will post |
| 202 | // another task when it runs. |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 203 | std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 204 | for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 205 | threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
| 206 | thread_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT, |
| 207 | PostNestedTask::YES))); |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 208 | threads_posting_tasks.back()->Start(); |
| 209 | } |
| 210 | |
| 211 | // Wait for all tasks to run. |
| 212 | for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 213 | thread_posting_tasks->Join(); |
| 214 | thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // Wait until all worker threads are idle to be sure that no task accesses |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 218 | // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 219 | thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 220 | } |
| 221 | |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 222 | TEST_P(TaskSchedulerThreadPoolImplTest, PostTasksWithOneAvailableThread) { |
fdoray | dace22d | 2016-04-29 04:35:47 +0900 | [diff] [blame^] | 223 | // Post blocking tasks to keep all threads busy except one until |event| is |
| 224 | // signaled. Use different factories so that tasks are added to different |
| 225 | // sequences and can run simultaneously when the execution mode is SEQUENCED. |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 226 | WaitableEvent event(true, false); |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 227 | std::vector<std::unique_ptr<test::TestTaskFactory>> blocked_task_factories; |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 228 | for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 229 | blocked_task_factories.push_back(WrapUnique(new test::TestTaskFactory( |
| 230 | thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
| 231 | GetParam()))); |
fdoray | dace22d | 2016-04-29 04:35:47 +0900 | [diff] [blame^] | 232 | EXPECT_TRUE(blocked_task_factories.back()->PostTask( |
| 233 | PostNestedTask::NO, Bind(&WaitableEvent::Wait, Unretained(&event)))); |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 234 | blocked_task_factories.back()->WaitForAllTasksToRun(); |
| 235 | } |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 236 | |
| 237 | // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact |
| 238 | // that only one thread in |thread_pool_| isn't busy. |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 239 | test::TestTaskFactory short_task_factory( |
| 240 | thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
| 241 | GetParam()); |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 242 | for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) |
fdoray | dace22d | 2016-04-29 04:35:47 +0900 | [diff] [blame^] | 243 | EXPECT_TRUE(short_task_factory.PostTask(PostNestedTask::NO, Closure())); |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 244 | short_task_factory.WaitForAllTasksToRun(); |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 245 | |
| 246 | // Release tasks waiting on |event|. |
| 247 | event.Signal(); |
| 248 | |
| 249 | // Wait until all worker threads are idle to be sure that no task accesses |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 250 | // its TestTaskFactory after it is destroyed. |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 251 | thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 252 | } |
| 253 | |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 254 | TEST_P(TaskSchedulerThreadPoolImplTest, Saturate) { |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 255 | // Verify that it is possible to have |kNumThreadsInThreadPool| |
fdoray | dace22d | 2016-04-29 04:35:47 +0900 | [diff] [blame^] | 256 | // tasks/sequences running simultaneously. Use different factories so that the |
| 257 | // blocking tasks are added to different sequences and can run simultaneously |
| 258 | // when the execution mode is SEQUENCED. |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 259 | WaitableEvent event(true, false); |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 260 | std::vector<std::unique_ptr<test::TestTaskFactory>> factories; |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 261 | for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 262 | factories.push_back(WrapUnique(new test::TestTaskFactory( |
| 263 | thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
| 264 | GetParam()))); |
fdoray | dace22d | 2016-04-29 04:35:47 +0900 | [diff] [blame^] | 265 | EXPECT_TRUE(factories.back()->PostTask( |
| 266 | PostNestedTask::NO, Bind(&WaitableEvent::Wait, Unretained(&event)))); |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 267 | factories.back()->WaitForAllTasksToRun(); |
| 268 | } |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 269 | |
| 270 | // Release tasks waiting on |event|. |
| 271 | event.Signal(); |
| 272 | |
| 273 | // Wait until all worker threads are idle to be sure that no task accesses |
fdoray | 570633b | 2016-04-26 01:24:46 +0900 | [diff] [blame] | 274 | // its TestTaskFactory after it is destroyed. |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 275 | thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 276 | } |
| 277 | |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 278 | // Verify that a Task can't be posted after shutdown. |
| 279 | TEST_P(TaskSchedulerThreadPoolImplTest, PostTaskAfterShutdown) { |
| 280 | auto task_runner = |
| 281 | thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()); |
| 282 | task_tracker_.Shutdown(); |
| 283 | EXPECT_FALSE(task_runner->PostTask(FROM_HERE, Bind(&ShouldNotRunCallback))); |
| 284 | } |
| 285 | |
| 286 | // Verify that a Task posted with a delay is added to the DelayedTaskManager and |
| 287 | // doesn't run before its delay expires. |
| 288 | TEST_P(TaskSchedulerThreadPoolImplTest, PostDelayedTask) { |
| 289 | EXPECT_TRUE(delayed_task_manager_.GetDelayedRunTime().is_null()); |
| 290 | |
| 291 | // Post a delayed task. |
| 292 | WaitableEvent task_ran(true, false); |
| 293 | EXPECT_TRUE(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()) |
| 294 | ->PostDelayedTask(FROM_HERE, Bind(&SignalEventCallback, |
| 295 | Unretained(&task_ran)), |
| 296 | TimeDelta::FromSeconds(10))); |
| 297 | |
| 298 | // The task should have been added to the DelayedTaskManager. |
| 299 | EXPECT_FALSE(delayed_task_manager_.GetDelayedRunTime().is_null()); |
| 300 | |
| 301 | // The task shouldn't run. |
| 302 | EXPECT_FALSE(task_ran.IsSignaled()); |
| 303 | |
| 304 | // Fast-forward time and post tasks that are ripe for execution. |
| 305 | delayed_task_manager_.SetCurrentTime( |
| 306 | delayed_task_manager_.GetDelayedRunTime()); |
| 307 | delayed_task_manager_.PostReadyTasks(); |
| 308 | |
| 309 | // The task should run. |
| 310 | task_ran.Wait(); |
| 311 | } |
| 312 | |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 313 | INSTANTIATE_TEST_CASE_P(Parallel, |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 314 | TaskSchedulerThreadPoolImplTest, |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 315 | ::testing::Values(ExecutionMode::PARALLEL)); |
| 316 | INSTANTIATE_TEST_CASE_P(Sequenced, |
fdoray | 9b0b233 | 2016-04-26 06:34:33 +0900 | [diff] [blame] | 317 | TaskSchedulerThreadPoolImplTest, |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 318 | ::testing::Values(ExecutionMode::SEQUENCED)); |
fdoray | f0683ab | 2016-04-29 00:45:35 +0900 | [diff] [blame] | 319 | INSTANTIATE_TEST_CASE_P(SingleThreaded, |
| 320 | TaskSchedulerThreadPoolImplTest, |
| 321 | ::testing::Values(ExecutionMode::SINGLE_THREADED)); |
fdoray | 8224323 | 2016-04-16 08:25:15 +0900 | [diff] [blame] | 322 | |
fdoray | a2d271b | 2016-04-15 23:09:08 +0900 | [diff] [blame] | 323 | } // namespace internal |
| 324 | } // namespace base |