Chromium Code Reviews| 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()) { |
| 87 } | |
| 88 | |
| 89 TestMockTimeTaskRunner::TestMockTimeTaskRunner(Time intial_time) | |
|
Lei Zhang
2015/02/04 20:27:37
typo
engedy
2015/02/05 10:22:57
Removed ctor, thanks for spotting the typo, though
| |
| 90 : now_(intial_time) { | |
| 52 } | 91 } |
| 53 | 92 |
| 54 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() { | 93 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() { |
| 55 } | 94 } |
| 56 | 95 |
| 57 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) { | 96 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) { |
| 58 DCHECK(thread_checker_.CalledOnValidThread()); | 97 DCHECK(thread_checker_.CalledOnValidThread()); |
| 59 | 98 |
| 60 OnBeforeSelectingTask(); | 99 OnBeforeSelectingTask(); |
| 61 | 100 |
| 62 const base::TimeTicks original_now = now_; | 101 const TimeTicks original_now_ticks = now_ticks_; |
| 63 TestPendingTask task_info; | 102 TestPendingTask task_info; |
| 64 while (DequeueNextTask(original_now, delta, &task_info)) { | 103 while (DequeueNextTask(original_now_ticks, delta, &task_info)) { |
| 65 if (task_info.GetTimeToRun() - now_ > base::TimeDelta()) { | 104 ForwardClocksUntilTickTime(task_info.GetTimeToRun()); |
| 66 now_ = task_info.GetTimeToRun(); | |
| 67 OnAfterTimePassed(); | |
| 68 } | |
| 69 | |
| 70 task_info.task.Run(); | 105 task_info.task.Run(); |
| 71 | |
| 72 OnAfterTaskRun(); | 106 OnAfterTaskRun(); |
| 73 OnBeforeSelectingTask(); | 107 OnBeforeSelectingTask(); |
| 74 } | 108 } |
| 75 | 109 |
| 76 if (!delta.is_max() && now_ - original_now < delta) { | 110 if (!delta.is_max()) |
| 77 now_ = original_now + delta; | 111 ForwardClocksUntilTickTime(original_now_ticks + delta); |
|
Lei Zhang
2015/02/04 20:27:37
Just trying to understand the original code here..
engedy
2015/02/05 10:22:57
You are correct: it has been, and it is still poss
Lei Zhang
2015/02/05 10:52:59
Is is possible to have both FastForwardBy() and Fa
engedy
2015/02/05 12:41:23
I went with a slightly less evil version of the ol
| |
| 78 OnAfterTimePassed(); | |
| 79 } | |
| 80 } | 112 } |
| 81 | 113 |
| 82 void TestMockTimeTaskRunner::RunUntilIdle() { | 114 void TestMockTimeTaskRunner::RunUntilIdle() { |
| 83 FastForwardBy(TimeDelta()); | 115 FastForwardBy(TimeDelta()); |
| 84 } | 116 } |
| 85 | 117 |
| 86 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() { | 118 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() { |
| 87 FastForwardBy(TimeDelta::Max()); | 119 FastForwardBy(TimeDelta::Max()); |
| 88 } | 120 } |
| 89 | 121 |
| 90 TimeTicks TestMockTimeTaskRunner::GetCurrentMockTime() const { | 122 Time TestMockTimeTaskRunner::Now() const { |
| 91 DCHECK(thread_checker_.CalledOnValidThread()); | 123 DCHECK(thread_checker_.CalledOnValidThread()); |
| 92 return now_; | 124 return now_; |
| 93 } | 125 } |
| 94 | 126 |
| 127 TimeTicks TestMockTimeTaskRunner::NowTicks() const { | |
| 128 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 129 return now_ticks_; | |
| 130 } | |
| 131 | |
| 132 scoped_ptr<Clock> TestMockTimeTaskRunner::GetMockClock() const { | |
| 133 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 134 return make_scoped_ptr(new MockClock(this)); | |
| 135 } | |
| 136 | |
| 95 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { | 137 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); | 138 DCHECK(thread_checker_.CalledOnValidThread()); |
| 97 return make_scoped_ptr(new MockTickClock(this)); | 139 return make_scoped_ptr(new MockTickClock(this)); |
| 98 } | 140 } |
| 99 | 141 |
| 100 bool TestMockTimeTaskRunner::HasPendingTask() const { | 142 bool TestMockTimeTaskRunner::HasPendingTask() const { |
| 101 DCHECK(thread_checker_.CalledOnValidThread()); | 143 DCHECK(thread_checker_.CalledOnValidThread()); |
| 102 return !tasks_.empty(); | 144 return !tasks_.empty(); |
| 103 } | 145 } |
| 104 | 146 |
| 105 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const { | 147 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const { |
| 106 DCHECK(thread_checker_.CalledOnValidThread()); | 148 DCHECK(thread_checker_.CalledOnValidThread()); |
| 107 return tasks_.size(); | 149 return tasks_.size(); |
| 108 } | 150 } |
| 109 | 151 |
| 110 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { | 152 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { |
| 111 DCHECK(thread_checker_.CalledOnValidThread()); | 153 DCHECK(thread_checker_.CalledOnValidThread()); |
| 112 return tasks_.empty() ? TimeDelta::Max() : tasks_.top().GetTimeToRun() - now_; | 154 return tasks_.empty() ? TimeDelta::Max() |
| 155 : tasks_.top().GetTimeToRun() - now_ticks_; | |
| 113 } | 156 } |
| 114 | 157 |
| 115 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { | 158 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { |
| 116 return thread_checker_.CalledOnValidThread(); | 159 return thread_checker_.CalledOnValidThread(); |
| 117 } | 160 } |
| 118 | 161 |
| 119 bool TestMockTimeTaskRunner::PostDelayedTask( | 162 bool TestMockTimeTaskRunner::PostDelayedTask( |
| 120 const tracked_objects::Location& from_here, | 163 const tracked_objects::Location& from_here, |
| 121 const Closure& task, | 164 const Closure& task, |
| 122 TimeDelta delay) { | 165 TimeDelta delay) { |
| 123 base::AutoLock scoped_lock(tasks_lock_); | 166 AutoLock scoped_lock(tasks_lock_); |
| 124 tasks_.push( | 167 tasks_.push(TestPendingTask(from_here, task, now_ticks_, delay, |
| 125 TestPendingTask(from_here, task, now_, delay, TestPendingTask::NESTABLE)); | 168 TestPendingTask::NESTABLE)); |
| 126 return true; | 169 return true; |
| 127 } | 170 } |
| 128 | 171 |
| 129 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( | 172 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( |
| 130 const tracked_objects::Location& from_here, | 173 const tracked_objects::Location& from_here, |
| 131 const Closure& task, | 174 const Closure& task, |
| 132 TimeDelta delay) { | 175 TimeDelta delay) { |
| 133 NOTREACHED(); | 176 NOTREACHED(); |
| 134 return false; | 177 return false; |
| 135 } | 178 } |
| 136 | 179 |
| 137 void TestMockTimeTaskRunner::OnBeforeSelectingTask() { | 180 void TestMockTimeTaskRunner::OnBeforeSelectingTask() { |
| 138 // Empty default implementation. | 181 // Empty default implementation. |
| 139 } | 182 } |
| 140 | 183 |
| 141 void TestMockTimeTaskRunner::OnAfterTimePassed() { | 184 void TestMockTimeTaskRunner::OnAfterTimePassed() { |
| 142 // Empty default implementation. | 185 // Empty default implementation. |
| 143 } | 186 } |
| 144 | 187 |
| 145 void TestMockTimeTaskRunner::OnAfterTaskRun() { | 188 void TestMockTimeTaskRunner::OnAfterTaskRun() { |
| 146 // Empty default implementation. | 189 // Empty default implementation. |
| 147 } | 190 } |
| 148 | 191 |
| 149 bool TestMockTimeTaskRunner::DequeueNextTask(const base::TimeTicks& reference, | 192 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) { |
| 150 const base::TimeDelta& max_delta, | 193 now_ += later_ticks - now_ticks_; |
|
Lei Zhang
2015/02/04 20:27:37
Can this make time move backwards? In the original
engedy
2015/02/05 10:22:57
Hmm, yes, if the consumer of this class posts task
| |
| 194 if (now_ticks_ < later_ticks) { | |
| 195 now_ticks_ = later_ticks; | |
| 196 OnAfterTimePassed(); | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference, | |
| 201 const TimeDelta& max_delta, | |
| 151 TestPendingTask* next_task) { | 202 TestPendingTask* next_task) { |
| 152 base::AutoLock scoped_lock(tasks_lock_); | 203 AutoLock scoped_lock(tasks_lock_); |
| 153 if (!tasks_.empty() && | 204 if (!tasks_.empty() && |
| 154 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { | 205 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { |
| 155 *next_task = tasks_.top(); | 206 *next_task = tasks_.top(); |
| 156 tasks_.pop(); | 207 tasks_.pop(); |
| 157 return true; | 208 return true; |
| 158 } | 209 } |
| 159 return false; | 210 return false; |
| 160 } | 211 } |
| 161 | 212 |
| 162 } // namespace base | 213 } // namespace base |
| OLD | NEW |