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