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

Side by Side Diff: base/test/task_runner_test_template.h

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « base/test/simple_test_tick_clock.cc ('k') | base/test/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. 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_CASE_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/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"
65
66 namespace base {
67
68 namespace internal {
69
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();
75
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);
80
81 std::map<int, int> GetTaskRunCounts() const;
82
83 // Returns after the tracker observes a total of |count| task completions.
84 void WaitForCompletedTasks(int count);
85
86 private:
87 friend class RefCountedThreadSafe<TaskTracker>;
88
89 ~TaskTracker();
90
91 void RunTask(const Closure& task, int i);
92
93 mutable Lock lock_;
94 std::map<int, int> task_run_counts_;
95 int task_runs_;
96 ConditionVariable task_runs_cv_;
97
98 DISALLOW_COPY_AND_ASSIGN(TaskTracker);
99 };
100
101 } // namespace internal
102
103 template <typename TaskRunnerTestDelegate>
104 class TaskRunnerTest : public testing::Test {
105 protected:
106 TaskRunnerTest() : task_tracker_(new internal::TaskTracker()) {}
107
108 const scoped_refptr<internal::TaskTracker> task_tracker_;
109 TaskRunnerTestDelegate delegate_;
110 };
111
112 TYPED_TEST_CASE_P(TaskRunnerTest);
113
114 // We can't really test much, since TaskRunner provides very few
115 // guarantees.
116
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;
121
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];
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.
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 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;
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 namespace internal {
164
165 // Calls RunsTasksOnCurrentThread() on |task_runner| and expects it to
166 // equal |expected_value|.
167 void ExpectRunsTasksOnCurrentThread(
168 bool expected_value,
169 const scoped_refptr<TaskRunner>& task_runner);
170
171 } // namespace internal
172
173 // Post a bunch of tasks to the task runner as well as to a separate
174 // thread, each checking the value of RunsTasksOnCurrentThread(),
175 // which should return true for the tasks posted on the task runner
176 // and false for the tasks posted on the separate thread.
177 TYPED_TEST_P(TaskRunnerTest, RunsTasksOnCurrentThread) {
178 std::map<int, int> expected_task_run_counts;
179
180 Thread thread("Non-task-runner thread");
181 ASSERT_TRUE(thread.Start());
182 this->delegate_.StartTaskRunner();
183
184 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
185 // Post each ith task i+1 times on the task runner and i+1 times on
186 // the non-task-runner thread.
187 for (int i = 0; i < 20; ++i) {
188 const Closure& ith_task_runner_task =
189 this->task_tracker_->WrapTask(
190 Bind(&internal::ExpectRunsTasksOnCurrentThread,
191 true, task_runner),
192 i);
193 const Closure& ith_non_task_runner_task =
194 this->task_tracker_->WrapTask(
195 Bind(&internal::ExpectRunsTasksOnCurrentThread,
196 false, task_runner),
197 i);
198 for (int j = 0; j < i + 1; ++j) {
199 task_runner->PostTask(FROM_HERE, ith_task_runner_task);
200 thread.task_runner()->PostTask(FROM_HERE, ith_non_task_runner_task);
201 expected_task_run_counts[i] += 2;
202 }
203 }
204
205 this->delegate_.StopTaskRunner();
206 thread.Stop();
207
208 EXPECT_EQ(expected_task_run_counts,
209 this->task_tracker_->GetTaskRunCounts());
210 }
211
212 REGISTER_TYPED_TEST_CASE_P(
213 TaskRunnerTest, Basic, Delayed, RunsTasksOnCurrentThread);
214
215 } // namespace base
216
217 #endif // BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
OLDNEW
« no previous file with comments | « base/test/simple_test_tick_clock.cc ('k') | base/test/task_runner_test_template.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698