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.
5 #include "base/test/test_mock_time_task_runner.h"
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"
16 // MockTickClock --------------------------------------------------------------
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
{
22 explicit MockTickClock(
23 scoped_refptr
<const TestMockTimeTaskRunner
> task_runner
);
26 TimeTicks
NowTicks() override
;
29 scoped_refptr
<const TestMockTimeTaskRunner
> task_runner_
;
31 DISALLOW_COPY_AND_ASSIGN(MockTickClock
);
34 MockTickClock::MockTickClock(
35 scoped_refptr
<const TestMockTimeTaskRunner
> task_runner
)
36 : task_runner_(task_runner
) {
39 TimeTicks
MockTickClock::NowTicks() {
40 return task_runner_
->NowTicks();
43 // MockClock ------------------------------------------------------------------
45 // Clock that always returns the then-current mock time of |task_runner| as the
47 class MockClock
: public Clock
{
49 explicit MockClock(scoped_refptr
<const TestMockTimeTaskRunner
> task_runner
);
55 scoped_refptr
<const TestMockTimeTaskRunner
> task_runner_
;
57 DISALLOW_COPY_AND_ASSIGN(MockClock
);
60 MockClock::MockClock(scoped_refptr
<const TestMockTimeTaskRunner
> task_runner
)
61 : task_runner_(task_runner
) {
64 Time
MockClock::Now() {
65 return task_runner_
->Now();
70 // TestMockTimeTaskRunner::TestOrderedPendingTask -----------------------------
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
,
83 TestNestability nestability
);
84 ~TestOrderedPendingTask();
89 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask()
93 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask(
94 const tracked_objects::Location
& location
,
99 TestNestability nestability
)
100 : base::TestPendingTask(location
, task
, post_time
, delay
, nestability
),
104 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() {
107 // TestMockTimeTaskRunner -----------------------------------------------------
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();
117 TestMockTimeTaskRunner::TestMockTimeTaskRunner()
118 : now_(Time::UnixEpoch()), next_task_ordinal_(0) {
121 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() {
124 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta
) {
125 DCHECK(thread_checker_
.CalledOnValidThread());
126 DCHECK_GE(delta
, TimeDelta());
128 const TimeTicks original_now_ticks
= now_ticks_
;
129 ProcessAllTasksNoLaterThan(delta
);
130 ForwardClocksUntilTickTime(original_now_ticks
+ delta
);
133 void TestMockTimeTaskRunner::RunUntilIdle() {
134 DCHECK(thread_checker_
.CalledOnValidThread());
135 ProcessAllTasksNoLaterThan(TimeDelta());
138 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() {
139 DCHECK(thread_checker_
.CalledOnValidThread());
140 ProcessAllTasksNoLaterThan(TimeDelta::Max());
143 void TestMockTimeTaskRunner::ClearPendingTasks() {
144 DCHECK(thread_checker_
.CalledOnValidThread());
145 AutoLock
scoped_lock(tasks_lock_
);
146 while (!tasks_
.empty())
150 Time
TestMockTimeTaskRunner::Now() const {
151 DCHECK(thread_checker_
.CalledOnValidThread());
155 TimeTicks
TestMockTimeTaskRunner::NowTicks() const {
156 DCHECK(thread_checker_
.CalledOnValidThread());
160 scoped_ptr
<Clock
> TestMockTimeTaskRunner::GetMockClock() const {
161 DCHECK(thread_checker_
.CalledOnValidThread());
162 return make_scoped_ptr(new MockClock(this));
165 scoped_ptr
<TickClock
> TestMockTimeTaskRunner::GetMockTickClock() const {
166 DCHECK(thread_checker_
.CalledOnValidThread());
167 return make_scoped_ptr(new MockTickClock(this));
170 bool TestMockTimeTaskRunner::HasPendingTask() const {
171 DCHECK(thread_checker_
.CalledOnValidThread());
172 return !tasks_
.empty();
175 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const {
176 DCHECK(thread_checker_
.CalledOnValidThread());
177 return tasks_
.size();
180 TimeDelta
TestMockTimeTaskRunner::NextPendingTaskDelay() const {
181 DCHECK(thread_checker_
.CalledOnValidThread());
182 return tasks_
.empty() ? TimeDelta::Max()
183 : tasks_
.top().GetTimeToRun() - now_ticks_
;
186 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const {
187 return thread_checker_
.CalledOnValidThread();
190 bool TestMockTimeTaskRunner::PostDelayedTask(
191 const tracked_objects::Location
& from_here
,
194 AutoLock
scoped_lock(tasks_lock_
);
195 tasks_
.push(TestOrderedPendingTask(from_here
, task
, now_ticks_
, delay
,
196 next_task_ordinal_
++,
197 TestPendingTask::NESTABLE
));
201 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask(
202 const tracked_objects::Location
& from_here
,
205 return PostDelayedTask(from_here
, task
, delay
);
208 bool TestMockTimeTaskRunner::IsElapsingStopped() {
212 void TestMockTimeTaskRunner::OnBeforeSelectingTask() {
213 // Empty default implementation.
216 void TestMockTimeTaskRunner::OnAfterTimePassed() {
217 // Empty default implementation.
220 void TestMockTimeTaskRunner::OnAfterTaskRun() {
221 // Empty default implementation.
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
))
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();
241 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks
) {
242 if (later_ticks
<= now_ticks_
)
245 now_
+= later_ticks
- now_ticks_
;
246 now_ticks_
= later_ticks
;
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();