diff options
author | nhiroki@chromium.org <nhiroki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-02 08:31:57 +0000 |
---|---|---|
committer | nhiroki@chromium.org <nhiroki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-02 08:31:57 +0000 |
commit | 113d2ac3da59a7a7635b0e0b287a938f11559c5b (patch) | |
tree | ab7656f3473aaee0381ef27b4df01089f95bec5e /base/test | |
parent | 3b9c4bdeeab22b88fc8fd5a71c2728939c7e17d9 (diff) | |
download | chromium_src-113d2ac3da59a7a7635b0e0b287a938f11559c5b.zip chromium_src-113d2ac3da59a7a7635b0e0b287a938f11559c5b.tar.gz chromium_src-113d2ac3da59a7a7635b0e0b287a938f11559c5b.tar.bz2 |
- Implement delayed task posting for SequencedWorkerPool.
- Copy unit tests from MessageLoop.
BUG=119657
TEST=base_unittests
Review URL: https://chromiumcodereview.appspot.com/10828299
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@159657 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/test')
-rw-r--r-- | base/test/sequenced_task_runner_test_template.cc | 4 | ||||
-rw-r--r-- | base/test/sequenced_task_runner_test_template.h | 163 |
2 files changed, 142 insertions, 25 deletions
diff --git a/base/test/sequenced_task_runner_test_template.cc b/base/test/sequenced_task_runner_test_template.cc index 3b58973..50487d2 100644 --- a/base/test/sequenced_task_runner_test_template.cc +++ b/base/test/sequenced_task_runner_test_template.cc @@ -101,10 +101,6 @@ void PrintTo(const TaskEvent& event, std::ostream* os) { *os << ")"; } -void SleepForOneSecond() { - base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); -} - namespace { // Returns the task ordinals for the task event type |type| in the order that diff --git a/base/test/sequenced_task_runner_test_template.h b/base/test/sequenced_task_runner_test_template.h index eb5aa08..0c8d135 100644 --- a/base/test/sequenced_task_runner_test_template.h +++ b/base/test/sequenced_task_runner_test_template.h @@ -94,8 +94,6 @@ class SequencedTaskTracker : public RefCountedThreadSafe<SequencedTaskTracker> { void PrintTo(const TaskEvent& event, std::ostream* os); -void SleepForOneSecond(); - // Checks the non-nestable task invariants for all tasks in |events|. // // The invariants are: @@ -127,29 +125,29 @@ TYPED_TEST_CASE_P(SequencedTaskRunnerTest); // overlapping. I.e. that each task starts only after the previously-posted // one has finished. TYPED_TEST_P(SequencedTaskRunnerTest, SequentialNonNestable) { - const int task_count = 1000; + const int kTaskCount = 1000; this->delegate_.StartTaskRunner(); const scoped_refptr<SequencedTaskRunner> task_runner = this->delegate_.GetTaskRunner(); this->task_tracker_->PostWrappedNonNestableTask( - task_runner, Bind(&internal::SleepForOneSecond)); - for (int i = 1; i < task_count; ++i) { + task_runner, Bind(&PlatformThread::Sleep, TimeDelta::FromSeconds(1))); + for (int i = 1; i < kTaskCount; ++i) { this->task_tracker_->PostWrappedNonNestableTask(task_runner, Closure()); } this->delegate_.StopTaskRunner(); EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), - task_count)); + kTaskCount)); } // This test posts N nestable tasks in sequence. It has the same expectations // as SequentialNonNestable because even though the tasks are nestable, they // will not be run nestedly in this case. TYPED_TEST_P(SequencedTaskRunnerTest, SequentialNestable) { - const int task_count = 1000; + const int kTaskCount = 1000; this->delegate_.StartTaskRunner(); const scoped_refptr<SequencedTaskRunner> task_runner = @@ -157,64 +155,65 @@ TYPED_TEST_P(SequencedTaskRunnerTest, SequentialNestable) { this->task_tracker_->PostWrappedNestableTask( task_runner, - Bind(&internal::SleepForOneSecond)); - for (int i = 1; i < task_count; ++i) { + Bind(&PlatformThread::Sleep, TimeDelta::FromSeconds(1))); + for (int i = 1; i < kTaskCount; ++i) { this->task_tracker_->PostWrappedNestableTask(task_runner, Closure()); } this->delegate_.StopTaskRunner(); EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), - task_count)); + kTaskCount)); } // This test posts non-nestable tasks in order of increasing delay, and checks // that that the tasks are run in FIFO order and that there is no execution // overlap whatsoever between any two tasks. TYPED_TEST_P(SequencedTaskRunnerTest, SequentialDelayedNonNestable) { + // TODO(akalin): Remove this check (http://crbug.com/149144). if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { DLOG(INFO) << "This SequencedTaskRunner doesn't handle " "non-zero delays; skipping"; return; } - const int task_count = 20; - const int delay_increment_ms = 50; + const int kTaskCount = 20; + const int kDelayIncrementMs = 50; this->delegate_.StartTaskRunner(); const scoped_refptr<SequencedTaskRunner> task_runner = this->delegate_.GetTaskRunner(); - for (int i = 0; i < task_count; ++i) { + for (int i = 0; i < kTaskCount; ++i) { this->task_tracker_->PostWrappedDelayedNonNestableTask( task_runner, Closure(), - TimeDelta::FromMilliseconds(delay_increment_ms * i)); + TimeDelta::FromMilliseconds(kDelayIncrementMs * i)); } this->delegate_.StopTaskRunner(); EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), - task_count)); + kTaskCount)); } // This test posts a fast, non-nestable task from within each of a number of // slow, non-nestable tasks and checks that they all run in the sequence they // were posted in and that there is no execution overlap whatsoever. TYPED_TEST_P(SequencedTaskRunnerTest, NonNestablePostFromNonNestableTask) { - const int parent_count = 10; - const int children_per_parent = 10; + const int kParentCount = 10; + const int kChildrenPerParent = 10; this->delegate_.StartTaskRunner(); const scoped_refptr<SequencedTaskRunner> task_runner = this->delegate_.GetTaskRunner(); - for (int i = 0; i < parent_count; ++i) { + for (int i = 0; i < kParentCount; ++i) { Closure task = Bind( &internal::SequencedTaskTracker::PostNonNestableTasks, this->task_tracker_, task_runner, - children_per_parent); + kChildrenPerParent); this->task_tracker_->PostWrappedNonNestableTask(task_runner, task); } @@ -222,9 +221,127 @@ TYPED_TEST_P(SequencedTaskRunnerTest, NonNestablePostFromNonNestableTask) { EXPECT_TRUE(CheckNonNestableInvariants( this->task_tracker_->GetTaskEvents(), - parent_count * (children_per_parent + 1))); + kParentCount * (kChildrenPerParent + 1))); +} + +// This test posts a delayed task, and checks that the task is run later than +// the specified time. +TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskBasic) { + // TODO(akalin): Remove this check (http://crbug.com/149144). + if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { + DLOG(INFO) << "This SequencedTaskRunner doesn't handle " + "non-zero delays; skipping"; + return; + } + + const int kTaskCount = 1; + const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); + + this->delegate_.StartTaskRunner(); + const scoped_refptr<SequencedTaskRunner> task_runner = + this->delegate_.GetTaskRunner(); + + Time time_before_run = Time::Now(); + this->task_tracker_->PostWrappedDelayedNonNestableTask( + task_runner, Closure(), kDelay); + this->delegate_.StopTaskRunner(); + Time time_after_run = Time::Now(); + + EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), + kTaskCount)); + EXPECT_LE(kDelay, time_after_run - time_before_run); } +// This test posts two tasks with the same delay, and checks that the tasks are +// run in the order in which they were posted. +// +// NOTE: This is actually an approximate test since the API only takes a +// "delay" parameter, so we are not exactly simulating two tasks that get +// posted at the exact same time. It would be nice if the API allowed us to +// specify the desired run time. +TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTasksSameDelay) { + // TODO(akalin): Remove this check (http://crbug.com/149144). + if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { + DLOG(INFO) << "This SequencedTaskRunner doesn't handle " + "non-zero delays; skipping"; + return; + } + + const int kTaskCount = 2; + const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); + + this->delegate_.StartTaskRunner(); + const scoped_refptr<SequencedTaskRunner> task_runner = + this->delegate_.GetTaskRunner(); + + this->task_tracker_->PostWrappedDelayedNonNestableTask( + task_runner, Closure(), kDelay); + this->task_tracker_->PostWrappedDelayedNonNestableTask( + task_runner, Closure(), kDelay); + this->delegate_.StopTaskRunner(); + + EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), + kTaskCount)); +} + +// This test posts a normal task and a delayed task, and checks that the +// delayed task runs after the normal task even if the normal task takes +// a long time to run. +TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskAfterLongTask) { + // TODO(akalin): Remove this check (http://crbug.com/149144). + if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { + DLOG(INFO) << "This SequencedTaskRunner doesn't handle " + "non-zero delays; skipping"; + return; + } + + const int kTaskCount = 2; + + this->delegate_.StartTaskRunner(); + const scoped_refptr<SequencedTaskRunner> task_runner = + this->delegate_.GetTaskRunner(); + + this->task_tracker_->PostWrappedNonNestableTask( + task_runner, base::Bind(&PlatformThread::Sleep, + TimeDelta::FromMilliseconds(50))); + this->task_tracker_->PostWrappedDelayedNonNestableTask( + task_runner, Closure(), TimeDelta::FromMilliseconds(10)); + this->delegate_.StopTaskRunner(); + + EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), + kTaskCount)); +} + +// Test that a pile of normal tasks and a delayed task run in the +// time-to-run order. +TYPED_TEST_P(SequencedTaskRunnerTest, DelayedTaskAfterManyLongTasks) { + // TODO(akalin): Remove this check (http://crbug.com/149144). + if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { + DLOG(INFO) << "This SequencedTaskRunner doesn't handle " + "non-zero delays; skipping"; + return; + } + + const int kTaskCount = 11; + + this->delegate_.StartTaskRunner(); + const scoped_refptr<SequencedTaskRunner> task_runner = + this->delegate_.GetTaskRunner(); + + for (int i = 0; i < kTaskCount - 1; i++) { + this->task_tracker_->PostWrappedNonNestableTask( + task_runner, base::Bind(&PlatformThread::Sleep, + TimeDelta::FromMilliseconds(50))); + } + this->task_tracker_->PostWrappedDelayedNonNestableTask( + task_runner, Closure(), TimeDelta::FromMilliseconds(10)); + this->delegate_.StopTaskRunner(); + + EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_->GetTaskEvents(), + kTaskCount)); +} + + // TODO(francoisk777@gmail.com) Add a test, similiar to the above, which runs // some tasked nestedly (which should be implemented in the test // delegate). Also add, to the the test delegate, a predicate which checks @@ -235,7 +352,11 @@ REGISTER_TYPED_TEST_CASE_P(SequencedTaskRunnerTest, SequentialNonNestable, SequentialNestable, SequentialDelayedNonNestable, - NonNestablePostFromNonNestableTask); + NonNestablePostFromNonNestableTask, + DelayedTaskBasic, + DelayedTasksSameDelay, + DelayedTaskAfterLongTask, + DelayedTaskAfterManyLongTasks); } // namespace base |