Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This class defines tests that implementations of TaskRunner should | 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 | 6 // pass in order to be conformant. Here's how you use it to test your |
| 7 // implementation. | 7 // implementation. |
| 8 // | 8 // |
| 9 // Say your class is called MyTaskRunner. Then you need to define a | 9 // Say your class is called MyTaskRunner. Then you need to define a |
| 10 // class called MyTaskRunnerTestDelegate in my_task_runner_unittest.cc | 10 // class called MyTaskRunnerTestDelegate in my_task_runner_unittest.cc |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 51 #ifndef BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_ | 51 #ifndef BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_ |
| 52 #define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_ | 52 #define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_ |
| 53 | 53 |
| 54 #include <cstddef> | 54 #include <cstddef> |
| 55 #include <map> | 55 #include <map> |
| 56 | 56 |
| 57 #include "base/basictypes.h" | 57 #include "base/basictypes.h" |
| 58 #include "base/bind.h" | 58 #include "base/bind.h" |
| 59 #include "base/callback.h" | 59 #include "base/callback.h" |
| 60 #include "base/memory/ref_counted.h" | 60 #include "base/memory/ref_counted.h" |
| 61 #include "base/synchronization/condition_variable.h" | |
| 61 #include "base/synchronization/lock.h" | 62 #include "base/synchronization/lock.h" |
| 62 #include "base/task_runner.h" | 63 #include "base/task_runner.h" |
| 63 #include "base/threading/thread.h" | 64 #include "base/threading/thread.h" |
| 64 #include "base/tracked_objects.h" | 65 #include "base/tracked_objects.h" |
| 65 #include "testing/gtest/include/gtest/gtest.h" | 66 #include "testing/gtest/include/gtest/gtest.h" |
| 66 | 67 |
| 67 namespace base { | 68 namespace base { |
| 68 | 69 |
| 69 namespace internal { | 70 namespace internal { |
| 70 | 71 |
| 71 // Utility class that keeps track of how many times particular tasks | 72 // Utility class that keeps track of how many times particular tasks |
| 72 // are run. | 73 // are run. |
| 73 class TaskTracker : public RefCountedThreadSafe<TaskTracker> { | 74 class TaskTracker : public RefCountedThreadSafe<TaskTracker> { |
| 74 public: | 75 public: |
| 75 TaskTracker(); | 76 TaskTracker(); |
| 76 | 77 |
| 77 // Returns a closure that runs the given task and increments the run | 78 // 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 // 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 // only one task wrapped by a given tracker will be run at a time. |
| 80 Closure WrapTask(const Closure& task, int i); | 81 Closure WrapTask(const Closure& task, int i); |
| 81 | 82 |
| 82 std::map<int, int> GetTaskRunCounts() const; | 83 std::map<int, int> GetTaskRunCounts() const; |
| 83 | 84 |
| 85 void WaitForTasksToComplete(int n); | |
|
jar (doing other things)
2013/01/15 22:59:59
nit: avoid single letter variable names. Suggest:
| |
| 86 | |
| 84 private: | 87 private: |
| 85 friend class RefCountedThreadSafe<TaskTracker>; | 88 friend class RefCountedThreadSafe<TaskTracker>; |
| 86 | 89 |
| 87 ~TaskTracker(); | 90 ~TaskTracker(); |
| 88 | 91 |
| 89 void RunTask(const Closure& task, int i); | 92 void RunTask(const Closure& task, int i); |
| 90 | 93 |
| 91 mutable Lock task_run_counts_lock_; | 94 mutable Lock lock_; |
| 92 std::map<int, int> task_run_counts_; | 95 std::map<int, int> task_run_counts_; |
| 96 int task_runs_; | |
| 97 ConditionVariable task_runs_cv_; | |
| 93 | 98 |
| 94 DISALLOW_COPY_AND_ASSIGN(TaskTracker); | 99 DISALLOW_COPY_AND_ASSIGN(TaskTracker); |
| 95 }; | 100 }; |
| 96 | 101 |
| 97 } // namespace internal | 102 } // namespace internal |
| 98 | 103 |
| 99 template <typename TaskRunnerTestDelegate> | 104 template <typename TaskRunnerTestDelegate> |
| 100 class TaskRunnerTest : public testing::Test { | 105 class TaskRunnerTest : public testing::Test { |
| 101 protected: | 106 protected: |
| 102 TaskRunnerTest() : task_tracker_(new internal::TaskTracker()) {} | 107 TaskRunnerTest() : task_tracker_(new internal::TaskTracker()) {} |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 133 | 138 |
| 134 // Post a bunch of delayed tasks to the task runner. They should all | 139 // Post a bunch of delayed tasks to the task runner. They should all |
| 135 // complete. | 140 // complete. |
| 136 TYPED_TEST_P(TaskRunnerTest, Delayed) { | 141 TYPED_TEST_P(TaskRunnerTest, Delayed) { |
| 137 if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { | 142 if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { |
| 138 DLOG(INFO) << "This TaskRunner doesn't handle non-zero delays; skipping"; | 143 DLOG(INFO) << "This TaskRunner doesn't handle non-zero delays; skipping"; |
| 139 return; | 144 return; |
| 140 } | 145 } |
| 141 | 146 |
| 142 std::map<int, int> expected_task_run_counts; | 147 std::map<int, int> expected_task_run_counts; |
| 148 int expected_total_tasks = 0; | |
| 143 | 149 |
| 144 this->delegate_.StartTaskRunner(); | 150 this->delegate_.StartTaskRunner(); |
| 145 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner(); | 151 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner(); |
| 146 // Post each ith task i+1 times with delays from 0-i. | 152 // Post each ith task i+1 times with delays from 0-i. |
| 147 for (int i = 0; i < 20; ++i) { | 153 for (int i = 0; i < 20; ++i) { |
| 148 const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i); | 154 const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i); |
| 149 for (int j = 0; j < i + 1; ++j) { | 155 for (int j = 0; j < i + 1; ++j) { |
| 150 task_runner->PostDelayedTask( | 156 task_runner->PostDelayedTask( |
| 151 FROM_HERE, ith_task, base::TimeDelta::FromMilliseconds(j)); | 157 FROM_HERE, ith_task, base::TimeDelta::FromMilliseconds(j)); |
| 152 ++expected_task_run_counts[i]; | 158 ++expected_task_run_counts[i]; |
| 159 ++expected_total_tasks; | |
| 153 } | 160 } |
| 154 } | 161 } |
| 162 this->task_tracker_->WaitForTasksToComplete(expected_total_tasks); | |
| 155 this->delegate_.StopTaskRunner(); | 163 this->delegate_.StopTaskRunner(); |
| 156 | 164 |
| 157 EXPECT_EQ(expected_task_run_counts, | 165 EXPECT_EQ(expected_task_run_counts, |
| 158 this->task_tracker_->GetTaskRunCounts()); | 166 this->task_tracker_->GetTaskRunCounts()); |
| 159 } | 167 } |
| 160 | 168 |
| 161 namespace internal { | 169 namespace internal { |
| 162 | 170 |
| 163 // Calls RunsTasksOnCurrentThread() on |task_runner| and expects it to | 171 // Calls RunsTasksOnCurrentThread() on |task_runner| and expects it to |
| 164 // equal |expected_value|. | 172 // equal |expected_value|. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 EXPECT_EQ(expected_task_run_counts, | 214 EXPECT_EQ(expected_task_run_counts, |
| 207 this->task_tracker_->GetTaskRunCounts()); | 215 this->task_tracker_->GetTaskRunCounts()); |
| 208 } | 216 } |
| 209 | 217 |
| 210 REGISTER_TYPED_TEST_CASE_P( | 218 REGISTER_TYPED_TEST_CASE_P( |
| 211 TaskRunnerTest, Basic, Delayed, RunsTasksOnCurrentThread); | 219 TaskRunnerTest, Basic, Delayed, RunsTasksOnCurrentThread); |
| 212 | 220 |
| 213 } // namespace base | 221 } // namespace base |
| 214 | 222 |
| 215 #endif //#define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_ | 223 #endif //#define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_ |
| OLD | NEW |