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_GE(delta, TimeDelta()); | |
59 | 95 |
60 OnBeforeSelectingTask(); | 96 TimeTicks original_now_ticks; |
Lei Zhang
2015/02/05 18:25:26
Do you to initialize this to |now_ticks_| ?
engedy
2015/02/05 18:31:54
Yes, this was a bug. Actually failed some try bots
| |
61 | 97 ProcessAllTasksNoLaterThan(delta); |
62 const base::TimeTicks original_now = now_; | 98 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 } | 99 } |
81 | 100 |
82 void TestMockTimeTaskRunner::RunUntilIdle() { | 101 void TestMockTimeTaskRunner::RunUntilIdle() { |
83 FastForwardBy(TimeDelta()); | 102 ProcessAllTasksNoLaterThan(TimeDelta()); |
84 } | 103 } |
85 | 104 |
86 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() { | 105 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() { |
87 FastForwardBy(TimeDelta::Max()); | 106 DCHECK(thread_checker_.CalledOnValidThread()); |
107 ProcessAllTasksNoLaterThan(TimeDelta::Max()); | |
88 } | 108 } |
89 | 109 |
90 TimeTicks TestMockTimeTaskRunner::GetCurrentMockTime() const { | 110 Time TestMockTimeTaskRunner::Now() const { |
91 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK(thread_checker_.CalledOnValidThread()); |
92 return now_; | 112 return now_; |
93 } | 113 } |
94 | 114 |
115 TimeTicks TestMockTimeTaskRunner::NowTicks() const { | |
116 DCHECK(thread_checker_.CalledOnValidThread()); | |
117 return now_ticks_; | |
118 } | |
119 | |
120 scoped_ptr<Clock> TestMockTimeTaskRunner::GetMockClock() const { | |
121 DCHECK(thread_checker_.CalledOnValidThread()); | |
122 return make_scoped_ptr(new MockClock(this)); | |
123 } | |
124 | |
95 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { | 125 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { |
96 DCHECK(thread_checker_.CalledOnValidThread()); | 126 DCHECK(thread_checker_.CalledOnValidThread()); |
97 return make_scoped_ptr(new MockTickClock(this)); | 127 return make_scoped_ptr(new MockTickClock(this)); |
98 } | 128 } |
99 | 129 |
100 bool TestMockTimeTaskRunner::HasPendingTask() const { | 130 bool TestMockTimeTaskRunner::HasPendingTask() const { |
101 DCHECK(thread_checker_.CalledOnValidThread()); | 131 DCHECK(thread_checker_.CalledOnValidThread()); |
102 return !tasks_.empty(); | 132 return !tasks_.empty(); |
103 } | 133 } |
104 | 134 |
105 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const { | 135 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const { |
106 DCHECK(thread_checker_.CalledOnValidThread()); | 136 DCHECK(thread_checker_.CalledOnValidThread()); |
107 return tasks_.size(); | 137 return tasks_.size(); |
108 } | 138 } |
109 | 139 |
110 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { | 140 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { |
111 DCHECK(thread_checker_.CalledOnValidThread()); | 141 DCHECK(thread_checker_.CalledOnValidThread()); |
112 return tasks_.empty() ? TimeDelta::Max() : tasks_.top().GetTimeToRun() - now_; | 142 return tasks_.empty() ? TimeDelta::Max() |
143 : tasks_.top().GetTimeToRun() - now_ticks_; | |
113 } | 144 } |
114 | 145 |
115 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { | 146 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { |
116 return thread_checker_.CalledOnValidThread(); | 147 return thread_checker_.CalledOnValidThread(); |
117 } | 148 } |
118 | 149 |
119 bool TestMockTimeTaskRunner::PostDelayedTask( | 150 bool TestMockTimeTaskRunner::PostDelayedTask( |
120 const tracked_objects::Location& from_here, | 151 const tracked_objects::Location& from_here, |
121 const Closure& task, | 152 const Closure& task, |
122 TimeDelta delay) { | 153 TimeDelta delay) { |
123 base::AutoLock scoped_lock(tasks_lock_); | 154 AutoLock scoped_lock(tasks_lock_); |
124 tasks_.push( | 155 tasks_.push(TestPendingTask(from_here, task, now_ticks_, delay, |
125 TestPendingTask(from_here, task, now_, delay, TestPendingTask::NESTABLE)); | 156 TestPendingTask::NESTABLE)); |
126 return true; | 157 return true; |
127 } | 158 } |
128 | 159 |
129 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( | 160 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( |
130 const tracked_objects::Location& from_here, | 161 const tracked_objects::Location& from_here, |
131 const Closure& task, | 162 const Closure& task, |
132 TimeDelta delay) { | 163 TimeDelta delay) { |
133 NOTREACHED(); | 164 NOTREACHED(); |
134 return false; | 165 return false; |
135 } | 166 } |
136 | 167 |
137 void TestMockTimeTaskRunner::OnBeforeSelectingTask() { | 168 void TestMockTimeTaskRunner::OnBeforeSelectingTask() { |
138 // Empty default implementation. | 169 // Empty default implementation. |
139 } | 170 } |
140 | 171 |
141 void TestMockTimeTaskRunner::OnAfterTimePassed() { | 172 void TestMockTimeTaskRunner::OnAfterTimePassed() { |
142 // Empty default implementation. | 173 // Empty default implementation. |
143 } | 174 } |
144 | 175 |
145 void TestMockTimeTaskRunner::OnAfterTaskRun() { | 176 void TestMockTimeTaskRunner::OnAfterTaskRun() { |
146 // Empty default implementation. | 177 // Empty default implementation. |
147 } | 178 } |
148 | 179 |
149 bool TestMockTimeTaskRunner::DequeueNextTask(const base::TimeTicks& reference, | 180 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan( |
150 const base::TimeDelta& max_delta, | 181 TimeDelta max_delta) { |
182 DCHECK_GT(max_delta, TimeDelta()); | |
183 const TimeTicks original_now_ticks = now_ticks_; | |
184 for (;;) { | |
185 OnBeforeSelectingTask(); | |
186 TestPendingTask task_info; | |
187 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info)) | |
188 break; | |
189 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will | |
190 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not | |
191 // moving the clock backwards in this case. | |
192 ForwardClocksUntilTickTime(task_info.GetTimeToRun()); | |
193 task_info.task.Run(); | |
194 OnAfterTaskRun(); | |
195 } | |
196 } | |
197 | |
198 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) { | |
199 if (later_ticks <= now_ticks_) | |
200 return; | |
201 | |
202 now_ += later_ticks - now_ticks_; | |
203 now_ticks_ = later_ticks; | |
204 OnAfterTimePassed(); | |
205 } | |
206 | |
207 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference, | |
208 const TimeDelta& max_delta, | |
151 TestPendingTask* next_task) { | 209 TestPendingTask* next_task) { |
152 base::AutoLock scoped_lock(tasks_lock_); | 210 AutoLock scoped_lock(tasks_lock_); |
153 if (!tasks_.empty() && | 211 if (!tasks_.empty() && |
154 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { | 212 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { |
155 *next_task = tasks_.top(); | 213 *next_task = tasks_.top(); |
156 tasks_.pop(); | 214 tasks_.pop(); |
157 return true; | 215 return true; |
158 } | 216 } |
159 return false; | 217 return false; |
160 } | 218 } |
161 | 219 |
162 } // namespace base | 220 } // namespace base |
OLD | NEW |