| 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. |
| 299 // TODO(alexclarke): Add a base::RunLoop observer and fix this. |
| 300 EXPECT_EQ(2u, task_count); |
| 291 } | 301 } |
| 292 | 302 |
| 293 TEST_F(ThrottlingHelperTest, | 303 TEST_F(ThrottlingHelperTest, |
| 294 SingleFutureThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) { | 304 SingleFutureThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) { |
| 295 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 305 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
| 296 | 306 |
| 297 base::TimeDelta delay(base::TimeDelta::FromSecondsD(15.5)); | 307 base::TimeDelta delay(base::TimeDelta::FromSecondsD(15.5)); |
| 298 timer_queue_->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay); | 308 timer_queue_->PostDelayedTask(FROM_HERE, base::Bind(&NopTask), delay); |
| 299 | 309 |
| 300 size_t task_count = 0; | 310 size_t task_count = 0; |
| 301 mock_task_runner_->RunTasksWhile( | 311 mock_task_runner_->RunTasksWhile( |
| 302 base::Bind(&MessageLoopTaskCounter, &task_count)); | 312 base::Bind(&MessageLoopTaskCounter, &task_count)); |
| 303 | 313 |
| 304 EXPECT_EQ(1u, task_count); | 314 // Run the delayed task. |
| 315 // TODO(alexclarke): Add a base::RunLoop observer and fix this. |
| 316 EXPECT_EQ(2u, task_count); |
| 305 } | 317 } |
| 306 | 318 |
| 307 TEST_F(ThrottlingHelperTest, | 319 TEST_F(ThrottlingHelperTest, |
| 308 TwoFutureThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) { | 320 TwoFutureThrottledTaskPumpedAndRunWithNoExtraneousMessageLoopTasks) { |
| 309 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 321 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
| 310 std::vector<base::TimeTicks> run_times; | 322 std::vector<base::TimeTicks> run_times; |
| 311 | 323 |
| 312 base::TimeDelta delay(base::TimeDelta::FromSecondsD(15.5)); | 324 base::TimeDelta delay(base::TimeDelta::FromSecondsD(15.5)); |
| 313 timer_queue_->PostDelayedTask( | 325 timer_queue_->PostDelayedTask( |
| 314 FROM_HERE, base::Bind(&TestTask, &run_times, clock_.get()), delay); | 326 FROM_HERE, base::Bind(&TestTask, &run_times, clock_.get()), delay); |
| 315 | 327 |
| 316 base::TimeDelta delay2(base::TimeDelta::FromSecondsD(5.5)); | 328 base::TimeDelta delay2(base::TimeDelta::FromSecondsD(5.5)); |
| 317 timer_queue_->PostDelayedTask( | 329 timer_queue_->PostDelayedTask( |
| 318 FROM_HERE, base::Bind(&TestTask, &run_times, clock_.get()), delay2); | 330 FROM_HERE, base::Bind(&TestTask, &run_times, clock_.get()), delay2); |
| 319 | 331 |
| 320 size_t task_count = 0; | 332 size_t task_count = 0; |
| 321 mock_task_runner_->RunTasksWhile( | 333 mock_task_runner_->RunTasksWhile( |
| 322 base::Bind(&MessageLoopTaskCounter, &task_count)); | 334 base::Bind(&MessageLoopTaskCounter, &task_count)); |
| 323 | 335 |
| 324 EXPECT_EQ(2u, task_count); // There are two since the cancelled task runs in | 336 // Run both delayed tasks. |
| 325 // the same DoWork batch. | 337 // TODO(alexclarke): Add a base::RunLoop observer and fix this. |
| 338 EXPECT_EQ(4u, task_count); |
| 326 | 339 |
| 327 EXPECT_THAT( | 340 EXPECT_THAT( |
| 328 run_times, | 341 run_times, |
| 329 ElementsAre(base::TimeTicks() + base::TimeDelta::FromSeconds(6), | 342 ElementsAre(base::TimeTicks() + base::TimeDelta::FromSeconds(6), |
| 330 base::TimeTicks() + base::TimeDelta::FromSeconds(16))); | 343 base::TimeTicks() + base::TimeDelta::FromSeconds(16))); |
| 331 } | 344 } |
| 332 | 345 |
| 333 TEST_F(ThrottlingHelperTest, TaskDelayIsBasedOnRealTime) { | 346 TEST_F(ThrottlingHelperTest, TaskDelayIsBasedOnRealTime) { |
| 334 std::vector<base::TimeTicks> run_times; | 347 std::vector<base::TimeTicks> run_times; |
| 335 | 348 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 366 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 379 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
| 367 EXPECT_EQ(timer_queue_->GetTimeDomain()->Now(), clock_->NowTicks()); | 380 EXPECT_EQ(timer_queue_->GetTimeDomain()->Now(), clock_->NowTicks()); |
| 368 | 381 |
| 369 clock_->Advance(base::TimeDelta::FromMilliseconds(250)); | 382 clock_->Advance(base::TimeDelta::FromMilliseconds(250)); |
| 370 // Make sure the throttled time domain's Now() reports the same as the | 383 // Make sure the throttled time domain's Now() reports the same as the |
| 371 // underlying clock. | 384 // underlying clock. |
| 372 EXPECT_EQ(timer_queue_->GetTimeDomain()->Now(), clock_->NowTicks()); | 385 EXPECT_EQ(timer_queue_->GetTimeDomain()->Now(), clock_->NowTicks()); |
| 373 } | 386 } |
| 374 | 387 |
| 375 TEST_F(ThrottlingHelperTest, TaskQueueDisabledTillPump) { | 388 TEST_F(ThrottlingHelperTest, TaskQueueDisabledTillPump) { |
| 376 timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask)); | 389 size_t count = 0; |
| 390 timer_queue_->PostTask(FROM_HERE, base::Bind(&AddOneTask, &count)); |
| 377 | 391 |
| 378 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); | 392 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); |
| 379 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 393 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
| 380 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 394 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
| 381 | 395 |
| 382 mock_task_runner_->RunUntilIdle(); // Wait until the pump. | 396 mock_task_runner_->RunUntilIdle(); // Wait until the pump. |
| 397 EXPECT_EQ(1u, count); // The task got run |
| 383 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); | 398 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); |
| 384 } | 399 } |
| 385 | 400 |
| 386 TEST_F(ThrottlingHelperTest, TaskQueueUnthrottle_InitiallyEnabled) { | 401 TEST_F(ThrottlingHelperTest, TaskQueueUnthrottle_InitiallyEnabled) { |
| 387 timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask)); | 402 timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask)); |
| 388 | 403 |
| 389 timer_queue_->SetQueueEnabled(true); // NOP | 404 timer_queue_->SetQueueEnabled(true); // NOP |
| 390 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 405 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
| 391 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 406 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
| 392 | 407 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 420 | 435 |
| 421 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 436 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
| 422 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 437 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
| 423 | 438 |
| 424 throttling_helper_->SetQueueEnabled(timer_queue_.get(), false); | 439 throttling_helper_->SetQueueEnabled(timer_queue_.get(), false); |
| 425 throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get()); | 440 throttling_helper_->DecreaseThrottleRefCount(timer_queue_.get()); |
| 426 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 441 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
| 427 } | 442 } |
| 428 | 443 |
| 429 TEST_F(ThrottlingHelperTest, TaskQueueDisabledTillPump_ThenManuallyDisabled) { | 444 TEST_F(ThrottlingHelperTest, TaskQueueDisabledTillPump_ThenManuallyDisabled) { |
| 430 timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask)); | 445 size_t count = 0; |
| 446 timer_queue_->PostTask(FROM_HERE, base::Bind(&AddOneTask, &count)); |
| 431 | 447 |
| 432 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); | 448 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); |
| 433 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 449 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
| 434 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 450 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
| 435 | 451 |
| 436 mock_task_runner_->RunUntilIdle(); // Wait until the pump. | 452 mock_task_runner_->RunUntilIdle(); // Wait until the pump. |
| 453 EXPECT_EQ(1u, count); // Task ran |
| 437 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); | 454 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); |
| 438 | 455 |
| 439 throttling_helper_->SetQueueEnabled(timer_queue_.get(), false); | 456 throttling_helper_->SetQueueEnabled(timer_queue_.get(), false); |
| 440 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 457 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
| 441 } | 458 } |
| 442 | 459 |
| 443 TEST_F(ThrottlingHelperTest, DoubleIncrementDoubleDecrement) { | 460 TEST_F(ThrottlingHelperTest, DoubleIncrementDoubleDecrement) { |
| 444 timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask)); | 461 timer_queue_->PostTask(FROM_HERE, base::Bind(&NopTask)); |
| 445 | 462 |
| 446 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); | 463 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 471 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); | 488 throttling_helper_->IncreaseThrottleRefCount(timer_queue_.get()); |
| 472 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); | 489 EXPECT_FALSE(timer_queue_->IsQueueEnabled()); |
| 473 | 490 |
| 474 scheduler_->EnableVirtualTime(); | 491 scheduler_->EnableVirtualTime(); |
| 475 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); | 492 EXPECT_TRUE(timer_queue_->IsQueueEnabled()); |
| 476 EXPECT_EQ(timer_queue_->GetTimeDomain(), scheduler_->GetVirtualTimeDomain()); | 493 EXPECT_EQ(timer_queue_->GetTimeDomain(), scheduler_->GetVirtualTimeDomain()); |
| 477 } | 494 } |
| 478 | 495 |
| 479 } // namespace scheduler | 496 } // namespace scheduler |
| 480 } // namespace blink | 497 } // namespace blink |
| OLD | NEW |