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