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