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

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