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

Side by Side Diff: cc/test/task_graph_runner_test_template.h

Issue 1449133002: TaskGraphRunner refactor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: feedback Created 5 years 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 | « cc/test/layer_tree_test.cc ('k') | cc/test/task_graph_runner_test_template.cc » ('j') | 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 2015 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 #ifndef CC_TEST_TASK_GRAPH_RUNNER_TEST_TEMPLATE_H_
6 #define CC_TEST_TASK_GRAPH_RUNNER_TEST_TEMPLATE_H_
7
8 #include "cc/raster/task_graph_runner.h"
9
10 #include <vector>
11
12 #include "base/bind.h"
13 #include "base/macros.h"
14 #include "base/synchronization/lock.h"
15 #include "base/threading/simple_thread.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace cc {
19
20 class TaskGraphRunnerTestBase {
21 public:
22 struct TaskInfo {
23 TaskInfo(int namespace_index,
24 unsigned id,
25 unsigned dependent_id,
26 unsigned dependent_count,
27 unsigned priority)
28 : namespace_index(namespace_index),
29 id(id),
30 dependent_id(dependent_id),
31 dependent_count(dependent_count),
32 priority(priority) {}
33
34 int namespace_index;
35 unsigned id;
36 unsigned dependent_id;
37 unsigned dependent_count;
38 unsigned priority;
39 };
40
41 TaskGraphRunnerTestBase() {}
42
43 void SetTaskGraphRunner(TaskGraphRunner* task_graph_runner);
44 void ResetIds(int namespace_index);
45 void RunAllTasks(int namespace_index);
46 void RunTaskOnWorkerThread(int namespace_index, unsigned id);
47 void OnTaskCompleted(int namespace_index, unsigned id);
48 const std::vector<unsigned>& run_task_ids(int namespace_index);
49 const std::vector<unsigned>& on_task_completed_ids(int namespace_index);
50 void ScheduleTasks(int namespace_index, const std::vector<TaskInfo>& tasks);
51
52 static const int kNamespaceCount = 3;
53
54 protected:
55 class FakeTaskImpl : public Task {
56 public:
57 FakeTaskImpl(TaskGraphRunnerTestBase* test, int namespace_index, int id)
58 : test_(test), namespace_index_(namespace_index), id_(id) {}
59
60 // Overridden from Task:
61 void RunOnWorkerThread() override;
62
63 virtual void CompleteOnOriginThread();
64
65 protected:
66 ~FakeTaskImpl() override {}
67
68 private:
69 TaskGraphRunnerTestBase* test_;
70 int namespace_index_;
71 int id_;
72
73 DISALLOW_COPY_AND_ASSIGN(FakeTaskImpl);
74 };
75
76 class FakeDependentTaskImpl : public FakeTaskImpl {
77 public:
78 FakeDependentTaskImpl(TaskGraphRunnerTestBase* test,
79 int namespace_index,
80 int id)
81 : FakeTaskImpl(test, namespace_index, id) {}
82
83 // Overridden from FakeTaskImpl:
84 void CompleteOnOriginThread() override {}
85
86 private:
87 ~FakeDependentTaskImpl() override {}
88
89 DISALLOW_COPY_AND_ASSIGN(FakeDependentTaskImpl);
90 };
91
92 TaskGraphRunner* task_graph_runner_;
93 NamespaceToken namespace_token_[kNamespaceCount];
94 Task::Vector tasks_[kNamespaceCount];
95 Task::Vector dependents_[kNamespaceCount];
96 std::vector<unsigned> run_task_ids_[kNamespaceCount];
97 base::Lock run_task_ids_lock_;
98 std::vector<unsigned> on_task_completed_ids_[kNamespaceCount];
99 };
100
101 template <typename TaskRunnerTestDelegate>
102 class TaskGraphRunnerTest : public TaskGraphRunnerTestBase,
103 public testing::Test {
104 public:
105 // Overridden from testing::Test:
106 void SetUp() override {
107 delegate_.StartTaskGraphRunner();
108 SetTaskGraphRunner(delegate_.GetTaskGraphRunner());
109
110 for (int i = 0; i < kNamespaceCount; ++i)
111 namespace_token_[i] = task_graph_runner_->GetNamespaceToken();
112 }
113 void TearDown() override { delegate_.StopTaskGraphRunner(); }
114
115 private:
116 TaskRunnerTestDelegate delegate_;
117 };
118
119 TYPED_TEST_CASE_P(TaskGraphRunnerTest);
120
121 TYPED_TEST_P(TaskGraphRunnerTest, Basic) {
122 const int kNamespaceCount = TaskGraphRunnerTestBase::kNamespaceCount;
123 using TaskInfo = TaskGraphRunnerTestBase::TaskInfo;
124
125 for (int i = 0; i < kNamespaceCount; ++i) {
126 EXPECT_EQ(0u, this->run_task_ids(i).size());
127 EXPECT_EQ(0u, this->on_task_completed_ids(i).size());
128
129 this->ScheduleTasks(i,
130 std::vector<TaskInfo>(1, TaskInfo(i, 0u, 0u, 0u, 0u)));
131 }
132
133 for (int i = 0; i < kNamespaceCount; ++i) {
134 this->RunAllTasks(i);
135
136 EXPECT_EQ(1u, this->run_task_ids(i).size());
137 EXPECT_EQ(1u, this->on_task_completed_ids(i).size());
138 }
139
140 for (int i = 0; i < kNamespaceCount; ++i)
141 this->ScheduleTasks(i,
142 std::vector<TaskInfo>(1, TaskInfo(i, 0u, 0u, 1u, 0u)));
143
144 for (int i = 0; i < kNamespaceCount; ++i) {
145 this->RunAllTasks(i);
146
147 EXPECT_EQ(3u, this->run_task_ids(i).size());
148 EXPECT_EQ(2u, this->on_task_completed_ids(i).size());
149 }
150
151 for (int i = 0; i < kNamespaceCount; ++i)
152 this->ScheduleTasks(i,
153 std::vector<TaskInfo>(1, TaskInfo(i, 0u, 0u, 2u, 0u)));
154
155 for (int i = 0; i < kNamespaceCount; ++i) {
156 this->RunAllTasks(i);
157
158 EXPECT_EQ(6u, this->run_task_ids(i).size());
159 EXPECT_EQ(3u, this->on_task_completed_ids(i).size());
160 }
161 }
162
163 TYPED_TEST_P(TaskGraphRunnerTest, Dependencies) {
164 const int kNamespaceCount = TaskGraphRunnerTestBase::kNamespaceCount;
165 using TaskInfo = TaskGraphRunnerTestBase::TaskInfo;
166
167 for (int i = 0; i < kNamespaceCount; ++i) {
168 this->ScheduleTasks(i, std::vector<TaskInfo>(1, TaskInfo(i, 0u, 1u,
169 1u, // 1 dependent
170 0u)));
171 }
172
173 for (int i = 0; i < kNamespaceCount; ++i) {
174 this->RunAllTasks(i);
175
176 // Check if task ran before dependent.
177 ASSERT_EQ(2u, this->run_task_ids(i).size());
178 EXPECT_EQ(0u, this->run_task_ids(i)[0]);
179 EXPECT_EQ(1u, this->run_task_ids(i)[1]);
180 ASSERT_EQ(1u, this->on_task_completed_ids(i).size());
181 EXPECT_EQ(0u, this->on_task_completed_ids(i)[0]);
182 }
183
184 for (int i = 0; i < kNamespaceCount; ++i) {
185 this->ScheduleTasks(i,
186 std::vector<TaskInfo>(1, TaskInfo(i, 2u, 3u,
187 2u, // 2 dependents
188 0u)));
189 }
190
191 for (int i = 0; i < kNamespaceCount; ++i) {
192 this->RunAllTasks(i);
193
194 // Task should only run once.
195 ASSERT_EQ(5u, this->run_task_ids(i).size());
196 EXPECT_EQ(2u, this->run_task_ids(i)[2]);
197 EXPECT_EQ(3u, this->run_task_ids(i)[3]);
198 EXPECT_EQ(3u, this->run_task_ids(i)[4]);
199 ASSERT_EQ(2u, this->on_task_completed_ids(i).size());
200 EXPECT_EQ(2u, this->on_task_completed_ids(i)[1]);
201 }
202 }
203
204 REGISTER_TYPED_TEST_CASE_P(TaskGraphRunnerTest, Basic, Dependencies);
205
206 template <typename TaskRunnerTestDelegate>
207 using SingleThreadTaskGraphRunnerTest =
208 TaskGraphRunnerTest<TaskRunnerTestDelegate>;
209
210 TYPED_TEST_CASE_P(SingleThreadTaskGraphRunnerTest);
211
212 TYPED_TEST_P(SingleThreadTaskGraphRunnerTest, Priority) {
213 const int kNamespaceCount = TaskGraphRunnerTestBase::kNamespaceCount;
214 using TaskInfo = TaskGraphRunnerTestBase::TaskInfo;
215
216 for (int i = 0; i < kNamespaceCount; ++i) {
217 TaskInfo tasks[] = {
218 TaskInfo(i, 0u, 2u, 1u, 1u), // Priority 1
219 TaskInfo(i, 1u, 3u, 1u, 0u) // Priority 0
220 };
221 this->ScheduleTasks(i,
222 std::vector<TaskInfo>(tasks, tasks + arraysize(tasks)));
223 }
224
225 for (int i = 0; i < kNamespaceCount; ++i) {
226 this->RunAllTasks(i);
227
228 // Check if tasks ran in order of priority.
229 ASSERT_EQ(4u, this->run_task_ids(i).size());
230 EXPECT_EQ(1u, this->run_task_ids(i)[0]);
231 EXPECT_EQ(3u, this->run_task_ids(i)[1]);
232 EXPECT_EQ(0u, this->run_task_ids(i)[2]);
233 EXPECT_EQ(2u, this->run_task_ids(i)[3]);
234 ASSERT_EQ(2u, this->on_task_completed_ids(i).size());
235 EXPECT_EQ(1u, this->on_task_completed_ids(i)[0]);
236 EXPECT_EQ(0u, this->on_task_completed_ids(i)[1]);
237 }
238 }
239
240 REGISTER_TYPED_TEST_CASE_P(SingleThreadTaskGraphRunnerTest, Priority);
241
242 } // namespace cc
243
244 #endif // CC_TEST_TASK_GRAPH_RUNNER_TEST_TEMPLATE_H_
OLDNEW
« no previous file with comments | « cc/test/layer_tree_test.cc ('k') | cc/test/task_graph_runner_test_template.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698