Chromium Code Reviews| 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 befpre making the re-entrant call. | |
|
Sami
2014/08/08 15:31:13
s/befpre/before/
alexclarke
2014/08/08 15:47:46
Done.
| |
| 151 if (m_reentrantCount <= 4 && Scheduler::shared()) { | |
| 152 Scheduler::shared()->postTask( | |
| 153 FROM_HERE, WTF::bind(&SchedulerTest::appendToVectorReentrantTask , this)); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 void appendToVectorReentrantInputTask() | |
| 158 { | |
| 159 m_reentrantOrder.push_back(m_reentrantCount++); | |
| 160 | |
| 161 if (m_reentrantCount <= 4) { | |
| 162 m_scheduler->postInputTask( | |
| 163 FROM_HERE, WTF::bind(&SchedulerTest::appendToVectorReentrantInpu tTask, this)); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 void appendToVectorReentrantCompositorTask() | |
| 168 { | |
| 169 m_reentrantOrder.push_back(m_reentrantCount++); | |
| 170 | |
| 171 if (m_reentrantCount <= 4) { | |
| 172 m_scheduler->postCompositorTask( | |
| 173 FROM_HERE, WTF::bind(&SchedulerTest::appendToVectorReentrantComp ositorTask, this)); | |
| 174 } | |
| 175 } | |
| 176 | |
| 134 protected: | 177 protected: |
| 135 SchedulerTestingPlatformSupport m_platformSupport; | 178 SchedulerTestingPlatformSupport m_platformSupport; |
| 136 Scheduler* m_scheduler; | 179 Scheduler* m_scheduler; |
| 180 std::vector<string> m_order; | |
| 181 std::vector<int> m_reentrantOrder; | |
| 182 int m_reentrantCount; | |
| 137 }; | 183 }; |
| 138 | 184 |
| 139 void orderedTestTask(int value, int* result) | 185 void orderedTestTask(int value, int* result) |
| 140 { | 186 { |
| 141 *result = (*result << 4) | value; | 187 *result = (*result << 4) | value; |
| 142 } | 188 } |
| 143 | 189 |
| 144 void unorderedTestTask(int value, int* result) | 190 void unorderedTestTask(int value, int* result) |
| 145 { | 191 { |
| 146 *result += value; | 192 *result += value; |
| 147 } | 193 } |
| 148 | 194 |
| 149 void idleTestTask(int value, int* result, double allottedTime) | 195 void idleTestTask(int value, int* result, double allottedTime) |
| 150 { | 196 { |
| 151 *result += value; | 197 *result += value; |
| 152 } | 198 } |
| 153 | 199 |
| 154 TEST_F(SchedulerTest, TestPostTask) | 200 TEST_F(SchedulerTest, TestPostTask) |
| 155 { | 201 { |
| 156 int result = 0; | 202 int result = 0; |
| 157 m_scheduler->postTask(FROM_HERE, bind(&orderedTestTask, 1, &result)); | 203 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 1, &result)); |
| 158 m_scheduler->postTask(FROM_HERE, bind(&orderedTestTask, 2, &result)); | 204 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 2, &result)); |
| 159 m_scheduler->postTask(FROM_HERE, bind(&orderedTestTask, 3, &result)); | 205 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 3, &result)); |
| 160 m_scheduler->postTask(FROM_HERE, bind(&orderedTestTask, 4, &result)); | 206 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 4, &result)); |
| 161 runPendingTasks(); | 207 runPendingTasks(); |
| 162 EXPECT_EQ(0x1234, result); | 208 EXPECT_EQ(0x1234, result); |
| 163 } | 209 } |
| 164 | 210 |
| 165 TEST_F(SchedulerTest, TestPostMixedTaskTypes) | 211 TEST_F(SchedulerTest, TestPostMixedTaskTypes) |
| 166 { | 212 { |
| 167 int result = 0; | 213 int result = 0; |
| 168 m_scheduler->postTask(FROM_HERE, bind(&unorderedTestTask, 1, &result)); | 214 m_scheduler->postTask(FROM_HERE, WTF::bind(&unorderedTestTask, 1, &result)); |
| 169 m_scheduler->postInputTask(FROM_HERE, bind(&unorderedTestTask, 2, &result)); | 215 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&unorderedTestTask, 2, &resu lt)); |
| 170 m_scheduler->postCompositorTask(FROM_HERE, bind(&unorderedTestTask, 4, &resu lt)); | 216 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&unorderedTestTask, 4, &result)); |
| 171 m_scheduler->postTask(FROM_HERE, bind(&unorderedTestTask, 8, &result)); | 217 m_scheduler->postTask(FROM_HERE, WTF::bind(&unorderedTestTask, 8, &result)); |
| 172 runPendingTasks(); | 218 runPendingTasks(); |
| 173 EXPECT_EQ(15, result); | 219 EXPECT_EQ(15, result); |
| 174 } | 220 } |
| 221 | |
| 222 TEST_F(SchedulerTest, TestTasksExecutedOnShutdown) | |
| 223 { | |
| 224 int result = 0; | |
| 225 m_scheduler->postTask(FROM_HERE, WTF::bind(&unorderedTestTask, 1, &result)); | |
| 226 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&unorderedTestTask, 2, &resu lt)); | |
| 227 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&unorderedTestTask, 4, &result)); | |
| 228 m_scheduler->postTask(FROM_HERE, WTF::bind(&unorderedTestTask, 8, &result)); | |
| 229 Scheduler::shutdown(); | |
| 230 runPendingTasks(); | |
| 231 EXPECT_EQ(15, result); | |
| 232 } | |
| 175 | 233 |
| 176 int s_sharedTimerTickCount; | 234 int s_sharedTimerTickCount; |
| 177 void sharedTimerFunction() | 235 void sharedTimerFunction() |
| 178 { | 236 { |
| 179 s_sharedTimerTickCount++; | 237 s_sharedTimerTickCount++; |
| 180 } | 238 } |
| 181 | 239 |
| 182 TEST_F(SchedulerTest, TestSharedTimer) | 240 TEST_F(SchedulerTest, TestSharedTimer) |
| 183 { | 241 { |
| 184 s_sharedTimerTickCount = 0; | 242 s_sharedTimerTickCount = 0; |
| 185 m_scheduler->setSharedTimerFiredFunction(&sharedTimerFunction); | 243 m_scheduler->setSharedTimerFiredFunction(&sharedTimerFunction); |
| 186 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); | 244 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); |
| 187 m_scheduler->setSharedTimerFireInterval(0); | 245 m_scheduler->setSharedTimerFireInterval(0); |
| 188 EXPECT_TRUE(m_platformSupport.sharedTimerRunning()); | 246 EXPECT_TRUE(m_platformSupport.sharedTimerRunning()); |
| 189 | 247 |
| 190 m_platformSupport.triggerSharedTimer(); | 248 m_platformSupport.triggerSharedTimer(); |
| 191 EXPECT_EQ(1, s_sharedTimerTickCount); | 249 EXPECT_EQ(1, s_sharedTimerTickCount); |
| 192 | 250 |
| 193 m_scheduler->stopSharedTimer(); | 251 m_scheduler->stopSharedTimer(); |
| 194 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); | 252 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); |
| 195 | 253 |
| 196 m_scheduler->setSharedTimerFiredFunction(nullptr); | 254 m_scheduler->setSharedTimerFiredFunction(nullptr); |
| 197 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); | 255 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); |
| 198 } | 256 } |
| 199 | 257 |
| 200 TEST_F(SchedulerTest, TestIdleTask) | 258 TEST_F(SchedulerTest, TestIdleTask) |
| 201 { | 259 { |
| 202 // TODO: Check task allottedTime when implemented in the scheduler. | 260 // TODO: Check task allottedTime when implemented in the scheduler. |
| 203 int result = 0; | 261 int result = 0; |
| 204 m_scheduler->postIdleTask(bind<double>(&idleTestTask, 1, &result)); | 262 m_scheduler->postIdleTask(WTF::bind<double>(&idleTestTask, 1, &result)); |
| 205 m_scheduler->postIdleTask(bind<double>(&idleTestTask, 1, &result)); | 263 m_scheduler->postIdleTask(WTF::bind<double>(&idleTestTask, 1, &result)); |
| 206 m_scheduler->postIdleTask(bind<double>(&idleTestTask, 1, &result)); | 264 m_scheduler->postIdleTask(WTF::bind<double>(&idleTestTask, 1, &result)); |
| 207 m_scheduler->postIdleTask(bind<double>(&idleTestTask, 1, &result)); | 265 m_scheduler->postIdleTask(WTF::bind<double>(&idleTestTask, 1, &result)); |
| 208 runPendingTasks(); | 266 runPendingTasks(); |
| 209 EXPECT_EQ(4, result); | 267 EXPECT_EQ(4, result); |
| 210 } | 268 } |
| 211 | 269 |
| 270 TEST_F(SchedulerTest, TestTaskPrioritization) | |
| 271 { | |
| 272 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector, t his, string("L1"))); | |
| 273 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector, t his, string("L2"))); | |
| 274 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect or, this, string("I1"))); | |
| 275 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect or, this, string("I2"))); | |
| 276 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT oVector, this, string("C1"))); | |
| 277 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT oVector, this, string("C2"))); | |
| 278 | |
| 279 runPendingTasks(); | |
| 280 EXPECT_THAT(m_order, testing::ElementsAre( | |
| 281 string("I1"), string("I2"), string("C1"), string("C2"), string("L1"), st ring("L2"))); | |
| 282 } | |
| 283 | |
| 284 TEST_F(SchedulerTest, TestRentrantTask) | |
| 285 { | |
| 286 m_scheduler->postTask( | |
| 287 FROM_HERE, WTF::bind(&SchedulerTest::appendToVectorReentrantTask, this)) ; | |
| 288 runPendingTasks(); | |
| 289 | |
| 290 EXPECT_THAT(m_reentrantOrder, testing::ElementsAre(0, 1, 2, 3, 4)); | |
| 291 } | |
| 292 | |
| 293 TEST_F(SchedulerTest, TestRentrantTaskAfterShutdown) | |
| 294 { | |
| 295 m_scheduler->postTask( | |
| 296 FROM_HERE, WTF::bind(&SchedulerTest::appendToVectorReentrantTask, this)) ; | |
| 297 Scheduler::shutdown(); | |
| 298 EXPECT_TRUE(m_reentrantOrder.empty()); | |
| 299 | |
| 300 // Regular tasks are delegated to the main run loop, and since the scheduler has shutdown it's | |
| 301 // no longer possible to post any more tasks through it. Hence only 1 task g ets executed. | |
| 302 runPendingTasks(); | |
| 303 EXPECT_THAT(m_reentrantOrder, testing::ElementsAre(0)); | |
| 304 } | |
| 305 | |
| 306 TEST_F(SchedulerTest, TestRentrantInputTaskDuringShutdown) | |
| 307 { | |
| 308 m_scheduler->postInputTask( | |
| 309 FROM_HERE, WTF::bind(&SchedulerTest::appendToVectorReentrantInputTask, t his)); | |
| 310 Scheduler::shutdown(); | |
| 311 | |
| 312 EXPECT_THAT(m_reentrantOrder, testing::ElementsAre(0, 1, 2, 3, 4)); | |
| 313 } | |
| 314 | |
| 315 TEST_F(SchedulerTest, TestRentrantCompositorTaskDuringShutdown) | |
| 316 { | |
| 317 m_scheduler->postCompositorTask( | |
| 318 FROM_HERE, WTF::bind(&SchedulerTest::appendToVectorReentrantCompositorTa sk, this)); | |
| 319 Scheduler::shutdown(); | |
| 320 | |
| 321 EXPECT_THAT(m_reentrantOrder, testing::ElementsAre(0, 1, 2, 3, 4)); | |
| 322 } | |
| 323 | |
| 212 } // namespace | 324 } // namespace |
| 325 | |
|
Sami
2014/08/08 15:31:13
nit: Some extra whitespace here.
alexclarke
2014/08/08 15:47:46
Done.
| |
| 326 | |
| OLD | NEW |