1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file defines tests that implementations of TaskRunner should
6 // pass in order to be conformant, as well as test cases for optional behavior.
7 // Here's how you use it to test your 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:
12 //
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 // }
20 //
21 // // Should return the task runner implementation. Only called
22 // // after StartTaskRunner and before StopTaskRunner.
23 // scoped_refptr<MyTaskRunner> GetTaskRunner() {
24 // ...
25 // }
26 //
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 // };
34 //
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.
38 //
39 // Then you simply #include this file as well as gtest.h and add the
40 // following statement to my_task_runner_unittest.cc:
41 //
42 // INSTANTIATE_TYPED_TEST_SUITE_P(
43 // MyTaskRunner, TaskRunnerTest, MyTaskRunnerTestDelegate);
44 //
45 // Easy!
46
47 #ifndef BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
48 #define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
49
50 #include <cstddef>
51 #include <map>
52
53 #include "base/functional/bind.h"
54 #include "base/functional/callback.h"
55 #include "base/location.h"
56 #include "base/memory/ref_counted.h"
57 #include "base/synchronization/condition_variable.h"
58 #include "base/synchronization/lock.h"
59 #include "base/task/single_thread_task_runner.h"
60 #include "base/task/task_runner.h"
61 #include "base/threading/thread.h"
62 #include "testing/gtest/include/gtest/gtest.h"
63
64 namespace base {
65
66 namespace test {
67
68 // Utility class that keeps track of how many times particular tasks
69 // are run.
70 class TaskTracker : public RefCountedThreadSafe<TaskTracker> {
71 public:
72 TaskTracker();
73
74 TaskTracker(const TaskTracker&) = delete;
75 TaskTracker& operator=(const TaskTracker&) = delete;
76
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 RepeatingClosure WrapTask(RepeatingClosure task, int i);
81
82 std::map<int, int> GetTaskRunCounts() const;
83
84 // Returns after the tracker observes a total of |count| task completions.
85 void WaitForCompletedTasks(int count);
86
87 private:
88 friend class RefCountedThreadSafe<TaskTracker>;
89
90 ~TaskTracker();
91
92 void RunTask(RepeatingClosure task, int i);
93
94 mutable Lock lock_;
95 std::map<int, int> task_run_counts_;
96 int task_runs_;
97 ConditionVariable task_runs_cv_;
98 };
99
100 } // namespace test
101
102 template <typename TaskRunnerTestDelegate>
103 class TaskRunnerTest : public testing::Test {
104 protected:
TaskRunnerTest()105 TaskRunnerTest() : task_tracker_(base::MakeRefCounted<test::TaskTracker>()) {}
106
107 const scoped_refptr<test::TaskTracker> task_tracker_;
108 TaskRunnerTestDelegate delegate_;
109 };
110
111 TYPED_TEST_SUITE_P(TaskRunnerTest);
112
113 // We can't really test much, since TaskRunner provides very few
114 // guarantees.
115
116 // Post a bunch of tasks to the task runner. They should all
117 // complete.
TYPED_TEST_P(TaskRunnerTest,Basic)118 TYPED_TEST_P(TaskRunnerTest, Basic) {
119 std::map<int, int> expected_task_run_counts;
120
121 this->delegate_.StartTaskRunner();
122 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
123 // Post each ith task i+1 times.
124 for (int i = 0; i < 20; ++i) {
125 RepeatingClosure ith_task =
126 this->task_tracker_->WrapTask(RepeatingClosure(), i);
127 for (int j = 0; j < i + 1; ++j) {
128 task_runner->PostTask(FROM_HERE, ith_task);
129 ++expected_task_run_counts[i];
130 }
131 }
132 this->delegate_.StopTaskRunner();
133
134 EXPECT_EQ(expected_task_run_counts,
135 this->task_tracker_->GetTaskRunCounts());
136 }
137
138 // Post a bunch of delayed tasks to the task runner. They should all
139 // complete.
TYPED_TEST_P(TaskRunnerTest,Delayed)140 TYPED_TEST_P(TaskRunnerTest, Delayed) {
141 std::map<int, int> expected_task_run_counts;
142 int expected_total_tasks = 0;
143
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 RepeatingClosure ith_task =
149 this->task_tracker_->WrapTask(RepeatingClosure(), i);
150 for (int j = 0; j < i + 1; ++j) {
151 task_runner->PostDelayedTask(FROM_HERE, ith_task, base::Milliseconds(j));
152 ++expected_task_run_counts[i];
153 ++expected_total_tasks;
154 }
155 }
156 this->task_tracker_->WaitForCompletedTasks(expected_total_tasks);
157 this->delegate_.StopTaskRunner();
158
159 EXPECT_EQ(expected_task_run_counts,
160 this->task_tracker_->GetTaskRunCounts());
161 }
162
163 // The TaskRunnerTest test case verifies behaviour that is expected from a
164 // task runner in order to be conformant.
165 REGISTER_TYPED_TEST_SUITE_P(TaskRunnerTest, Basic, Delayed);
166
167 } // namespace base
168
169 #endif // BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
170