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 comparison | |
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(); |
52 OrderedSimpleTaskRunner(scoped_refptr<TestNowSource> now_src, | |
53 bool advance_now); | |
20 | 54 |
21 virtual void RunPendingTasks() OVERRIDE; | 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 // Set a maximum number of tasks to run at once. Useful as a timeout to | |
67 // prevent infinite task loops. | |
68 static const size_t absolute_max_tasks; | |
Sami
2014/09/01 11:35:33
kAbsoluteMaxTasks
mithro-old
2014/09/01 12:00:19
Done.
| |
69 void SetRunTaskLimit(size_t max_tasks) { max_tasks_ = max_tasks; } | |
70 void ClearTimeout() { max_tasks_ = absolute_max_tasks; } | |
Sami
2014/09/01 11:35:33
ClearRunTaskLimit()?
mithro-old
2014/09/01 12:00:19
Done.
| |
71 | |
72 // Allow task runner to advance now when running tasks. | |
73 void SetAutoAdvanceNowToPendingTasks(bool advance_now) { | |
74 advance_now_ = advance_now; | |
75 } | |
76 | |
77 base::TimeTicks NextTaskTime(); | |
78 base::TimeDelta DelayToNextTaskTime(); | |
79 | |
80 // Run tasks while the callback returns true or too many tasks have been run. | |
81 // Returns true if there are still pending tasks left. | |
82 bool RunTasks(base::Callback<bool(void)> condition); | |
83 | |
84 // Run tasks while *all* of the callbacks return true or too many tasks have | |
85 // been run. Exits on the *first* condition which returns false, skipping | |
86 // calling all remaining condition. | |
87 // Returns true if there are still pending tasks left. | |
88 bool RunTasks(const std::vector<base::Callback<bool(void)> > conditions); | |
Sami
2014/09/01 11:35:33
This should probably be a reference:
const std::v
mithro-old
2014/09/01 12:00:19
Done.
| |
89 | |
90 // Convenience functions to run tasks to common conditions | |
91 | |
92 // Run tasks which existed at the start of this call. | |
93 // Return code indicates tasks still exist to run. | |
94 bool RunPendingTasks(); | |
95 // Keep running tasks until no tasks are left. | |
96 // Return code indicates tasks still exist to run which also indicates if | |
97 // runner reached idle. | |
98 bool RunUntilIdle(); | |
99 // Keep running tasks until given time period. | |
100 // Return code indicates tasks still exist to run. | |
101 bool RunUntilTime(base::TimeTicks time); | |
102 bool RunForPeriod(base::TimeDelta period); | |
103 | |
104 // base::debug tracing functionality | |
105 scoped_refptr<base::debug::ConvertableToTraceFormat> AsValue() const; | |
106 virtual void AsValueInto(base::debug::TracedValue* state) const; | |
107 | |
108 // Common conditions to run for, exposed publicly to allow external users to | |
109 // use their own combinations. | |
110 // ------------------------------------------------------------------------- | |
111 | |
112 // Keep running until the given number of tasks have run. | |
113 // You generally shouldn't use this check as it will cause your tests to fail | |
114 // when code is changed adding a new task. It is useful as a "timeout" type | |
115 // solution. | |
116 base::Callback<bool(void)> WhileTaskRunCountBelow(size_t max_tasks); | |
117 | |
118 // Keep running until a task which didn't exist initially would run. | |
119 base::Callback<bool(void)> WhileTaskExistedInitially(); | |
120 | |
121 // Stop running tasks when NextTaskTime() >= stop_at | |
122 base::Callback<bool(void)> WhileNowBefore(base::TimeTicks stop_at); | |
123 | |
124 // Advance Now() to the next task to run. | |
125 base::Callback<bool(void)> AdvanceNow(); | |
22 | 126 |
23 protected: | 127 protected: |
128 bool WhileTaskRunCountBelowCallback(size_t max_tasks, size_t* task_run); | |
129 bool WhileTaskExistedInitiallyCallback( | |
130 const std::set<TestOrderablePendingTask>& existing_tasks); | |
131 bool WhileNowBeforeCallback(base::TimeTicks stop_at); | |
132 bool AdvanceNowCallback(); | |
133 | |
24 virtual ~OrderedSimpleTaskRunner(); | 134 virtual ~OrderedSimpleTaskRunner(); |
25 | 135 |
136 base::ThreadChecker thread_checker_; | |
137 | |
138 bool advance_now_; | |
139 scoped_refptr<TestNowSource> now_src_; | |
140 | |
141 size_t max_tasks_; | |
142 | |
143 bool inside_run_tasks_until_; | |
144 std::set<TestOrderablePendingTask> pending_tasks_; | |
145 | |
26 private: | 146 private: |
27 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunner); | 147 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunner); |
28 }; | 148 }; |
29 | 149 |
30 } // namespace cc | 150 } // namespace cc |
31 | 151 |
32 #endif // CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ | 152 #endif // CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ |
OLD | NEW |