Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(671)

Side by Side Diff: base/test/test_mock_time_task_runner.cc

Issue 2720903002: Fix lack of lock in MTTR::TakePendingTasks() and add a few more safety checks. (Closed)
Patch Set: update dependency Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "base/test/test_mock_time_task_runner.h" 5 #include "base/test/test_mock_time_task_runner.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 DCHECK(thread_checker_.CalledOnValidThread()); 190 DCHECK(thread_checker_.CalledOnValidThread());
191 return MakeUnique<MockClock>(this); 191 return MakeUnique<MockClock>(this);
192 } 192 }
193 193
194 std::unique_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { 194 std::unique_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const {
195 DCHECK(thread_checker_.CalledOnValidThread()); 195 DCHECK(thread_checker_.CalledOnValidThread());
196 return MakeUnique<MockTickClock>(this); 196 return MakeUnique<MockTickClock>(this);
197 } 197 }
198 198
199 std::deque<TestPendingTask> TestMockTimeTaskRunner::TakePendingTasks() { 199 std::deque<TestPendingTask> TestMockTimeTaskRunner::TakePendingTasks() {
200 AutoLock scoped_lock(tasks_lock_);
200 std::deque<TestPendingTask> tasks; 201 std::deque<TestPendingTask> tasks;
201 while (!tasks_.empty()) { 202 while (!tasks_.empty()) {
202 // It's safe to remove const and consume |task| here, since |task| is not 203 // It's safe to remove const and consume |task| here, since |task| is not
203 // used for ordering the item. 204 // used for ordering the item.
204 tasks.push_back( 205 tasks.push_back(
205 std::move(const_cast<TestOrderedPendingTask&>(tasks_.top()))); 206 std::move(const_cast<TestOrderedPendingTask&>(tasks_.top())));
206 tasks_.pop(); 207 tasks_.pop();
207 } 208 }
208 return tasks; 209 return tasks;
209 } 210 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 260
260 void TestMockTimeTaskRunner::OnAfterTimePassed() { 261 void TestMockTimeTaskRunner::OnAfterTimePassed() {
261 // Empty default implementation. 262 // Empty default implementation.
262 } 263 }
263 264
264 void TestMockTimeTaskRunner::OnAfterTaskRun() { 265 void TestMockTimeTaskRunner::OnAfterTaskRun() {
265 // Empty default implementation. 266 // Empty default implementation.
266 } 267 }
267 268
268 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) { 269 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) {
270 DCHECK(thread_checker_.CalledOnValidThread());
269 DCHECK_GE(max_delta, TimeDelta()); 271 DCHECK_GE(max_delta, TimeDelta());
270 272
271 // Multiple test task runners can share the same thread for determinism in 273 // Multiple test task runners can share the same thread for determinism in
272 // unit tests. Make sure this TestMockTimeTaskRunner's tasks run in its scope. 274 // unit tests. Make sure this TestMockTimeTaskRunner's tasks run in its scope.
273 ScopedClosureRunner undo_override; 275 ScopedClosureRunner undo_override;
274 if (!ThreadTaskRunnerHandle::IsSet() || 276 if (!ThreadTaskRunnerHandle::IsSet() ||
275 ThreadTaskRunnerHandle::Get() != this) { 277 ThreadTaskRunnerHandle::Get() != this) {
276 undo_override = ThreadTaskRunnerHandle::OverrideForTesting(this); 278 undo_override = ThreadTaskRunnerHandle::OverrideForTesting(this);
277 } 279 }
278 280
279 const TimeTicks original_now_ticks = now_ticks_; 281 const TimeTicks original_now_ticks = now_ticks_;
280 while (!IsElapsingStopped()) { 282 while (!IsElapsingStopped()) {
281 OnBeforeSelectingTask(); 283 OnBeforeSelectingTask();
282 TestPendingTask task_info; 284 TestPendingTask task_info;
283 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info)) 285 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info))
284 break; 286 break;
285 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will 287 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will
286 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not 288 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not
287 // moving the clock backwards in this case. 289 // moving the clock backwards in this case.
288 ForwardClocksUntilTickTime(task_info.GetTimeToRun()); 290 ForwardClocksUntilTickTime(task_info.GetTimeToRun());
289 std::move(task_info.task).Run(); 291 std::move(task_info.task).Run();
290 OnAfterTaskRun(); 292 OnAfterTaskRun();
291 } 293 }
292 } 294 }
293 295
294 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) { 296 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) {
297 DCHECK(thread_checker_.CalledOnValidThread());
295 if (later_ticks <= now_ticks_) 298 if (later_ticks <= now_ticks_)
296 return; 299 return;
297 300
298 now_ += later_ticks - now_ticks_; 301 now_ += later_ticks - now_ticks_;
299 now_ticks_ = later_ticks; 302 now_ticks_ = later_ticks;
300 OnAfterTimePassed(); 303 OnAfterTimePassed();
301 } 304 }
302 305
303 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference, 306 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference,
304 const TimeDelta& max_delta, 307 const TimeDelta& max_delta,
305 TestPendingTask* next_task) { 308 TestPendingTask* next_task) {
306 AutoLock scoped_lock(tasks_lock_); 309 AutoLock scoped_lock(tasks_lock_);
307 if (!tasks_.empty() && 310 if (!tasks_.empty() &&
308 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { 311 (tasks_.top().GetTimeToRun() - reference) <= max_delta) {
309 // It's safe to remove const and consume |task| here, since |task| is not 312 // It's safe to remove const and consume |task| here, since |task| is not
310 // used for ordering the item. 313 // used for ordering the item.
311 *next_task = std::move(const_cast<TestOrderedPendingTask&>(tasks_.top())); 314 *next_task = std::move(const_cast<TestOrderedPendingTask&>(tasks_.top()));
312 tasks_.pop(); 315 tasks_.pop();
313 return true; 316 return true;
314 } 317 }
315 return false; 318 return false;
316 } 319 }
317 320
318 } // namespace base 321 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698