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

Side by Side Diff: base/task_runner_unittest.h

Issue 9401032: Make SequencedWorkerPool a TaskRunner (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 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 | Annotate | Revision Log
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 //
34 // The TaskRunnerTest test harness will have a member variable of
35 // this delegate type and will call its functions in the various
36 // tests.
37 //
38 // Then you simply #include this file as well as gtest.h and add the
39 // following statement to my_task_runner_unittest.cc:
40 //
41 // INSTANTIATE_TYPED_TEST_CASE_P(
42 // MyTaskRunner, TaskRunnerTest, MyTaskRunnerTestDelegate);
43 //
44 // Easy!
45
46 #ifndef BASE_MEMORY_TASK_RUNNER_UNITTEST_H_
47 #define BASE_MEMORY_TASK_RUNNER_UNITTEST_H_
48 #pragma once
49
50 #include <cstddef>
51 #include <map>
52
53 #include "base/basictypes.h"
54 #include "base/bind.h"
55 #include "base/memory/ref_counted.h"
56 #include "base/synchronization/lock.h"
57 #include "base/task_runner.h"
58 #include "base/tracked_objects.h"
59 #include "testing/gtest/include/gtest/gtest.h"
60
61 namespace base {
62 namespace internal {
63
64 // Utility class used in the tests below.
65 class TaskTracker : public RefCountedThreadSafe<TaskTracker> {
66 public:
67 TaskTracker();
68
69 void RunTask(int i);
70
71 std::map<int, int> GetTaskRunCounts() const;
72
73 private:
74 friend class RefCountedThreadSafe<TaskTracker>;
75
76 ~TaskTracker();
77
78 mutable Lock task_run_counts_lock_;
79 std::map<int, int> task_run_counts_;
80
81 DISALLOW_COPY_AND_ASSIGN(TaskTracker);
82 };
83
84 template <typename TaskRunnerTestDelegate>
85 class TaskRunnerTest : public testing::Test {
86 protected:
87 TaskRunnerTest() : task_tracker_(new TaskTracker()) {}
88
89 const scoped_refptr<TaskTracker> task_tracker_;
90 TaskRunnerTestDelegate delegate_;
91 };
92
93 TYPED_TEST_CASE_P(TaskRunnerTest);
94
95 // We can't really test much, since TaskRunner provides very little
96 // guarantees.
97
98 // Post a bunch of tasks to the task runner. They should all
99 // complete.
100 TYPED_TEST_P(TaskRunnerTest, Basic) {
101 std::map<int, int> expected_task_run_counts;
102
103 this->delegate_.StartTaskRunner();
104 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
105 for (int i = 0; i < 20; ++i) {
106 Closure task = Bind(&TaskTracker::RunTask, this->task_tracker_, i);
107 for (int j = 0; j < i + 1; ++j) {
108 task_runner->PostTask(FROM_HERE, task);
109 ++expected_task_run_counts[i];
110 }
111 }
112 this->delegate_.StopTaskRunner();
113
114 EXPECT_EQ(expected_task_run_counts,
115 this->task_tracker_->GetTaskRunCounts());
116 }
117
118 // Post a bunch of delayed tasks to the task runner. They should all
119 // complete.
120 TYPED_TEST_P(TaskRunnerTest, Delayed) {
121 std::map<int, int> expected_task_run_counts;
122
123 this->delegate_.StartTaskRunner();
124 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
125 for (int i = 0; i < 20; ++i) {
126 Closure task = Bind(&TaskTracker::RunTask, this->task_tracker_, i);
127 for (int j = 0; j < i + 1; ++j) {
128 task_runner->PostDelayedTask(FROM_HERE, task, j);
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 REGISTER_TYPED_TEST_CASE_P(TaskRunnerTest, Basic, Delayed);
139
140 } // namespace internal
141 } // namespace base
142
143 #endif // BASE_MEMORY_REF_COUNTED_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698