Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(706)

Side by Side Diff: base/task_runner_test_template.h

Issue 9657025: Move task_runner_test_template.* into base/test and test_support_base (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/base.gyp ('k') | base/task_runner_test_template.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
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:
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.
29 // void StopTaskRunner() {
30 // ...
31 // }
32 //
33 // // Returns whether or not the task runner obeys non-zero delays.
34 // bool TaskRunnerHandlesNonZeroDelays() const {
35 // return true;
36 // }
37 // };
38 //
39 // The TaskRunnerTest test harness will have a member variable of
40 // this delegate type and will call its functions in the various
41 // tests.
42 //
43 // Then you simply #include this file as well as gtest.h and add the
44 // following statement to my_task_runner_unittest.cc:
45 //
46 // INSTANTIATE_TYPED_TEST_CASE_P(
47 // MyTaskRunner, TaskRunnerTest, MyTaskRunnerTestDelegate);
48 //
49 // Easy!
50
51 #ifndef BASE_TASK_RUNNER_TEST_TEMPLATE_H_
52 #define BASE_TASK_RUNNER_TEST_TEMPLATE_H_
53 #pragma once
54
55 #include <cstddef>
56 #include <map>
57
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/lock.h"
63 #include "base/task_runner.h"
64 #include "base/threading/thread.h"
65 #include "base/tracked_objects.h"
66 #include "testing/gtest/include/gtest/gtest.h"
67
68 namespace base {
69
70 namespace internal {
71
72 // Utility class that keeps track of how many times particular tasks
73 // are run.
74 class TaskTracker : public RefCountedThreadSafe<TaskTracker> {
75 public:
76 TaskTracker();
77
78 // Returns a closure that runs the given task and increments the run
79 // count of |i| by one. |task| may be null. It is guaranteed that
80 // only one task wrapped by a given tracker will be run at a time.
81 Closure WrapTask(const Closure& task, int i);
82
83 std::map<int, int> GetTaskRunCounts() const;
84
85 private:
86 friend class RefCountedThreadSafe<TaskTracker>;
87
88 ~TaskTracker();
89
90 void RunTask(const Closure& task, int i);
91
92 mutable Lock task_run_counts_lock_;
93 std::map<int, int> task_run_counts_;
94
95 DISALLOW_COPY_AND_ASSIGN(TaskTracker);
96 };
97
98 } // namespace internal
99
100 template <typename TaskRunnerTestDelegate>
101 class TaskRunnerTest : public testing::Test {
102 protected:
103 TaskRunnerTest() : task_tracker_(new internal::TaskTracker()) {}
104
105 const scoped_refptr<internal::TaskTracker> task_tracker_;
106 TaskRunnerTestDelegate delegate_;
107 };
108
109 TYPED_TEST_CASE_P(TaskRunnerTest);
110
111 // We can't really test much, since TaskRunner provides very few
112 // guarantees.
113
114 // Post a bunch of tasks to the task runner. They should all
115 // complete.
116 TYPED_TEST_P(TaskRunnerTest, Basic) {
117 std::map<int, int> expected_task_run_counts;
118
119 this->delegate_.StartTaskRunner();
120 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
121 // Post each ith task i+1 times.
122 for (int i = 0; i < 20; ++i) {
123 const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i);
124 for (int j = 0; j < i + 1; ++j) {
125 task_runner->PostTask(FROM_HERE, ith_task);
126 ++expected_task_run_counts[i];
127 }
128 }
129 this->delegate_.StopTaskRunner();
130
131 EXPECT_EQ(expected_task_run_counts,
132 this->task_tracker_->GetTaskRunCounts());
133 }
134
135 // Post a bunch of delayed tasks to the task runner. They should all
136 // complete.
137 TYPED_TEST_P(TaskRunnerTest, Delayed) {
138 if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) {
139 DLOG(INFO) << "This TaskRunner doesn't handle non-zero delays; skipping";
140 return;
141 }
142
143 std::map<int, int> expected_task_run_counts;
144
145 this->delegate_.StartTaskRunner();
146 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
147 // Post each ith task i+1 times with delays from 0-i.
148 for (int i = 0; i < 20; ++i) {
149 const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i);
150 for (int j = 0; j < i + 1; ++j) {
151 task_runner->PostDelayedTask(FROM_HERE, ith_task, j);
152 ++expected_task_run_counts[i];
153 }
154 }
155 this->delegate_.StopTaskRunner();
156
157 EXPECT_EQ(expected_task_run_counts,
158 this->task_tracker_->GetTaskRunCounts());
159 }
160
161 namespace internal {
162
163 // Calls RunsTasksOnCurrentThread() on |task_runner| and expects it to
164 // equal |expected_value|.
165 void ExpectRunsTasksOnCurrentThread(
166 bool expected_value,
167 const scoped_refptr<TaskRunner>& task_runner);
168
169 } // namespace internal
170
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;
177
178 Thread thread("Non-task-runner thread");
179 ASSERT_TRUE(thread.Start());
180 this->delegate_.StartTaskRunner();
181
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,
189 true, task_runner),
190 i);
191 const Closure& ith_non_task_runner_task =
192 this->task_tracker_->WrapTask(
193 Bind(&internal::ExpectRunsTasksOnCurrentThread,
194 false, task_runner),
195 i);
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;
200 }
201 }
202
203 this->delegate_.StopTaskRunner();
204 thread.Stop();
205
206 EXPECT_EQ(expected_task_run_counts,
207 this->task_tracker_->GetTaskRunCounts());
208 }
209
210 REGISTER_TYPED_TEST_CASE_P(
211 TaskRunnerTest, Basic, Delayed, RunsTasksOnCurrentThread);
212
213 } // namespace base
214
215 #endif //#define BASE_TASK_RUNNER_TEST_TEMPLATE_H_
OLDNEW
« no previous file with comments | « base/base.gyp ('k') | base/task_runner_test_template.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698