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 <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
11 #include <memory> | 11 #include <memory> |
12 #include <set> | 12 #include <set> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/test/simple_test_tick_clock.h" | 18 #include "base/test/simple_test_tick_clock.h" |
19 #include "base/test/test_simple_task_runner.h" | 19 #include "base/test/test_simple_task_runner.h" |
20 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
21 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
22 | 22 |
23 namespace cc { | 23 namespace cc { |
24 | 24 |
25 // Subclass of TestPendingTask which has a unique ID for every task, supports | 25 // Subclass of TestPendingTask which has a unique ID for every task, supports |
26 // being used inside a std::set and has debug tracing support. | 26 // being used inside a std::set and has debug tracing support. |
27 class TestOrderablePendingTask : public base::TestPendingTask { | 27 class TestOrderablePendingTaskInfo : public base::TestPendingTaskInfo { |
28 public: | 28 public: |
29 TestOrderablePendingTask(); | 29 TestOrderablePendingTaskInfo(); |
30 TestOrderablePendingTask(const tracked_objects::Location& location, | 30 TestOrderablePendingTaskInfo(const tracked_objects::Location& location, |
31 const base::Closure& task, | 31 base::TimeTicks post_time, |
32 base::TimeTicks post_time, | 32 base::TimeDelta delay, |
33 base::TimeDelta delay, | 33 TestNestability nestability); |
34 TestNestability nestability); | 34 ~TestOrderablePendingTaskInfo(); |
35 ~TestOrderablePendingTask(); | |
36 | 35 |
37 // operators needed by std::set and comparison | 36 // operators needed by std::set and comparison |
38 bool operator==(const TestOrderablePendingTask& other) const; | 37 bool operator==(const TestOrderablePendingTaskInfo& other) const; |
39 bool operator<(const TestOrderablePendingTask& other) const; | 38 bool operator<(const TestOrderablePendingTaskInfo& other) const; |
40 | 39 |
41 // base::trace_event tracing functionality | 40 // base::trace_event tracing functionality |
42 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> AsValue() const; | 41 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> AsValue() const; |
43 void AsValueInto(base::trace_event::TracedValue* state) const; | 42 void AsValueInto(base::trace_event::TracedValue* state) const; |
44 | 43 |
| 44 size_t task_id() const { return task_id_; } |
| 45 |
45 private: | 46 private: |
46 static size_t task_id_counter; | 47 static size_t task_id_counter; |
47 const size_t task_id_; | 48 const size_t task_id_; |
48 }; | 49 }; |
49 | 50 |
| 51 using TestOrderablePendingTaskQueue = |
| 52 std::map<TestOrderablePendingTaskInfo, base::OnceClosure>; |
| 53 |
50 // This runs pending tasks based on task's post_time + delay. | 54 // This runs pending tasks based on task's post_time + delay. |
51 // We should not execute a delayed task sooner than some of the queued tasks | 55 // We should not execute a delayed task sooner than some of the queued tasks |
52 // which don't have a delay even though it is queued early. | 56 // which don't have a delay even though it is queued early. |
53 class OrderedSimpleTaskRunner : public base::SingleThreadTaskRunner { | 57 class OrderedSimpleTaskRunner : public base::SingleThreadTaskRunner { |
54 public: | 58 public: |
55 OrderedSimpleTaskRunner(base::SimpleTestTickClock* now_src, bool advance_now); | 59 OrderedSimpleTaskRunner(base::SimpleTestTickClock* now_src, bool advance_now); |
56 | 60 |
57 // base::TestSimpleTaskRunner implementation: | 61 // base::TestSimpleTaskRunner implementation: |
58 bool PostDelayedTask(const tracked_objects::Location& from_here, | 62 bool PostDelayedTask(const tracked_objects::Location& from_here, |
59 const base::Closure& task, | 63 const base::Closure& task, |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 base::Callback<bool(void)> TaskExistedInitially(); | 129 base::Callback<bool(void)> TaskExistedInitially(); |
126 | 130 |
127 // Stop running tasks when NextTaskTime() >= stop_at | 131 // Stop running tasks when NextTaskTime() >= stop_at |
128 base::Callback<bool(void)> NowBefore(base::TimeTicks stop_at); | 132 base::Callback<bool(void)> NowBefore(base::TimeTicks stop_at); |
129 | 133 |
130 // Advance Now() to the next task to run. | 134 // Advance Now() to the next task to run. |
131 base::Callback<bool(void)> AdvanceNow(); | 135 base::Callback<bool(void)> AdvanceNow(); |
132 | 136 |
133 protected: | 137 protected: |
134 static bool TaskRunCountBelowCallback(size_t max_tasks, size_t* task_run); | 138 static bool TaskRunCountBelowCallback(size_t max_tasks, size_t* task_run); |
135 bool TaskExistedInitiallyCallback( | 139 bool TaskExistedInitiallyCallback(const std::set<size_t>& existing_task_ids); |
136 const std::set<TestOrderablePendingTask>& existing_tasks); | |
137 bool NowBeforeCallback(base::TimeTicks stop_at); | 140 bool NowBeforeCallback(base::TimeTicks stop_at); |
138 bool AdvanceNowCallback(); | 141 bool AdvanceNowCallback(); |
139 | 142 |
140 ~OrderedSimpleTaskRunner() override; | 143 ~OrderedSimpleTaskRunner() override; |
141 | 144 |
142 base::ThreadChecker thread_checker_; | 145 base::ThreadChecker thread_checker_; |
143 | 146 |
144 bool advance_now_; | 147 bool advance_now_; |
145 // Not owned. | 148 // Not owned. |
146 base::SimpleTestTickClock* now_src_; | 149 base::SimpleTestTickClock* now_src_; |
147 | 150 |
148 size_t max_tasks_; | 151 size_t max_tasks_; |
149 | 152 |
150 bool inside_run_tasks_until_; | 153 bool inside_run_tasks_until_; |
151 std::set<TestOrderablePendingTask> pending_tasks_; | 154 TestOrderablePendingTaskQueue pending_tasks_; |
152 | 155 |
153 private: | 156 private: |
154 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunner); | 157 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunner); |
155 }; | 158 }; |
156 | 159 |
157 } // namespace cc | 160 } // namespace cc |
158 | 161 |
159 #endif // CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ | 162 #endif // CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ |
OLD | NEW |