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

Side by Side Diff: base/task_scheduler/task_scheduler_impl_unittest.cc

Issue 1701343003: TaskScheduler [13] TaskSchedulerImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_6_threadpool
Patch Set: fix ios build error Created 4 years, 7 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 unified diff | Download patch
« no previous file with comments | « base/task_scheduler/task_scheduler_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 TaskTraits traits;
37 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 #if ENABLE_THREAD_RESTRICTIONS
58 // Returns whether I/O calls are allowed on the current thread.
59 bool GetIOAllowed() {
60 const bool previous_value = ThreadRestrictions::SetIOAllowed(true);
61 ThreadRestrictions::SetIOAllowed(previous_value);
62 return previous_value;
63 }
64 #endif
65
66 // Verify that the current thread priority and I/O restrictions are appropriate
67 // to run a Task with |traits|.
68 // Note: ExecutionMode is verified inside TestTaskFactory.
69 void VerifyTaskEnvironement(const TaskTraits& traits) {
70 EXPECT_EQ(traits.priority() == TaskPriority::BACKGROUND
71 ? ThreadPriority::BACKGROUND
72 : ThreadPriority::NORMAL,
73 PlatformThread::GetCurrentThreadPriority());
74
75 #if ENABLE_THREAD_RESTRICTIONS
76 // The #if above is required because GetIOAllowed() always returns true when
77 // !ENABLE_THREAD_RESTRICTIONS, even when |traits| don't allow file I/O.
78 EXPECT_EQ(traits.with_file_io(), GetIOAllowed());
79 #endif
80 }
81
82 void VerifyTaskEnvironementAndSignalEvent(const TaskTraits& traits,
83 WaitableEvent* event) {
84 DCHECK(event);
85 VerifyTaskEnvironement(traits);
86 event->Signal();
87 }
88
89 class ThreadPostingTasks : public SimpleThread {
90 public:
91 // Creates a thread that posts Tasks to |scheduler| with |traits| and
92 // |execution_mode|.
93 ThreadPostingTasks(TaskSchedulerImpl* scheduler,
94 const TaskTraits& traits,
95 ExecutionMode execution_mode)
96 : SimpleThread("ThreadPostingTasks"),
97 traits_(traits),
98 factory_(scheduler->CreateTaskRunnerWithTraits(traits, execution_mode),
99 execution_mode) {}
100
101 void WaitForAllTasksToRun() { factory_.WaitForAllTasksToRun(); }
102
103 private:
104 void Run() override {
105 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread());
106
107 const size_t kNumTasksPerThread = 150;
108 for (size_t i = 0; i < kNumTasksPerThread; ++i) {
109 factory_.PostTask(test::TestTaskFactory::PostNestedTask::NO,
110 Bind(&VerifyTaskEnvironement, traits_));
111 }
112 }
113
114 const TaskTraits traits_;
115 test::TestTaskFactory factory_;
116
117 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks);
118 };
119
120 // Returns a vector with a TraitsExecutionModePair for each valid
121 // combination of {ExecutionMode, TaskPriority, WithFileIO()}.
122 std::vector<TraitsExecutionModePair> GetTraitsExecutionModePairs() {
123 std::vector<TraitsExecutionModePair> params;
124
125 const ExecutionMode execution_modes[] = {ExecutionMode::PARALLEL,
126 ExecutionMode::SEQUENCED,
127 ExecutionMode::SINGLE_THREADED};
128
129 for (ExecutionMode execution_mode : execution_modes) {
130 for (size_t priority_index = static_cast<size_t>(TaskPriority::LOWEST);
131 priority_index <= static_cast<size_t>(TaskPriority::HIGHEST);
132 ++priority_index) {
133 const TaskPriority priority = static_cast<TaskPriority>(priority_index);
134 params.push_back(TraitsExecutionModePair(
135 TaskTraits().WithPriority(priority), execution_mode));
136 params.push_back(TraitsExecutionModePair(
137 TaskTraits().WithPriority(priority).WithFileIO(), execution_mode));
138 }
139 }
140
141 return params;
142 }
143
144 } // namespace
145
146 // Verifies that a Task posted via PostTaskWithTraits with parameterized
147 // TaskTraits runs on a thread with the expected priority and I/O restrictions.
148 // The ExecutionMode parameter is ignored by this test.
149 TEST_P(TaskSchedulerImplTest, PostTaskWithTraits) {
150 WaitableEvent task_ran(true, false);
151 scheduler_->PostTaskWithTraits(
152 FROM_HERE, GetParam().traits,
153 Bind(&VerifyTaskEnvironementAndSignalEvent, GetParam().traits,
154 Unretained(&task_ran)));
155 task_ran.Wait();
156 }
157
158 // Verifies that Tasks posted via a TaskRunner with parameterized TaskTraits and
159 // ExecutionMode run on a thread with the expected priority and I/O restrictions
160 // and respect the characteristics of their ExecutionMode.
161 TEST_P(TaskSchedulerImplTest, PostTasksViaTaskRunner) {
162 test::TestTaskFactory factory(
163 scheduler_->CreateTaskRunnerWithTraits(GetParam().traits,
164 GetParam().execution_mode),
165 GetParam().execution_mode);
166 EXPECT_FALSE(factory.task_runner()->RunsTasksOnCurrentThread());
167
168 const size_t kNumTasksPerTest = 150;
169 for (size_t i = 0; i < kNumTasksPerTest; ++i) {
170 factory.PostTask(test::TestTaskFactory::PostNestedTask::NO,
171 Bind(&VerifyTaskEnvironement, GetParam().traits));
172 }
173
174 factory.WaitForAllTasksToRun();
175 }
176
177 INSTANTIATE_TEST_CASE_P(OneTraitsExecutionModePair,
178 TaskSchedulerImplTest,
179 ::testing::ValuesIn(GetTraitsExecutionModePairs()));
180
181 // Spawns threads that simultaneously post Tasks to TaskRunners with various
182 // TaskTraits and ExecutionModes. Verifies that each Task runs on a thread with
183 // the expected priority and I/O restrictions and respects the characteristics
184 // of its ExecutionMode.
185 TEST(TaskSchedulerImplTest, MultipleTraitsExecutionModePairs) {
186 std::unique_ptr<TaskSchedulerImpl> scheduler = TaskSchedulerImpl::Create();
187
188 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
189 for (const auto& traits_execution_mode_pair : GetTraitsExecutionModePairs()) {
190 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks(
191 scheduler.get(), traits_execution_mode_pair.traits,
192 traits_execution_mode_pair.execution_mode)));
193 threads_posting_tasks.back()->Start();
194 }
195
196 for (const auto& thread : threads_posting_tasks) {
197 thread->WaitForAllTasksToRun();
198 thread->Join();
199 }
200
201 scheduler->JoinForTesting();
202 }
203
204 // TODO(fdoray): Add tests with Sequences that move around thread pools once
205 // child TaskRunners are supported.
206
207 } // namespace internal
208 } // namespace base
OLDNEW
« no previous file with comments | « base/task_scheduler/task_scheduler_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698