Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcastagna | 29e7fc6 | 2015-07-31 23:20:06 | [diff] [blame] | 5 | // This file defines tests that implementations of TaskRunner should |
| 6 | // pass in order to be conformant, as well as test cases for optional behavior. |
| 7 | // Here's how you use it to test your implementation. |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 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 |
[email protected] | 13ecb5e9 | 2013-03-07 01:35:37 | [diff] [blame] | 28 | // // this is called are run. Caveat: delayed tasks are not run, |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 29 | // they're simply deleted. |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 30 | // void StopTaskRunner() { |
| 31 | // ... |
| 32 | // } |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 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 | // |
Victor Costan | 033b9ac | 2019-01-29 00:52:16 | [diff] [blame] | 42 | // INSTANTIATE_TYPED_TEST_SUITE_P( |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 43 | // MyTaskRunner, TaskRunnerTest, MyTaskRunnerTestDelegate); |
| 44 | // |
| 45 | // Easy! |
dcastagna | 29e7fc6 | 2015-07-31 23:20:06 | [diff] [blame] | 46 | |
[email protected] | c8cae7c | 2012-03-09 06:20:18 | [diff] [blame] | 47 | #ifndef BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_ |
| 48 | #define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_ |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 49 | |
| 50 | #include <cstddef> |
| 51 | #include <map> |
| 52 | |
Avi Drissman | 63e1f99 | 2023-01-13 18:54:43 | [diff] [blame] | 53 | #include "base/functional/bind.h" |
| 54 | #include "base/functional/callback.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 55 | #include "base/location.h" |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 56 | #include "base/memory/ref_counted.h" |
[email protected] | 13ecb5e9 | 2013-03-07 01:35:37 | [diff] [blame] | 57 | #include "base/synchronization/condition_variable.h" |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 58 | #include "base/synchronization/lock.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 59 | #include "base/task/single_thread_task_runner.h" |
| 60 | #include "base/task/task_runner.h" |
[email protected] | 1ab698d | 2012-03-09 04:24:34 | [diff] [blame] | 61 | #include "base/threading/thread.h" |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 62 | #include "testing/gtest/include/gtest/gtest.h" |
| 63 | |
| 64 | namespace base { |
| 65 | |
fdoray | 39c7a5e | 2016-03-03 17:58:08 | [diff] [blame] | 66 | namespace test { |
[email protected] | 1ab698d | 2012-03-09 04:24:34 | [diff] [blame] | 67 | |
| 68 | // Utility class that keeps track of how many times particular tasks |
| 69 | // are run. |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 70 | class TaskTracker : public RefCountedThreadSafe<TaskTracker> { |
| 71 | public: |
| 72 | TaskTracker(); |
| 73 | |
Peter Boström | 75cd3c0 | 2021-09-28 15:23:18 | [diff] [blame] | 74 | TaskTracker(const TaskTracker&) = delete; |
| 75 | TaskTracker& operator=(const TaskTracker&) = delete; |
| 76 | |
[email protected] | 1ab698d | 2012-03-09 04:24:34 | [diff] [blame] | 77 | // 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 | // only one task wrapped by a given tracker will be run at a time. |
kylechar | 5c4d56c1 | 2019-05-15 17:22:42 | [diff] [blame] | 80 | RepeatingClosure WrapTask(RepeatingClosure task, int i); |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 81 | |
| 82 | std::map<int, int> GetTaskRunCounts() const; |
| 83 | |
[email protected] | 13ecb5e9 | 2013-03-07 01:35:37 | [diff] [blame] | 84 | // Returns after the tracker observes a total of |count| task completions. |
| 85 | void WaitForCompletedTasks(int count); |
| 86 | |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 87 | private: |
| 88 | friend class RefCountedThreadSafe<TaskTracker>; |
| 89 | |
| 90 | ~TaskTracker(); |
| 91 | |
kylechar | 5c4d56c1 | 2019-05-15 17:22:42 | [diff] [blame] | 92 | void RunTask(RepeatingClosure task, int i); |
[email protected] | 1ab698d | 2012-03-09 04:24:34 | [diff] [blame] | 93 | |
[email protected] | 13ecb5e9 | 2013-03-07 01:35:37 | [diff] [blame] | 94 | mutable Lock lock_; |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 95 | std::map<int, int> task_run_counts_; |
[email protected] | 13ecb5e9 | 2013-03-07 01:35:37 | [diff] [blame] | 96 | int task_runs_; |
| 97 | ConditionVariable task_runs_cv_; |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 98 | }; |
| 99 | |
fdoray | 39c7a5e | 2016-03-03 17:58:08 | [diff] [blame] | 100 | } // namespace test |
[email protected] | 1ab698d | 2012-03-09 04:24:34 | [diff] [blame] | 101 | |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 102 | template <typename TaskRunnerTestDelegate> |
| 103 | class TaskRunnerTest : public testing::Test { |
| 104 | protected: |
kylechar | 5c4d56c1 | 2019-05-15 17:22:42 | [diff] [blame] | 105 | TaskRunnerTest() : task_tracker_(base::MakeRefCounted<test::TaskTracker>()) {} |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 106 | |
fdoray | 39c7a5e | 2016-03-03 17:58:08 | [diff] [blame] | 107 | const scoped_refptr<test::TaskTracker> task_tracker_; |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 108 | TaskRunnerTestDelegate delegate_; |
| 109 | }; |
| 110 | |
Victor Costan | 033b9ac | 2019-01-29 00:52:16 | [diff] [blame] | 111 | TYPED_TEST_SUITE_P(TaskRunnerTest); |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 112 | |
| 113 | // We can't really test much, since TaskRunner provides very few |
| 114 | // guarantees. |
| 115 | |
| 116 | // Post a bunch of tasks to the task runner. They should all |
| 117 | // complete. |
| 118 | TYPED_TEST_P(TaskRunnerTest, Basic) { |
| 119 | std::map<int, int> expected_task_run_counts; |
| 120 | |
| 121 | this->delegate_.StartTaskRunner(); |
| 122 | scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner(); |
| 123 | // Post each ith task i+1 times. |
| 124 | for (int i = 0; i < 20; ++i) { |
kylechar | 5c4d56c1 | 2019-05-15 17:22:42 | [diff] [blame] | 125 | RepeatingClosure ith_task = |
| 126 | this->task_tracker_->WrapTask(RepeatingClosure(), i); |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 127 | for (int j = 0; j < i + 1; ++j) { |
[email protected] | 1ab698d | 2012-03-09 04:24:34 | [diff] [blame] | 128 | task_runner->PostTask(FROM_HERE, ith_task); |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 129 | ++expected_task_run_counts[i]; |
| 130 | } |
| 131 | } |
| 132 | this->delegate_.StopTaskRunner(); |
| 133 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 134 | EXPECT_EQ(expected_task_run_counts, this->task_tracker_->GetTaskRunCounts()); |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Post a bunch of delayed tasks to the task runner. They should all |
| 138 | // complete. |
| 139 | TYPED_TEST_P(TaskRunnerTest, Delayed) { |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 140 | std::map<int, int> expected_task_run_counts; |
[email protected] | 13ecb5e9 | 2013-03-07 01:35:37 | [diff] [blame] | 141 | int expected_total_tasks = 0; |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 142 | |
| 143 | this->delegate_.StartTaskRunner(); |
| 144 | scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner(); |
| 145 | // Post each ith task i+1 times with delays from 0-i. |
| 146 | for (int i = 0; i < 20; ++i) { |
kylechar | 5c4d56c1 | 2019-05-15 17:22:42 | [diff] [blame] | 147 | RepeatingClosure ith_task = |
| 148 | this->task_tracker_->WrapTask(RepeatingClosure(), i); |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 149 | for (int j = 0; j < i + 1; ++j) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 150 | task_runner->PostDelayedTask(FROM_HERE, ith_task, base::Milliseconds(j)); |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 151 | ++expected_task_run_counts[i]; |
[email protected] | 13ecb5e9 | 2013-03-07 01:35:37 | [diff] [blame] | 152 | ++expected_total_tasks; |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 153 | } |
| 154 | } |
[email protected] | 13ecb5e9 | 2013-03-07 01:35:37 | [diff] [blame] | 155 | this->task_tracker_->WaitForCompletedTasks(expected_total_tasks); |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 156 | this->delegate_.StopTaskRunner(); |
| 157 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 158 | EXPECT_EQ(expected_task_run_counts, this->task_tracker_->GetTaskRunCounts()); |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 159 | } |
| 160 | |
dcastagna | 29e7fc6 | 2015-07-31 23:20:06 | [diff] [blame] | 161 | // The TaskRunnerTest test case verifies behaviour that is expected from a |
| 162 | // task runner in order to be conformant. |
Victor Costan | 033b9ac | 2019-01-29 00:52:16 | [diff] [blame] | 163 | REGISTER_TYPED_TEST_SUITE_P(TaskRunnerTest, Basic, Delayed); |
dcastagna | ef1312d | 2015-07-24 23:28:11 | [diff] [blame] | 164 | |
[email protected] | 506fc23 | 2012-02-29 01:33:03 | [diff] [blame] | 165 | } // namespace base |
| 166 | |
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 167 | #endif // BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_ |