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
3 // found in the LICENSE file.
5 // This class defines tests that implementations of TaskRunner should
6 // pass in order to be conformant. Here's how you use it to test your
9 // Say your class is called MyTaskRunner. Then you need to define a
10 // class called MyTaskRunnerTestDelegate in my_task_runner_unittest.cc
13 // class MyTaskRunnerTestDelegate {
15 // // Tasks posted to the task runner after this and before
16 // // StopTaskRunner() is called is called should run successfully.
17 // void StartTaskRunner() {
21 // // Should return the task runner implementation. Only called
22 // // after StartTaskRunner and before StopTaskRunner.
23 // scoped_refptr<MyTaskRunner> GetTaskRunner() {
27 // // Stop the task runner and make sure all tasks posted before
28 // // this is called are run. Caveat: delayed tasks are not run,
29 // they're simply deleted.
30 // void StopTaskRunner() {
34 // // Returns whether or not the task runner obeys non-zero delays.
35 // bool TaskRunnerHandlesNonZeroDelays() const {
40 // The TaskRunnerTest test harness will have a member variable of
41 // this delegate type and will call its functions in the various
44 // Then you simply #include this file as well as gtest.h and add the
45 // following statement to my_task_runner_unittest.cc:
47 // INSTANTIATE_TYPED_TEST_CASE_P(
48 // MyTaskRunner, TaskRunnerTest, MyTaskRunnerTestDelegate);
52 #ifndef BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
53 #define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
58 #include "base/basictypes.h"
59 #include "base/bind.h"
60 #include "base/callback.h"
61 #include "base/memory/ref_counted.h"
62 #include "base/synchronization/condition_variable.h"
63 #include "base/synchronization/lock.h"
64 #include "base/task_runner.h"
65 #include "base/threading/thread.h"
66 #include "base/tracked_objects.h"
67 #include "testing/gtest/include/gtest/gtest.h"
73 // Utility class that keeps track of how many times particular tasks
75 class TaskTracker : public RefCountedThreadSafe<TaskTracker> {
79 // Returns a closure that runs the given task and increments the run
80 // count of |i| by one. |task| may be null. It is guaranteed that
81 // only one task wrapped by a given tracker will be run at a time.
82 Closure WrapTask(const Closure& task, int i);
84 std::map<int, int> GetTaskRunCounts() const;
86 // Returns after the tracker observes a total of |count| task completions.
87 void WaitForCompletedTasks(int count);
90 friend class RefCountedThreadSafe<TaskTracker>;
94 void RunTask(const Closure& task, int i);
97 std::map<int, int> task_run_counts_;
99 ConditionVariable task_runs_cv_;
101 DISALLOW_COPY_AND_ASSIGN(TaskTracker);
104 } // namespace internal
106 template <typename TaskRunnerTestDelegate>
107 class TaskRunnerTest : public testing::Test {
109 TaskRunnerTest() : task_tracker_(new internal::TaskTracker()) {}
111 const scoped_refptr<internal::TaskTracker> task_tracker_;
112 TaskRunnerTestDelegate delegate_;
115 TYPED_TEST_CASE_P(TaskRunnerTest);
117 // We can't really test much, since TaskRunner provides very few
120 // Post a bunch of tasks to the task runner. They should all
122 TYPED_TEST_P(TaskRunnerTest, Basic) {
123 std::map<int, int> expected_task_run_counts;
125 this->delegate_.StartTaskRunner();
126 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
127 // Post each ith task i+1 times.
128 for (int i = 0; i < 20; ++i) {
129 const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i);
130 for (int j = 0; j < i + 1; ++j) {
131 task_runner->PostTask(FROM_HERE, ith_task);
132 ++expected_task_run_counts[i];
135 this->delegate_.StopTaskRunner();
137 EXPECT_EQ(expected_task_run_counts,
138 this->task_tracker_->GetTaskRunCounts());
141 // Post a bunch of delayed tasks to the task runner. They should all
143 TYPED_TEST_P(TaskRunnerTest, Delayed) {
144 if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) {
145 DLOG(INFO) << "This TaskRunner doesn't handle non-zero delays; skipping";
149 std::map<int, int> expected_task_run_counts;
150 int expected_total_tasks = 0;
152 this->delegate_.StartTaskRunner();
153 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
154 // Post each ith task i+1 times with delays from 0-i.
155 for (int i = 0; i < 20; ++i) {
156 const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i);
157 for (int j = 0; j < i + 1; ++j) {
158 task_runner->PostDelayedTask(
159 FROM_HERE, ith_task, base::TimeDelta::FromMilliseconds(j));
160 ++expected_task_run_counts[i];
161 ++expected_total_tasks;
164 this->task_tracker_->WaitForCompletedTasks(expected_total_tasks);
165 this->delegate_.StopTaskRunner();
167 EXPECT_EQ(expected_task_run_counts,
168 this->task_tracker_->GetTaskRunCounts());
173 // Calls RunsTasksOnCurrentThread() on |task_runner| and expects it to
174 // equal |expected_value|.
175 void ExpectRunsTasksOnCurrentThread(
177 const scoped_refptr<TaskRunner>& task_runner);
179 } // namespace internal
181 // Post a bunch of tasks to the task runner as well as to a separate
182 // thread, each checking the value of RunsTasksOnCurrentThread(),
183 // which should return true for the tasks posted on the task runner
184 // and false for the tasks posted on the separate thread.
185 TYPED_TEST_P(TaskRunnerTest, RunsTasksOnCurrentThread) {
186 std::map<int, int> expected_task_run_counts;
188 Thread thread("Non-task-runner thread");
189 ASSERT_TRUE(thread.Start());
190 this->delegate_.StartTaskRunner();
192 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
193 // Post each ith task i+1 times on the task runner and i+1 times on
194 // the non-task-runner thread.
195 for (int i = 0; i < 20; ++i) {
196 const Closure& ith_task_runner_task =
197 this->task_tracker_->WrapTask(
198 Bind(&internal::ExpectRunsTasksOnCurrentThread,
201 const Closure& ith_non_task_runner_task =
202 this->task_tracker_->WrapTask(
203 Bind(&internal::ExpectRunsTasksOnCurrentThread,
206 for (int j = 0; j < i + 1; ++j) {
207 task_runner->PostTask(FROM_HERE, ith_task_runner_task);
208 thread.message_loop()->PostTask(FROM_HERE, ith_non_task_runner_task);
209 expected_task_run_counts[i] += 2;
213 this->delegate_.StopTaskRunner();
216 EXPECT_EQ(expected_task_run_counts,
217 this->task_tracker_->GetTaskRunCounts());
220 REGISTER_TYPED_TEST_CASE_P(
221 TaskRunnerTest, Basic, Delayed, RunsTasksOnCurrentThread);
225 #endif //#define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_