OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "platform/scheduler/Scheduler.h" | 6 #include "platform/scheduler/Scheduler.h" |
7 | 7 |
8 #include "platform/TestingPlatformSupport.h" | 8 #include "platform/TestingPlatformSupport.h" |
9 #include "platform/TraceLocation.h" | 9 #include "platform/TraceLocation.h" |
10 #include "public/platform/Platform.h" | 10 #include "public/platform/Platform.h" |
11 #include "public/platform/WebThread.h" | 11 #include "public/platform/WebThread.h" |
12 | 12 |
13 #include <gmock/gmock.h> | |
13 #include <gtest/gtest.h> | 14 #include <gtest/gtest.h> |
15 #include <string> | |
16 #include <vector> | |
14 | 17 |
15 using blink::Scheduler; | 18 using blink::Scheduler; |
19 using namespace std; | |
16 | 20 |
17 namespace { | 21 namespace { |
18 | 22 |
19 class TestMainThread : public blink::WebThread { | 23 class TestMainThread : public blink::WebThread { |
20 public: | 24 public: |
21 // blink::WebThread implementation. | 25 // blink::WebThread implementation. |
22 virtual void postTask(Task* task) OVERRIDE | 26 virtual void postTask(Task* task) OVERRIDE |
23 { | 27 { |
24 m_pendingTasks.append(adoptPtr(task)); | 28 m_pendingTasks.append(adoptPtr(task)); |
25 } | 29 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 private: | 113 private: |
110 TestMainThread m_mainThread; | 114 TestMainThread m_mainThread; |
111 SharedTimerFunction m_sharedTimerFunction; | 115 SharedTimerFunction m_sharedTimerFunction; |
112 bool m_sharedTimerRunning; | 116 bool m_sharedTimerRunning; |
113 double m_sharedTimerFireInterval; | 117 double m_sharedTimerFireInterval; |
114 }; | 118 }; |
115 | 119 |
116 class SchedulerTest : public testing::Test { | 120 class SchedulerTest : public testing::Test { |
117 public: | 121 public: |
118 SchedulerTest() | 122 SchedulerTest() |
123 : m_reentrantCount(0) | |
119 { | 124 { |
120 Scheduler::initializeOnMainThread(); | 125 Scheduler::initializeOnMainThread(); |
121 m_scheduler = Scheduler::shared(); | 126 m_scheduler = Scheduler::shared(); |
122 } | 127 } |
123 | 128 |
124 ~SchedulerTest() | 129 ~SchedulerTest() |
125 { | 130 { |
126 Scheduler::shutdown(); | 131 Scheduler::shutdown(); |
127 } | 132 } |
128 | 133 |
129 void runPendingTasks() | 134 void runPendingTasks() |
130 { | 135 { |
131 m_platformSupport.runPendingTasks(); | 136 m_platformSupport.runPendingTasks(); |
132 } | 137 } |
133 | 138 |
139 void appendToVector(string value) | |
140 { | |
141 m_order.push_back(value); | |
142 } | |
143 | |
144 void appendToVectorReentrantTask() | |
145 { | |
146 m_reentrantOrder.push_back(m_reentrantCount++); | |
147 | |
148 // Note TestRentrantTaskAfterShutdown exposes the case where a low prior ity task | |
149 // is executed after the Scheduler has shut down. It's necessary to chec k here | |
150 // that the Scheduler still exists before making the re-entrant call. | |
151 if (m_reentrantCount <= 4 && Scheduler::shared()) { | |
eseidel
2014/08/12 16:16:01
Why this magic value of 4? I would at least put t
alexclarke
2014/08/13 10:08:59
Done.
| |
152 Scheduler::shared()->postTask(FROM_HERE, WTF::bind(&SchedulerTest::a ppendToVectorReentrantTask, this)); | |
153 } | |
154 } | |
155 | |
156 void appendToVectorReentrantInputTask() | |
157 { | |
158 m_reentrantOrder.push_back(m_reentrantCount++); | |
159 | |
160 if (m_reentrantCount > 4) | |
161 return; | |
162 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendTo VectorReentrantInputTask, this)); | |
163 } | |
164 | |
165 void appendToVectorReentrantCompositorTask() | |
166 { | |
167 m_reentrantOrder.push_back(m_reentrantCount++); | |
168 | |
169 if (m_reentrantCount > 4) | |
170 return; | |
171 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::app endToVectorReentrantCompositorTask, this)); | |
172 } | |
173 | |
134 protected: | 174 protected: |
135 SchedulerTestingPlatformSupport m_platformSupport; | 175 SchedulerTestingPlatformSupport m_platformSupport; |
136 Scheduler* m_scheduler; | 176 Scheduler* m_scheduler; |
177 std::vector<string> m_order; | |
178 std::vector<int> m_reentrantOrder; | |
179 int m_reentrantCount; | |
137 }; | 180 }; |
138 | 181 |
139 void orderedTestTask(int value, int* result) | 182 void orderedTestTask(int value, int* result) |
140 { | 183 { |
141 *result = (*result << 4) | value; | 184 *result = (*result << 4) | value; |
142 } | 185 } |
143 | 186 |
144 void unorderedTestTask(int value, int* result) | 187 void unorderedTestTask(int value, int* result) |
145 { | 188 { |
146 *result += value; | 189 *result += value; |
147 } | 190 } |
148 | 191 |
149 void idleTestTask(int value, int* result, double allottedTime) | 192 void idleTestTask(int value, int* result, double allottedTime) |
150 { | 193 { |
151 *result += value; | 194 *result += value; |
152 } | 195 } |
153 | 196 |
154 TEST_F(SchedulerTest, TestPostTask) | 197 TEST_F(SchedulerTest, TestPostTask) |
155 { | 198 { |
156 int result = 0; | 199 int result = 0; |
157 m_scheduler->postTask(FROM_HERE, bind(&orderedTestTask, 1, &result)); | 200 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 1, &result)); |
158 m_scheduler->postTask(FROM_HERE, bind(&orderedTestTask, 2, &result)); | 201 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 2, &result)); |
159 m_scheduler->postTask(FROM_HERE, bind(&orderedTestTask, 3, &result)); | 202 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 3, &result)); |
160 m_scheduler->postTask(FROM_HERE, bind(&orderedTestTask, 4, &result)); | 203 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 4, &result)); |
161 runPendingTasks(); | 204 runPendingTasks(); |
162 EXPECT_EQ(0x1234, result); | 205 EXPECT_EQ(0x1234, result); |
163 } | 206 } |
164 | 207 |
165 TEST_F(SchedulerTest, TestPostMixedTaskTypes) | 208 TEST_F(SchedulerTest, TestPostMixedTaskTypes) |
166 { | 209 { |
167 int result = 0; | 210 int result = 0; |
168 m_scheduler->postTask(FROM_HERE, bind(&unorderedTestTask, 1, &result)); | 211 m_scheduler->postTask(FROM_HERE, WTF::bind(&unorderedTestTask, 1, &result)); |
169 m_scheduler->postInputTask(FROM_HERE, bind(&unorderedTestTask, 2, &result)); | 212 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&unorderedTestTask, 2, &resu lt)); |
170 m_scheduler->postCompositorTask(FROM_HERE, bind(&unorderedTestTask, 4, &resu lt)); | 213 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&unorderedTestTask, 4, &result)); |
171 m_scheduler->postTask(FROM_HERE, bind(&unorderedTestTask, 8, &result)); | 214 m_scheduler->postTask(FROM_HERE, WTF::bind(&unorderedTestTask, 8, &result)); |
172 runPendingTasks(); | 215 runPendingTasks(); |
173 EXPECT_EQ(15, result); | 216 EXPECT_EQ(15, result); |
174 } | 217 } |
175 | 218 |
176 int s_sharedTimerTickCount; | 219 int s_sharedTimerTickCount; |
177 void sharedTimerFunction() | 220 void sharedTimerFunction() |
178 { | 221 { |
179 s_sharedTimerTickCount++; | 222 s_sharedTimerTickCount++; |
180 } | 223 } |
181 | 224 |
(...skipping 12 matching lines...) Expand all Loading... | |
194 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); | 237 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); |
195 | 238 |
196 m_scheduler->setSharedTimerFiredFunction(nullptr); | 239 m_scheduler->setSharedTimerFiredFunction(nullptr); |
197 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); | 240 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); |
198 } | 241 } |
199 | 242 |
200 TEST_F(SchedulerTest, TestIdleTask) | 243 TEST_F(SchedulerTest, TestIdleTask) |
201 { | 244 { |
202 // TODO: Check task allottedTime when implemented in the scheduler. | 245 // TODO: Check task allottedTime when implemented in the scheduler. |
203 int result = 0; | 246 int result = 0; |
204 m_scheduler->postIdleTask(bind<double>(&idleTestTask, 1, &result)); | 247 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, 1, &re sult)); |
205 m_scheduler->postIdleTask(bind<double>(&idleTestTask, 1, &result)); | 248 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, 1, &re sult)); |
206 m_scheduler->postIdleTask(bind<double>(&idleTestTask, 1, &result)); | 249 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, 1, &re sult)); |
207 m_scheduler->postIdleTask(bind<double>(&idleTestTask, 1, &result)); | 250 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, 1, &re sult)); |
208 runPendingTasks(); | 251 runPendingTasks(); |
209 EXPECT_EQ(4, result); | 252 EXPECT_EQ(4, result); |
210 } | 253 } |
211 | 254 |
255 TEST_F(SchedulerTest, TestTaskPrioritization) | |
256 { | |
257 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector, t his, string("L1"))); | |
258 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector, t his, string("L2"))); | |
259 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect or, this, string("I1"))); | |
260 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect or, this, string("I2"))); | |
261 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT oVector, this, string("C1"))); | |
262 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT oVector, this, string("C2"))); | |
263 | |
264 runPendingTasks(); | |
265 EXPECT_THAT(m_order, testing::ElementsAre( | |
266 string("I1"), string("I2"), string("C1"), string("C2"), string("L1"), st ring("L2"))); | |
267 } | |
268 | |
269 TEST_F(SchedulerTest, TestRentrantTask) | |
270 { | |
271 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVectorRee ntrantTask, this)); | |
272 runPendingTasks(); | |
273 | |
274 EXPECT_THAT(m_reentrantOrder, testing::ElementsAre(0, 1, 2, 3, 4)); | |
275 } | |
276 | |
277 | |
278 TEST_F(SchedulerTest, TestRentrantInputTaskDuringShutdown) | |
279 { | |
280 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect orReentrantInputTask, this)); | |
281 Scheduler::shutdown(); | |
282 | |
283 EXPECT_THAT(m_reentrantOrder, testing::ElementsAre(0, 1, 2, 3, 4)); | |
284 } | |
285 | |
286 TEST_F(SchedulerTest, TestRentrantCompositorTaskDuringShutdown) | |
287 { | |
288 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT oVectorReentrantCompositorTask, this)); | |
289 Scheduler::shutdown(); | |
290 | |
291 EXPECT_THAT(m_reentrantOrder, testing::ElementsAre(0, 1, 2, 3, 4)); | |
292 } | |
293 | |
212 } // namespace | 294 } // namespace |
OLD | NEW |