| 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 "components/scheduler/child/idle_helper.h" | 5 #include "components/scheduler/child/idle_helper.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "cc/test/ordered_simple_task_runner.h" | 8 #include "cc/test/ordered_simple_task_runner.h" |
| 9 #include "cc/test/test_now_source.h" | 9 #include "cc/test/test_now_source.h" |
| 10 #include "components/scheduler/child/nestable_task_runner_for_test.h" | 10 #include "components/scheduler/child/nestable_task_runner_for_test.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 void AppendToVectorIdleTestTask(std::vector<std::string>* vector, | 31 void AppendToVectorIdleTestTask(std::vector<std::string>* vector, |
| 32 std::string value, | 32 std::string value, |
| 33 base::TimeTicks deadline) { | 33 base::TimeTicks deadline) { |
| 34 AppendToVectorTestTask(vector, value); | 34 AppendToVectorTestTask(vector, value); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void NullTask() { | 37 void NullTask() { |
| 38 } | 38 } |
| 39 | 39 |
| 40 void NullIdleTask(base::TimeTicks deadline) { |
| 41 } |
| 42 |
| 40 void AppendToVectorReentrantTask(base::SingleThreadTaskRunner* task_runner, | 43 void AppendToVectorReentrantTask(base::SingleThreadTaskRunner* task_runner, |
| 41 std::vector<int>* vector, | 44 std::vector<int>* vector, |
| 42 int* reentrant_count, | 45 int* reentrant_count, |
| 43 int max_reentrant_count) { | 46 int max_reentrant_count) { |
| 44 vector->push_back((*reentrant_count)++); | 47 vector->push_back((*reentrant_count)++); |
| 45 if (*reentrant_count < max_reentrant_count) { | 48 if (*reentrant_count < max_reentrant_count) { |
| 46 task_runner->PostTask( | 49 task_runner->PostTask( |
| 47 FROM_HERE, | 50 FROM_HERE, |
| 48 base::Bind(AppendToVectorReentrantTask, base::Unretained(task_runner), | 51 base::Bind(AppendToVectorReentrantTask, base::Unretained(task_runner), |
| 49 vector, reentrant_count, max_reentrant_count)); | 52 vector, reentrant_count, max_reentrant_count)); |
| 50 } | 53 } |
| 51 } | 54 } |
| 52 | 55 |
| 53 void NullIdleTask(base::TimeTicks) { | |
| 54 } | |
| 55 | |
| 56 void IdleTestTask(int* run_count, | 56 void IdleTestTask(int* run_count, |
| 57 base::TimeTicks* deadline_out, | 57 base::TimeTicks* deadline_out, |
| 58 base::TimeTicks deadline) { | 58 base::TimeTicks deadline) { |
| 59 (*run_count)++; | 59 (*run_count)++; |
| 60 *deadline_out = deadline; | 60 *deadline_out = deadline; |
| 61 } | 61 } |
| 62 | 62 |
| 63 int max_idle_task_reposts = 2; | 63 int max_idle_task_reposts = 2; |
| 64 | 64 |
| 65 void RepostingIdleTestTask(SingleThreadIdleTaskRunner* idle_task_runner, | 65 void RepostingIdleTestTask(SingleThreadIdleTaskRunner* idle_task_runner, |
| 66 int* run_count, | 66 int* run_count, |
| 67 base::TimeTicks* deadline_out, |
| 67 base::TimeTicks deadline) { | 68 base::TimeTicks deadline) { |
| 68 if ((*run_count + 1) < max_idle_task_reposts) { | 69 if ((*run_count + 1) < max_idle_task_reposts) { |
| 69 idle_task_runner->PostIdleTask( | 70 idle_task_runner->PostIdleTask( |
| 70 FROM_HERE, base::Bind(&RepostingIdleTestTask, | 71 FROM_HERE, |
| 71 base::Unretained(idle_task_runner), run_count)); | 72 base::Bind(&RepostingIdleTestTask, base::Unretained(idle_task_runner), |
| 73 run_count, deadline_out)); |
| 72 } | 74 } |
| 75 *deadline_out = deadline; |
| 73 (*run_count)++; | 76 (*run_count)++; |
| 74 } | 77 } |
| 75 | 78 |
| 79 void RepostingUpdateClockIdleTestTask( |
| 80 SingleThreadIdleTaskRunner* idle_task_runner, |
| 81 int* run_count, |
| 82 scoped_refptr<cc::TestNowSource> clock, |
| 83 base::TimeDelta advance_time, |
| 84 std::vector<base::TimeTicks>* deadlines, |
| 85 base::TimeTicks deadline) { |
| 86 if ((*run_count + 1) < max_idle_task_reposts) { |
| 87 idle_task_runner->PostIdleTask( |
| 88 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask, |
| 89 base::Unretained(idle_task_runner), run_count, |
| 90 clock, advance_time, deadlines)); |
| 91 } |
| 92 deadlines->push_back(deadline); |
| 93 (*run_count)++; |
| 94 clock->AdvanceNow(advance_time); |
| 95 } |
| 96 |
| 76 void RepeatingTask(base::SingleThreadTaskRunner* task_runner, | 97 void RepeatingTask(base::SingleThreadTaskRunner* task_runner, |
| 77 int num_repeats, | 98 int num_repeats, |
| 78 base::TimeDelta delay) { | 99 base::TimeDelta delay) { |
| 79 if (num_repeats > 1) { | 100 if (num_repeats > 1) { |
| 80 task_runner->PostDelayedTask( | 101 task_runner->PostDelayedTask( |
| 81 FROM_HERE, base::Bind(&RepeatingTask, base::Unretained(task_runner), | 102 FROM_HERE, base::Bind(&RepeatingTask, base::Unretained(task_runner), |
| 82 num_repeats - 1, delay), | 103 num_repeats - 1, delay), |
| 83 delay); | 104 delay); |
| 84 } | 105 } |
| 85 } | 106 } |
| 86 | 107 |
| 87 void UpdateClockToDeadlineIdleTestTask( | 108 void UpdateClockIdleTestTask(scoped_refptr<cc::TestNowSource> clock, |
| 88 scoped_refptr<cc::TestNowSource> clock, | 109 int* run_count, |
| 89 base::SingleThreadTaskRunner* task_runner, | 110 base::TimeTicks set_time, |
| 90 int* run_count, | 111 base::TimeTicks deadline) { |
| 91 base::TimeTicks deadline) { | 112 clock->SetNow(set_time); |
| 92 clock->SetNow(deadline); | |
| 93 // Due to the way in which OrderedSimpleTestRunner orders tasks and the fact | |
| 94 // that we updated the time within a task, the delayed pending task to call | |
| 95 // EndIdlePeriod will not happen until after a TaskQueueManager DoWork, so | |
| 96 // post a normal task here to ensure it runs before the next idle task. | |
| 97 task_runner->PostTask(FROM_HERE, base::Bind(NullTask)); | |
| 98 (*run_count)++; | 113 (*run_count)++; |
| 99 } | 114 } |
| 100 | 115 |
| 116 void UpdateClockToDeadlineIdleTestTask(scoped_refptr<cc::TestNowSource> clock, |
| 117 int* run_count, |
| 118 base::TimeTicks deadline) { |
| 119 UpdateClockIdleTestTask(clock, run_count, deadline, deadline); |
| 120 } |
| 121 |
| 122 void EndIdlePeriodIdleTask(IdleHelper* idle_helper, base::TimeTicks deadline) { |
| 123 idle_helper->EndIdlePeriod(); |
| 124 } |
| 125 |
| 101 scoped_refptr<NestableSingleThreadTaskRunner> | 126 scoped_refptr<NestableSingleThreadTaskRunner> |
| 102 CreateNestableSingleThreadTaskRunner( | 127 CreateNestableSingleThreadTaskRunner( |
| 103 base::MessageLoop* message_loop, | 128 base::MessageLoop* message_loop, |
| 104 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner) { | 129 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner) { |
| 105 if (message_loop) | 130 if (message_loop) |
| 106 return SchedulerMessageLoopDelegate::Create(message_loop); | 131 return SchedulerMessageLoopDelegate::Create(message_loop); |
| 107 | 132 |
| 108 return NestableTaskRunnerForTest::Create(mock_task_runner); | 133 return NestableTaskRunnerForTest::Create(mock_task_runner); |
| 109 } | 134 } |
| 110 | 135 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 IdleHelper::IdlePeriodState::IDLE_PERIOD_STATE_COUNT, | 226 IdleHelper::IdlePeriodState::IDLE_PERIOD_STATE_COUNT, |
| 202 &IdleHelper::IdlePeriodStateToString); | 227 &IdleHelper::IdlePeriodStateToString); |
| 203 } | 228 } |
| 204 | 229 |
| 205 protected: | 230 protected: |
| 206 static base::TimeDelta maximum_idle_period_duration() { | 231 static base::TimeDelta maximum_idle_period_duration() { |
| 207 return base::TimeDelta::FromMilliseconds( | 232 return base::TimeDelta::FromMilliseconds( |
| 208 IdleHelper::kMaximumIdlePeriodMillis); | 233 IdleHelper::kMaximumIdlePeriodMillis); |
| 209 } | 234 } |
| 210 | 235 |
| 211 base::TimeTicks CurrentIdleTaskDeadlineForTesting() { | 236 static base::TimeDelta retry_enable_long_idle_period_delay() { |
| 212 base::TimeTicks deadline; | 237 return base::TimeDelta::FromMilliseconds( |
| 213 idle_helper_->CurrentIdleTaskDeadlineCallback(&deadline); | 238 IdleHelper::kRetryEnableLongIdlePeriodDelayMillis); |
| 214 return deadline; | 239 } |
| 240 |
| 241 base::TimeTicks CurrentIdleTaskDeadline() { |
| 242 return idle_helper_->CurrentIdleTaskDeadline(); |
| 243 } |
| 244 |
| 245 void CheckIdlePeriodStateIs(const char* expected) { |
| 246 IdleHelper::IdlePeriodState actual = |
| 247 idle_helper_->state_.idle_period_state(); |
| 248 EXPECT_STREQ(expected, IdleHelper::IdlePeriodStateToString(actual)); |
| 215 } | 249 } |
| 216 | 250 |
| 217 scoped_refptr<cc::TestNowSource> clock_; | 251 scoped_refptr<cc::TestNowSource> clock_; |
| 218 // Only one of mock_task_runner_ or message_loop_ will be set. | 252 // Only one of mock_task_runner_ or message_loop_ will be set. |
| 219 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; | 253 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; |
| 220 scoped_ptr<base::MessageLoop> message_loop_; | 254 scoped_ptr<base::MessageLoop> message_loop_; |
| 221 | 255 |
| 222 scoped_refptr<NestableSingleThreadTaskRunner> nestable_task_runner_; | 256 scoped_refptr<NestableSingleThreadTaskRunner> nestable_task_runner_; |
| 223 scoped_ptr<SchedulerHelper> scheduler_helper_; | 257 scoped_ptr<SchedulerHelper> scheduler_helper_; |
| 224 scoped_ptr<IdleHelperForTest> idle_helper_; | 258 scoped_ptr<IdleHelperForTest> idle_helper_; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 250 | 284 |
| 251 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(100)); | 285 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(100)); |
| 252 idle_task_runner_->PostIdleTask( | 286 idle_task_runner_->PostIdleTask( |
| 253 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 287 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 254 | 288 |
| 255 RunUntilIdle(); | 289 RunUntilIdle(); |
| 256 EXPECT_EQ(0, run_count); | 290 EXPECT_EQ(0, run_count); |
| 257 | 291 |
| 258 idle_helper_->StartIdlePeriod( | 292 idle_helper_->StartIdlePeriod( |
| 259 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 293 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 260 expected_deadline, true); | 294 expected_deadline); |
| 261 RunUntilIdle(); | 295 RunUntilIdle(); |
| 262 EXPECT_EQ(1, run_count); | 296 EXPECT_EQ(1, run_count); |
| 263 EXPECT_EQ(expected_deadline, deadline_in_task); | 297 EXPECT_EQ(expected_deadline, deadline_in_task); |
| 264 } | 298 } |
| 265 | 299 |
| 266 TEST_F(IdleHelperTest, TestPostIdleTask_EndIdlePeriod) { | 300 TEST_F(IdleHelperTest, TestPostIdleTask_EndIdlePeriod) { |
| 267 int run_count = 0; | 301 int run_count = 0; |
| 268 base::TimeTicks deadline_in_task; | 302 base::TimeTicks deadline_in_task; |
| 269 | 303 |
| 270 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(100)); | 304 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(100)); |
| 271 idle_task_runner_->PostIdleTask( | 305 idle_task_runner_->PostIdleTask( |
| 272 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 306 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 273 | 307 |
| 274 RunUntilIdle(); | 308 RunUntilIdle(); |
| 275 EXPECT_EQ(0, run_count); | 309 EXPECT_EQ(0, run_count); |
| 276 | 310 |
| 277 idle_helper_->StartIdlePeriod( | 311 idle_helper_->StartIdlePeriod( |
| 278 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 312 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 279 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 313 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 280 idle_helper_->EndIdlePeriod(); | 314 idle_helper_->EndIdlePeriod(); |
| 281 RunUntilIdle(); | 315 RunUntilIdle(); |
| 282 EXPECT_EQ(0, run_count); | 316 EXPECT_EQ(0, run_count); |
| 283 } | 317 } |
| 284 | 318 |
| 285 TEST_F(IdleHelperTest, TestRepostingIdleTask) { | 319 TEST_F(IdleHelperTest, TestRepostingIdleTask) { |
| 320 base::TimeTicks actual_deadline; |
| 286 int run_count = 0; | 321 int run_count = 0; |
| 287 | 322 |
| 288 max_idle_task_reposts = 2; | 323 max_idle_task_reposts = 2; |
| 289 idle_task_runner_->PostIdleTask( | 324 idle_task_runner_->PostIdleTask( |
| 290 FROM_HERE, | 325 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_, |
| 291 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count)); | 326 &run_count, &actual_deadline)); |
| 292 idle_helper_->StartIdlePeriod( | 327 idle_helper_->StartIdlePeriod( |
| 293 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 328 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 294 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 329 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 295 RunUntilIdle(); | 330 RunUntilIdle(); |
| 296 EXPECT_EQ(1, run_count); | 331 EXPECT_EQ(1, run_count); |
| 297 | 332 |
| 298 // Reposted tasks shouldn't run until next idle period. | 333 // Reposted tasks shouldn't run until next idle period. |
| 299 RunUntilIdle(); | 334 RunUntilIdle(); |
| 300 EXPECT_EQ(1, run_count); | 335 EXPECT_EQ(1, run_count); |
| 301 | 336 |
| 302 idle_helper_->StartIdlePeriod( | 337 idle_helper_->StartIdlePeriod( |
| 303 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 338 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 304 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 339 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 305 RunUntilIdle(); | 340 RunUntilIdle(); |
| 306 EXPECT_EQ(2, run_count); | 341 EXPECT_EQ(2, run_count); |
| 307 } | 342 } |
| 308 | 343 |
| 309 TEST_F(IdleHelperTest, TestIdleTaskExceedsDeadline) { | 344 TEST_F(IdleHelperTest, TestIdleTaskExceedsDeadline) { |
| 310 int run_count = 0; | 345 int run_count = 0; |
| 311 | 346 |
| 312 // Post two UpdateClockToDeadlineIdleTestTask tasks. | 347 // Post two UpdateClockToDeadlineIdleTestTask tasks. |
| 313 idle_task_runner_->PostIdleTask( | 348 idle_task_runner_->PostIdleTask( |
| 314 FROM_HERE, base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, | 349 FROM_HERE, |
| 315 default_task_runner_, &run_count)); | 350 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, &run_count)); |
| 316 idle_task_runner_->PostIdleTask( | 351 idle_task_runner_->PostIdleTask( |
| 317 FROM_HERE, base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, | 352 FROM_HERE, |
| 318 default_task_runner_, &run_count)); | 353 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, &run_count)); |
| 319 | 354 |
| 320 idle_helper_->StartIdlePeriod( | 355 idle_helper_->StartIdlePeriod( |
| 321 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 356 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 322 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 357 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 323 RunUntilIdle(); | 358 RunUntilIdle(); |
| 324 // Only the first idle task should execute since it's used up the deadline. | 359 // Only the first idle task should execute since it's used up the deadline. |
| 325 EXPECT_EQ(1, run_count); | 360 EXPECT_EQ(1, run_count); |
| 326 | 361 |
| 327 idle_helper_->EndIdlePeriod(); | 362 idle_helper_->EndIdlePeriod(); |
| 328 idle_helper_->StartIdlePeriod( | 363 idle_helper_->StartIdlePeriod( |
| 329 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 364 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 330 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 365 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 331 RunUntilIdle(); | 366 RunUntilIdle(); |
| 332 // Second task should be run on the next idle period. | 367 // Second task should be run on the next idle period. |
| 333 EXPECT_EQ(2, run_count); | 368 EXPECT_EQ(2, run_count); |
| 334 } | 369 } |
| 335 | 370 |
| 336 TEST_F(IdleHelperTest, TestPostIdleTaskAfterWakeup) { | 371 TEST_F(IdleHelperTest, TestPostIdleTaskAfterWakeup) { |
| 337 base::TimeTicks deadline_in_task; | 372 base::TimeTicks deadline_in_task; |
| 338 int run_count = 0; | 373 int run_count = 0; |
| 339 | 374 |
| 340 idle_task_runner_->PostIdleTaskAfterWakeup( | 375 idle_task_runner_->PostIdleTaskAfterWakeup( |
| 341 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 376 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 342 | 377 |
| 343 idle_helper_->StartIdlePeriod( | 378 idle_helper_->StartIdlePeriod( |
| 344 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 379 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 345 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 380 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 346 RunUntilIdle(); | 381 RunUntilIdle(); |
| 347 // Shouldn't run yet as no other task woke up the scheduler. | 382 // Shouldn't run yet as no other task woke up the scheduler. |
| 348 EXPECT_EQ(0, run_count); | 383 EXPECT_EQ(0, run_count); |
| 349 | 384 |
| 350 idle_task_runner_->PostIdleTaskAfterWakeup( | 385 idle_task_runner_->PostIdleTaskAfterWakeup( |
| 351 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 386 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 352 | 387 |
| 353 idle_helper_->StartIdlePeriod( | 388 idle_helper_->StartIdlePeriod( |
| 354 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 389 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 355 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 390 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 356 RunUntilIdle(); | 391 RunUntilIdle(); |
| 357 // Another after wakeup idle task shouldn't wake the scheduler. | 392 // Another after wakeup idle task shouldn't wake the scheduler. |
| 358 EXPECT_EQ(0, run_count); | 393 EXPECT_EQ(0, run_count); |
| 359 | 394 |
| 360 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | 395 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); |
| 361 | 396 |
| 362 RunUntilIdle(); | 397 RunUntilIdle(); |
| 363 // Must start a new idle period before idle task runs. | 398 // Must start a new idle period before idle task runs. |
| 364 idle_helper_->StartIdlePeriod( | 399 idle_helper_->StartIdlePeriod( |
| 365 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 400 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 366 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 401 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 367 RunUntilIdle(); | 402 RunUntilIdle(); |
| 368 // Execution of default task queue task should trigger execution of idle task. | 403 // Execution of default task queue task should trigger execution of idle task. |
| 369 EXPECT_EQ(2, run_count); | 404 EXPECT_EQ(2, run_count); |
| 370 } | 405 } |
| 371 | 406 |
| 372 TEST_F(IdleHelperTest, TestPostIdleTaskAfterWakeupWhileAwake) { | 407 TEST_F(IdleHelperTest, TestPostIdleTaskAfterWakeupWhileAwake) { |
| 373 base::TimeTicks deadline_in_task; | 408 base::TimeTicks deadline_in_task; |
| 374 int run_count = 0; | 409 int run_count = 0; |
| 375 | 410 |
| 376 idle_task_runner_->PostIdleTaskAfterWakeup( | 411 idle_task_runner_->PostIdleTaskAfterWakeup( |
| 377 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 412 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 378 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | 413 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); |
| 379 | 414 |
| 380 RunUntilIdle(); | 415 RunUntilIdle(); |
| 381 // Must start a new idle period before idle task runs. | 416 // Must start a new idle period before idle task runs. |
| 382 idle_helper_->StartIdlePeriod( | 417 idle_helper_->StartIdlePeriod( |
| 383 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 418 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 384 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 419 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 385 RunUntilIdle(); | 420 RunUntilIdle(); |
| 386 // Should run as the scheduler was already awakened by the normal task. | 421 // Should run as the scheduler was already awakened by the normal task. |
| 387 EXPECT_EQ(1, run_count); | 422 EXPECT_EQ(1, run_count); |
| 388 } | 423 } |
| 389 | 424 |
| 390 TEST_F(IdleHelperTest, TestPostIdleTaskWakesAfterWakeupIdleTask) { | 425 TEST_F(IdleHelperTest, TestPostIdleTaskWakesAfterWakeupIdleTask) { |
| 391 base::TimeTicks deadline_in_task; | 426 base::TimeTicks deadline_in_task; |
| 392 int run_count = 0; | 427 int run_count = 0; |
| 393 | 428 |
| 394 idle_task_runner_->PostIdleTaskAfterWakeup( | 429 idle_task_runner_->PostIdleTaskAfterWakeup( |
| 395 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 430 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 396 idle_task_runner_->PostIdleTask( | 431 idle_task_runner_->PostIdleTask( |
| 397 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 432 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 398 | 433 |
| 399 idle_helper_->StartIdlePeriod( | 434 idle_helper_->StartIdlePeriod( |
| 400 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 435 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 401 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 436 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 402 RunUntilIdle(); | 437 RunUntilIdle(); |
| 403 // Must start a new idle period before after-wakeup idle task runs. | 438 // Must start a new idle period before after-wakeup idle task runs. |
| 404 idle_helper_->StartIdlePeriod( | 439 idle_helper_->StartIdlePeriod( |
| 405 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 440 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 406 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 441 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 407 RunUntilIdle(); | 442 RunUntilIdle(); |
| 408 // Normal idle task should wake up after-wakeup idle task. | 443 // Normal idle task should wake up after-wakeup idle task. |
| 409 EXPECT_EQ(2, run_count); | 444 EXPECT_EQ(2, run_count); |
| 410 } | 445 } |
| 411 | 446 |
| 412 TEST_F(IdleHelperTest, TestDelayedEndIdlePeriod) { | |
| 413 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
| 414 | |
| 415 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | |
| 416 .Times(2) | |
| 417 .WillRepeatedly(Return(true)); | |
| 418 | |
| 419 // We need an idle task posted or EnableLongIdlePeriod will use the | |
| 420 // control_task_after_wakeup_runner_ instead of the control_task_runner_. | |
| 421 idle_task_runner_->PostIdleTask(FROM_HERE, base::Bind(&NullIdleTask)); | |
| 422 idle_helper_->EnableLongIdlePeriod(); | |
| 423 | |
| 424 // Check there is a pending delayed task. | |
| 425 EXPECT_GT(task_queue_manager()->NextPendingDelayedTaskRunTime(), | |
| 426 base::TimeTicks()); | |
| 427 | |
| 428 RunUntilIdle(); | |
| 429 | |
| 430 // If the delayed task ran, it will an EnableLongIdlePeriod on the control | |
| 431 // task after wake up queue. | |
| 432 EXPECT_FALSE(task_queue_manager()->IsQueueEmpty( | |
| 433 SchedulerHelper::CONTROL_TASK_AFTER_WAKEUP_QUEUE)); | |
| 434 } | |
| 435 | |
| 436 TEST_F(IdleHelperTest, TestDelayedEndIdlePeriodCanceled) { | |
| 437 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
| 438 | |
| 439 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | |
| 440 .Times(1) | |
| 441 .WillRepeatedly(Return(true)); | |
| 442 | |
| 443 // We need an idle task posted or EnableLongIdlePeriod will use the | |
| 444 // control_task_after_wakeup_runner_ instead of the control_task_runner_. | |
| 445 idle_task_runner_->PostIdleTask(FROM_HERE, base::Bind(&NullIdleTask)); | |
| 446 idle_helper_->EnableLongIdlePeriod(); | |
| 447 | |
| 448 // Check there is a pending delayed task. | |
| 449 EXPECT_GT(task_queue_manager()->NextPendingDelayedTaskRunTime(), | |
| 450 base::TimeTicks()); | |
| 451 | |
| 452 idle_helper_->EndIdlePeriod(); | |
| 453 RunUntilIdle(); | |
| 454 | |
| 455 // If the delayed task didn't run, there will be nothing on the control task | |
| 456 // after wake up queue. | |
| 457 EXPECT_TRUE(scheduler_helper_->IsQueueEmpty( | |
| 458 SchedulerHelper::CONTROL_TASK_AFTER_WAKEUP_QUEUE)); | |
| 459 } | |
| 460 | |
| 461 class IdleHelperWithMessageLoopTest : public BaseIdleHelperTest { | 447 class IdleHelperWithMessageLoopTest : public BaseIdleHelperTest { |
| 462 public: | 448 public: |
| 463 IdleHelperWithMessageLoopTest() | 449 IdleHelperWithMessageLoopTest() |
| 464 : BaseIdleHelperTest(new base::MessageLoop(), base::TimeDelta()) {} | 450 : BaseIdleHelperTest(new base::MessageLoop(), base::TimeDelta()) {} |
| 465 ~IdleHelperWithMessageLoopTest() override {} | 451 ~IdleHelperWithMessageLoopTest() override {} |
| 466 | 452 |
| 467 void PostFromNestedRunloop(std::vector< | 453 void PostFromNestedRunloop(std::vector< |
| 468 std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>* tasks) { | 454 std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>* tasks) { |
| 469 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop_.get()); | 455 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop_.get()); |
| 470 for (std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>& pair : *tasks) { | 456 for (std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>& pair : *tasks) { |
| 471 if (pair.second) { | 457 if (pair.second) { |
| 472 idle_task_runner_->PostIdleTask(FROM_HERE, pair.first); | 458 idle_task_runner_->PostIdleTask(FROM_HERE, pair.first); |
| 473 } else { | 459 } else { |
| 474 idle_task_runner_->PostNonNestableIdleTask(FROM_HERE, pair.first); | 460 idle_task_runner_->PostNonNestableIdleTask(FROM_HERE, pair.first); |
| 475 } | 461 } |
| 476 } | 462 } |
| 477 idle_helper_->StartIdlePeriod( | 463 idle_helper_->StartIdlePeriod( |
| 478 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 464 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 479 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 465 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 480 message_loop_->RunUntilIdle(); | 466 message_loop_->RunUntilIdle(); |
| 481 } | 467 } |
| 482 | 468 |
| 483 private: | 469 private: |
| 484 DISALLOW_COPY_AND_ASSIGN(IdleHelperWithMessageLoopTest); | 470 DISALLOW_COPY_AND_ASSIGN(IdleHelperWithMessageLoopTest); |
| 485 }; | 471 }; |
| 486 | 472 |
| 487 TEST_F(IdleHelperWithMessageLoopTest, | 473 TEST_F(IdleHelperWithMessageLoopTest, |
| 488 NonNestableIdleTaskDoesntExecuteInNestedLoop) { | 474 NonNestableIdleTaskDoesntExecuteInNestedLoop) { |
| 489 std::vector<std::string> order; | 475 std::vector<std::string> order; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 505 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("5")), true)); | 491 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("5")), true)); |
| 506 | 492 |
| 507 default_task_runner_->PostTask( | 493 default_task_runner_->PostTask( |
| 508 FROM_HERE, | 494 FROM_HERE, |
| 509 base::Bind(&IdleHelperWithMessageLoopTest::PostFromNestedRunloop, | 495 base::Bind(&IdleHelperWithMessageLoopTest::PostFromNestedRunloop, |
| 510 base::Unretained(this), | 496 base::Unretained(this), |
| 511 base::Unretained(&tasks_to_post_from_nested_loop))); | 497 base::Unretained(&tasks_to_post_from_nested_loop))); |
| 512 | 498 |
| 513 idle_helper_->StartIdlePeriod( | 499 idle_helper_->StartIdlePeriod( |
| 514 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 500 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 515 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 501 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 516 RunUntilIdle(); | 502 RunUntilIdle(); |
| 517 // Note we expect task 3 to run last because it's non-nestable. | 503 // Note we expect task 3 to run last because it's non-nestable. |
| 518 EXPECT_THAT(order, testing::ElementsAre(std::string("1"), std::string("2"), | 504 EXPECT_THAT(order, testing::ElementsAre(std::string("1"), std::string("2"), |
| 519 std::string("4"), std::string("5"), | 505 std::string("4"), std::string("5"), |
| 520 std::string("3"))); | 506 std::string("3"))); |
| 521 } | 507 } |
| 522 | 508 |
| 523 TEST_F(IdleHelperTest, TestLongIdlePeriod) { | 509 TEST_F(IdleHelperTest, TestLongIdlePeriod) { |
| 524 base::TimeTicks expected_deadline = | 510 base::TimeTicks expected_deadline = |
| 525 clock_->Now() + maximum_idle_period_duration(); | 511 clock_->Now() + maximum_idle_period_duration(); |
| 526 base::TimeTicks deadline_in_task; | 512 base::TimeTicks deadline_in_task; |
| 527 int run_count = 0; | 513 int run_count = 0; |
| 528 | 514 |
| 529 idle_task_runner_->PostIdleTask( | 515 idle_task_runner_->PostIdleTask( |
| 530 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 516 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 531 | 517 |
| 532 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | 518 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 533 .Times(2) | 519 .Times(1) |
| 534 .WillRepeatedly(Return(true)); | 520 .WillRepeatedly(Return(true)); |
| 535 | 521 |
| 536 RunUntilIdle(); | 522 RunUntilIdle(); |
| 537 EXPECT_EQ(0, run_count); // Shouldn't run yet as no idle period. | 523 EXPECT_EQ(0, run_count); // Shouldn't run yet as no idle period. |
| 538 | 524 |
| 539 idle_helper_->EnableLongIdlePeriod(); | 525 idle_helper_->EnableLongIdlePeriod(); |
| 540 RunUntilIdle(); | 526 RunUntilIdle(); |
| 541 EXPECT_EQ(1, run_count); // Should have run in a long idle time. | 527 EXPECT_EQ(1, run_count); // Should have run in a long idle time. |
| 542 EXPECT_EQ(expected_deadline, deadline_in_task); | 528 EXPECT_EQ(expected_deadline, deadline_in_task); |
| 543 } | 529 } |
| 544 | 530 |
| 545 TEST_F(IdleHelperTest, TestLongIdlePeriodWithPendingDelayedTask) { | 531 TEST_F(IdleHelperTest, TestLongIdlePeriodWithPendingDelayedTask) { |
| 546 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(30); | 532 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(30); |
| 547 base::TimeTicks expected_deadline = clock_->Now() + pending_task_delay; | 533 base::TimeTicks expected_deadline = clock_->Now() + pending_task_delay; |
| 548 base::TimeTicks deadline_in_task; | 534 base::TimeTicks deadline_in_task; |
| 549 int run_count = 0; | 535 int run_count = 0; |
| 550 | 536 |
| 551 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | 537 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 552 .Times(2) | 538 .Times(1) |
| 553 .WillRepeatedly(Return(true)); | 539 .WillRepeatedly(Return(true)); |
| 554 | 540 |
| 555 idle_task_runner_->PostIdleTask( | 541 idle_task_runner_->PostIdleTask( |
| 556 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 542 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 557 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), | 543 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), |
| 558 pending_task_delay); | 544 pending_task_delay); |
| 559 | 545 |
| 560 idle_helper_->EnableLongIdlePeriod(); | 546 idle_helper_->EnableLongIdlePeriod(); |
| 561 RunUntilIdle(); | 547 RunUntilIdle(); |
| 562 EXPECT_EQ(1, run_count); // Should have run in a long idle time. | 548 EXPECT_EQ(1, run_count); // Should have run in a long idle time. |
| 563 EXPECT_EQ(expected_deadline, deadline_in_task); | 549 EXPECT_EQ(expected_deadline, deadline_in_task); |
| 564 } | 550 } |
| 565 | 551 |
| 566 TEST_F(IdleHelperTest, TestLongIdlePeriodWithLatePendingDelayedTask) { | 552 TEST_F(IdleHelperTest, TestLongIdlePeriodWithLatePendingDelayedTask) { |
| 567 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(10); | 553 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(10); |
| 568 base::TimeTicks deadline_in_task; | 554 base::TimeTicks deadline_in_task; |
| 569 int run_count = 0; | 555 int run_count = 0; |
| 570 | 556 |
| 571 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | 557 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 572 .Times(3) | 558 .Times(2) |
| 573 .WillRepeatedly(Return(true)); | 559 .WillRepeatedly(Return(true)); |
| 574 | 560 |
| 575 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), | 561 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), |
| 576 pending_task_delay); | 562 pending_task_delay); |
| 577 | 563 |
| 578 // Advance clock until after delayed task was meant to be run. | 564 // Advance clock until after delayed task was meant to be run. |
| 579 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(20)); | 565 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(20)); |
| 580 | 566 |
| 581 // Post an idle task and then EnableLongIdlePeriod. Since there is a late | 567 // Post an idle task and then EnableLongIdlePeriod. Since there is a late |
| 582 // pending delayed task this shouldn't actually start an idle period. | 568 // pending delayed task this shouldn't actually start an idle period. |
| 583 idle_task_runner_->PostIdleTask( | 569 idle_task_runner_->PostIdleTask( |
| 584 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 570 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 585 idle_helper_->EnableLongIdlePeriod(); | 571 idle_helper_->EnableLongIdlePeriod(); |
| 586 RunUntilIdle(); | 572 RunUntilIdle(); |
| 587 EXPECT_EQ(0, run_count); | 573 EXPECT_EQ(0, run_count); |
| 588 | 574 |
| 589 // After the delayed task has been run we should trigger an idle period. | 575 // After the delayed task has been run we should trigger an idle period. |
| 590 clock_->AdvanceNow(maximum_idle_period_duration()); | 576 clock_->AdvanceNow(maximum_idle_period_duration()); |
| 591 RunUntilIdle(); | 577 RunUntilIdle(); |
| 592 EXPECT_EQ(1, run_count); | 578 EXPECT_EQ(1, run_count); |
| 593 } | 579 } |
| 594 | 580 |
| 595 TEST_F(IdleHelperTest, TestLongIdlePeriodRepeating) { | 581 TEST_F(IdleHelperTest, TestLongIdlePeriodRepeating) { |
| 582 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); |
| 583 std::vector<base::TimeTicks> actual_deadlines; |
| 596 int run_count = 0; | 584 int run_count = 0; |
| 597 | 585 |
| 598 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | 586 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 599 .Times(2) | 587 .Times(4) |
| 600 .WillRepeatedly(Return(true)); | 588 .WillRepeatedly(Return(true)); |
| 601 | 589 |
| 602 max_idle_task_reposts = 3; | 590 max_idle_task_reposts = 3; |
| 591 base::TimeTicks clock_before(clock_->Now()); |
| 592 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); |
| 603 idle_task_runner_->PostIdleTask( | 593 idle_task_runner_->PostIdleTask( |
| 604 FROM_HERE, | 594 FROM_HERE, |
| 605 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count)); | 595 base::Bind(&RepostingUpdateClockIdleTestTask, idle_task_runner_, |
| 596 &run_count, clock_, idle_task_runtime, &actual_deadlines)); |
| 606 | 597 |
| 598 // Check each idle task runs in their own idle period. |
| 607 idle_helper_->EnableLongIdlePeriod(); | 599 idle_helper_->EnableLongIdlePeriod(); |
| 608 RunUntilIdle(); | 600 RunUntilIdle(); |
| 609 EXPECT_EQ(1, run_count); // Should only run once per idle period. | 601 EXPECT_EQ(3, run_count); |
| 602 EXPECT_THAT( |
| 603 actual_deadlines, |
| 604 testing::ElementsAre( |
| 605 clock_before + maximum_idle_period_duration(), |
| 606 clock_before + idle_task_runtime + maximum_idle_period_duration(), |
| 607 clock_before + (2 * idle_task_runtime) + |
| 608 maximum_idle_period_duration())); |
| 610 | 609 |
| 611 // Advance time to start of next long idle period and check task reposted task | 610 max_idle_task_reposts = 5; |
| 612 // gets run. | 611 idle_task_runner_->PostIdleTask( |
| 613 clock_->AdvanceNow(maximum_idle_period_duration()); | 612 FROM_HERE, |
| 613 base::Bind(&RepostingUpdateClockIdleTestTask, idle_task_runner_, |
| 614 &run_count, clock_, idle_task_runtime, &actual_deadlines)); |
| 615 idle_task_runner_->PostIdleTask( |
| 616 FROM_HERE, |
| 617 base::Bind(&EndIdlePeriodIdleTask, base::Unretained(idle_helper_.get()))); |
| 618 |
| 619 // Ensure that reposting tasks stop after EndIdlePeriod is called. |
| 614 RunUntilIdle(); | 620 RunUntilIdle(); |
| 615 EXPECT_EQ(2, run_count); | 621 EXPECT_EQ(4, run_count); |
| 616 | |
| 617 // Advance time to start of next long idle period then end the idle period and | |
| 618 // check the task doesn't get run. | |
| 619 clock_->AdvanceNow(maximum_idle_period_duration()); | |
| 620 idle_helper_->EndIdlePeriod(); | |
| 621 RunUntilIdle(); | |
| 622 EXPECT_EQ(2, run_count); | |
| 623 } | 622 } |
| 624 | 623 |
| 625 TEST_F(IdleHelperTest, TestLongIdlePeriodDoesNotWakeScheduler) { | 624 TEST_F(IdleHelperTest, TestLongIdlePeriodDoesNotWakeScheduler) { |
| 626 base::TimeTicks deadline_in_task; | 625 base::TimeTicks deadline_in_task; |
| 627 int run_count = 0; | 626 int run_count = 0; |
| 628 | 627 |
| 629 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | 628 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 630 .Times(3) | 629 .Times(2) |
| 631 .WillRepeatedly(Return(true)); | 630 .WillRepeatedly(Return(true)); |
| 632 | 631 |
| 633 // Start a long idle period and get the time it should end. | 632 // Start a long idle period and get the time it should end. |
| 634 idle_helper_->EnableLongIdlePeriod(); | 633 idle_helper_->EnableLongIdlePeriod(); |
| 635 // The scheduler should not run the initiate_next_long_idle_period task if | 634 // The scheduler should not run the enable_next_long_idle_period task if |
| 636 // there are no idle tasks and no other task woke up the scheduler, thus | 635 // there are no idle tasks and no other task woke up the scheduler, thus |
| 637 // the idle period deadline shouldn't update at the end of the current long | 636 // the idle period deadline shouldn't update at the end of the current long |
| 638 // idle period. | 637 // idle period. |
| 639 base::TimeTicks idle_period_deadline = CurrentIdleTaskDeadlineForTesting(); | 638 base::TimeTicks idle_period_deadline = CurrentIdleTaskDeadline(); |
| 640 clock_->AdvanceNow(maximum_idle_period_duration()); | 639 clock_->AdvanceNow(maximum_idle_period_duration()); |
| 641 RunUntilIdle(); | 640 RunUntilIdle(); |
| 642 | 641 |
| 643 base::TimeTicks new_idle_period_deadline = | 642 base::TimeTicks new_idle_period_deadline = CurrentIdleTaskDeadline(); |
| 644 CurrentIdleTaskDeadlineForTesting(); | |
| 645 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); | 643 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); |
| 646 | 644 |
| 647 // Posting a after-wakeup idle task also shouldn't wake the scheduler or | 645 // Posting a after-wakeup idle task also shouldn't wake the scheduler or |
| 648 // initiate the next long idle period. | 646 // initiate the next long idle period. |
| 649 idle_task_runner_->PostIdleTaskAfterWakeup( | 647 idle_task_runner_->PostIdleTaskAfterWakeup( |
| 650 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 648 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 651 RunUntilIdle(); | 649 RunUntilIdle(); |
| 652 new_idle_period_deadline = CurrentIdleTaskDeadlineForTesting(); | 650 new_idle_period_deadline = CurrentIdleTaskDeadline(); |
| 653 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); | 651 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); |
| 654 EXPECT_EQ(0, run_count); | 652 EXPECT_EQ(0, run_count); |
| 655 | 653 |
| 656 // Running a normal task should initiate a new long idle period though. | 654 // Running a normal task should initiate a new long idle period though. |
| 657 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | 655 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); |
| 658 RunUntilIdle(); | 656 RunUntilIdle(); |
| 659 new_idle_period_deadline = CurrentIdleTaskDeadlineForTesting(); | 657 new_idle_period_deadline = CurrentIdleTaskDeadline(); |
| 660 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(), | 658 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(), |
| 661 new_idle_period_deadline); | 659 new_idle_period_deadline); |
| 662 | 660 |
| 663 EXPECT_EQ(1, run_count); | 661 EXPECT_EQ(1, run_count); |
| 664 } | 662 } |
| 665 | 663 |
| 666 TEST_F(IdleHelperTest, TestLongIdlePeriodWhenNotCanEnterLongIdlePeriod) { | 664 TEST_F(IdleHelperTest, TestLongIdlePeriodWhenNotCanEnterLongIdlePeriod) { |
| 667 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(1000); | 665 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(1000); |
| 668 base::TimeDelta halfDelay = base::TimeDelta::FromMilliseconds(500); | 666 base::TimeDelta halfDelay = base::TimeDelta::FromMilliseconds(500); |
| 669 base::TimeTicks delayOver = clock_->Now() + delay; | 667 base::TimeTicks delayOver = clock_->Now() + delay; |
| 670 base::TimeTicks deadline_in_task; | 668 base::TimeTicks deadline_in_task; |
| 671 int run_count = 0; | 669 int run_count = 0; |
| 672 | 670 |
| 673 ON_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | 671 ON_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 674 .WillByDefault(Invoke( | 672 .WillByDefault(Invoke( |
| 675 [delay, delayOver](base::TimeTicks now, | 673 [delay, delayOver](base::TimeTicks now, |
| 676 base::TimeDelta* next_long_idle_period_delay_out) { | 674 base::TimeDelta* next_long_idle_period_delay_out) { |
| 677 if (now >= delayOver) | 675 if (now >= delayOver) |
| 678 return true; | 676 return true; |
| 679 *next_long_idle_period_delay_out = delay; | 677 *next_long_idle_period_delay_out = delay; |
| 680 return false; | 678 return false; |
| 681 })); | 679 })); |
| 682 | 680 |
| 683 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)).Times(3); | 681 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)).Times(2); |
| 684 | 682 |
| 685 idle_task_runner_->PostIdleTask( | 683 idle_task_runner_->PostIdleTask( |
| 686 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 684 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 687 | 685 |
| 688 // Make sure Idle tasks don't run until the delay has occured. | 686 // Make sure Idle tasks don't run until the delay has occurred. |
| 689 idle_helper_->EnableLongIdlePeriod(); | 687 idle_helper_->EnableLongIdlePeriod(); |
| 690 RunUntilIdle(); | 688 RunUntilIdle(); |
| 691 EXPECT_EQ(0, run_count); | 689 EXPECT_EQ(0, run_count); |
| 692 | 690 |
| 693 clock_->AdvanceNow(halfDelay); | 691 clock_->AdvanceNow(halfDelay); |
| 694 RunUntilIdle(); | 692 RunUntilIdle(); |
| 695 EXPECT_EQ(0, run_count); | 693 EXPECT_EQ(0, run_count); |
| 696 | 694 |
| 697 // Delay is finished, idle task should run. | 695 // Delay is finished, idle task should run. |
| 698 clock_->AdvanceNow(halfDelay); | 696 clock_->AdvanceNow(halfDelay); |
| 699 RunUntilIdle(); | 697 RunUntilIdle(); |
| 700 EXPECT_EQ(1, run_count); | 698 EXPECT_EQ(1, run_count); |
| 701 } | 699 } |
| 702 | 700 |
| 701 TEST_F(IdleHelperTest, TestLongIdlePeriodImmediatelyRestartsIfMaxDeadline) { |
| 702 base::TimeTicks actual_deadline; |
| 703 int run_count = 0; |
| 704 |
| 705 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 706 .Times(2) |
| 707 .WillRepeatedly(Return(true)); |
| 708 |
| 709 base::TimeDelta idle_task_duration(base::TimeDelta::FromMilliseconds(10)); |
| 710 base::TimeTicks expected_deadline_1(clock_->Now() + |
| 711 maximum_idle_period_duration()); |
| 712 base::TimeTicks expected_deadline_2(clock_->Now() + idle_task_duration + |
| 713 maximum_idle_period_duration()); |
| 714 |
| 715 // The second idle period should happen immediately after the first the |
| 716 // they have max deadlines. |
| 717 max_idle_task_reposts = 2; |
| 718 idle_task_runner_->PostIdleTask( |
| 719 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_, |
| 720 &run_count, &actual_deadline)); |
| 721 idle_task_runner_->PostIdleTask( |
| 722 FROM_HERE, base::Bind(UpdateClockIdleTestTask, clock_, &run_count, |
| 723 clock_->Now() + idle_task_duration)); |
| 724 |
| 725 idle_helper_->EnableLongIdlePeriod(); |
| 726 RunUntilIdle(); |
| 727 EXPECT_EQ(2, run_count); |
| 728 EXPECT_EQ(expected_deadline_1, actual_deadline); |
| 729 |
| 730 RunUntilIdle(); |
| 731 EXPECT_EQ(3, run_count); |
| 732 EXPECT_EQ(expected_deadline_2, actual_deadline); |
| 733 } |
| 734 |
| 735 TEST_F(IdleHelperTest, TestLongIdlePeriodRestartWaitsIfNotMaxDeadline) { |
| 736 base::TimeTicks actual_deadline; |
| 737 int run_count = 0; |
| 738 |
| 739 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 740 .Times(3) |
| 741 .WillRepeatedly(Return(true)); |
| 742 |
| 743 base::TimeDelta pending_task_delay(base::TimeDelta::FromMilliseconds(20)); |
| 744 base::TimeDelta idle_task_duration(base::TimeDelta::FromMilliseconds(10)); |
| 745 base::TimeTicks expected_deadline(clock_->Now() + pending_task_delay + |
| 746 maximum_idle_period_duration() + |
| 747 retry_enable_long_idle_period_delay()); |
| 748 |
| 749 // Post delayed task to ensure idle period doesn't have a max deadline. |
| 750 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), |
| 751 pending_task_delay); |
| 752 |
| 753 max_idle_task_reposts = 2; |
| 754 idle_task_runner_->PostIdleTask( |
| 755 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_, |
| 756 &run_count, &actual_deadline)); |
| 757 idle_helper_->EnableLongIdlePeriod(); |
| 758 RunUntilIdle(); |
| 759 EXPECT_EQ(1, run_count); |
| 760 clock_->AdvanceNow(idle_task_duration); |
| 761 |
| 762 // Next idle period shouldn't happen until the pending task has been run. |
| 763 RunUntilIdle(); |
| 764 EXPECT_EQ(1, run_count); |
| 765 |
| 766 // Once the pending task is run the new idle period should start. |
| 767 clock_->AdvanceNow(pending_task_delay - idle_task_duration); |
| 768 RunUntilIdle(); |
| 769 // Since the idle period tried to start before the pending task ran we have to |
| 770 // wait for the idle helper to retry starting the long idle period. |
| 771 clock_->AdvanceNow(retry_enable_long_idle_period_delay()); |
| 772 RunUntilIdle(); |
| 773 |
| 774 EXPECT_EQ(2, run_count); |
| 775 EXPECT_EQ(expected_deadline, actual_deadline); |
| 776 } |
| 777 |
| 778 TEST_F(IdleHelperTest, TestLongIdlePeriodPaused) { |
| 779 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); |
| 780 std::vector<base::TimeTicks> actual_deadlines; |
| 781 int run_count = 0; |
| 782 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 783 .Times(3) |
| 784 .WillRepeatedly(Return(true)); |
| 785 |
| 786 // If there are no idle tasks posted we should start in the paused state. |
| 787 idle_helper_->EnableLongIdlePeriod(); |
| 788 CheckIdlePeriodStateIs("in_long_idle_period_paused"); |
| 789 // There shouldn't be any delayed tasks posted by the idle helper when paused. |
| 790 EXPECT_EQ(base::TimeTicks(), |
| 791 scheduler_helper_->NextPendingDelayedTaskRunTime()); |
| 792 |
| 793 // Posting a task should transition us to the an active state. |
| 794 max_idle_task_reposts = 2; |
| 795 base::TimeTicks clock_before(clock_->Now()); |
| 796 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); |
| 797 idle_task_runner_->PostIdleTask( |
| 798 FROM_HERE, |
| 799 base::Bind(&RepostingUpdateClockIdleTestTask, idle_task_runner_, |
| 800 &run_count, clock_, idle_task_runtime, &actual_deadlines)); |
| 801 RunUntilIdle(); |
| 802 EXPECT_EQ(2, run_count); |
| 803 EXPECT_THAT( |
| 804 actual_deadlines, |
| 805 testing::ElementsAre( |
| 806 clock_before + maximum_idle_period_duration(), |
| 807 clock_before + idle_task_runtime + maximum_idle_period_duration())); |
| 808 |
| 809 // Once all task have been run we should go back to the paused state. |
| 810 CheckIdlePeriodStateIs("in_long_idle_period_paused"); |
| 811 EXPECT_EQ(base::TimeTicks(), |
| 812 scheduler_helper_->NextPendingDelayedTaskRunTime()); |
| 813 |
| 814 idle_helper_->EndIdlePeriod(); |
| 815 CheckIdlePeriodStateIs("not_in_idle_period"); |
| 816 } |
| 817 |
| 818 TEST_F(IdleHelperTest, TestLongIdlePeriodWhenShutdown) { |
| 819 base::TimeTicks deadline_in_task; |
| 820 int run_count = 0; |
| 821 |
| 822 idle_task_runner_->PostIdleTask( |
| 823 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 824 scheduler_helper_->Shutdown(); |
| 825 |
| 826 // We shouldn't be able to enter a long idle period when shutdown |
| 827 idle_helper_->EnableLongIdlePeriod(); |
| 828 RunUntilIdle(); |
| 829 CheckIdlePeriodStateIs("not_in_idle_period"); |
| 830 EXPECT_EQ(0, run_count); |
| 831 } |
| 832 |
| 703 void TestCanExceedIdleDeadlineIfRequiredTask(IdleHelperForTest* idle_helper, | 833 void TestCanExceedIdleDeadlineIfRequiredTask(IdleHelperForTest* idle_helper, |
| 704 bool* can_exceed_idle_deadline_out, | 834 bool* can_exceed_idle_deadline_out, |
| 705 int* run_count, | 835 int* run_count, |
| 706 base::TimeTicks deadline) { | 836 base::TimeTicks deadline) { |
| 707 *can_exceed_idle_deadline_out = | 837 *can_exceed_idle_deadline_out = |
| 708 idle_helper->CanExceedIdleDeadlineIfRequired(); | 838 idle_helper->CanExceedIdleDeadlineIfRequired(); |
| 709 (*run_count)++; | 839 (*run_count)++; |
| 710 } | 840 } |
| 711 | 841 |
| 712 TEST_F(IdleHelperTest, CanExceedIdleDeadlineIfRequired) { | 842 TEST_F(IdleHelperTest, CanExceedIdleDeadlineIfRequired) { |
| 713 int run_count = 0; | 843 int run_count = 0; |
| 714 bool can_exceed_idle_deadline = false; | 844 bool can_exceed_idle_deadline = false; |
| 715 | 845 |
| 716 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | 846 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 717 .Times(3) | 847 .Times(2) |
| 718 .WillRepeatedly(Return(true)); | 848 .WillRepeatedly(Return(true)); |
| 719 | 849 |
| 720 // Should return false if not in an idle period. | 850 // Should return false if not in an idle period. |
| 721 EXPECT_FALSE(idle_helper_->CanExceedIdleDeadlineIfRequired()); | 851 EXPECT_FALSE(idle_helper_->CanExceedIdleDeadlineIfRequired()); |
| 722 | 852 |
| 723 // Should return false for short idle periods. | 853 // Should return false for short idle periods. |
| 724 idle_task_runner_->PostIdleTask( | 854 idle_task_runner_->PostIdleTask( |
| 725 FROM_HERE, | 855 FROM_HERE, |
| 726 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, idle_helper_.get(), | 856 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, idle_helper_.get(), |
| 727 &can_exceed_idle_deadline, &run_count)); | 857 &can_exceed_idle_deadline, &run_count)); |
| 728 idle_helper_->StartIdlePeriod( | 858 idle_helper_->StartIdlePeriod( |
| 729 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), | 859 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->Now(), |
| 730 clock_->Now() + base::TimeDelta::FromMilliseconds(10), true); | 860 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 731 RunUntilIdle(); | 861 RunUntilIdle(); |
| 732 EXPECT_EQ(1, run_count); | 862 EXPECT_EQ(1, run_count); |
| 733 EXPECT_FALSE(can_exceed_idle_deadline); | 863 EXPECT_FALSE(can_exceed_idle_deadline); |
| 734 | 864 |
| 735 // Should return false for a long idle period which is shortened due to a | 865 // Should return false for a long idle period which is shortened due to a |
| 736 // pending delayed task. | 866 // pending delayed task. |
| 737 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), | 867 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), |
| 738 base::TimeDelta::FromMilliseconds(10)); | 868 base::TimeDelta::FromMilliseconds(10)); |
| 739 idle_task_runner_->PostIdleTask( | 869 idle_task_runner_->PostIdleTask( |
| 740 FROM_HERE, | 870 FROM_HERE, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 777 RunUntilIdle(); | 907 RunUntilIdle(); |
| 778 } | 908 } |
| 779 | 909 |
| 780 private: | 910 private: |
| 781 DISALLOW_COPY_AND_ASSIGN(IdleHelperWithQuiescencePeriodTest); | 911 DISALLOW_COPY_AND_ASSIGN(IdleHelperWithQuiescencePeriodTest); |
| 782 }; | 912 }; |
| 783 | 913 |
| 784 TEST_F(IdleHelperWithQuiescencePeriodTest, | 914 TEST_F(IdleHelperWithQuiescencePeriodTest, |
| 785 LongIdlePeriodStartsImmediatelyIfQuiescent) { | 915 LongIdlePeriodStartsImmediatelyIfQuiescent) { |
| 786 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | 916 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 787 .Times(2) | 917 .Times(1) |
| 788 .WillRepeatedly(Return(true)); | 918 .WillRepeatedly(Return(true)); |
| 789 | 919 |
| 920 base::TimeTicks actual_deadline; |
| 790 int run_count = 0; | 921 int run_count = 0; |
| 791 max_idle_task_reposts = 1; | 922 max_idle_task_reposts = 1; |
| 792 idle_task_runner_->PostIdleTask( | 923 idle_task_runner_->PostIdleTask( |
| 793 FROM_HERE, | 924 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_, |
| 794 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count)); | 925 &run_count, &actual_deadline)); |
| 795 | 926 |
| 796 idle_helper_->EnableLongIdlePeriod(); | 927 idle_helper_->EnableLongIdlePeriod(); |
| 797 RunUntilIdle(); | 928 RunUntilIdle(); |
| 798 | 929 |
| 799 EXPECT_EQ(1, run_count); | 930 EXPECT_EQ(1, run_count); |
| 800 } | 931 } |
| 801 | 932 |
| 802 TEST_F(IdleHelperWithQuiescencePeriodTest, | 933 TEST_F(IdleHelperWithQuiescencePeriodTest, |
| 803 LongIdlePeriodDoesNotStartsImmediatelyIfBusy) { | 934 LongIdlePeriodDoesNotStartsImmediatelyIfBusy) { |
| 804 MakeNonQuiescent(); | 935 MakeNonQuiescent(); |
| 805 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)).Times(0); | 936 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)).Times(0); |
| 806 | 937 |
| 938 base::TimeTicks actual_deadline; |
| 807 int run_count = 0; | 939 int run_count = 0; |
| 808 max_idle_task_reposts = 1; | 940 max_idle_task_reposts = 1; |
| 809 idle_task_runner_->PostIdleTask( | 941 idle_task_runner_->PostIdleTask( |
| 810 FROM_HERE, | 942 FROM_HERE, base::Bind(&RepostingIdleTestTask, idle_task_runner_, |
| 811 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count)); | 943 &run_count, &actual_deadline)); |
| 812 | 944 |
| 813 idle_helper_->EnableLongIdlePeriod(); | 945 idle_helper_->EnableLongIdlePeriod(); |
| 814 RunUntilIdle(); | 946 RunUntilIdle(); |
| 815 | 947 |
| 816 EXPECT_EQ(0, run_count); | 948 EXPECT_EQ(0, run_count); |
| 817 | 949 |
| 818 scheduler_helper_->Shutdown(); | 950 scheduler_helper_->Shutdown(); |
| 819 } | 951 } |
| 820 | 952 |
| 821 TEST_F(IdleHelperWithQuiescencePeriodTest, | 953 TEST_F(IdleHelperWithQuiescencePeriodTest, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 852 QuescienceCheckedForAfterLongIdlePeriodEnds) { | 984 QuescienceCheckedForAfterLongIdlePeriodEnds) { |
| 853 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) | 985 EXPECT_CALL(*idle_helper_, CanEnterLongIdlePeriod(_, _)) |
| 854 .Times(AnyNumber()) | 986 .Times(AnyNumber()) |
| 855 .WillRepeatedly(Return(true)); | 987 .WillRepeatedly(Return(true)); |
| 856 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | 988 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); |
| 857 | 989 |
| 858 idle_task_runner_->PostIdleTask(FROM_HERE, base::Bind(&NullIdleTask)); | 990 idle_task_runner_->PostIdleTask(FROM_HERE, base::Bind(&NullIdleTask)); |
| 859 idle_helper_->EnableLongIdlePeriod(); | 991 idle_helper_->EnableLongIdlePeriod(); |
| 860 RunUntilIdle(); | 992 RunUntilIdle(); |
| 861 | 993 |
| 994 // Post a normal task to make the scheduler non-quiescent. |
| 995 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); |
| 996 RunUntilIdle(); |
| 997 |
| 998 // Post an idle task. The idle task won't run initially because the system is |
| 999 // not judged to be quiescent, but should be run after the quiescence delay. |
| 862 int run_count = 0; | 1000 int run_count = 0; |
| 863 base::TimeTicks deadline_in_task; | 1001 base::TimeTicks deadline_in_task; |
| 864 // Post an idle task, it won't run initially because the | |
| 865 // EnableLongIdlePeriod task was posted on an after wakeup runner. | |
| 866 idle_task_runner_->PostIdleTask( | |
| 867 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | |
| 868 RunUntilIdle(); | |
| 869 EXPECT_EQ(0, run_count); | |
| 870 | |
| 871 // Post a normal task to wake the system up. The idle task won't run | |
| 872 // immediately because the system isn't judged to be quiescent until the | |
| 873 // second time EnableLongIdlePeriod is run. | |
| 874 base::TimeTicks expected_deadline = | 1002 base::TimeTicks expected_deadline = |
| 875 clock_->Now() + | 1003 clock_->Now() + |
| 876 base::TimeDelta::FromMilliseconds(kQuiescenceDelayMs + kLongIdlePeriodMs); | 1004 base::TimeDelta::FromMilliseconds(kQuiescenceDelayMs + kLongIdlePeriodMs); |
| 877 default_task_runner_->PostTask(FROM_HERE, base::Bind(NullTask)); | 1005 idle_task_runner_->PostIdleTask( |
| 1006 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 1007 idle_helper_->EnableLongIdlePeriod(); |
| 878 RunUntilIdle(); | 1008 RunUntilIdle(); |
| 879 | 1009 |
| 880 EXPECT_EQ(1, run_count); | 1010 EXPECT_EQ(1, run_count); |
| 881 EXPECT_EQ(expected_deadline, deadline_in_task); | 1011 EXPECT_EQ(expected_deadline, deadline_in_task); |
| 882 } | 1012 } |
| 883 | 1013 |
| 884 } // namespace scheduler | 1014 } // namespace scheduler |
| OLD | NEW |