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/callback.h" |
| 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/task_scheduler/task_traits.h" |
| 17 #include "base/task_scheduler/test_task_factory.h" |
| 18 #include "base/threading/platform_thread.h" |
| 19 #include "base/threading/simple_thread.h" |
| 20 #include "base/threading/thread.h" |
| 21 #include "base/threading/thread_restrictions.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 |
| 24 namespace base { |
| 25 namespace internal { |
| 26 |
| 27 namespace { |
| 28 |
| 29 struct TraitsExecutionModePair { |
| 30 TraitsExecutionModePair(const TaskTraits& traits, |
| 31 ExecutionMode execution_mode) |
| 32 : traits(traits), execution_mode(execution_mode) {} |
| 33 |
| 34 const TaskTraits traits; |
| 35 const ExecutionMode execution_mode; |
| 36 }; |
| 37 |
| 38 class TaskSchedulerImplTest |
| 39 : public testing::TestWithParam<TraitsExecutionModePair> { |
| 40 protected: |
| 41 TaskSchedulerImplTest() = default; |
| 42 |
| 43 void TearDown() override { scheduler_.JoinForTesting(); } |
| 44 |
| 45 TaskSchedulerImpl scheduler_; |
| 46 |
| 47 private: |
| 48 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImplTest); |
| 49 }; |
| 50 |
| 51 bool GetIOAllowed() { |
| 52 // SetIOAllowed() is the only function that returns the current value of the |
| 53 // I/O allowed bit. We don't use |
| 54 // EXPECT_DCHECK_DEATH({ ThreadRestrictions::AssertIOAllowed(); }, ""); |
| 55 // to verify that I/O is disallowed because EXPECT_DCHECK_DEATH is slow on |
| 56 // Windows. |
| 57 const bool previous_value = ThreadRestrictions::SetIOAllowed(true); |
| 58 ThreadRestrictions::SetIOAllowed(previous_value); |
| 59 return previous_value; |
| 60 } |
| 61 |
| 62 void VerifyTaskEnvironement(const TaskTraits& traits) { |
| 63 EXPECT_EQ((traits.priority() == TaskPriority::BACKGROUND) |
| 64 ? ThreadPriority::BACKGROUND |
| 65 : ThreadPriority::NORMAL, |
| 66 PlatformThread::GetCurrentThreadPriority()); |
| 67 |
| 68 #if ENABLE_THREAD_RESTRICTIONS |
| 69 EXPECT_EQ(traits.with_file_io(), GetIOAllowed()); |
| 70 #endif |
| 71 } |
| 72 |
| 73 class ThreadPostingTasks : public SimpleThread { |
| 74 public: |
| 75 // Creates a thread that posts Tasks to |scheduler| with |traits| and |
| 76 // |execution_mode|. |
| 77 ThreadPostingTasks(TaskSchedulerImpl* scheduler, |
| 78 const TaskTraits& traits, |
| 79 ExecutionMode execution_mode) |
| 80 : SimpleThread("ThreadPostingTasks"), |
| 81 traits_(traits), |
| 82 factory_(scheduler->CreateTaskRunnerWithTraits(traits, execution_mode), |
| 83 execution_mode) {} |
| 84 |
| 85 void WaitForAllTasksToRun() { factory_.WaitForAllTasksToRun(); } |
| 86 |
| 87 private: |
| 88 void Run() override { |
| 89 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); |
| 90 |
| 91 const size_t kNumTasksPerThread = 150; |
| 92 for (size_t i = 0; i < kNumTasksPerThread; ++i) { |
| 93 factory_.PostTask(test::TestTaskFactory::PostNestedTask::NO, |
| 94 Bind(&VerifyTaskEnvironement, traits_)); |
| 95 } |
| 96 } |
| 97 |
| 98 const TaskTraits traits_; |
| 99 test::TestTaskFactory factory_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); |
| 102 }; |
| 103 |
| 104 // Returns a vector with a TraitsExecutionModePair for each valid |
| 105 // combination of {ExecutionMode, TaskPriority, WithFileIO()}. |
| 106 std::vector<TraitsExecutionModePair> GetTraitsExecutionModePairs() { |
| 107 std::vector<TraitsExecutionModePair> params; |
| 108 |
| 109 const ExecutionMode execution_modes[] = {ExecutionMode::PARALLEL, |
| 110 ExecutionMode::SEQUENCED, |
| 111 ExecutionMode::SINGLE_THREADED}; |
| 112 |
| 113 for (ExecutionMode execution_mode : execution_modes) { |
| 114 for (size_t priority_index = static_cast<size_t>(TaskPriority::LOWEST); |
| 115 priority_index <= static_cast<size_t>(TaskPriority::HIGHEST); |
| 116 ++priority_index) { |
| 117 const TaskPriority priority = static_cast<TaskPriority>(priority_index); |
| 118 params.push_back(TraitsExecutionModePair( |
| 119 TaskTraits().WithPriority(priority), execution_mode)); |
| 120 params.push_back(TraitsExecutionModePair( |
| 121 TaskTraits().WithPriority(priority).WithFileIO(), execution_mode)); |
| 122 } |
| 123 } |
| 124 |
| 125 return params; |
| 126 } |
| 127 |
| 128 } // namespace |
| 129 |
| 130 // Verifies that Tasks posted with parameterized TaskTraits and ExecutionMode |
| 131 // run on a thread with the expected priority and I/O restrictions and respect |
| 132 // the characteristics of their ExecutionMode. |
| 133 TEST_P(TaskSchedulerImplTest, PostTasks) { |
| 134 test::TestTaskFactory factory( |
| 135 scheduler_.CreateTaskRunnerWithTraits(GetParam().traits, |
| 136 GetParam().execution_mode), |
| 137 GetParam().execution_mode); |
| 138 EXPECT_FALSE(factory.task_runner()->RunsTasksOnCurrentThread()); |
| 139 |
| 140 const size_t kNumTasksPerTest = 150; |
| 141 for (size_t i = 0; i < kNumTasksPerTest; ++i) { |
| 142 factory.PostTask(test::TestTaskFactory::PostNestedTask::NO, |
| 143 Bind(&VerifyTaskEnvironement, GetParam().traits)); |
| 144 } |
| 145 |
| 146 factory.WaitForAllTasksToRun(); |
| 147 } |
| 148 |
| 149 INSTANTIATE_TEST_CASE_P(OneTraitsExecutionModePair, |
| 150 TaskSchedulerImplTest, |
| 151 ::testing::ValuesIn(GetTraitsExecutionModePairs())); |
| 152 |
| 153 // Spawns threads that simultaneously post Tasks with various TaskTraits and |
| 154 // ExecutionMode. Verifies that each Task runs on a thread with the expected |
| 155 // priority and I/O restrictions and respects the characteristics of its |
| 156 // ExecutionMode. |
| 157 TEST(TaskSchedulerImplTest, MultipleTraitsExecutionModePairs) { |
| 158 TaskSchedulerImpl scheduler; |
| 159 |
| 160 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 161 for (const auto& traits_execution_mode_pair : GetTraitsExecutionModePairs()) { |
| 162 threads_posting_tasks.push_back(WrapUnique( |
| 163 new ThreadPostingTasks(&scheduler, traits_execution_mode_pair.traits, |
| 164 traits_execution_mode_pair.execution_mode))); |
| 165 threads_posting_tasks.back()->Start(); |
| 166 } |
| 167 |
| 168 for (const auto& thread : threads_posting_tasks) { |
| 169 thread->WaitForAllTasksToRun(); |
| 170 thread->Join(); |
| 171 } |
| 172 |
| 173 scheduler.JoinForTesting(); |
| 174 } |
| 175 |
| 176 } // namespace internal |
| 177 } // namespace base |
OLD | NEW |