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 #include "cc/test/ordered_simple_task_runner.h" | 5 #include "cc/test/ordered_simple_task_runner.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <set> | 8 #include <set> |
9 #include <sstream> | 9 #include <sstream> |
10 #include <string> | 10 #include <string> |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 return state; | 69 return state; |
70 } | 70 } |
71 | 71 |
72 void TestOrderablePendingTask::AsValueInto( | 72 void TestOrderablePendingTask::AsValueInto( |
73 base::trace_event::TracedValue* state) const { | 73 base::trace_event::TracedValue* state) const { |
74 state->SetInteger("id", base::saturated_cast<int>(task_id_)); | 74 state->SetInteger("id", base::saturated_cast<int>(task_id_)); |
75 state->SetInteger("run_at", GetTimeToRun().ToInternalValue()); | 75 state->SetInteger("run_at", GetTimeToRun().ToInternalValue()); |
76 state->SetString("posted_from", location.ToString()); | 76 state->SetString("posted_from", location.ToString()); |
77 } | 77 } |
78 | 78 |
79 OrderedSimpleTaskRunner::OrderedSimpleTaskRunner() | |
80 : advance_now_(true), | |
81 now_src_(TestNowSource::Create(0)), | |
82 max_tasks_(kAbsoluteMaxTasks), | |
83 inside_run_tasks_until_(false) { | |
84 } | |
85 | |
86 OrderedSimpleTaskRunner::OrderedSimpleTaskRunner( | 79 OrderedSimpleTaskRunner::OrderedSimpleTaskRunner( |
87 scoped_refptr<TestNowSource> now_src, | 80 base::SimpleTestTickClock* now_src, |
88 bool advance_now) | 81 bool advance_now) |
89 : advance_now_(advance_now), | 82 : advance_now_(advance_now), |
90 now_src_(now_src), | 83 now_src_(now_src), |
91 max_tasks_(kAbsoluteMaxTasks), | 84 max_tasks_(kAbsoluteMaxTasks), |
92 inside_run_tasks_until_(false) { | 85 inside_run_tasks_until_(false) { |
93 } | 86 } |
94 | 87 |
95 OrderedSimpleTaskRunner::~OrderedSimpleTaskRunner() {} | 88 OrderedSimpleTaskRunner::~OrderedSimpleTaskRunner() {} |
96 | 89 |
| 90 // static |
| 91 base::TimeTicks OrderedSimpleTaskRunner::AbsoluteMaxNow() { |
| 92 return base::TimeTicks::FromInternalValue( |
| 93 std::numeric_limits<int64_t>::max()); |
| 94 } |
| 95 |
97 // base::TestSimpleTaskRunner implementation | 96 // base::TestSimpleTaskRunner implementation |
98 bool OrderedSimpleTaskRunner::PostDelayedTask( | 97 bool OrderedSimpleTaskRunner::PostDelayedTask( |
99 const tracked_objects::Location& from_here, | 98 const tracked_objects::Location& from_here, |
100 const base::Closure& task, | 99 const base::Closure& task, |
101 base::TimeDelta delay) { | 100 base::TimeDelta delay) { |
102 DCHECK(thread_checker_.CalledOnValidThread()); | 101 DCHECK(thread_checker_.CalledOnValidThread()); |
103 TestOrderablePendingTask pt( | 102 TestOrderablePendingTask pt(from_here, task, now_src_->NowTicks(), delay, |
104 from_here, task, now_src_->Now(), delay, base::TestPendingTask::NESTABLE); | 103 base::TestPendingTask::NESTABLE); |
105 | 104 |
106 TRACE_TASK("OrderedSimpleTaskRunner::PostDelayedTask", pt); | 105 TRACE_TASK("OrderedSimpleTaskRunner::PostDelayedTask", pt); |
107 pending_tasks_.insert(pt); | 106 pending_tasks_.insert(pt); |
108 return true; | 107 return true; |
109 } | 108 } |
110 | 109 |
111 bool OrderedSimpleTaskRunner::PostNonNestableDelayedTask( | 110 bool OrderedSimpleTaskRunner::PostNonNestableDelayedTask( |
112 const tracked_objects::Location& from_here, | 111 const tracked_objects::Location& from_here, |
113 const base::Closure& task, | 112 const base::Closure& task, |
114 base::TimeDelta delay) { | 113 base::TimeDelta delay) { |
115 DCHECK(thread_checker_.CalledOnValidThread()); | 114 DCHECK(thread_checker_.CalledOnValidThread()); |
116 TestOrderablePendingTask pt(from_here, | 115 TestOrderablePendingTask pt(from_here, task, now_src_->NowTicks(), delay, |
117 task, | |
118 now_src_->Now(), | |
119 delay, | |
120 base::TestPendingTask::NON_NESTABLE); | 116 base::TestPendingTask::NON_NESTABLE); |
121 | 117 |
122 TRACE_TASK("OrderedSimpleTaskRunner::PostNonNestableDelayedTask", pt); | 118 TRACE_TASK("OrderedSimpleTaskRunner::PostNonNestableDelayedTask", pt); |
123 pending_tasks_.insert(pt); | 119 pending_tasks_.insert(pt); |
124 return true; | 120 return true; |
125 } | 121 } |
126 | 122 |
127 bool OrderedSimpleTaskRunner::RunsTasksOnCurrentThread() const { | 123 bool OrderedSimpleTaskRunner::RunsTasksOnCurrentThread() const { |
128 DCHECK(thread_checker_.CalledOnValidThread()); | 124 DCHECK(thread_checker_.CalledOnValidThread()); |
129 return true; | 125 return true; |
130 } | 126 } |
131 | 127 |
132 size_t OrderedSimpleTaskRunner::NumPendingTasks() const { | 128 size_t OrderedSimpleTaskRunner::NumPendingTasks() const { |
133 return pending_tasks_.size(); | 129 return pending_tasks_.size(); |
134 } | 130 } |
135 | 131 |
136 bool OrderedSimpleTaskRunner::HasPendingTasks() const { | 132 bool OrderedSimpleTaskRunner::HasPendingTasks() const { |
137 return pending_tasks_.size() > 0; | 133 return pending_tasks_.size() > 0; |
138 } | 134 } |
139 | 135 |
140 base::TimeTicks OrderedSimpleTaskRunner::NextTaskTime() { | 136 base::TimeTicks OrderedSimpleTaskRunner::NextTaskTime() { |
141 if (pending_tasks_.size() <= 0) { | 137 if (pending_tasks_.size() <= 0) { |
142 return TestNowSource::kAbsoluteMaxNow; | 138 return AbsoluteMaxNow(); |
143 } | 139 } |
144 | 140 |
145 return pending_tasks_.begin()->GetTimeToRun(); | 141 return pending_tasks_.begin()->GetTimeToRun(); |
146 } | 142 } |
147 | 143 |
148 base::TimeDelta OrderedSimpleTaskRunner::DelayToNextTaskTime() { | 144 base::TimeDelta OrderedSimpleTaskRunner::DelayToNextTaskTime() { |
149 DCHECK(thread_checker_.CalledOnValidThread()); | 145 DCHECK(thread_checker_.CalledOnValidThread()); |
150 | 146 |
151 if (pending_tasks_.size() <= 0) { | 147 if (pending_tasks_.size() <= 0) { |
152 return TestNowSource::kAbsoluteMaxNow - base::TimeTicks(); | 148 return AbsoluteMaxNow() - base::TimeTicks(); |
153 } | 149 } |
154 | 150 |
155 base::TimeDelta delay = NextTaskTime() - now_src_->Now(); | 151 base::TimeDelta delay = NextTaskTime() - now_src_->NowTicks(); |
156 if (delay > base::TimeDelta()) | 152 if (delay > base::TimeDelta()) |
157 return delay; | 153 return delay; |
158 return base::TimeDelta(); | 154 return base::TimeDelta(); |
159 } | 155 } |
160 | 156 |
161 const size_t OrderedSimpleTaskRunner::kAbsoluteMaxTasks = | 157 const size_t OrderedSimpleTaskRunner::kAbsoluteMaxTasks = |
162 std::numeric_limits<size_t>::max(); | 158 std::numeric_limits<size_t>::max(); |
163 | 159 |
164 bool OrderedSimpleTaskRunner::RunTasksWhile( | 160 bool OrderedSimpleTaskRunner::RunTasksWhile( |
165 base::Callback<bool(void)> condition) { | 161 base::Callback<bool(void)> condition) { |
(...skipping 20 matching lines...) Expand all Loading... |
186 | 182 |
187 // Make a copy so we can append some extra run checks. | 183 // Make a copy so we can append some extra run checks. |
188 std::vector<base::Callback<bool(void)>> modifiable_conditions(conditions); | 184 std::vector<base::Callback<bool(void)>> modifiable_conditions(conditions); |
189 | 185 |
190 // Provide a timeout base on number of tasks run so this doesn't loop | 186 // Provide a timeout base on number of tasks run so this doesn't loop |
191 // forever. | 187 // forever. |
192 modifiable_conditions.push_back(TaskRunCountBelow(max_tasks_)); | 188 modifiable_conditions.push_back(TaskRunCountBelow(max_tasks_)); |
193 | 189 |
194 // If to advance now or not | 190 // If to advance now or not |
195 if (!advance_now_) { | 191 if (!advance_now_) { |
196 modifiable_conditions.push_back(NowBefore(now_src_->Now())); | 192 modifiable_conditions.push_back(NowBefore(now_src_->NowTicks())); |
197 } else { | 193 } else { |
198 modifiable_conditions.push_back(AdvanceNow()); | 194 modifiable_conditions.push_back(AdvanceNow()); |
199 } | 195 } |
200 | 196 |
201 while (pending_tasks_.size() > 0) { | 197 while (pending_tasks_.size() > 0) { |
202 // Check if we should continue to run pending tasks. | 198 // Check if we should continue to run pending tasks. |
203 bool condition_success = true; | 199 bool condition_success = true; |
204 for (std::vector<base::Callback<bool(void)>>::iterator it = | 200 for (std::vector<base::Callback<bool(void)>>::iterator it = |
205 modifiable_conditions.begin(); | 201 modifiable_conditions.begin(); |
206 it != modifiable_conditions.end(); | 202 it != modifiable_conditions.end(); |
(...skipping 28 matching lines...) Expand all Loading... |
235 bool OrderedSimpleTaskRunner::RunPendingTasks() { | 231 bool OrderedSimpleTaskRunner::RunPendingTasks() { |
236 return RunTasksWhile(TaskExistedInitially()); | 232 return RunTasksWhile(TaskExistedInitially()); |
237 } | 233 } |
238 | 234 |
239 bool OrderedSimpleTaskRunner::RunUntilIdle() { | 235 bool OrderedSimpleTaskRunner::RunUntilIdle() { |
240 return RunTasksWhile(std::vector<base::Callback<bool(void)>>()); | 236 return RunTasksWhile(std::vector<base::Callback<bool(void)>>()); |
241 } | 237 } |
242 | 238 |
243 bool OrderedSimpleTaskRunner::RunUntilTime(base::TimeTicks time) { | 239 bool OrderedSimpleTaskRunner::RunUntilTime(base::TimeTicks time) { |
244 // If we are not auto advancing, force now forward to the time. | 240 // If we are not auto advancing, force now forward to the time. |
245 if (!advance_now_ && now_src_->Now() < time) | 241 if (!advance_now_ && now_src_->NowTicks() < time) |
246 now_src_->SetNow(time); | 242 now_src_->Advance(time - now_src_->NowTicks()); |
247 | 243 |
248 // Run tasks | 244 // Run tasks |
249 bool result = RunTasksWhile(NowBefore(time)); | 245 bool result = RunTasksWhile(NowBefore(time)); |
250 | 246 |
251 // If the next task is after the stopping time and auto-advancing now, then | 247 // If the next task is after the stopping time and auto-advancing now, then |
252 // force time to be the stopping time. | 248 // force time to be the stopping time. |
253 if (!result && advance_now_ && now_src_->Now() < time) { | 249 if (!result && advance_now_ && now_src_->NowTicks() < time) { |
254 now_src_->SetNow(time); | 250 now_src_->Advance(time - now_src_->NowTicks()); |
255 } | 251 } |
256 | 252 |
257 return result; | 253 return result; |
258 } | 254 } |
259 | 255 |
260 bool OrderedSimpleTaskRunner::RunForPeriod(base::TimeDelta period) { | 256 bool OrderedSimpleTaskRunner::RunForPeriod(base::TimeDelta period) { |
261 return RunUntilTime(now_src_->Now() + period); | 257 return RunUntilTime(now_src_->NowTicks() + period); |
262 } | 258 } |
263 | 259 |
264 // base::trace_event tracing functionality | 260 // base::trace_event tracing functionality |
265 scoped_refptr<base::trace_event::ConvertableToTraceFormat> | 261 scoped_refptr<base::trace_event::ConvertableToTraceFormat> |
266 OrderedSimpleTaskRunner::AsValue() const { | 262 OrderedSimpleTaskRunner::AsValue() const { |
267 scoped_refptr<base::trace_event::TracedValue> state = | 263 scoped_refptr<base::trace_event::TracedValue> state = |
268 new base::trace_event::TracedValue(); | 264 new base::trace_event::TracedValue(); |
269 AsValueInto(state.get()); | 265 AsValueInto(state.get()); |
270 return state; | 266 return state; |
271 } | 267 } |
272 | 268 |
273 void OrderedSimpleTaskRunner::AsValueInto( | 269 void OrderedSimpleTaskRunner::AsValueInto( |
274 base::trace_event::TracedValue* state) const { | 270 base::trace_event::TracedValue* state) const { |
275 state->SetInteger("pending_tasks", | 271 state->SetInteger("pending_tasks", |
276 base::saturated_cast<int>(pending_tasks_.size())); | 272 base::saturated_cast<int>(pending_tasks_.size())); |
277 | 273 |
278 state->BeginArray("tasks"); | 274 state->BeginArray("tasks"); |
279 for (std::set<TestOrderablePendingTask>::const_iterator it = | 275 for (std::set<TestOrderablePendingTask>::const_iterator it = |
280 pending_tasks_.begin(); | 276 pending_tasks_.begin(); |
281 it != pending_tasks_.end(); | 277 it != pending_tasks_.end(); |
282 ++it) { | 278 ++it) { |
283 state->BeginDictionary(); | 279 state->BeginDictionary(); |
284 it->AsValueInto(state); | 280 it->AsValueInto(state); |
285 state->EndDictionary(); | 281 state->EndDictionary(); |
286 } | 282 } |
287 state->EndArray(); | 283 state->EndArray(); |
288 | 284 |
289 state->BeginDictionary("now_src"); | 285 state->BeginDictionary("now_src"); |
290 now_src_->AsValueInto(state); | 286 state->SetDouble("now_in_ms", (now_src_->NowTicks() - base::TimeTicks()) |
| 287 .InMillisecondsF()); |
291 state->EndDictionary(); | 288 state->EndDictionary(); |
292 | 289 |
293 state->SetBoolean("advance_now", advance_now_); | 290 state->SetBoolean("advance_now", advance_now_); |
294 state->SetBoolean("inside_run_tasks_until", inside_run_tasks_until_); | 291 state->SetBoolean("inside_run_tasks_until", inside_run_tasks_until_); |
295 state->SetString("max_tasks", base::SizeTToString(max_tasks_)); | 292 state->SetString("max_tasks", base::SizeTToString(max_tasks_)); |
296 } | 293 } |
297 | 294 |
298 base::Callback<bool(void)> OrderedSimpleTaskRunner::TaskRunCountBelow( | 295 base::Callback<bool(void)> OrderedSimpleTaskRunner::TaskRunCountBelow( |
299 size_t max_tasks) { | 296 size_t max_tasks) { |
300 return base::Bind(&OrderedSimpleTaskRunner::TaskRunCountBelowCallback, | 297 return base::Bind(&OrderedSimpleTaskRunner::TaskRunCountBelowCallback, |
(...skipping 28 matching lines...) Expand all Loading... |
329 return NextTaskTime() <= stop_at; | 326 return NextTaskTime() <= stop_at; |
330 } | 327 } |
331 | 328 |
332 base::Callback<bool(void)> OrderedSimpleTaskRunner::AdvanceNow() { | 329 base::Callback<bool(void)> OrderedSimpleTaskRunner::AdvanceNow() { |
333 return base::Bind(&OrderedSimpleTaskRunner::AdvanceNowCallback, | 330 return base::Bind(&OrderedSimpleTaskRunner::AdvanceNowCallback, |
334 base::Unretained(this)); | 331 base::Unretained(this)); |
335 } | 332 } |
336 | 333 |
337 bool OrderedSimpleTaskRunner::AdvanceNowCallback() { | 334 bool OrderedSimpleTaskRunner::AdvanceNowCallback() { |
338 base::TimeTicks next_task_time = NextTaskTime(); | 335 base::TimeTicks next_task_time = NextTaskTime(); |
339 if (now_src_->Now() < next_task_time) { | 336 if (now_src_->NowTicks() < next_task_time) { |
340 now_src_->SetNow(next_task_time); | 337 now_src_->Advance(next_task_time - now_src_->NowTicks()); |
341 } | 338 } |
342 return true; | 339 return true; |
343 } | 340 } |
344 | 341 |
345 } // namespace cc | 342 } // namespace cc |
OLD | NEW |