OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "platform/scheduler/renderer/throttling_helper.h" | 5 #include "platform/scheduler/renderer/throttling_helper.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/test/simple_test_tick_clock.h" | 14 #include "base/test/simple_test_tick_clock.h" |
15 #include "cc/test/ordered_simple_task_runner.h" | 15 #include "cc/test/ordered_simple_task_runner.h" |
16 #include "platform/scheduler/base/test_time_source.h" | 16 #include "platform/scheduler/base/test_time_source.h" |
| 17 #include "platform/scheduler/base/real_time_domain.h" |
17 #include "platform/scheduler/child/scheduler_tqm_delegate_for_test.h" | 18 #include "platform/scheduler/child/scheduler_tqm_delegate_for_test.h" |
18 #include "platform/scheduler/renderer/auto_advancing_virtual_time_domain.h" | 19 #include "platform/scheduler/renderer/auto_advancing_virtual_time_domain.h" |
19 #include "platform/scheduler/renderer/renderer_scheduler_impl.h" | 20 #include "platform/scheduler/renderer/renderer_scheduler_impl.h" |
20 #include "platform/scheduler/renderer/web_frame_scheduler_impl.h" | 21 #include "platform/scheduler/renderer/web_frame_scheduler_impl.h" |
21 #include "platform/scheduler/renderer/web_view_scheduler_impl.h" | 22 #include "platform/scheduler/renderer/web_view_scheduler_impl.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 | 25 |
25 using testing::ElementsAre; | 26 using testing::ElementsAre; |
26 | 27 |
27 namespace blink { | 28 namespace blink { |
28 namespace scheduler { | 29 namespace scheduler { |
29 | 30 |
30 namespace { | 31 namespace { |
31 void CountingTask(size_t* count, scoped_refptr<TaskQueue> timer_queue) { | 32 void RunTenTimesTask(size_t* count, scoped_refptr<TaskQueue> timer_queue) { |
32 if (++(*count) < 10) { | 33 if (++(*count) < 10) { |
33 timer_queue->PostTask(FROM_HERE, | 34 timer_queue->PostTask(FROM_HERE, |
34 base::Bind(&CountingTask, count, timer_queue)); | 35 base::Bind(&RunTenTimesTask, count, timer_queue)); |
35 } | 36 } |
36 } | 37 } |
37 } | 38 } |
38 | 39 |
39 class ThrottlingHelperTest : public testing::Test { | 40 class ThrottlingHelperTest : public testing::Test { |
40 public: | 41 public: |
41 ThrottlingHelperTest() {} | 42 ThrottlingHelperTest() {} |
42 ~ThrottlingHelperTest() override {} | 43 ~ThrottlingHelperTest() override {} |
43 | 44 |
44 void SetUp() override { | 45 void SetUp() override { |
45 clock_.reset(new base::SimpleTestTickClock()); | 46 clock_.reset(new base::SimpleTestTickClock()); |
46 clock_->Advance(base::TimeDelta::FromMicroseconds(5000)); | 47 clock_->Advance(base::TimeDelta::FromMicroseconds(5000)); |
47 mock_task_runner_ = | 48 mock_task_runner_ = |
48 make_scoped_refptr(new cc::OrderedSimpleTaskRunner(clock_.get(), true)); | 49 make_scoped_refptr(new cc::OrderedSimpleTaskRunner(clock_.get(), true)); |
49 delegate_ = SchedulerTqmDelegateForTest::Create( | 50 delegate_ = SchedulerTqmDelegateForTest::Create( |
50 mock_task_runner_, base::MakeUnique<TestTimeSource>(clock_.get())); | 51 mock_task_runner_, base::MakeUnique<TestTimeSource>(clock_.get())); |
51 scheduler_.reset(new RendererSchedulerImpl(delegate_)); | 52 scheduler_.reset(new RendererSchedulerImpl(delegate_)); |
52 throttling_helper_ = scheduler_->throttling_helper(); | 53 throttling_helper_ = scheduler_->throttling_helper(); |
53 timer_queue_ = scheduler_->NewTimerTaskRunner("test_queue"); | 54 timer_queue_ = scheduler_->NewTimerTaskRunner("test_queue"); |
54 } | 55 } |
55 | 56 |
56 void TearDown() override { | 57 void TearDown() override { |
57 scheduler_->Shutdown(); | 58 scheduler_->Shutdown(); |
58 scheduler_.reset(); | 59 scheduler_.reset(); |
59 } | 60 } |
60 | 61 |
61 void ExpectThrottled(scoped_refptr<TaskQueue> timer_queue) { | 62 void ExpectThrottled(scoped_refptr<TaskQueue> timer_queue) { |
62 size_t count = 0; | 63 size_t count = 0; |
63 timer_queue->PostTask(FROM_HERE, | 64 timer_queue->PostTask(FROM_HERE, |
64 base::Bind(&CountingTask, &count, timer_queue)); | 65 base::Bind(&RunTenTimesTask, &count, timer_queue)); |
65 | 66 |
66 mock_task_runner_->RunForPeriod(base::TimeDelta::FromSeconds(1)); | 67 mock_task_runner_->RunForPeriod(base::TimeDelta::FromSeconds(1)); |
67 EXPECT_LT(count, 10u); | 68 EXPECT_LE(count, 1u); |
68 mock_task_runner_->RunUntilIdle(); | 69 |
| 70 // Make sure the rest of the tasks run or we risk a UAF on |count|. |
| 71 mock_task_runner_->RunForPeriod(base::TimeDelta::FromSeconds(10)); |
| 72 EXPECT_EQ(10u, count); |
69 } | 73 } |
70 | 74 |
71 void ExpectUnthrottled(scoped_refptr<TaskQueue> timer_queue) { | 75 void ExpectUnthrottled(scoped_refptr<TaskQueue> timer_queue) { |
72 size_t count = 0; | 76 size_t count = 0; |
73 timer_queue->PostTask(FROM_HERE, | 77 timer_queue->PostTask(FROM_HERE, |
74 base::Bind(&CountingTask, &count, timer_queue)); | 78 base::Bind(&RunTenTimesTask, &count, timer_queue)); |
75 | 79 |
76 mock_task_runner_->RunForPeriod(base::TimeDelta::FromSeconds(1)); | 80 mock_task_runner_->RunForPeriod(base::TimeDelta::FromSeconds(1)); |
77 EXPECT_EQ(count, 10u); | 81 EXPECT_EQ(10u, count); |
78 mock_task_runner_->RunUntilIdle(); | 82 mock_task_runner_->RunUntilIdle(); |
79 } | 83 } |
80 | 84 |
81 protected: | 85 protected: |
82 std::unique_ptr<base::SimpleTestTickClock> clock_; | 86 std::unique_ptr<base::SimpleTestTickClock> clock_; |
83 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; | 87 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; |
84 scoped_refptr<SchedulerTqmDelegate> delegate_; | 88 scoped_refptr<SchedulerTqmDelegate> delegate_; |
85 std::unique_ptr<RendererSchedulerImpl> scheduler_; | 89 std::unique_ptr<RendererSchedulerImpl> scheduler_; |
86 scoped_refptr<TaskQueue> timer_queue_; | 90 scoped_refptr<TaskQueue> timer_queue_; |
87 ThrottlingHelper* throttling_helper_; // NOT OWNED | 91 ThrottlingHelper* throttling_helper_; // NOT OWNED |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 } | 271 } |
268 | 272 |
269 namespace { | 273 namespace { |
270 bool MessageLoopTaskCounter(size_t* count) { | 274 bool MessageLoopTaskCounter(size_t* count) { |
271 *count = *count + 1; | 275 *count = *count + 1; |
272 return true; | 276 return true; |
273 } | 277 } |
274 | 278 |
275 void NopTask() {} | 279 void NopTask() {} |
276 | 280 |
| 281 void AddOneTask(size_t* count) { |
| 282 (*count)++; |
| 283 } |
| 284 |
277 } // namespace | 285 } // namespace |
278 | 286 |
279 TEST_F(ThrottlingHelperTest, | 287 TEST_F(ThrottlingHelperTest, |
280 SingleThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) { | 288 SingleThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) { |
281 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 289 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
282 | 290 |
283 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10)); | 291 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(10)); |
284 timer_queue_->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay); | 292 timer_queue_->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay); |
285 | 293 |
286 size_t task_count = 0; | 294 size_t task_count = 0; |
287 mock_task_runner_->RunTasksWhile( | 295 mock_task_runner_->RunTasksWhile( |
288 base::Bind(&MessageLoopTaskCounter, &task_count)); | 296 base::Bind(&MessageLoopTaskCounter, &task_count)); |
289 | 297 |
290 EXPECT_EQ(1u, task_count); | 298 // Run the task and ThrottlingHelperTest::MaybeDisableQueue. |
| 299 EXPECT_EQ(2u, task_count); |
291 } | 300 } |
292 | 301 |
293 TEST_F(ThrottlingHelperTest, | 302 TEST_F(ThrottlingHelperTest, |
294 SingleFutureThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) { | 303 SingleFutureThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) { |
295 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 304 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
296 | 305 |
297 base::TimeDelta delay(base::TimeDelta::FromSecondsD(15.5)); | 306 base::TimeDelta delay(base::TimeDelta::FromSecondsD(15.5)); |
298 timer_queue_->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay); | 307 timer_queue_->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay); |
299 | 308 |
300 size_t task_count = 0; | 309 size_t task_count = 0; |
301 mock_task_runner_->RunTasksWhile( | 310 mock_task_runner_->RunTasksWhile( |
302 base::Bind(&MessageLoopTaskCounter, &task_count)); | 311 base::Bind(&MessageLoopTaskCounter, &task_count)); |
303 | 312 |
304 EXPECT_EQ(1u, task_count); | 313 // Run the delayed task and ThrottlingHelperTest::MaybeDisableQueue. |
| 314 EXPECT_EQ(2u, task_count); |
305 } | 315 } |
306 | 316 |
307 TEST_F(ThrottlingHelperTest, | 317 TEST_F(ThrottlingHelperTest, |
308 TwoFutureThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) { | 318 TwoFutureThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) { |
309 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 319 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
310 std::vector<base::TimeTicks> run_times; | 320 std::vector<base::TimeTicks> run_times; |
311 | 321 |
312 base::TimeDelta delay(base::TimeDelta::FromSecondsD(15.5)); | 322 base::TimeDelta delay(base::TimeDelta::FromSecondsD(15.5)); |
313 timer_queue_->PostDelayedTask( | 323 timer_queue_->PostDelayedTask( |
314 FROM_HERE, base::Bind(&TestTask, &run_times, clock_.get()), delay); | 324 FROM_HERE, base::Bind(&TestTask, &run_times, clock_.get()), delay); |
315 | 325 |
316 base::TimeDelta delay2(base::TimeDelta::FromSecondsD(5.5)); | 326 base::TimeDelta delay2(base::TimeDelta::FromSecondsD(5.5)); |
317 timer_queue_->PostDelayedTask( | 327 timer_queue_->PostDelayedTask( |
318 FROM_HERE, base::Bind(&TestTask, &run_times, clock_.get()), delay2); | 328 FROM_HERE, base::Bind(&TestTask, &run_times, clock_.get()), delay2); |
319 | 329 |
320 size_t task_count = 0; | 330 size_t task_count = 0; |
321 mock_task_runner_->RunTasksWhile( | 331 mock_task_runner_->RunTasksWhile( |
322 base::Bind(&MessageLoopTaskCounter, &task_count)); | 332 base::Bind(&MessageLoopTaskCounter, &task_count)); |
323 | 333 |
324 EXPECT_EQ(2u, task_count); // There are two since the cancelled task runs in | 334 // Run both delayed tasks and 2x ThrottlingHelperTest::MaybeDisableQueue. |
325 // the same DoWork batch. | 335 EXPECT_EQ(4u, task_count); |
326 | 336 |
327 EXPECT_THAT( | 337 EXPECT_THAT( |
328 run_times, | 338 run_times, |
329 ElementsAre(base::TimeTicks() + base::TimeDelta::FromSeconds(6), | 339 ElementsAre(base::TimeTicks() + base::TimeDelta::FromSeconds(6), |
330 base::TimeTicks() + base::TimeDelta::FromSeconds(16))); | 340 base::TimeTicks() + base::TimeDelta::FromSeconds(16))); |
331 } | 341 } |
332 | 342 |
333 TEST_F(ThrottlingHelperTest, TaskDelayIsBasedOnRealTime) { | 343 TEST_F(ThrottlingHelperTest, TaskDelayIsBasedOnRealTime) { |
334 std::vector<base::TimeTicks> run_times; | 344 std::vector<base::TimeTicks> run_times; |
335 | 345 |
(...skipping 30 matching lines...) Expand all Loading... |
366 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 376 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
367 EXPECT_EQ(timer_queue_->GetTimeDomain()->Now(), clock_->NowTicks()); | 377 EXPECT_EQ(timer_queue_->GetTimeDomain()->Now(), clock_->NowTicks()); |
368 | 378 |
369 clock_->Advance(base::TimeDelta::FromMilliseconds(250)); | 379 clock_->Advance(base::TimeDelta::FromMilliseconds(250)); |
370 // Make sure the throttled time domain's Now() reports the same as the | 380 // Make sure the throttled time domain's Now() reports the same as the |
371 // underlying clock. | 381 // underlying clock. |
372 EXPECT_EQ(timer_queue_->GetTimeDomain()->Now(), clock_->NowTicks()); | 382 EXPECT_EQ(timer_queue_->GetTimeDomain()->Now(), clock_->NowTicks()); |
373 } | 383 } |
374 | 384 |
375 TEST_F(ThrottlingHelperTest, TaskQueueDisabledTillPump) { | 385 TEST_F(ThrottlingHelperTest, TaskQueueDisabledTillPump) { |
376 timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask)); | 386 size_t count = 0; |
| 387 timer_queue_->PostTask(FROM_HERE, base::Bind(&AddOneTask, &count)); |
377 | 388 |
378 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); | 389 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); |
379 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 390 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
380 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 391 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
381 | 392 |
382 mock_task_runner_->RunUntilIdle(); // Wait until the pump. | 393 mock_task_runner_->RunUntilIdle(); // Wait until the pump. |
383 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); | 394 // The queue must have been enabled for the task to run but after the pump it |
| 395 // gets disabled again. |
| 396 EXPECT_EQ(1u, count); |
| 397 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
384 } | 398 } |
385 | 399 |
386 TEST_F(ThrottlingHelperTest, TaskQueueUnthrottle_InitiallyEnabled) { | 400 TEST_F(ThrottlingHelperTest, TaskQueueUnthrottle_InitiallyEnabled) { |
387 timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask)); | 401 timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask)); |
388 | 402 |
389 timer_queue_->SetQueueEnabled(true); // NOP | 403 timer_queue_->SetQueueEnabled(true); // NOP |
390 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 404 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
391 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 405 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
392 | 406 |
393 throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get()); | 407 throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get()); |
(...skipping 26 matching lines...) Expand all Loading... |
420 | 434 |
421 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 435 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
422 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 436 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
423 | 437 |
424 throttling_helper_->SetQueueEnabled(timer_queue_.get(), false); | 438 throttling_helper_->SetQueueEnabled(timer_queue_.get(), false); |
425 throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get()); | 439 throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get()); |
426 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 440 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
427 } | 441 } |
428 | 442 |
429 TEST_F(ThrottlingHelperTest, TaskQueueDisabledTillPump_ThenManuallyDisabled) { | 443 TEST_F(ThrottlingHelperTest, TaskQueueDisabledTillPump_ThenManuallyDisabled) { |
430 timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask)); | 444 size_t count = 0; |
| 445 timer_queue_->PostTask(FROM_HERE, base::Bind(&AddOneTask, &count)); |
431 | 446 |
432 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); | 447 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); |
433 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 448 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
434 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 449 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
435 | 450 |
436 mock_task_runner_->RunUntilIdle(); // Wait until the pump. | 451 mock_task_runner_->RunUntilIdle(); // Wait until the pump. |
437 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); | 452 // The queue must have been enabled for the task to run but after the pump it |
| 453 // gets disabled again. |
| 454 EXPECT_EQ(1u, count); |
| 455 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
438 | 456 |
439 throttling_helper_->SetQueueEnabled(timer_queue_.get(), false); | 457 throttling_helper_->SetQueueEnabled(timer_queue_.get(), false); |
440 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 458 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
441 } | 459 } |
442 | 460 |
443 TEST_F(ThrottlingHelperTest, DoubleIncrementDoubleDecrement) { | 461 TEST_F(ThrottlingHelperTest, DoubleIncrementDoubleDecrement) { |
444 timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask)); | 462 timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask)); |
445 | 463 |
446 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); | 464 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); |
447 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 465 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
(...skipping 23 matching lines...) Expand all Loading... |
471 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 489 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
472 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 490 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
473 | 491 |
474 scheduler_->EnableVirtualTime(); | 492 scheduler_->EnableVirtualTime(); |
475 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); | 493 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); |
476 EXPECT_EQ(timer_queue_->GetTimeDomain(), scheduler_->GetVirtualTimeDomain()); | 494 EXPECT_EQ(timer_queue_->GetTimeDomain(), scheduler_->GetVirtualTimeDomain()); |
477 } | 495 } |
478 | 496 |
479 } // namespace scheduler | 497 } // namespace scheduler |
480 } // namespace blink | 498 } // namespace blink |
OLD | NEW |