Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ | 5 #ifndef CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ |
| 6 #define CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ | 6 #define CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ |
| 7 | 7 |
| 8 #include <limits> | |
| 9 #include <set> | |
| 10 #include <vector> | |
| 11 | |
| 8 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 9 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/debug/trace_event.h" | |
| 15 #include "base/logging.h" | |
| 10 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
| 17 #include "cc/test/test_now_source.h" | |
| 11 | 18 |
| 12 namespace cc { | 19 namespace cc { |
| 13 | 20 |
| 21 // Subclass of TestPendingTask which has a unique ID for every task, supports | |
| 22 // being used inside a std::set and has debug tracing support. | |
| 23 class TestOrderablePendingTask : public base::TestPendingTask { | |
| 24 public: | |
| 25 TestOrderablePendingTask(); | |
| 26 TestOrderablePendingTask(const tracked_objects::Location& location, | |
| 27 const base::Closure& task, | |
| 28 base::TimeTicks post_time, | |
| 29 base::TimeDelta delay, | |
| 30 TestNestability nestability); | |
| 31 ~TestOrderablePendingTask(); | |
| 32 | |
| 33 // operators needed by std::set and comparision | |
|
Sami
2014/08/29 13:13:52
s/comparision/comparison/
mithro-old
2014/09/01 06:19:46
Done.
| |
| 34 bool operator==(const TestOrderablePendingTask& other) const; | |
| 35 bool operator<(const TestOrderablePendingTask& other) const; | |
| 36 | |
| 37 // debug tracing functions | |
| 38 scoped_refptr<base::debug::ConvertableToTraceFormat> AsValue() const; | |
| 39 void AsValueInto(base::debug::TracedValue* state) const; | |
| 40 | |
| 41 private: | |
| 42 static size_t task_id_counter; | |
| 43 const size_t task_id_; | |
| 44 }; | |
| 45 | |
| 14 // This runs pending tasks based on task's post_time + delay. | 46 // This runs pending tasks based on task's post_time + delay. |
| 15 // We should not execute a delayed task sooner than some of the queued tasks | 47 // We should not execute a delayed task sooner than some of the queued tasks |
| 16 // which don't have a delay even though it is queued early. | 48 // which don't have a delay even though it is queued early. |
| 17 class OrderedSimpleTaskRunner : public base::TestSimpleTaskRunner { | 49 class OrderedSimpleTaskRunner : public base::SingleThreadTaskRunner { |
| 18 public: | 50 public: |
| 19 OrderedSimpleTaskRunner(); | 51 OrderedSimpleTaskRunner(); |
| 20 | 52 OrderedSimpleTaskRunner(scoped_refptr<TestNowSource> now_src, |
| 21 virtual void RunPendingTasks() OVERRIDE; | 53 bool advance_now); |
| 54 | |
| 55 // base::TestSimpleTaskRunner implementation: | |
| 56 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 57 const base::Closure& task, | |
| 58 base::TimeDelta delay) OVERRIDE; | |
| 59 virtual bool PostNonNestableDelayedTask( | |
| 60 const tracked_objects::Location& from_here, | |
| 61 const base::Closure& task, | |
| 62 base::TimeDelta delay) OVERRIDE; | |
| 63 | |
| 64 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; | |
| 65 | |
| 66 // Base class for checks which control how many of the pending tasks the | |
| 67 // OrderedSimpleTaskRunner will run. | |
| 68 class RunCheck { | |
| 69 public: | |
| 70 virtual ~RunCheck() {} | |
| 71 | |
| 72 virtual bool Check() = 0; | |
| 73 | |
| 74 // This callback is only valid as long as this object continues to exist. | |
| 75 base::Callback<bool(void)> AsCallback() const; | |
|
brianderson
2014/08/28 22:15:16
Why is this needed?
mithro-old
2014/09/01 06:19:46
Removed now.
| |
| 76 }; | |
| 77 | |
| 78 // Base class for checks which need access to the next pending task. | |
| 79 class RunCheckNeedsNextTask : public RunCheck { | |
| 80 public: | |
| 81 explicit RunCheckNeedsNextTask(OrderedSimpleTaskRunner* task_runner); | |
| 82 virtual ~RunCheckNeedsNextTask() {} | |
| 83 | |
| 84 virtual bool Check() OVERRIDE; | |
|
Sami
2014/08/29 13:13:51
It might be worth adding a comment saying when Che
mithro-old
2014/09/01 06:19:46
Done.
| |
| 85 | |
| 86 protected: | |
| 87 virtual bool CheckWithTask(const TestOrderablePendingTask& next_task) = 0; | |
| 88 | |
| 89 private: | |
| 90 OrderedSimpleTaskRunner* task_runner_; | |
| 91 }; | |
| 92 | |
| 93 // Keep running until the given number of tasks have run. | |
| 94 // You generally shouldn't use this check as it will cause your tests to fail | |
| 95 // when code is changed adding a new task. It is useful as a "timeout" type | |
| 96 // solution. | |
| 97 class RunNTasks : public RunCheck { | |
| 98 public: | |
| 99 explicit RunNTasks(size_t number); | |
| 100 virtual ~RunNTasks(); | |
| 101 | |
| 102 virtual bool Check() OVERRIDE; | |
| 103 | |
| 104 private: | |
| 105 size_t tasks_run_; | |
| 106 size_t tasks_to_run_; | |
| 107 }; | |
| 108 | |
| 109 // Keep running until a task which didn't exist initially would run. | |
| 110 class RunOnlyExisting : public RunCheckNeedsNextTask { | |
| 111 public: | |
| 112 explicit RunOnlyExisting(OrderedSimpleTaskRunner* task_runner); | |
| 113 virtual ~RunOnlyExisting(); | |
| 114 | |
| 115 protected: | |
| 116 virtual bool CheckWithTask( | |
| 117 const TestOrderablePendingTask& next_task) OVERRIDE; | |
| 118 | |
| 119 private: | |
| 120 const std::set<TestOrderablePendingTask> pending_tasks_; | |
| 121 }; | |
| 122 | |
| 123 // Base class for run checks which compare time | |
| 124 class RunCheckNeedsNextTaskTime : public RunCheckNeedsNextTask { | |
| 125 public: | |
| 126 RunCheckNeedsNextTaskTime(OrderedSimpleTaskRunner* task_runner, | |
| 127 scoped_refptr<TestNowSource> now_src); | |
| 128 virtual ~RunCheckNeedsNextTaskTime(); | |
| 129 | |
| 130 protected: | |
| 131 virtual bool CheckWithTask( | |
| 132 const TestOrderablePendingTask& next_task) OVERRIDE; | |
| 133 virtual bool CheckWithTaskTime(base::TimeTicks now, | |
| 134 base::TimeTicks next_task) = 0; | |
| 135 | |
| 136 scoped_refptr<TestNowSource> now_src_; | |
| 137 }; | |
| 138 | |
| 139 // Stop running tasks when TimeToRun() >= stop_at | |
| 140 class RunOnlyBefore : public RunCheckNeedsNextTaskTime { | |
|
brianderson
2014/08/28 22:15:16
This class is 4 levels of inheritance deep! My hea
Sami
2014/08/29 13:13:51
Yeah, I think we could just have one Check() that
mithro-old
2014/09/01 06:19:46
After some thinking, I refactored this code to jus
| |
| 141 public: | |
| 142 RunOnlyBefore(OrderedSimpleTaskRunner* task_runner, | |
| 143 scoped_refptr<TestNowSource> now_src, | |
| 144 base::TimeTicks stop_at); | |
| 145 virtual ~RunOnlyBefore(); | |
| 146 | |
| 147 protected: | |
| 148 virtual bool CheckWithTaskTime(base::TimeTicks now, | |
| 149 base::TimeTicks next_task) OVERRIDE; | |
| 150 | |
| 151 private: | |
| 152 base::TimeTicks stop_at_; | |
| 153 }; | |
| 154 | |
| 155 // Advance Now() to the next task to run. | |
| 156 class RunAdvanceNow : public RunCheckNeedsNextTaskTime { | |
| 157 public: | |
| 158 RunAdvanceNow(OrderedSimpleTaskRunner* task_runner, | |
| 159 scoped_refptr<TestNowSource> now_src); | |
| 160 virtual ~RunAdvanceNow(); | |
| 161 | |
| 162 protected: | |
| 163 virtual bool CheckWithTaskTime(base::TimeTicks now, | |
| 164 base::TimeTicks next_task) OVERRIDE; | |
| 165 }; | |
| 166 | |
| 167 // | |
| 168 static const size_t absolute_max_tasks = std::numeric_limits<size_t>::max(); | |
| 169 void SetTimeout(size_t max_tasks) { max_tasks_ = max_tasks; } | |
|
Sami
2014/08/29 13:13:51
Instead of adding this arbitrary limit, should we
mithro-old
2014/09/01 06:19:46
This makes our tests fail much faster when things
| |
| 170 void ClearTimeout() { max_tasks_ = absolute_max_tasks; } | |
| 171 | |
| 172 bool RunPendingTasks(); | |
| 173 bool RunUntilIdle(); | |
| 174 bool RunUntilTime(base::TimeTicks time); | |
| 175 bool RunForPeriod(base::TimeDelta period); | |
| 176 | |
| 177 bool RunTasksUntil(const RunCheck& check_callback); | |
|
Sami
2014/08/29 13:13:51
Do we need all of these entrypoints? How about add
mithro-old
2014/09/01 06:19:46
Drastically simplified "RunTaskUntil" function wit
Sami
2014/09/01 11:35:32
Is that in a future patch or did I miss it? It fee
| |
| 178 bool RunTasksUntil(RunCheck* check_callback); | |
| 179 bool RunTasksUntil(base::Callback<bool(void)> check_callback); | |
| 180 | |
| 181 bool RunTasksUntil(const std::vector<RunCheck*> check_callbacks); | |
| 182 bool RunTasksUntil( | |
| 183 const std::vector<base::Callback<bool(void)> > check_callbacks); | |
| 184 | |
| 185 void SetAutoAdvanceNowToPendingTasks(bool advance_now) { | |
| 186 advance_now_ = advance_now; | |
| 187 } | |
| 188 base::TimeDelta DelayToNextPendingTask(); | |
| 189 | |
| 190 // base::debug tracing functionality | |
| 191 scoped_refptr<base::debug::ConvertableToTraceFormat> AsValue() const; | |
| 192 virtual void AsValueInto(base::debug::TracedValue* state) const; | |
| 22 | 193 |
| 23 protected: | 194 protected: |
| 24 virtual ~OrderedSimpleTaskRunner(); | 195 virtual ~OrderedSimpleTaskRunner(); |
| 25 | 196 |
| 197 base::ThreadChecker thread_checker_; | |
| 198 | |
| 199 bool advance_now_; | |
| 200 scoped_refptr<TestNowSource> now_src_; | |
|
Sami
2014/08/29 13:13:52
scoped_ptr?
mithro-old
2014/09/01 06:19:46
See above.
| |
| 201 | |
| 202 size_t max_tasks_; | |
| 203 | |
| 204 bool inside_run_tasks_until_; | |
| 205 std::set<TestOrderablePendingTask> pending_tasks_; | |
| 206 | |
| 26 private: | 207 private: |
| 27 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunner); | 208 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunner); |
| 28 }; | 209 }; |
| 29 | 210 |
| 30 } // namespace cc | 211 } // namespace cc |
| 31 | 212 |
| 32 #endif // CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ | 213 #endif // CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ |
| OLD | NEW |