Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
| 63 // Utility class used in the tests below. | |
| 64 class TaskTracker : public RefCountedThreadSafe<TaskTracker> { | |
| 65 public: | |
| 66 TaskTracker(); | |
| 67 | |
| 68 void RunTask(int i); | |
| 69 | |
| 70 std::map<int, int> GetTaskRunCounts() const; | |
| 71 | |
| 72 private: | |
| 73 friend class RefCountedThreadSafe<TaskTracker>; | |
| 74 | |
| 75 ~TaskTracker(); | |
| 76 | |
| 77 mutable Lock task_run_counts_lock_; | |
| 78 std::map<int, int> task_run_counts_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(TaskTracker); | |
| 81 }; | |
| 82 | |
| 83 template <typename TaskRunnerTestDelegate> | |
| 84 class TaskRunnerTest : public testing::Test { | |
| 85 protected: | |
| 86 TaskRunnerTest() : task_tracker_(new TaskTracker()) {} | |
| 87 | |
| 88 const scoped_refptr<TaskTracker> task_tracker_; | |
| 89 TaskRunnerTestDelegate delegate_; | |
| 90 }; | |
| 91 | |
| 92 TYPED_TEST_CASE_P(TaskRunnerTest); | |
| 93 | |
| 94 // We can't really test much, since TaskRunner provides very few | |
| 95 // guarantees. | |
| 96 | |
| 97 // Post a bunch of tasks to the task runner. They should all | |
| 98 // complete. | |
| 99 TYPED_TEST_P(TaskRunnerTest, Basic) { | |
| 100 std::map<int, int> expected_task_run_counts; | |
| 101 | |
| 102 this->delegate_.StartTaskRunner(); | |
| 103 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner(); | |
| 104 for (int i = 0; i < 20; ++i) { | |
|
willchan no longer on Chromium
2012/02/23 19:39:57
Add comments explaining what the two loops are doi
akalin
2012/02/23 22:52:21
Done.
| |
| 105 Closure task = Bind(&TaskTracker::RunTask, this->task_tracker_, i); | |
| 106 for (int j = 0; j < i + 1; ++j) { | |
| 107 task_runner->PostTask(FROM_HERE, task); | |
| 108 ++expected_task_run_counts[i]; | |
| 109 } | |
| 110 } | |
| 111 this->delegate_.StopTaskRunner(); | |
|
willchan no longer on Chromium
2012/02/23 19:39:57
I don't know that this is correct. There's no guar
akalin
2012/02/23 22:52:21
It should be the job of the delegate class to do t
| |
| 112 | |
| 113 EXPECT_EQ(expected_task_run_counts, | |
| 114 this->task_tracker_->GetTaskRunCounts()); | |
| 115 } | |
| 116 | |
| 117 // Post a bunch of delayed tasks to the task runner. They should all | |
| 118 // complete. | |
| 119 TYPED_TEST_P(TaskRunnerTest, Delayed) { | |
| 120 std::map<int, int> expected_task_run_counts; | |
| 121 | |
| 122 this->delegate_.StartTaskRunner(); | |
| 123 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner(); | |
| 124 for (int i = 0; i < 20; ++i) { | |
| 125 Closure task = Bind(&TaskTracker::RunTask, this->task_tracker_, i); | |
| 126 for (int j = 0; j < i + 1; ++j) { | |
| 127 task_runner->PostDelayedTask(FROM_HERE, task, j); | |
| 128 ++expected_task_run_counts[i]; | |
| 129 } | |
| 130 } | |
| 131 this->delegate_.StopTaskRunner(); | |
| 132 | |
| 133 EXPECT_EQ(expected_task_run_counts, | |
| 134 this->task_tracker_->GetTaskRunCounts()); | |
| 135 } | |
| 136 | |
| 137 REGISTER_TYPED_TEST_CASE_P(TaskRunnerTest, Basic, Delayed); | |
| 138 | |
|
willchan no longer on Chromium
2012/02/23 19:39:57
We should add TODOs for future tests to verify Run
akalin
2012/02/23 22:52:21
Done.
| |
| 139 } // namespace base | |
| 140 | |
| 141 #endif // BASE_MEMORY_REF_COUNTED_H_ | |
| OLD | NEW |