Chromium Code Reviews| Index: base/task_scheduler/task_scheduler_impl_unittest.cc |
| diff --git a/base/task_scheduler/task_scheduler_impl_unittest.cc b/base/task_scheduler/task_scheduler_impl_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4e2601c7d868288cc7735135e8a4fcd7677ca396 |
| --- /dev/null |
| +++ b/base/task_scheduler/task_scheduler_impl_unittest.cc |
| @@ -0,0 +1,110 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/task_scheduler/task_scheduler_impl.h" |
| + |
| +#include <stddef.h> |
| + |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/macros.h" |
| +#include "base/task_scheduler/task_traits.h" |
| +#include "base/task_scheduler/test_task_factory.h" |
| +#include "base/threading/platform_thread.h" |
| +#include "base/threading/thread.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace base { |
| +namespace internal { |
| + |
| +namespace { |
| + |
| +const size_t kNumTasksPerTest = 150; |
| + |
| +struct TaskSchedulerImplTestParams { |
| + TaskSchedulerImplTestParams(const TaskTraits& traits, |
| + ExecutionMode execution_mode) |
| + : traits(traits), execution_mode(execution_mode) {} |
| + |
| + const TaskTraits traits; |
| + const ExecutionMode execution_mode; |
| +}; |
| + |
| +class TaskSchedulerImplTest |
| + : public testing::TestWithParam<TaskSchedulerImplTestParams> { |
| + protected: |
| + TaskSchedulerImplTest() = default; |
| + |
| + void SetUp() override { |
| + scheduler_ = TaskSchedulerImpl::Create(); |
| + EXPECT_TRUE(scheduler_); |
| + } |
| + |
| + void TearDown() override { scheduler_->JoinForTesting(); } |
| + |
| + std::unique_ptr<TaskSchedulerImpl> scheduler_; |
|
gab
2016/04/27 19:15:28
If we get rid of Create() this can just be a plain
fdoray
2016/04/28 18:36:32
Done.
|
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImplTest); |
| +}; |
| + |
| +std::vector<TaskSchedulerImplTestParams> GetTaskSchedulerImplTestParams() { |
|
gab
2016/04/27 19:15:29
// Returns a vector with a TaskSchedulerImplTestPa
fdoray
2016/04/28 18:36:32
Done.
|
| + std::vector<TaskSchedulerImplTestParams> params; |
| + |
| + // TODO(fdoray): Add ExecutionMode::SINGLE_THREADED once it's supported. |
|
gab
2016/04/27 19:15:29
Isn't this based on top of the CL that makes it su
fdoray
2016/04/28 18:36:32
It is now :)
|
| + ExecutionMode execution_modes[] = {ExecutionMode::PARALLEL, |
| + ExecutionMode::SEQUENCED}; |
| + |
| + for (size_t execution_mode_index = 0; |
|
gab
2016/04/27 19:15:28
for (ExecutionMode execution_mode : execution_mode
fdoray
2016/04/28 18:36:32
Done.
|
| + execution_mode_index < arraysize(execution_modes); |
| + ++execution_mode_index) { |
| + for (size_t priority_index = static_cast<size_t>(TaskPriority::LOWEST); |
| + priority_index <= static_cast<size_t>(TaskPriority::HIGHEST); |
| + ++priority_index) { |
| + const TaskPriority priority = static_cast<TaskPriority>(priority_index); |
| + params.push_back( |
| + TaskSchedulerImplTestParams(TaskTraits().WithPriority(priority), |
| + execution_modes[execution_mode_index])); |
| + params.push_back(TaskSchedulerImplTestParams( |
| + TaskTraits().WithPriority(priority).WithFileIO(), |
| + execution_modes[execution_mode_index])); |
| + } |
| + } |
| + |
| + return params; |
| +} |
| + |
| +void ExpectThreadPriority(ThreadPriority priority) { |
| + EXPECT_EQ(priority, PlatformThread::GetCurrentThreadPriority()); |
| +} |
| + |
| +} // namespace |
| + |
| +TEST_P(TaskSchedulerImplTest, PostTasks) { |
| + test::TestTaskFactory factory( |
| + scheduler_->CreateTaskRunnerWithTraits(GetParam().traits, |
| + GetParam().execution_mode), |
| + GetParam().execution_mode); |
| + EXPECT_FALSE(factory.task_runner()->RunsTasksOnCurrentThread()); |
| + |
| + for (size_t i = 0; i < kNumTasksPerTest; ++i) { |
| + factory.PostTask( |
| + test::TestTaskFactory::PostNestedTask::NO, |
| + Bind(&ExpectThreadPriority, |
|
gab
2016/04/27 19:15:29
Also add AssertIOAllowed expectations (to verify W
fdoray
2016/04/28 18:36:32
Done.
|
| + GetParam().traits.priority() == TaskPriority::BACKGROUND |
| + ? ThreadPriority::BACKGROUND |
| + : ThreadPriority::NORMAL)); |
| + } |
| + |
| + factory.WaitForAllTasksToRun(); |
| +} |
| + |
|
gab
2016/04/27 19:15:28
One of the key part of TaskSchedulerImpl (well I g
fdoray
2016/04/28 18:36:32
Done.
|
| +INSTANTIATE_TEST_CASE_P(TaskTraitsExecutionModeCombinations, |
| + TaskSchedulerImplTest, |
| + ::testing::ValuesIn(GetTaskSchedulerImplTestParams())); |
|
gab
2016/04/27 19:15:28
Whatttt!!!?!?! This doesn't have to be statically
fdoray
2016/04/28 18:36:32
The name of a test is TaskTraitsExecutionModeCombi
gab
2016/04/28 19:29:02
Cool :-), I guess if one of them was flaky the fla
|
| + |
| +} // namespace internal |
| +} // namespace base |