Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2984)

Unified Diff: base/task_scheduler/scheduler_thread_pool_unittest.cc

Issue 1911493002: TaskScheduler: Move TaskFactory out of scheduler_thread_pool_unittest.cc (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..19a8efbb6adc329e8ea567e458f935474ae77a8c 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,65 @@ 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 = TestTaskFactory::PostNestedTask;
class ThreadPostingTasks : public SimpleThread {
public:
+ enum class WaitForAllThreadsIdle {
+ YES,
robliao 2016/04/21 01:38:17 Maybe this can be enum class ThreadIdleBehavior {
fdoray 2016/04/22 15:54:03 Done.
+ NO,
+ };
+
// 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
+ // |execution_mode| task runner. If |wait_for_all_threads_idle| is YES, 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)
+ WaitForAllThreadsIdle wait_for_all_threads_idle,
+ PostNestedTask post_nested_task)
: SimpleThread("ThreadPostingTasks"),
thread_pool_(thread_pool),
wait_for_all_threads_idle_(wait_for_all_threads_idle),
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 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_for_all_threads_idle_ == WaitForAllThreadsIdle::YES)
thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
- factory_.PostTestTask(post_nested_task_, nullptr);
+ 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 WaitForAllThreadsIdle wait_for_all_threads_idle_;
+ const TestTaskFactory::PostNestedTask post_nested_task_;
+ TestTaskFactory factory_;
DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks);
};
+using WaitForAllThreadsIdle = ThreadPostingTasks::WaitForAllThreadsIdle;
+
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)));
+ WaitForAllThreadsIdle::NO, PostNestedTask::NO)));
threads_posting_tasks.back()->Start();
}
@@ -213,12 +138,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 +151,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(), WaitForAllThreadsIdle::YES,
+ PostNestedTask::NO)));
threads_posting_tasks.back()->Start();
}
@@ -240,12 +161,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 +173,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(), WaitForAllThreadsIdle::NO,
+ PostNestedTask::YES)));
threads_posting_tasks.back()->Start();
}
@@ -266,12 +183,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 +195,29 @@ 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<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 TestTaskFactory(
+ thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()),
+ GetParam())));
+ 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());
+ TestTaskFactory short_task_factory(
+ thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()),
+ GetParam());
for (size_t i = 0; i < kNumTasksPostedPerThread; ++i)
- short_task_factory.PostTestTask(false, nullptr);
+ 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 +227,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<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 TestTaskFactory(
+ thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()),
+ GetParam())));
+ factories.back()->PostTask(PostNestedTask::NO, &event);
factories.back()->WaitForAllTasksToRun();
}
@@ -321,7 +240,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();
}

Powered by Google App Engine
This is Rietveld 408576698