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 ccec38b13c506c2eeac8ca042ce5bec4227fa974..4f21d2eac67f354a596d7819fc290504e517f8df 100644 |
--- a/base/task_scheduler/scheduler_thread_pool_unittest.cc |
+++ b/base/task_scheduler/scheduler_thread_pool_unittest.cc |
@@ -31,7 +31,8 @@ const size_t kNumThreadsInThreadPool = 4; |
const size_t kNumThreadsPostingTasks = 4; |
const size_t kNumTasksPostedPerThread = 150; |
-class TaskSchedulerThreadPoolTest : public testing::Test { |
+class TaskSchedulerThreadPoolTest |
+ : public testing::TestWithParam<ExecutionMode> { |
protected: |
TaskSchedulerThreadPoolTest() = default; |
@@ -71,19 +72,22 @@ class TaskSchedulerThreadPoolTest : public testing::Test { |
class TaskFactory { |
public: |
- TaskFactory() : cv_(&lock_) {} |
- |
- // Posts a task through |task_runner|. If |post_nested_task| is true, the task |
- // will post a new task through |task_runner| when it runs. If |event| is set, |
- // the task will block until it is signaled. |
- void PostTask(scoped_refptr<TaskRunner> task_runner, |
- bool post_nested_task, |
- WaitableEvent* event) { |
+ // Constructs a TaskFactory that posts tasks through |task_runner|. |
+ // |execution_mode| is the execution mode of |task_runner|. |
+ TaskFactory(scoped_refptr<TaskRunner> task_runner, |
robliao
2016/04/04 18:45:50
I wonder if there's a way we can extract this out
fdoray
2016/04/04 19:41:18
I changed the code to avoid relying on the caller
|
+ ExecutionMode execution_mode) |
+ : cv_(&lock_), |
+ task_runner_(std::move(task_runner)), |
+ execution_mode_(execution_mode) {} |
+ |
+ // Posts a task. 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 PostTask(bool post_nested_task, WaitableEvent* event) { |
AutoLock auto_lock(lock_); |
- task_runner->PostTask( |
- FROM_HERE, Bind(&TaskFactory::RunTaskCallback, Unretained(this), |
- num_created_tasks_++, task_runner, post_nested_task, |
- Unretained(event))); |
+ task_runner_->PostTask( |
+ FROM_HERE, |
+ Bind(&TaskFactory::RunTaskCallback, Unretained(this), |
+ num_created_tasks_++, post_nested_task, Unretained(event))); |
} |
// Waits for all tasks posted by PostTask() to start running. It is not |
@@ -101,17 +105,21 @@ class TaskFactory { |
private: |
void RunTaskCallback(size_t task_index, |
- scoped_refptr<TaskRunner> task_runner, |
bool post_nested_task, |
WaitableEvent* event) { |
if (post_nested_task) |
- PostTask(task_runner, false, nullptr); |
+ PostTask(false, nullptr); |
- EXPECT_TRUE(task_runner->RunsTasksOnCurrentThread()); |
+ EXPECT_TRUE(task_runner_->RunsTasksOnCurrentThread()); |
{ |
AutoLock auto_lock(lock_); |
+ if (execution_mode_ == ExecutionMode::SEQUENCED && |
+ task_index != run_tasks_.size()) { |
+ ADD_FAILURE() << "A SEQUENCED task didn't run in the expected order."; |
+ } |
+ |
if (run_tasks_.find(task_index) != run_tasks_.end()) |
ADD_FAILURE() << "A task ran more than once."; |
run_tasks_.insert(task_index); |
@@ -129,6 +137,12 @@ class TaskFactory { |
// 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 PostTask(). |
size_t num_created_tasks_ = 0; |
@@ -154,7 +168,8 @@ class ThreadPostingTasks : public SimpleThread { |
task_runner_(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), |
execution_mode)), |
wait_for_all_threads_idle_(wait_for_all_threads_idle), |
- post_nested_task_(post_nested_task) {} |
+ post_nested_task_(post_nested_task), |
+ factory_(task_runner_, execution_mode) {} |
const TaskFactory* factory() const { return &factory_; } |
@@ -165,7 +180,7 @@ class ThreadPostingTasks : public SimpleThread { |
for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { |
if (wait_for_all_threads_idle_) |
thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
- factory_.PostTask(task_runner_, post_nested_task_, nullptr); |
+ factory_.PostTask(post_nested_task_, nullptr); |
} |
} |
@@ -178,8 +193,8 @@ class ThreadPostingTasks : public SimpleThread { |
DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); |
}; |
-TEST_F(TaskSchedulerThreadPoolTest, PostParallelTasks) { |
- // Create threads to post tasks to PARALLEL TaskRunners. |
+TEST_P(TaskSchedulerThreadPoolTest, PostTasks) { |
+ // Create threads to post tasks. |
std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
for (size_t j = 0; j < kNumThreadsPostingTasks; ++j) { |
threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
@@ -196,10 +211,10 @@ TEST_F(TaskSchedulerThreadPoolTest, PostParallelTasks) { |
} |
} |
-TEST_F(TaskSchedulerThreadPoolTest, PostParallelTasksWaitAllThreadsIdle) { |
- // Create threads to post tasks to PARALLEL TaskRunners. To verify that |
- // worker threads can sleep and be woken up when new tasks are posted, wait |
- // for all threads to become idle before posting a new task. |
+TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) { |
+ // Create threads to post tasks. To verify that worker threads can sleep and |
+ // be woken up when new tasks are posted, wait for all threads to become idle |
+ // before posting a new task. |
std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
for (size_t j = 0; j < kNumThreadsPostingTasks; ++j) { |
threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
@@ -216,9 +231,9 @@ TEST_F(TaskSchedulerThreadPoolTest, PostParallelTasksWaitAllThreadsIdle) { |
} |
} |
-TEST_F(TaskSchedulerThreadPoolTest, NestedPostParallelTasks) { |
- // Create threads to post tasks to PARALLEL TaskRunners. Each task posted by |
- // these threads will post another task when it runs. |
+TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) { |
+ // Create threads to post tasks. Each task posted by these threads will post |
+ // another task when it runs. |
std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
for (size_t j = 0; j < kNumThreadsPostingTasks; ++j) { |
threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
@@ -239,51 +254,66 @@ TEST_F(TaskSchedulerThreadPoolTest, NestedPostParallelTasks) { |
thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
} |
-TEST_F(TaskSchedulerThreadPoolTest, PostParallelTasksWithOneAvailableThread) { |
- TaskFactory factory; |
- |
+TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) { |
// Post tasks to keep all threads busy except one until |event| is signaled. |
+ // Use different factories so that tasks are added to different sequences when |
+ // the execution mode is SEQUENCED. |
WaitableEvent event(true, false); |
- auto task_runner = thread_pool_->CreateTaskRunnerWithTraits( |
- TaskTraits(), ExecutionMode::PARALLEL); |
- for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) |
- factory.PostTask(task_runner, false, &event); |
- factory.WaitForAllTasksToRun(); |
+ std::vector<scoped_ptr<TaskFactory>> factories; |
+ for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { |
+ factories.push_back(make_scoped_ptr(new TaskFactory( |
+ thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
+ GetParam()))); |
+ factories.back()->PostTask(false, &event); |
+ factories.back()->WaitForAllTasksToRun(); |
+ } |
// Post |kNumTasksPostedPerThread| tasks that should all run despite the fact |
// that only one thread in |thread_pool_| isn't busy. |
+ factories.push_back(make_scoped_ptr(new TaskFactory( |
robliao
2016/04/04 18:45:50
Should this really be a part of |factories| or doe
fdoray
2016/04/04 19:41:18
Done.
|
+ thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
+ GetParam()))); |
for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) |
- factory.PostTask(task_runner, false, nullptr); |
- factory.WaitForAllTasksToRun(); |
+ factories.back()->PostTask(false, nullptr); |
+ factories.back()->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 |factory| is destroyed. |
+ // its TaskFactory after |factories| is destroyed. |
thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
} |
-TEST_F(TaskSchedulerThreadPoolTest, Saturate) { |
- TaskFactory factory; |
- |
+TEST_P(TaskSchedulerThreadPoolTest, Saturate) { |
// Verify that it is possible to have |kNumThreadsInThreadPool| tasks running |
- // simultaneously. |
+ // simultaneously. Use different factories so that tasks are added to |
+ // different sequences when the execution mode is SEQUENCED. |
WaitableEvent event(true, false); |
- auto task_runner = thread_pool_->CreateTaskRunnerWithTraits( |
- TaskTraits(), ExecutionMode::PARALLEL); |
- for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) |
- factory.PostTask(task_runner, false, &event); |
- factory.WaitForAllTasksToRun(); |
+ std::vector<scoped_ptr<TaskFactory>> factories; |
+ for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { |
+ factories.push_back(make_scoped_ptr(new TaskFactory( |
+ thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
+ GetParam()))); |
+ factories.back()->PostTask(false, &event); |
+ factories.back()->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 |factory| is destroyed. |
+ // its TaskFactory after |factories| is destroyed. |
thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
} |
+INSTANTIATE_TEST_CASE_P(Parallel, |
+ TaskSchedulerThreadPoolTest, |
+ ::testing::Values(ExecutionMode::PARALLEL)); |
+INSTANTIATE_TEST_CASE_P(Sequenced, |
+ TaskSchedulerThreadPoolTest, |
+ ::testing::Values(ExecutionMode::SEQUENCED)); |
+ |
// Checks that when PostTaskHelper is called with an empty sequence, the task |
// is added to the sequence and the sequence is added to the priority queue. |
TEST(TaskSchedulerPostTaskHelperTest, PostTaskInEmptySequence) { |