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

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

Issue 2434783002: Use OnceClosure in TestPendingTask (Closed)
Patch Set: fix Created 4 years, 2 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 | base/test/test_pending_task.h » ('j') | base/test/test_pending_task.h » ('J')
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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 // in the order of being posted. 76 // in the order of being posted.
77 struct TestMockTimeTaskRunner::TestOrderedPendingTask 77 struct TestMockTimeTaskRunner::TestOrderedPendingTask
78 : public base::TestPendingTask { 78 : public base::TestPendingTask {
79 TestOrderedPendingTask(); 79 TestOrderedPendingTask();
80 TestOrderedPendingTask(const tracked_objects::Location& location, 80 TestOrderedPendingTask(const tracked_objects::Location& location,
81 const Closure& task, 81 const Closure& task,
82 TimeTicks post_time, 82 TimeTicks post_time,
83 TimeDelta delay, 83 TimeDelta delay,
84 size_t ordinal, 84 size_t ordinal,
85 TestNestability nestability); 85 TestNestability nestability);
86 TestOrderedPendingTask(TestOrderedPendingTask&&);
86 ~TestOrderedPendingTask(); 87 ~TestOrderedPendingTask();
87 88
89 TestOrderedPendingTask& operator=(TestOrderedPendingTask&&);
90
88 size_t ordinal; 91 size_t ordinal;
92
93 DISALLOW_COPY_AND_ASSIGN(TestOrderedPendingTask);
danakj 2016/10/20 20:07:46 private
tzik 2016/10/27 08:20:49 Done.
89 }; 94 };
90 95
91 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask() 96 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask()
92 : ordinal(0) { 97 : ordinal(0) {
93 } 98 }
94 99
95 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask( 100 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask(
101 TestOrderedPendingTask&&) = default;
102
103 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask(
96 const tracked_objects::Location& location, 104 const tracked_objects::Location& location,
97 const Closure& task, 105 const Closure& task,
98 TimeTicks post_time, 106 TimeTicks post_time,
99 TimeDelta delay, 107 TimeDelta delay,
100 size_t ordinal, 108 size_t ordinal,
101 TestNestability nestability) 109 TestNestability nestability)
102 : base::TestPendingTask(location, task, post_time, delay, nestability), 110 : base::TestPendingTask(location, task, post_time, delay, nestability),
103 ordinal(ordinal) { 111 ordinal(ordinal) {}
104 }
105 112
106 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() { 113 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() {
107 } 114 }
108 115
116 TestMockTimeTaskRunner::TestOrderedPendingTask&
117 TestMockTimeTaskRunner::TestOrderedPendingTask::operator=(
118 TestOrderedPendingTask&&) = default;
119
109 // TestMockTimeTaskRunner ----------------------------------------------------- 120 // TestMockTimeTaskRunner -----------------------------------------------------
110 121
111 bool TestMockTimeTaskRunner::TemporalOrder::operator()( 122 bool TestMockTimeTaskRunner::TemporalOrder::operator()(
112 const TestOrderedPendingTask& first_task, 123 const TestOrderedPendingTask& first_task,
113 const TestOrderedPendingTask& second_task) const { 124 const TestOrderedPendingTask& second_task) const {
114 if (first_task.GetTimeToRun() == second_task.GetTimeToRun()) 125 if (first_task.GetTimeToRun() == second_task.GetTimeToRun())
115 return first_task.ordinal > second_task.ordinal; 126 return first_task.ordinal > second_task.ordinal;
116 return first_task.GetTimeToRun() > second_task.GetTimeToRun(); 127 return first_task.GetTimeToRun() > second_task.GetTimeToRun();
117 } 128 }
118 129
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 const TimeTicks original_now_ticks = now_ticks_; 243 const TimeTicks original_now_ticks = now_ticks_;
233 while (!IsElapsingStopped()) { 244 while (!IsElapsingStopped()) {
234 OnBeforeSelectingTask(); 245 OnBeforeSelectingTask();
235 TestPendingTask task_info; 246 TestPendingTask task_info;
236 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info)) 247 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info))
237 break; 248 break;
238 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will 249 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will
239 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not 250 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not
240 // moving the clock backwards in this case. 251 // moving the clock backwards in this case.
241 ForwardClocksUntilTickTime(task_info.GetTimeToRun()); 252 ForwardClocksUntilTickTime(task_info.GetTimeToRun());
242 task_info.task.Run(); 253 std::move(task_info.task).Run();
danakj 2016/10/20 20:07:46 I think we should very soon write an email explain
tzik 2016/10/27 08:20:49 OK, I'm writing go/callback-explainer. Will send t
danakj 2016/10/27 21:15:21 Super great :D
243 OnAfterTaskRun(); 254 OnAfterTaskRun();
244 } 255 }
245 } 256 }
246 257
247 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) { 258 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) {
248 if (later_ticks <= now_ticks_) 259 if (later_ticks <= now_ticks_)
249 return; 260 return;
250 261
251 now_ += later_ticks - now_ticks_; 262 now_ += later_ticks - now_ticks_;
252 now_ticks_ = later_ticks; 263 now_ticks_ = later_ticks;
253 OnAfterTimePassed(); 264 OnAfterTimePassed();
254 } 265 }
255 266
256 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference, 267 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference,
257 const TimeDelta& max_delta, 268 const TimeDelta& max_delta,
258 TestPendingTask* next_task) { 269 TestPendingTask* next_task) {
259 AutoLock scoped_lock(tasks_lock_); 270 AutoLock scoped_lock(tasks_lock_);
260 if (!tasks_.empty() && 271 if (!tasks_.empty() &&
261 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { 272 (tasks_.top().GetTimeToRun() - reference) <= max_delta) {
262 *next_task = tasks_.top(); 273 *next_task = std::move(const_cast<TestOrderedPendingTask&>(tasks_.top()));
263 tasks_.pop(); 274 tasks_.pop();
264 return true; 275 return true;
265 } 276 }
266 return false; 277 return false;
267 } 278 }
268 279
269 } // namespace base 280 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/test/test_pending_task.h » ('j') | base/test/test_pending_task.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698