| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This class defines tests that implementations of SequencedTaskRunner should | 5 // This class defines tests that implementations of SequencedTaskRunner should |
| 6 // pass in order to be conformant. See task_runner_test_template.h for a | 6 // pass in order to be conformant. See task_runner_test_template.h for a |
| 7 // description of how to use the constructs in this file; these work the same. | 7 // description of how to use the constructs in this file; these work the same. |
| 8 | 8 |
| 9 #ifndef BASE_SEQUENCED_TASK_RUNNER_TEST_TEMPLATE_H_ | 9 #ifndef BASE_SEQUENCED_TASK_RUNNER_TEST_TEMPLATE_H_ |
| 10 #define BASE_SEQUENCED_TASK_RUNNER_TEST_TEMPLATE_H_ | 10 #define BASE_SEQUENCED_TASK_RUNNER_TEST_TEMPLATE_H_ |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 this->delegate_.StopTaskRunner(); | 171 this->delegate_.StopTaskRunner(); |
| 172 | 172 |
| 173 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), | 173 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), |
| 174 kTaskCount)); | 174 kTaskCount)); |
| 175 } | 175 } |
| 176 | 176 |
| 177 // This test posts non-nestable tasks in order of increasing delay, and checks | 177 // This test posts non-nestable tasks in order of increasing delay, and checks |
| 178 // that that the tasks are run in FIFO order and that there is no execution | 178 // that that the tasks are run in FIFO order and that there is no execution |
| 179 // overlap whatsoever between any two tasks. | 179 // overlap whatsoever between any two tasks. |
| 180 TYPED_TEST_P(SequencedTaskRunnerTest, SequentialDelayedNonNestable) { | 180 TYPED_TEST_P(SequencedTaskRunnerTest, SequentialDelayedNonNestable) { |
| 181 // TODO(akalin): Remove this check (http://crbug.com/149144). | |
| 182 if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { | |
| 183 DLOG(INFO) << "This SequencedTaskRunner doesn't handle " | |
| 184 "non-zero delays; skipping"; | |
| 185 return; | |
| 186 } | |
| 187 | |
| 188 const int kTaskCount = 20; | 181 const int kTaskCount = 20; |
| 189 const int kDelayIncrementMs = 50; | 182 const int kDelayIncrementMs = 50; |
| 190 | 183 |
| 191 this->delegate_.StartTaskRunner(); | 184 this->delegate_.StartTaskRunner(); |
| 192 const scoped_refptr<SequencedTaskRunner> task_runner = | 185 const scoped_refptr<SequencedTaskRunner> task_runner = |
| 193 this->delegate_.GetTaskRunner(); | 186 this->delegate_.GetTaskRunner(); |
| 194 | 187 |
| 195 for (int i = 0; i < kTaskCount; ++i) { | 188 for (int i = 0; i < kTaskCount; ++i) { |
| 196 this->task_tracker_->PostWrappedDelayedNonNestableTask( | 189 this->task_tracker_->PostWrappedDelayedNonNestableTask( |
| 197 task_runner, | 190 task_runner, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 this->delegate_.StopTaskRunner(); | 222 this->delegate_.StopTaskRunner(); |
| 230 | 223 |
| 231 EXPECT_TRUE(CheckNonNestableInvariants( | 224 EXPECT_TRUE(CheckNonNestableInvariants( |
| 232 this->task_tracker_->GetTaskEvents(), | 225 this->task_tracker_->GetTaskEvents(), |
| 233 kParentCount * (kChildrenPerParent + 1))); | 226 kParentCount * (kChildrenPerParent + 1))); |
| 234 } | 227 } |
| 235 | 228 |
| 236 // This test posts a delayed task, and checks that the task is run later than | 229 // This test posts a delayed task, and checks that the task is run later than |
| 237 // the specified time. | 230 // the specified time. |
| 238 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskBasic) { | 231 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskBasic) { |
| 239 // TODO(akalin): Remove this check (http://crbug.com/149144). | |
| 240 if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { | |
| 241 DLOG(INFO) << "This SequencedTaskRunner doesn't handle " | |
| 242 "non-zero delays; skipping"; | |
| 243 return; | |
| 244 } | |
| 245 | |
| 246 const int kTaskCount = 1; | 232 const int kTaskCount = 1; |
| 247 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); | 233 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); |
| 248 | 234 |
| 249 this->delegate_.StartTaskRunner(); | 235 this->delegate_.StartTaskRunner(); |
| 250 const scoped_refptr<SequencedTaskRunner> task_runner = | 236 const scoped_refptr<SequencedTaskRunner> task_runner = |
| 251 this->delegate_.GetTaskRunner(); | 237 this->delegate_.GetTaskRunner(); |
| 252 | 238 |
| 253 Time time_before_run = Time::Now(); | 239 Time time_before_run = Time::Now(); |
| 254 this->task_tracker_->PostWrappedDelayedNonNestableTask( | 240 this->task_tracker_->PostWrappedDelayedNonNestableTask( |
| 255 task_runner, Closure(), kDelay); | 241 task_runner, Closure(), kDelay); |
| 256 this->task_tracker_->WaitForCompletedTasks(kTaskCount); | 242 this->task_tracker_->WaitForCompletedTasks(kTaskCount); |
| 257 this->delegate_.StopTaskRunner(); | 243 this->delegate_.StopTaskRunner(); |
| 258 Time time_after_run = Time::Now(); | 244 Time time_after_run = Time::Now(); |
| 259 | 245 |
| 260 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), | 246 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), |
| 261 kTaskCount)); | 247 kTaskCount)); |
| 262 EXPECT_LE(kDelay, time_after_run - time_before_run); | 248 EXPECT_LE(kDelay, time_after_run - time_before_run); |
| 263 } | 249 } |
| 264 | 250 |
| 265 // This test posts two tasks with the same delay, and checks that the tasks are | 251 // This test posts two tasks with the same delay, and checks that the tasks are |
| 266 // run in the order in which they were posted. | 252 // run in the order in which they were posted. |
| 267 // | 253 // |
| 268 // NOTE: This is actually an approximate test since the API only takes a | 254 // NOTE: This is actually an approximate test since the API only takes a |
| 269 // "delay" parameter, so we are not exactly simulating two tasks that get | 255 // "delay" parameter, so we are not exactly simulating two tasks that get |
| 270 // posted at the exact same time. It would be nice if the API allowed us to | 256 // posted at the exact same time. It would be nice if the API allowed us to |
| 271 // specify the desired run time. | 257 // specify the desired run time. |
| 272 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTasksSameDelay) { | 258 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTasksSameDelay) { |
| 273 // TODO(akalin): Remove this check (http://crbug.com/149144). | |
| 274 if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { | |
| 275 DLOG(INFO) << "This SequencedTaskRunner doesn't handle " | |
| 276 "non-zero delays; skipping"; | |
| 277 return; | |
| 278 } | |
| 279 | |
| 280 const int kTaskCount = 2; | 259 const int kTaskCount = 2; |
| 281 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); | 260 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); |
| 282 | 261 |
| 283 this->delegate_.StartTaskRunner(); | 262 this->delegate_.StartTaskRunner(); |
| 284 const scoped_refptr<SequencedTaskRunner> task_runner = | 263 const scoped_refptr<SequencedTaskRunner> task_runner = |
| 285 this->delegate_.GetTaskRunner(); | 264 this->delegate_.GetTaskRunner(); |
| 286 | 265 |
| 287 this->task_tracker_->PostWrappedDelayedNonNestableTask( | 266 this->task_tracker_->PostWrappedDelayedNonNestableTask( |
| 288 task_runner, Closure(), kDelay); | 267 task_runner, Closure(), kDelay); |
| 289 this->task_tracker_->PostWrappedDelayedNonNestableTask( | 268 this->task_tracker_->PostWrappedDelayedNonNestableTask( |
| 290 task_runner, Closure(), kDelay); | 269 task_runner, Closure(), kDelay); |
| 291 this->task_tracker_->WaitForCompletedTasks(kTaskCount); | 270 this->task_tracker_->WaitForCompletedTasks(kTaskCount); |
| 292 this->delegate_.StopTaskRunner(); | 271 this->delegate_.StopTaskRunner(); |
| 293 | 272 |
| 294 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), | 273 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), |
| 295 kTaskCount)); | 274 kTaskCount)); |
| 296 } | 275 } |
| 297 | 276 |
| 298 // This test posts a normal task and a delayed task, and checks that the | 277 // This test posts a normal task and a delayed task, and checks that the |
| 299 // delayed task runs after the normal task even if the normal task takes | 278 // delayed task runs after the normal task even if the normal task takes |
| 300 // a long time to run. | 279 // a long time to run. |
| 301 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskAfterLongTask) { | 280 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskAfterLongTask) { |
| 302 // TODO(akalin): Remove this check (http://crbug.com/149144). | |
| 303 if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { | |
| 304 DLOG(INFO) << "This SequencedTaskRunner doesn't handle " | |
| 305 "non-zero delays; skipping"; | |
| 306 return; | |
| 307 } | |
| 308 | |
| 309 const int kTaskCount = 2; | 281 const int kTaskCount = 2; |
| 310 | 282 |
| 311 this->delegate_.StartTaskRunner(); | 283 this->delegate_.StartTaskRunner(); |
| 312 const scoped_refptr<SequencedTaskRunner> task_runner = | 284 const scoped_refptr<SequencedTaskRunner> task_runner = |
| 313 this->delegate_.GetTaskRunner(); | 285 this->delegate_.GetTaskRunner(); |
| 314 | 286 |
| 315 this->task_tracker_->PostWrappedNonNestableTask( | 287 this->task_tracker_->PostWrappedNonNestableTask( |
| 316 task_runner, base::Bind(&PlatformThread::Sleep, | 288 task_runner, base::Bind(&PlatformThread::Sleep, |
| 317 TimeDelta::FromMilliseconds(50))); | 289 TimeDelta::FromMilliseconds(50))); |
| 318 this->task_tracker_->PostWrappedDelayedNonNestableTask( | 290 this->task_tracker_->PostWrappedDelayedNonNestableTask( |
| 319 task_runner, Closure(), TimeDelta::FromMilliseconds(10)); | 291 task_runner, Closure(), TimeDelta::FromMilliseconds(10)); |
| 320 this->task_tracker_->WaitForCompletedTasks(kTaskCount); | 292 this->task_tracker_->WaitForCompletedTasks(kTaskCount); |
| 321 this->delegate_.StopTaskRunner(); | 293 this->delegate_.StopTaskRunner(); |
| 322 | 294 |
| 323 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), | 295 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), |
| 324 kTaskCount)); | 296 kTaskCount)); |
| 325 } | 297 } |
| 326 | 298 |
| 327 // Test that a pile of normal tasks and a delayed task run in the | 299 // Test that a pile of normal tasks and a delayed task run in the |
| 328 // time-to-run order. | 300 // time-to-run order. |
| 329 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskAfterManyLongTasks) { | 301 TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskAfterManyLongTasks) { |
| 330 // TODO(akalin): Remove this check (http://crbug.com/149144). | |
| 331 if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { | |
| 332 DLOG(INFO) << "This SequencedTaskRunner doesn't handle " | |
| 333 "non-zero delays; skipping"; | |
| 334 return; | |
| 335 } | |
| 336 | |
| 337 const int kTaskCount = 11; | 302 const int kTaskCount = 11; |
| 338 | 303 |
| 339 this->delegate_.StartTaskRunner(); | 304 this->delegate_.StartTaskRunner(); |
| 340 const scoped_refptr<SequencedTaskRunner> task_runner = | 305 const scoped_refptr<SequencedTaskRunner> task_runner = |
| 341 this->delegate_.GetTaskRunner(); | 306 this->delegate_.GetTaskRunner(); |
| 342 | 307 |
| 343 for (int i = 0; i < kTaskCount - 1; i++) { | 308 for (int i = 0; i < kTaskCount - 1; i++) { |
| 344 this->task_tracker_->PostWrappedNonNestableTask( | 309 this->task_tracker_->PostWrappedNonNestableTask( |
| 345 task_runner, base::Bind(&PlatformThread::Sleep, | 310 task_runner, base::Bind(&PlatformThread::Sleep, |
| 346 TimeDelta::FromMilliseconds(50))); | 311 TimeDelta::FromMilliseconds(50))); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 367 SequentialDelayedNonNestable, | 332 SequentialDelayedNonNestable, |
| 368 NonNestablePostFromNonNestableTask, | 333 NonNestablePostFromNonNestableTask, |
| 369 DelayedTaskBasic, | 334 DelayedTaskBasic, |
| 370 DelayedTasksSameDelay, | 335 DelayedTasksSameDelay, |
| 371 DelayedTaskAfterLongTask, | 336 DelayedTaskAfterLongTask, |
| 372 DelayedTaskAfterManyLongTasks); | 337 DelayedTaskAfterManyLongTasks); |
| 373 | 338 |
| 374 } // namespace base | 339 } // namespace base |
| 375 | 340 |
| 376 #endif // BASE_TASK_RUNNER_TEST_TEMPLATE_H_ | 341 #endif // BASE_TASK_RUNNER_TEST_TEMPLATE_H_ |
| OLD | NEW |