OLD | NEW |
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/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/time/clock.h" |
| 10 #include "base/time/tick_clock.h" |
9 | 11 |
10 namespace base { | 12 namespace base { |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
14 // TickClock that always returns the then-current mock time of |task_runner| as | 16 // MockTickClock -------------------------------------------------------------- |
15 // the current time. | 17 |
| 18 // TickClock that always returns the then-current mock time ticks of |
| 19 // |task_runner| as the current time ticks. |
16 class MockTickClock : public TickClock { | 20 class MockTickClock : public TickClock { |
17 public: | 21 public: |
18 explicit MockTickClock( | 22 explicit MockTickClock( |
19 scoped_refptr<const TestMockTimeTaskRunner> task_runner); | 23 scoped_refptr<const TestMockTimeTaskRunner> task_runner); |
20 ~MockTickClock() override; | |
21 | 24 |
22 // TickClock: | 25 // TickClock: |
23 TimeTicks NowTicks() override; | 26 TimeTicks NowTicks() override; |
24 | 27 |
25 private: | 28 private: |
26 scoped_refptr<const TestMockTimeTaskRunner> task_runner_; | 29 scoped_refptr<const TestMockTimeTaskRunner> task_runner_; |
27 | 30 |
28 DISALLOW_COPY_AND_ASSIGN(MockTickClock); | 31 DISALLOW_COPY_AND_ASSIGN(MockTickClock); |
29 }; | 32 }; |
30 | 33 |
31 MockTickClock::MockTickClock( | 34 MockTickClock::MockTickClock( |
32 scoped_refptr<const TestMockTimeTaskRunner> task_runner) | 35 scoped_refptr<const TestMockTimeTaskRunner> task_runner) |
33 : task_runner_(task_runner) { | 36 : task_runner_(task_runner) { |
34 } | 37 } |
35 | 38 |
36 MockTickClock::~MockTickClock() { | 39 TimeTicks MockTickClock::NowTicks() { |
| 40 return task_runner_->NowTicks(); |
37 } | 41 } |
38 | 42 |
39 TimeTicks MockTickClock::NowTicks() { | 43 // MockClock ------------------------------------------------------------------ |
40 return task_runner_->GetCurrentMockTime(); | 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(); |
41 } | 66 } |
42 | 67 |
43 } // namespace | 68 } // namespace |
44 | 69 |
| 70 // TestMockTimeTaskRunner ----------------------------------------------------- |
| 71 |
45 bool TestMockTimeTaskRunner::TemporalOrder::operator()( | 72 bool TestMockTimeTaskRunner::TemporalOrder::operator()( |
46 const TestPendingTask& first_task, | 73 const TestPendingTask& first_task, |
47 const TestPendingTask& second_task) const { | 74 const TestPendingTask& second_task) const { |
48 return first_task.GetTimeToRun() > second_task.GetTimeToRun(); | 75 return first_task.GetTimeToRun() > second_task.GetTimeToRun(); |
49 } | 76 } |
50 | 77 |
51 TestMockTimeTaskRunner::TestMockTimeTaskRunner() { | 78 TestMockTimeTaskRunner::TestMockTimeTaskRunner() : now_(Time::UnixEpoch()) { |
52 } | 79 } |
53 | 80 |
54 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() { | 81 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() { |
55 } | 82 } |
56 | 83 |
57 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) { | 84 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) { |
58 DCHECK(thread_checker_.CalledOnValidThread()); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
| 86 DCHECK_GE(delta, TimeDelta()); |
59 | 87 |
60 OnBeforeSelectingTask(); | 88 const TimeTicks original_now_ticks = now_ticks_; |
61 | 89 ProcessAllTasksNoLaterThan(delta); |
62 const base::TimeTicks original_now = now_; | 90 ForwardClocksUntilTickTime(original_now_ticks + delta); |
63 TestPendingTask task_info; | |
64 while (DequeueNextTask(original_now, delta, &task_info)) { | |
65 if (task_info.GetTimeToRun() - now_ > base::TimeDelta()) { | |
66 now_ = task_info.GetTimeToRun(); | |
67 OnAfterTimePassed(); | |
68 } | |
69 | |
70 task_info.task.Run(); | |
71 | |
72 OnAfterTaskRun(); | |
73 OnBeforeSelectingTask(); | |
74 } | |
75 | |
76 if (!delta.is_max() && now_ - original_now < delta) { | |
77 now_ = original_now + delta; | |
78 OnAfterTimePassed(); | |
79 } | |
80 } | 91 } |
81 | 92 |
82 void TestMockTimeTaskRunner::RunUntilIdle() { | 93 void TestMockTimeTaskRunner::RunUntilIdle() { |
83 FastForwardBy(TimeDelta()); | 94 DCHECK(thread_checker_.CalledOnValidThread()); |
| 95 ProcessAllTasksNoLaterThan(TimeDelta()); |
84 } | 96 } |
85 | 97 |
86 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() { | 98 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() { |
87 FastForwardBy(TimeDelta::Max()); | 99 DCHECK(thread_checker_.CalledOnValidThread()); |
| 100 ProcessAllTasksNoLaterThan(TimeDelta::Max()); |
88 } | 101 } |
89 | 102 |
90 TimeTicks TestMockTimeTaskRunner::GetCurrentMockTime() const { | 103 Time TestMockTimeTaskRunner::Now() const { |
91 DCHECK(thread_checker_.CalledOnValidThread()); | 104 DCHECK(thread_checker_.CalledOnValidThread()); |
92 return now_; | 105 return now_; |
93 } | 106 } |
94 | 107 |
| 108 TimeTicks TestMockTimeTaskRunner::NowTicks() const { |
| 109 DCHECK(thread_checker_.CalledOnValidThread()); |
| 110 return now_ticks_; |
| 111 } |
| 112 |
| 113 scoped_ptr<Clock> TestMockTimeTaskRunner::GetMockClock() const { |
| 114 DCHECK(thread_checker_.CalledOnValidThread()); |
| 115 return make_scoped_ptr(new MockClock(this)); |
| 116 } |
| 117 |
95 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { | 118 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { |
96 DCHECK(thread_checker_.CalledOnValidThread()); | 119 DCHECK(thread_checker_.CalledOnValidThread()); |
97 return make_scoped_ptr(new MockTickClock(this)); | 120 return make_scoped_ptr(new MockTickClock(this)); |
98 } | 121 } |
99 | 122 |
100 bool TestMockTimeTaskRunner::HasPendingTask() const { | 123 bool TestMockTimeTaskRunner::HasPendingTask() const { |
101 DCHECK(thread_checker_.CalledOnValidThread()); | 124 DCHECK(thread_checker_.CalledOnValidThread()); |
102 return !tasks_.empty(); | 125 return !tasks_.empty(); |
103 } | 126 } |
104 | 127 |
105 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const { | 128 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const { |
106 DCHECK(thread_checker_.CalledOnValidThread()); | 129 DCHECK(thread_checker_.CalledOnValidThread()); |
107 return tasks_.size(); | 130 return tasks_.size(); |
108 } | 131 } |
109 | 132 |
110 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { | 133 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { |
111 DCHECK(thread_checker_.CalledOnValidThread()); | 134 DCHECK(thread_checker_.CalledOnValidThread()); |
112 return tasks_.empty() ? TimeDelta::Max() : tasks_.top().GetTimeToRun() - now_; | 135 return tasks_.empty() ? TimeDelta::Max() |
| 136 : tasks_.top().GetTimeToRun() - now_ticks_; |
113 } | 137 } |
114 | 138 |
115 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { | 139 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { |
116 return thread_checker_.CalledOnValidThread(); | 140 return thread_checker_.CalledOnValidThread(); |
117 } | 141 } |
118 | 142 |
119 bool TestMockTimeTaskRunner::PostDelayedTask( | 143 bool TestMockTimeTaskRunner::PostDelayedTask( |
120 const tracked_objects::Location& from_here, | 144 const tracked_objects::Location& from_here, |
121 const Closure& task, | 145 const Closure& task, |
122 TimeDelta delay) { | 146 TimeDelta delay) { |
123 base::AutoLock scoped_lock(tasks_lock_); | 147 AutoLock scoped_lock(tasks_lock_); |
124 tasks_.push( | 148 tasks_.push(TestPendingTask(from_here, task, now_ticks_, delay, |
125 TestPendingTask(from_here, task, now_, delay, TestPendingTask::NESTABLE)); | 149 TestPendingTask::NESTABLE)); |
126 return true; | 150 return true; |
127 } | 151 } |
128 | 152 |
129 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( | 153 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( |
130 const tracked_objects::Location& from_here, | 154 const tracked_objects::Location& from_here, |
131 const Closure& task, | 155 const Closure& task, |
132 TimeDelta delay) { | 156 TimeDelta delay) { |
133 NOTREACHED(); | 157 NOTREACHED(); |
134 return false; | 158 return false; |
135 } | 159 } |
136 | 160 |
137 void TestMockTimeTaskRunner::OnBeforeSelectingTask() { | 161 void TestMockTimeTaskRunner::OnBeforeSelectingTask() { |
138 // Empty default implementation. | 162 // Empty default implementation. |
139 } | 163 } |
140 | 164 |
141 void TestMockTimeTaskRunner::OnAfterTimePassed() { | 165 void TestMockTimeTaskRunner::OnAfterTimePassed() { |
142 // Empty default implementation. | 166 // Empty default implementation. |
143 } | 167 } |
144 | 168 |
145 void TestMockTimeTaskRunner::OnAfterTaskRun() { | 169 void TestMockTimeTaskRunner::OnAfterTaskRun() { |
146 // Empty default implementation. | 170 // Empty default implementation. |
147 } | 171 } |
148 | 172 |
149 bool TestMockTimeTaskRunner::DequeueNextTask(const base::TimeTicks& reference, | 173 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) { |
150 const base::TimeDelta& max_delta, | 174 DCHECK_GE(max_delta, TimeDelta()); |
| 175 const TimeTicks original_now_ticks = now_ticks_; |
| 176 while (true) { |
| 177 OnBeforeSelectingTask(); |
| 178 TestPendingTask task_info; |
| 179 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info)) |
| 180 break; |
| 181 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will |
| 182 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not |
| 183 // moving the clock backwards in this case. |
| 184 ForwardClocksUntilTickTime(task_info.GetTimeToRun()); |
| 185 task_info.task.Run(); |
| 186 OnAfterTaskRun(); |
| 187 } |
| 188 } |
| 189 |
| 190 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) { |
| 191 if (later_ticks <= now_ticks_) |
| 192 return; |
| 193 |
| 194 now_ += later_ticks - now_ticks_; |
| 195 now_ticks_ = later_ticks; |
| 196 OnAfterTimePassed(); |
| 197 } |
| 198 |
| 199 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference, |
| 200 const TimeDelta& max_delta, |
151 TestPendingTask* next_task) { | 201 TestPendingTask* next_task) { |
152 base::AutoLock scoped_lock(tasks_lock_); | 202 AutoLock scoped_lock(tasks_lock_); |
153 if (!tasks_.empty() && | 203 if (!tasks_.empty() && |
154 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { | 204 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { |
155 *next_task = tasks_.top(); | 205 *next_task = tasks_.top(); |
156 tasks_.pop(); | 206 tasks_.pop(); |
157 return true; | 207 return true; |
158 } | 208 } |
159 return false; | 209 return false; |
160 } | 210 } |
161 | 211 |
162 } // namespace base | 212 } // namespace base |
OLD | NEW |