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.
29 // void StopTaskRunner() {
33 // // Returns whether or not the task runner obeys non-zero delays.
34 // bool TaskRunnerHandlesNonZeroDelays() const {
39 // The TaskRunnerTest test harness will have a member variable of
40 // this delegate type and will call its functions in the various
43 // Then you simply #include this file as well as gtest.h and add the
44 // following statement to my_task_runner_unittest.cc:
46 // INSTANTIATE_TYPED_TEST_CASE_P(
47 // MyTaskRunner, TaskRunnerTest, MyTaskRunnerTestDelegate);
51 #ifndef BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
52 #define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
57 #include "base/basictypes.h"
58 #include "base/bind.h"
59 #include "base/callback.h"
60 #include "base/memory/ref_counted.h"
61 #include "base/synchronization/lock.h"
62 #include "base/task_runner.h"
63 #include "base/threading/thread.h"
64 #include "base/tracked_objects.h"
65 #include "testing/gtest/include/gtest/gtest.h"
71 // Utility class that keeps track of how many times particular tasks
73 class TaskTracker
: public RefCountedThreadSafe
<TaskTracker
> {
77 // Returns a closure that runs the given task and increments the run
78 // count of |i| by one. |task| may be null. It is guaranteed that
79 // only one task wrapped by a given tracker will be run at a time.
80 Closure
WrapTask(const Closure
& task
, int i
);
82 std::map
<int, int> GetTaskRunCounts() const;
85 friend class RefCountedThreadSafe
<TaskTracker
>;
89 void RunTask(const Closure
& task
, int i
);
91 mutable Lock task_run_counts_lock_
;
92 std::map
<int, int> task_run_counts_
;
94 DISALLOW_COPY_AND_ASSIGN(TaskTracker
);
97 } // namespace internal
99 template <typename TaskRunnerTestDelegate
>
100 class TaskRunnerTest
: public testing::Test
{
102 TaskRunnerTest() : task_tracker_(new internal::TaskTracker()) {}
104 const scoped_refptr
<internal::TaskTracker
> task_tracker_
;
105 TaskRunnerTestDelegate delegate_
;
108 TYPED_TEST_CASE_P(TaskRunnerTest
);
110 // We can't really test much, since TaskRunner provides very few
113 // Post a bunch of tasks to the task runner. They should all
115 TYPED_TEST_P(TaskRunnerTest
, Basic
) {
116 std::map
<int, int> expected_task_run_counts
;
118 this->delegate_
.StartTaskRunner();
119 scoped_refptr
<TaskRunner
> task_runner
= this->delegate_
.GetTaskRunner();
120 // Post each ith task i+1 times.
121 for (int i
= 0; i
< 20; ++i
) {
122 const Closure
& ith_task
= this->task_tracker_
->WrapTask(Closure(), i
);
123 for (int j
= 0; j
< i
+ 1; ++j
) {
124 task_runner
->PostTask(FROM_HERE
, ith_task
);
125 ++expected_task_run_counts
[i
];
128 this->delegate_
.StopTaskRunner();
130 EXPECT_EQ(expected_task_run_counts
,
131 this->task_tracker_
->GetTaskRunCounts());
134 // Post a bunch of delayed tasks to the task runner. They should all
136 TYPED_TEST_P(TaskRunnerTest
, Delayed
) {
137 if (!this->delegate_
.TaskRunnerHandlesNonZeroDelays()) {
138 DLOG(INFO
) << "This TaskRunner doesn't handle non-zero delays; skipping";
142 std::map
<int, int> expected_task_run_counts
;
144 this->delegate_
.StartTaskRunner();
145 scoped_refptr
<TaskRunner
> task_runner
= this->delegate_
.GetTaskRunner();
146 // Post each ith task i+1 times with delays from 0-i.
147 for (int i
= 0; i
< 20; ++i
) {
148 const Closure
& ith_task
= this->task_tracker_
->WrapTask(Closure(), i
);
149 for (int j
= 0; j
< i
+ 1; ++j
) {
150 task_runner
->PostDelayedTask(
151 FROM_HERE
, ith_task
, base::TimeDelta::FromMilliseconds(j
));
152 ++expected_task_run_counts
[i
];
155 this->delegate_
.StopTaskRunner();
157 EXPECT_EQ(expected_task_run_counts
,
158 this->task_tracker_
->GetTaskRunCounts());
163 // Calls RunsTasksOnCurrentThread() on |task_runner| and expects it to
164 // equal |expected_value|.
165 void ExpectRunsTasksOnCurrentThread(
167 const scoped_refptr
<TaskRunner
>& task_runner
);
169 } // namespace internal
171 // Post a bunch of tasks to the task runner as well as to a separate
172 // thread, each checking the value of RunsTasksOnCurrentThread(),
173 // which should return true for the tasks posted on the task runner
174 // and false for the tasks posted on the separate thread.
175 TYPED_TEST_P(TaskRunnerTest
, RunsTasksOnCurrentThread
) {
176 std::map
<int, int> expected_task_run_counts
;
178 Thread
thread("Non-task-runner thread");
179 ASSERT_TRUE(thread
.Start());
180 this->delegate_
.StartTaskRunner();
182 scoped_refptr
<TaskRunner
> task_runner
= this->delegate_
.GetTaskRunner();
183 // Post each ith task i+1 times on the task runner and i+1 times on
184 // the non-task-runner thread.
185 for (int i
= 0; i
< 20; ++i
) {
186 const Closure
& ith_task_runner_task
=
187 this->task_tracker_
->WrapTask(
188 Bind(&internal::ExpectRunsTasksOnCurrentThread
,
191 const Closure
& ith_non_task_runner_task
=
192 this->task_tracker_
->WrapTask(
193 Bind(&internal::ExpectRunsTasksOnCurrentThread
,
196 for (int j
= 0; j
< i
+ 1; ++j
) {
197 task_runner
->PostTask(FROM_HERE
, ith_task_runner_task
);
198 thread
.message_loop()->PostTask(FROM_HERE
, ith_non_task_runner_task
);
199 expected_task_run_counts
[i
] += 2;
203 this->delegate_
.StopTaskRunner();
206 EXPECT_EQ(expected_task_run_counts
,
207 this->task_tracker_
->GetTaskRunCounts());
210 REGISTER_TYPED_TEST_CASE_P(
211 TaskRunnerTest
, Basic
, Delayed
, RunsTasksOnCurrentThread
);
215 #endif //#define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_