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

Side by Side Diff: base/task_runner_test_template.h

Issue 9401032: Make SequencedWorkerPool a TaskRunner (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix typo 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/memory/ref_counted.h"
61 #include "base/synchronization/lock.h"
62 #include "base/task_runner.h"
63 #include "base/tracked_objects.h"
64 #include "testing/gtest/include/gtest/gtest.h"
65
66 namespace base {
67
68 // Utility class used in the tests below.
69 class TaskTracker : public RefCountedThreadSafe<TaskTracker> {
70 public:
71 TaskTracker();
72
73 void RunTask(int i);
74
75 std::map<int, int> GetTaskRunCounts() const;
76
77 private:
78 friend class RefCountedThreadSafe<TaskTracker>;
79
80 ~TaskTracker();
81
82 mutable Lock task_run_counts_lock_;
83 std::map<int, int> task_run_counts_;
84
85 DISALLOW_COPY_AND_ASSIGN(TaskTracker);
86 };
87
88 template <typename TaskRunnerTestDelegate>
89 class TaskRunnerTest : public testing::Test {
90 protected:
91 TaskRunnerTest() : task_tracker_(new TaskTracker()) {}
92
93 const scoped_refptr<TaskTracker> task_tracker_;
94 TaskRunnerTestDelegate delegate_;
95 };
96
97 TYPED_TEST_CASE_P(TaskRunnerTest);
98
99 // We can't really test much, since TaskRunner provides very few
100 // guarantees.
101
102 // Post a bunch of tasks to the task runner. They should all
103 // complete.
104 TYPED_TEST_P(TaskRunnerTest, Basic) {
105 std::map<int, int> expected_task_run_counts;
106
107 this->delegate_.StartTaskRunner();
108 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
109 // Post each ith task i+1 times.
110 for (int i = 0; i < 20; ++i) {
111 Closure task = Bind(&TaskTracker::RunTask, this->task_tracker_, i);
112 for (int j = 0; j < i + 1; ++j) {
113 task_runner->PostTask(FROM_HERE, task);
114 ++expected_task_run_counts[i];
115 }
116 }
117 this->delegate_.StopTaskRunner();
118
119 EXPECT_EQ(expected_task_run_counts,
120 this->task_tracker_->GetTaskRunCounts());
121 }
122
123 // Post a bunch of delayed tasks to the task runner. They should all
124 // complete.
125 TYPED_TEST_P(TaskRunnerTest, Delayed) {
126 if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) {
127 DLOG(INFO) << "This TaskRunner doesn't handle non-zero delays; skipping";
128 return;
129 }
130
131 std::map<int, int> expected_task_run_counts;
132
133 this->delegate_.StartTaskRunner();
134 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
135 // Post each ith task i+1 times with delays from 0-i.
136 for (int i = 0; i < 20; ++i) {
137 Closure task = Bind(&TaskTracker::RunTask, this->task_tracker_, i);
138 for (int j = 0; j < i + 1; ++j) {
139 task_runner->PostDelayedTask(FROM_HERE, task, j);
140 ++expected_task_run_counts[i];
141 }
142 }
143 this->delegate_.StopTaskRunner();
144
145 EXPECT_EQ(expected_task_run_counts,
146 this->task_tracker_->GetTaskRunCounts());
147 }
148
149 // TODO(akalin): Add test to verify RunsTaskOnCurrentThread() returns
150 // true for tasks runs on the TaskRunner and returns false on a
151 // separate PlatformThread.
152
153 REGISTER_TYPED_TEST_CASE_P(TaskRunnerTest, Basic, Delayed);
154
155 } // namespace base
156
157 #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