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

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

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 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 | « base/test/test_mock_time_task_runner.h ('k') | base/test/test_pending_task.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/test/test_mock_time_task_runner.h"
6
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/time/clock.h"
10 #include "base/time/tick_clock.h"
11
12 namespace base {
13
14 namespace {
15
16 // MockTickClock --------------------------------------------------------------
17
18 // TickClock that always returns the then-current mock time ticks of
19 // |task_runner| as the current time ticks.
20 class MockTickClock : public TickClock {
21 public:
22 explicit MockTickClock(
23 scoped_refptr<const TestMockTimeTaskRunner> task_runner);
24
25 // TickClock:
26 TimeTicks NowTicks() override;
27
28 private:
29 scoped_refptr<const TestMockTimeTaskRunner> task_runner_;
30
31 DISALLOW_COPY_AND_ASSIGN(MockTickClock);
32 };
33
34 MockTickClock::MockTickClock(
35 scoped_refptr<const TestMockTimeTaskRunner> task_runner)
36 : task_runner_(task_runner) {
37 }
38
39 TimeTicks MockTickClock::NowTicks() {
40 return task_runner_->NowTicks();
41 }
42
43 // MockClock ------------------------------------------------------------------
44
45 // Clock that always returns the then-current mock time of |task_runner| as the
46 // current time.
47 class MockClock : public Clock {
48 public:
49 explicit MockClock(scoped_refptr<const TestMockTimeTaskRunner> task_runner);
50
51 // Clock:
52 Time Now() override;
53
54 private:
55 scoped_refptr<const TestMockTimeTaskRunner> task_runner_;
56
57 DISALLOW_COPY_AND_ASSIGN(MockClock);
58 };
59
60 MockClock::MockClock(scoped_refptr<const TestMockTimeTaskRunner> task_runner)
61 : task_runner_(task_runner) {
62 }
63
64 Time MockClock::Now() {
65 return task_runner_->Now();
66 }
67
68 } // namespace
69
70 // TestMockTimeTaskRunner::TestOrderedPendingTask -----------------------------
71
72 // Subclass of TestPendingTask which has a strictly monotonically increasing ID
73 // for every task, so that tasks posted with the same 'time to run' can be run
74 // in the order of being posted.
75 struct TestMockTimeTaskRunner::TestOrderedPendingTask
76 : public base::TestPendingTask {
77 TestOrderedPendingTask();
78 TestOrderedPendingTask(const tracked_objects::Location& location,
79 const Closure& task,
80 TimeTicks post_time,
81 TimeDelta delay,
82 size_t ordinal,
83 TestNestability nestability);
84 ~TestOrderedPendingTask();
85
86 size_t ordinal;
87 };
88
89 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask()
90 : ordinal(0) {
91 }
92
93 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask(
94 const tracked_objects::Location& location,
95 const Closure& task,
96 TimeTicks post_time,
97 TimeDelta delay,
98 size_t ordinal,
99 TestNestability nestability)
100 : base::TestPendingTask(location, task, post_time, delay, nestability),
101 ordinal(ordinal) {
102 }
103
104 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() {
105 }
106
107 // TestMockTimeTaskRunner -----------------------------------------------------
108
109 bool TestMockTimeTaskRunner::TemporalOrder::operator()(
110 const TestOrderedPendingTask& first_task,
111 const TestOrderedPendingTask& second_task) const {
112 if (first_task.GetTimeToRun() == second_task.GetTimeToRun())
113 return first_task.ordinal > second_task.ordinal;
114 return first_task.GetTimeToRun() > second_task.GetTimeToRun();
115 }
116
117 TestMockTimeTaskRunner::TestMockTimeTaskRunner()
118 : now_(Time::UnixEpoch()), next_task_ordinal_(0) {
119 }
120
121 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() {
122 }
123
124 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) {
125 DCHECK(thread_checker_.CalledOnValidThread());
126 DCHECK_GE(delta, TimeDelta());
127
128 const TimeTicks original_now_ticks = now_ticks_;
129 ProcessAllTasksNoLaterThan(delta);
130 ForwardClocksUntilTickTime(original_now_ticks + delta);
131 }
132
133 void TestMockTimeTaskRunner::RunUntilIdle() {
134 DCHECK(thread_checker_.CalledOnValidThread());
135 ProcessAllTasksNoLaterThan(TimeDelta());
136 }
137
138 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() {
139 DCHECK(thread_checker_.CalledOnValidThread());
140 ProcessAllTasksNoLaterThan(TimeDelta::Max());
141 }
142
143 void TestMockTimeTaskRunner::ClearPendingTasks() {
144 DCHECK(thread_checker_.CalledOnValidThread());
145 AutoLock scoped_lock(tasks_lock_);
146 while (!tasks_.empty())
147 tasks_.pop();
148 }
149
150 Time TestMockTimeTaskRunner::Now() const {
151 DCHECK(thread_checker_.CalledOnValidThread());
152 return now_;
153 }
154
155 TimeTicks TestMockTimeTaskRunner::NowTicks() const {
156 DCHECK(thread_checker_.CalledOnValidThread());
157 return now_ticks_;
158 }
159
160 scoped_ptr<Clock> TestMockTimeTaskRunner::GetMockClock() const {
161 DCHECK(thread_checker_.CalledOnValidThread());
162 return make_scoped_ptr(new MockClock(this));
163 }
164
165 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const {
166 DCHECK(thread_checker_.CalledOnValidThread());
167 return make_scoped_ptr(new MockTickClock(this));
168 }
169
170 bool TestMockTimeTaskRunner::HasPendingTask() const {
171 DCHECK(thread_checker_.CalledOnValidThread());
172 return !tasks_.empty();
173 }
174
175 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const {
176 DCHECK(thread_checker_.CalledOnValidThread());
177 return tasks_.size();
178 }
179
180 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const {
181 DCHECK(thread_checker_.CalledOnValidThread());
182 return tasks_.empty() ? TimeDelta::Max()
183 : tasks_.top().GetTimeToRun() - now_ticks_;
184 }
185
186 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const {
187 return thread_checker_.CalledOnValidThread();
188 }
189
190 bool TestMockTimeTaskRunner::PostDelayedTask(
191 const tracked_objects::Location& from_here,
192 const Closure& task,
193 TimeDelta delay) {
194 AutoLock scoped_lock(tasks_lock_);
195 tasks_.push(TestOrderedPendingTask(from_here, task, now_ticks_, delay,
196 next_task_ordinal_++,
197 TestPendingTask::NESTABLE));
198 return true;
199 }
200
201 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask(
202 const tracked_objects::Location& from_here,
203 const Closure& task,
204 TimeDelta delay) {
205 return PostDelayedTask(from_here, task, delay);
206 }
207
208 bool TestMockTimeTaskRunner::IsElapsingStopped() {
209 return false;
210 }
211
212 void TestMockTimeTaskRunner::OnBeforeSelectingTask() {
213 // Empty default implementation.
214 }
215
216 void TestMockTimeTaskRunner::OnAfterTimePassed() {
217 // Empty default implementation.
218 }
219
220 void TestMockTimeTaskRunner::OnAfterTaskRun() {
221 // Empty default implementation.
222 }
223
224 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) {
225 DCHECK_GE(max_delta, TimeDelta());
226 const TimeTicks original_now_ticks = now_ticks_;
227 while (!IsElapsingStopped()) {
228 OnBeforeSelectingTask();
229 TestPendingTask task_info;
230 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info))
231 break;
232 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will
233 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not
234 // moving the clock backwards in this case.
235 ForwardClocksUntilTickTime(task_info.GetTimeToRun());
236 task_info.task.Run();
237 OnAfterTaskRun();
238 }
239 }
240
241 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) {
242 if (later_ticks <= now_ticks_)
243 return;
244
245 now_ += later_ticks - now_ticks_;
246 now_ticks_ = later_ticks;
247 OnAfterTimePassed();
248 }
249
250 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference,
251 const TimeDelta& max_delta,
252 TestPendingTask* next_task) {
253 AutoLock scoped_lock(tasks_lock_);
254 if (!tasks_.empty() &&
255 (tasks_.top().GetTimeToRun() - reference) <= max_delta) {
256 *next_task = tasks_.top();
257 tasks_.pop();
258 return true;
259 }
260 return false;
261 }
262
263 } // namespace base
OLDNEW
« no previous file with comments | « base/test/test_mock_time_task_runner.h ('k') | base/test/test_pending_task.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698