Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "base/task_scheduler/task_scheduler_impl.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <utility> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/bind_helpers.h" | |
| 14 #include "base/callback.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/ptr_util.h" | |
| 17 #include "base/synchronization/waitable_event.h" | |
| 18 #include "base/task_scheduler/task_traits.h" | |
| 19 #include "base/task_scheduler/test_task_factory.h" | |
| 20 #include "base/threading/platform_thread.h" | |
| 21 #include "base/threading/simple_thread.h" | |
| 22 #include "base/threading/thread.h" | |
| 23 #include "base/threading/thread_restrictions.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 | |
| 26 namespace base { | |
| 27 namespace internal { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 struct TraitsExecutionModePair { | |
| 32 TraitsExecutionModePair(const TaskTraits& traits, | |
| 33 ExecutionMode execution_mode) | |
| 34 : traits(traits), execution_mode(execution_mode) {} | |
| 35 | |
| 36 const TaskTraits traits; | |
| 37 const ExecutionMode execution_mode; | |
| 38 }; | |
| 39 | |
| 40 class TaskSchedulerImplTest | |
| 41 : public testing::TestWithParam<TraitsExecutionModePair> { | |
| 42 protected: | |
| 43 TaskSchedulerImplTest() = default; | |
| 44 | |
| 45 void SetUp() override { | |
| 46 scheduler_ = TaskSchedulerImpl::Create(); | |
| 47 EXPECT_TRUE(scheduler_); | |
| 48 } | |
| 49 void TearDown() override { scheduler_->JoinForTesting(); } | |
| 50 | |
| 51 std::unique_ptr<TaskSchedulerImpl> scheduler_; | |
| 52 | |
| 53 private: | |
| 54 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImplTest); | |
| 55 }; | |
| 56 | |
| 57 // Returns whether I/O calls are allowed on the current thread. | |
| 58 bool GetIOAllowed() { | |
| 59 const bool previous_value = ThreadRestrictions::SetIOAllowed(true); | |
| 60 ThreadRestrictions::SetIOAllowed(previous_value); | |
| 61 return previous_value; | |
| 62 } | |
| 63 | |
| 64 // Verify that the current thread priority and I/O restrictions are appropriate | |
| 65 // to run a Task with |traits|. | |
| 66 // Note: ExecutionMode is verified inside TestTaskFactory. | |
| 67 void VerifyTaskEnvironement(const TaskTraits& traits) { | |
| 68 EXPECT_EQ(traits.priority() == TaskPriority::BACKGROUND | |
| 69 ? ThreadPriority::BACKGROUND | |
| 70 : ThreadPriority::NORMAL, | |
| 71 PlatformThread::GetCurrentThreadPriority()); | |
| 72 | |
| 73 #if ENABLE_THREAD_RESTRICTIONS | |
| 74 // The #if above is required because GetIOAllowed() always returns true when | |
| 75 // !ENABLE_THREAD_RESTRICTIONS, even when |traits| don't allow file I/O. | |
| 76 EXPECT_EQ(traits.with_file_io(), GetIOAllowed()); | |
| 77 #endif | |
| 78 } | |
| 79 | |
| 80 void VerifyTaskEnvironementAndSignalEvent(const TaskTraits& traits, | |
| 81 WaitableEvent* event) { | |
| 82 DCHECK(event); | |
| 83 VerifyTaskEnvironement(traits); | |
| 84 event->Signal(); | |
| 85 } | |
| 86 | |
| 87 class ThreadPostingTasks : public SimpleThread { | |
| 88 public: | |
| 89 // Creates a thread that posts Tasks to |scheduler| with |traits| and | |
| 90 // |execution_mode|. | |
| 91 ThreadPostingTasks(TaskSchedulerImpl* scheduler, | |
| 92 const TaskTraits& traits, | |
| 93 ExecutionMode execution_mode) | |
| 94 : SimpleThread("ThreadPostingTasks"), | |
| 95 traits_(traits), | |
| 96 factory_(scheduler->CreateTaskRunnerWithTraits(traits, execution_mode), | |
| 97 execution_mode) {} | |
| 98 | |
| 99 void WaitForAllTasksToRun() { factory_.WaitForAllTasksToRun(); } | |
| 100 | |
| 101 private: | |
| 102 void Run() override { | |
| 103 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); | |
| 104 | |
| 105 const size_t kNumTasksPerThread = 150; | |
| 106 for (size_t i = 0; i < kNumTasksPerThread; ++i) { | |
| 107 factory_.PostTask(test::TestTaskFactory::PostNestedTask::NO, | |
| 108 Bind(&VerifyTaskEnvironement, traits_)); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 const TaskTraits traits_; | |
| 113 test::TestTaskFactory factory_; | |
| 114 | |
| 115 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); | |
| 116 }; | |
| 117 | |
| 118 // Returns a vector with a TraitsExecutionModePair for each valid | |
| 119 // combination of {ExecutionMode, TaskPriority, WithFileIO()}. | |
| 120 std::vector<TraitsExecutionModePair> GetTraitsExecutionModePairs() { | |
| 121 std::vector<TraitsExecutionModePair> params; | |
| 122 | |
| 123 const ExecutionMode execution_modes[] = {ExecutionMode::PARALLEL, | |
| 124 ExecutionMode::SEQUENCED, | |
| 125 ExecutionMode::SINGLE_THREADED}; | |
| 126 | |
| 127 for (ExecutionMode execution_mode : execution_modes) { | |
| 128 for (size_t priority_index = static_cast<size_t>(TaskPriority::LOWEST); | |
| 129 priority_index <= static_cast<size_t>(TaskPriority::HIGHEST); | |
| 130 ++priority_index) { | |
| 131 const TaskPriority priority = static_cast<TaskPriority>(priority_index); | |
| 132 params.push_back(TraitsExecutionModePair( | |
| 133 TaskTraits().WithPriority(priority), execution_mode)); | |
| 134 params.push_back(TraitsExecutionModePair( | |
| 135 TaskTraits().WithPriority(priority).WithFileIO(), execution_mode)); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 return params; | |
| 140 } | |
| 141 | |
| 142 } // namespace | |
| 143 | |
| 144 // Verifies that a Task posted via PostTaskWithTraits with parameterized | |
| 145 // TaskTraits runs on a thread with the expected priority and I/O restrictions. | |
| 146 // The ExecutionMode parameter is ignored by this test. | |
| 147 TEST_P(TaskSchedulerImplTest, PostTaskWithTraits) { | |
| 148 WaitableEvent task_ran(true, false); | |
| 149 scheduler_->PostTaskWithTraits( | |
| 150 FROM_HERE, GetParam().traits, | |
| 151 Bind(&VerifyTaskEnvironementAndSignalEvent, GetParam().traits, | |
| 152 Unretained(&task_ran))); | |
| 153 task_ran.Wait(); | |
| 154 } | |
| 155 | |
| 156 // Verifies that Tasks posted via a TaskRunner with parameterized TaskTraits and | |
| 157 // ExecutionMode run on a thread with the expected priority and I/O restrictions | |
| 158 // and respect the characteristics of their ExecutionMode. | |
| 159 TEST_P(TaskSchedulerImplTest, PostTasksViaTaskRunner) { | |
| 160 test::TestTaskFactory factory( | |
| 161 scheduler_->CreateTaskRunnerWithTraits(GetParam().traits, | |
| 162 GetParam().execution_mode), | |
| 163 GetParam().execution_mode); | |
| 164 EXPECT_FALSE(factory.task_runner()->RunsTasksOnCurrentThread()); | |
| 165 | |
| 166 const size_t kNumTasksPerTest = 150; | |
| 167 for (size_t i = 0; i < kNumTasksPerTest; ++i) { | |
| 168 factory.PostTask(test::TestTaskFactory::PostNestedTask::NO, | |
| 169 Bind(&VerifyTaskEnvironement, GetParam().traits)); | |
| 170 } | |
| 171 | |
| 172 factory.WaitForAllTasksToRun(); | |
| 173 } | |
| 174 | |
| 175 INSTANTIATE_TEST_CASE_P(OneTraitsExecutionModePair, | |
| 176 TaskSchedulerImplTest, | |
| 177 ::testing::ValuesIn(GetTraitsExecutionModePairs())); | |
| 178 | |
| 179 // Spawns threads that simultaneously post Tasks to TaskRunners with various | |
| 180 // TaskTraits and ExecutionModes. Verifies that each Task runs on a thread with | |
| 181 // the expected priority and I/O restrictions and respects the characteristics | |
| 182 // of its ExecutionMode. | |
| 183 TEST(TaskSchedulerImplTest, MultipleTraitsExecutionModePairs) { | |
| 184 auto scheduler = TaskSchedulerImpl::Create(); | |
|
robliao
2016/04/29 19:56:03
I don't know how I missed this on the first pass.
fdoray
2016/04/29 20:26:20
It's not a leak because TaskSchedulerImpl::Create
robliao
2016/04/29 20:45:07
Ah, right you are! std::unique_ptr would indeed be
| |
| 185 | |
| 186 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | |
| 187 for (const auto& traits_execution_mode_pair : GetTraitsExecutionModePairs()) { | |
| 188 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( | |
| 189 scheduler.get(), traits_execution_mode_pair.traits, | |
| 190 traits_execution_mode_pair.execution_mode))); | |
| 191 threads_posting_tasks.back()->Start(); | |
| 192 } | |
| 193 | |
| 194 for (const auto& thread : threads_posting_tasks) { | |
| 195 thread->WaitForAllTasksToRun(); | |
| 196 thread->Join(); | |
| 197 } | |
| 198 | |
| 199 scheduler->JoinForTesting(); | |
| 200 } | |
| 201 | |
| 202 // TODO(fdoray): Add tests with Sequences that move around thread pools once | |
| 203 // child TaskRunners are supported. | |
| 204 | |
| 205 } // namespace internal | |
| 206 } // namespace base | |
| OLD | NEW |