Index: base/task_scheduler/scheduler_thread_pool_unittest.cc |
diff --git a/base/task_scheduler/scheduler_thread_pool_unittest.cc b/base/task_scheduler/scheduler_thread_pool_unittest.cc |
index 7e1ec5c2cb1195864e6e812072b13f7a540ad4e6..39c28ab49d54793ad9ed6bbd36d7b75d4fc22da7 100644 |
--- a/base/task_scheduler/scheduler_thread_pool_unittest.cc |
+++ b/base/task_scheduler/scheduler_thread_pool_unittest.cc |
@@ -23,6 +23,7 @@ |
#include "base/task_scheduler/sequence.h" |
#include "base/task_scheduler/sequence_sort_key.h" |
#include "base/task_scheduler/task_tracker.h" |
+#include "base/task_scheduler/test_task_factory.h" |
#include "base/threading/platform_thread.h" |
#include "base/threading/simple_thread.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -71,141 +72,67 @@ class TaskSchedulerThreadPoolTest |
DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest); |
}; |
-class TaskFactory { |
- public: |
- // Constructs a TaskFactory that posts tasks with |execution_mode| to |
- // |thread_pool|. |
- TaskFactory(SchedulerThreadPool* thread_pool, ExecutionMode execution_mode) |
- : cv_(&lock_), |
- task_runner_(thread_pool->CreateTaskRunnerWithTraits(TaskTraits(), |
- execution_mode)), |
- execution_mode_(execution_mode) {} |
- |
- // Posts a task through |task_runner_|. If |post_nested_task| is true, the |
- // task will post a new task when it runs. If |event| is set, the task will |
- // block until it is signaled. |
- void PostTestTask(bool post_nested_task, WaitableEvent* event) { |
- AutoLock auto_lock(lock_); |
- EXPECT_TRUE(task_runner_->PostTask( |
- FROM_HERE, |
- Bind(&TaskFactory::RunTaskCallback, Unretained(this), |
- num_created_tasks_++, post_nested_task, Unretained(event)))); |
- } |
- |
- // Waits for all tasks posted by PostTestTask() to start running. It is not |
- // guaranteed that the tasks have completed their execution when this returns. |
- void WaitForAllTasksToRun() const { |
- AutoLock auto_lock(lock_); |
- while (ran_tasks_.size() < num_created_tasks_) |
- cv_.Wait(); |
- } |
- |
- size_t NumRunTasks() const { |
- AutoLock auto_lock(lock_); |
- return ran_tasks_.size(); |
- } |
- |
- const TaskRunner* task_runner() const { return task_runner_.get(); } |
- |
- private: |
- void RunTaskCallback(size_t task_index, |
- bool post_nested_task, |
- WaitableEvent* event) { |
- if (post_nested_task) |
- PostTestTask(false, nullptr); |
- |
- EXPECT_TRUE(task_runner_->RunsTasksOnCurrentThread()); |
- |
- { |
- AutoLock auto_lock(lock_); |
- |
- if (execution_mode_ == ExecutionMode::SEQUENCED && |
- task_index != ran_tasks_.size()) { |
- ADD_FAILURE() << "A SEQUENCED task didn't run in the expected order."; |
- } |
- |
- if (ran_tasks_.find(task_index) != ran_tasks_.end()) |
- ADD_FAILURE() << "A task ran more than once."; |
- ran_tasks_.insert(task_index); |
- |
- cv_.Signal(); |
- } |
- |
- if (event) |
- event->Wait(); |
- } |
- |
- // Synchronizes access to all members below. |
- mutable Lock lock_; |
- |
- // Condition variable signaled when a task runs. |
- mutable ConditionVariable cv_; |
- |
- // Task runner through which this factory posts tasks. |
- const scoped_refptr<TaskRunner> task_runner_; |
- |
- // Execution mode of |task_runner_|. |
- const ExecutionMode execution_mode_; |
- |
- // Number of tasks posted by PostTestTask(). |
- size_t num_created_tasks_ = 0; |
- |
- // Indexes of tasks that ran. |
- std::unordered_set<size_t> ran_tasks_; |
- |
- DISALLOW_COPY_AND_ASSIGN(TaskFactory); |
-}; |
+using PostNestedTask = test::TestTaskFactory::PostNestedTask; |
class ThreadPostingTasks : public SimpleThread { |
public: |
+ enum class WaitBeforePostTask { |
+ NO_WAIT, |
+ WAIT_FOR_ALL_THREADS_IDLE, |
+ }; |
+ |
// Constructs a thread that posts tasks to |thread_pool| through an |
- // |execution_mode| task runner. If |wait_for_all_threads_idle| is true, the |
- // thread wait until all worker threads in |thread_pool| are idle before |
- // posting a new task. If |post_nested_task| is true, each task posted by this |
- // thread posts another task when it runs. |
+ // |execution_mode| task runner. If |wait_before_post_task| is |
+ // WAIT_FOR_ALL_THREADS_IDLE, the thread waits until all worker threads in |
+ // |thread_pool| are idle before posting a new task. If |post_nested_task| is |
+ // YES, each task posted by this thread posts another task when it runs. |
ThreadPostingTasks(SchedulerThreadPool* thread_pool, |
ExecutionMode execution_mode, |
- bool wait_for_all_threads_idle, |
- bool post_nested_task) |
+ WaitBeforePostTask wait_before_post_task, |
+ PostNestedTask post_nested_task) |
: SimpleThread("ThreadPostingTasks"), |
thread_pool_(thread_pool), |
- wait_for_all_threads_idle_(wait_for_all_threads_idle), |
+ wait_before_post_task_(wait_before_post_task), |
post_nested_task_(post_nested_task), |
- factory_(thread_pool_, execution_mode) { |
+ factory_(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), |
+ execution_mode), |
+ execution_mode) { |
DCHECK(thread_pool_); |
} |
- const TaskFactory* factory() const { return &factory_; } |
+ const test::TestTaskFactory* factory() const { return &factory_; } |
private: |
void Run() override { |
EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); |
for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { |
- if (wait_for_all_threads_idle_) |
+ if (wait_before_post_task_ == |
+ WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE) { |
thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
- factory_.PostTestTask(post_nested_task_, nullptr); |
+ } |
+ EXPECT_TRUE(factory_.PostTask(post_nested_task_, nullptr)); |
} |
} |
SchedulerThreadPool* const thread_pool_; |
const scoped_refptr<TaskRunner> task_runner_; |
- const bool wait_for_all_threads_idle_; |
- const bool post_nested_task_; |
- TaskFactory factory_; |
+ const WaitBeforePostTask wait_before_post_task_; |
+ const PostNestedTask post_nested_task_; |
+ test::TestTaskFactory factory_; |
DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); |
}; |
+using WaitBeforePostTask = ThreadPostingTasks::WaitBeforePostTask; |
+ |
TEST_P(TaskSchedulerThreadPoolTest, PostTasks) { |
// Create threads to post tasks. |
std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
- const bool kWaitForAllThreadIdle = false; |
- const bool kPostNestedTasks = false; |
- threads_posting_tasks.push_back(WrapUnique( |
- new ThreadPostingTasks(thread_pool_.get(), GetParam(), |
- kWaitForAllThreadIdle, kPostNestedTasks))); |
+ threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
+ thread_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT, |
+ PostNestedTask::NO))); |
threads_posting_tasks.back()->Start(); |
} |
@@ -213,12 +140,10 @@ TEST_P(TaskSchedulerThreadPoolTest, PostTasks) { |
for (const auto& thread_posting_tasks : threads_posting_tasks) { |
thread_posting_tasks->Join(); |
thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
- EXPECT_EQ(kNumTasksPostedPerThread, |
- thread_posting_tasks->factory()->NumRunTasks()); |
} |
// Wait until all worker threads are idle to be sure that no task accesses |
- // its TaskFactory after |thread_posting_tasks| is destroyed. |
+ // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
} |
@@ -228,11 +153,9 @@ TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) { |
// before posting a new task. |
std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
- const bool kWaitForAllThreadIdle = true; |
- const bool kPostNestedTasks = false; |
- threads_posting_tasks.push_back(WrapUnique( |
- new ThreadPostingTasks(thread_pool_.get(), GetParam(), |
- kWaitForAllThreadIdle, kPostNestedTasks))); |
+ threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
+ thread_pool_.get(), GetParam(), |
+ WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE, PostNestedTask::NO))); |
threads_posting_tasks.back()->Start(); |
} |
@@ -240,12 +163,10 @@ TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) { |
for (const auto& thread_posting_tasks : threads_posting_tasks) { |
thread_posting_tasks->Join(); |
thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
- EXPECT_EQ(kNumTasksPostedPerThread, |
- thread_posting_tasks->factory()->NumRunTasks()); |
} |
// Wait until all worker threads are idle to be sure that no task accesses |
- // its TaskFactory after |thread_posting_tasks| is destroyed. |
+ // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
} |
@@ -254,11 +175,9 @@ TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) { |
// another task when it runs. |
std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
- const bool kWaitForAllThreadIdle = false; |
- const bool kPostNestedTasks = true; |
- threads_posting_tasks.push_back(WrapUnique( |
- new ThreadPostingTasks(thread_pool_.get(), GetParam(), |
- kWaitForAllThreadIdle, kPostNestedTasks))); |
+ threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
+ thread_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT, |
+ PostNestedTask::YES))); |
threads_posting_tasks.back()->Start(); |
} |
@@ -266,12 +185,10 @@ TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) { |
for (const auto& thread_posting_tasks : threads_posting_tasks) { |
thread_posting_tasks->Join(); |
thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
- EXPECT_EQ(2 * kNumTasksPostedPerThread, |
- thread_posting_tasks->factory()->NumRunTasks()); |
} |
// Wait until all worker threads are idle to be sure that no task accesses |
- // its TaskFactory after |thread_posting_tasks| is destroyed. |
+ // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
} |
@@ -280,26 +197,30 @@ TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) { |
// Use different factories so that tasks are added to different sequences and |
// can run simultaneously when the execution mode is SEQUENCED. |
WaitableEvent event(true, false); |
- std::vector<std::unique_ptr<TaskFactory>> blocked_task_factories; |
+ std::vector<std::unique_ptr<test::TestTaskFactory>> blocked_task_factories; |
for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { |
- blocked_task_factories.push_back( |
- WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); |
- blocked_task_factories.back()->PostTestTask(false, &event); |
+ blocked_task_factories.push_back(WrapUnique(new test::TestTaskFactory( |
+ thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
+ GetParam()))); |
+ EXPECT_TRUE( |
+ blocked_task_factories.back()->PostTask(PostNestedTask::NO, &event)); |
blocked_task_factories.back()->WaitForAllTasksToRun(); |
} |
// Post |kNumTasksPostedPerThread| tasks that should all run despite the fact |
// that only one thread in |thread_pool_| isn't busy. |
- TaskFactory short_task_factory(thread_pool_.get(), GetParam()); |
+ test::TestTaskFactory short_task_factory( |
+ thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
+ GetParam()); |
for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) |
- short_task_factory.PostTestTask(false, nullptr); |
+ EXPECT_TRUE(short_task_factory.PostTask(PostNestedTask::NO, nullptr)); |
short_task_factory.WaitForAllTasksToRun(); |
// Release tasks waiting on |event|. |
event.Signal(); |
// Wait until all worker threads are idle to be sure that no task accesses |
- // its TaskFactory after it is destroyed. |
+ // its TestTaskFactory after it is destroyed. |
thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
} |
@@ -309,11 +230,12 @@ TEST_P(TaskSchedulerThreadPoolTest, Saturate) { |
// tasks are added to different sequences and can run simultaneously when the |
// execution mode is SEQUENCED. |
WaitableEvent event(true, false); |
- std::vector<std::unique_ptr<TaskFactory>> factories; |
+ std::vector<std::unique_ptr<test::TestTaskFactory>> factories; |
for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { |
- factories.push_back( |
- WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); |
- factories.back()->PostTestTask(false, &event); |
+ factories.push_back(WrapUnique(new test::TestTaskFactory( |
+ thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
+ GetParam()))); |
+ EXPECT_TRUE(factories.back()->PostTask(PostNestedTask::NO, &event)); |
factories.back()->WaitForAllTasksToRun(); |
} |
@@ -321,7 +243,7 @@ TEST_P(TaskSchedulerThreadPoolTest, Saturate) { |
event.Signal(); |
// Wait until all worker threads are idle to be sure that no task accesses |
- // its TaskFactory after it is destroyed. |
+ // its TestTaskFactory after it is destroyed. |
thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
} |