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

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

Powered by Google App Engine
This is Rietveld 408576698