Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / test / task_runner_test_template.h
blobc98ec173f696d24b2d1fdcbdacdecb97a2e9aeb5
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
7 // implementation.
8 //
9 // Say your class is called MyTaskRunner. Then you need to define a
10 // class called MyTaskRunnerTestDelegate in my_task_runner_unittest.cc
11 // like this:
13 // class MyTaskRunnerTestDelegate {
14 // public:
15 // // Tasks posted to the task runner after this and before
16 // // StopTaskRunner() is called is called should run successfully.
17 // void StartTaskRunner() {
18 // ...
19 // }
21 // // Should return the task runner implementation. Only called
22 // // after StartTaskRunner and before StopTaskRunner.
23 // scoped_refptr<MyTaskRunner> GetTaskRunner() {
24 // ...
25 // }
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() {
31 // ...
32 // }
33 // };
35 // The TaskRunnerTest test harness will have a member variable of
36 // this delegate type and will call its functions in the various
37 // tests.
39 // Then you simply #include this file as well as gtest.h and add the
40 // following statement to my_task_runner_unittest.cc:
42 // INSTANTIATE_TYPED_TEST_CASE_P(
43 // MyTaskRunner, TaskRunnerTest, MyTaskRunnerTestDelegate);
45 // Easy!
47 #ifndef BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
48 #define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
50 #include <cstddef>
51 #include <map>
53 #include "base/basictypes.h"
54 #include "base/bind.h"
55 #include "base/callback.h"
56 #include "base/location.h"
57 #include "base/memory/ref_counted.h"
58 #include "base/single_thread_task_runner.h"
59 #include "base/synchronization/condition_variable.h"
60 #include "base/synchronization/lock.h"
61 #include "base/task_runner.h"
62 #include "base/threading/thread.h"
63 #include "base/tracked_objects.h"
64 #include "testing/gtest/include/gtest/gtest.h"
66 namespace base {
68 namespace internal {
70 // Utility class that keeps track of how many times particular tasks
71 // are run.
72 class TaskTracker : public RefCountedThreadSafe<TaskTracker> {
73 public:
74 TaskTracker();
76 // Returns a closure that runs the given task and increments the run
77 // count of |i| by one. |task| may be null. It is guaranteed that
78 // only one task wrapped by a given tracker will be run at a time.
79 Closure WrapTask(const Closure& task, int i);
81 std::map<int, int> GetTaskRunCounts() const;
83 // Returns after the tracker observes a total of |count| task completions.
84 void WaitForCompletedTasks(int count);
86 private:
87 friend class RefCountedThreadSafe<TaskTracker>;
89 ~TaskTracker();
91 void RunTask(const Closure& task, int i);
93 mutable Lock lock_;
94 std::map<int, int> task_run_counts_;
95 int task_runs_;
96 ConditionVariable task_runs_cv_;
98 DISALLOW_COPY_AND_ASSIGN(TaskTracker);
101 } // namespace internal
103 template <typename TaskRunnerTestDelegate>
104 class TaskRunnerTest : public testing::Test {
105 protected:
106 TaskRunnerTest() : task_tracker_(new internal::TaskTracker()) {}
108 const scoped_refptr<internal::TaskTracker> task_tracker_;
109 TaskRunnerTestDelegate delegate_;
112 TYPED_TEST_CASE_P(TaskRunnerTest);
114 // We can't really test much, since TaskRunner provides very few
115 // guarantees.
117 // Post a bunch of tasks to the task runner. They should all
118 // complete.
119 TYPED_TEST_P(TaskRunnerTest, Basic) {
120 std::map<int, int> expected_task_run_counts;
122 this->delegate_.StartTaskRunner();
123 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
124 // Post each ith task i+1 times.
125 for (int i = 0; i < 20; ++i) {
126 const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i);
127 for (int j = 0; j < i + 1; ++j) {
128 task_runner->PostTask(FROM_HERE, ith_task);
129 ++expected_task_run_counts[i];
132 this->delegate_.StopTaskRunner();
134 EXPECT_EQ(expected_task_run_counts,
135 this->task_tracker_->GetTaskRunCounts());
138 // Post a bunch of delayed tasks to the task runner. They should all
139 // complete.
140 TYPED_TEST_P(TaskRunnerTest, Delayed) {
141 std::map<int, int> expected_task_run_counts;
142 int expected_total_tasks = 0;
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];
153 ++expected_total_tasks;
156 this->task_tracker_->WaitForCompletedTasks(expected_total_tasks);
157 this->delegate_.StopTaskRunner();
159 EXPECT_EQ(expected_task_run_counts,
160 this->task_tracker_->GetTaskRunCounts());
163 REGISTER_TYPED_TEST_CASE_P(TaskRunnerTest, Basic, Delayed);
165 namespace internal {
167 // Calls RunsTasksOnCurrentThread() on |task_runner| and expects it to
168 // equal |expected_value|.
169 void ExpectRunsTasksOnCurrentThread(
170 bool expected_value,
171 const scoped_refptr<TaskRunner>& task_runner);
173 } // namespace internal
175 template <typename TaskRunnerTestDelegate>
176 class TaskRunnerAffinityTest : public TaskRunnerTest<TaskRunnerTestDelegate> {};
178 TYPED_TEST_CASE_P(TaskRunnerAffinityTest);
180 // Post a bunch of tasks to the task runner as well as to a separate
181 // thread, each checking the value of RunsTasksOnCurrentThread(),
182 // which should return true for the tasks posted on the task runner
183 // and false for the tasks posted on the separate thread.
184 TYPED_TEST_P(TaskRunnerAffinityTest, RunsTasksOnCurrentThread) {
185 std::map<int, int> expected_task_run_counts;
187 Thread thread("Non-task-runner thread");
188 ASSERT_TRUE(thread.Start());
189 this->delegate_.StartTaskRunner();
191 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
192 // Post each ith task i+1 times on the task runner and i+1 times on
193 // the non-task-runner thread.
194 for (int i = 0; i < 20; ++i) {
195 const Closure& ith_task_runner_task =
196 this->task_tracker_->WrapTask(
197 Bind(&internal::ExpectRunsTasksOnCurrentThread,
198 true, task_runner),
200 const Closure& ith_non_task_runner_task =
201 this->task_tracker_->WrapTask(
202 Bind(&internal::ExpectRunsTasksOnCurrentThread,
203 false, task_runner),
205 for (int j = 0; j < i + 1; ++j) {
206 task_runner->PostTask(FROM_HERE, ith_task_runner_task);
207 thread.task_runner()->PostTask(FROM_HERE, ith_non_task_runner_task);
208 expected_task_run_counts[i] += 2;
212 this->delegate_.StopTaskRunner();
213 thread.Stop();
215 EXPECT_EQ(expected_task_run_counts,
216 this->task_tracker_->GetTaskRunCounts());
219 REGISTER_TYPED_TEST_CASE_P(TaskRunnerAffinityTest, RunsTasksOnCurrentThread);
221 } // namespace base
223 #endif // BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_