Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(510)

Side by Side Diff: base/test/test_mock_time_task_runner.cc

Issue 899863002: Add support in TestMockTimeTaskRunner for vending out mock Time and mock Clocks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Start ticks from 0, some tests depend on this. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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()
79 : now_(Time::UnixEpoch()) {
bartfab (slow) 2015/02/16 13:40:34 Nit: Why break this line? It fits on one line.
engedy 2015/02/16 13:49:29 Done in https://codereview.chromium.org/929923003.
52 } 80 }
53 81
54 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() { 82 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() {
55 } 83 }
56 84
57 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) { 85 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) {
58 DCHECK(thread_checker_.CalledOnValidThread()); 86 DCHECK(thread_checker_.CalledOnValidThread());
87 DCHECK_GE(delta, TimeDelta());
59 88
60 OnBeforeSelectingTask(); 89 const TimeTicks original_now_ticks = now_ticks_;
61 90 ProcessAllTasksNoLaterThan(delta);
62 const base::TimeTicks original_now = now_; 91 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 } 92 }
81 93
82 void TestMockTimeTaskRunner::RunUntilIdle() { 94 void TestMockTimeTaskRunner::RunUntilIdle() {
83 FastForwardBy(TimeDelta()); 95 DCHECK(thread_checker_.CalledOnValidThread());
96 ProcessAllTasksNoLaterThan(TimeDelta());
84 } 97 }
85 98
86 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() { 99 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() {
87 FastForwardBy(TimeDelta::Max()); 100 DCHECK(thread_checker_.CalledOnValidThread());
101 ProcessAllTasksNoLaterThan(TimeDelta::Max());
88 } 102 }
89 103
90 TimeTicks TestMockTimeTaskRunner::GetCurrentMockTime() const { 104 Time TestMockTimeTaskRunner::Now() const {
91 DCHECK(thread_checker_.CalledOnValidThread()); 105 DCHECK(thread_checker_.CalledOnValidThread());
92 return now_; 106 return now_;
93 } 107 }
94 108
109 TimeTicks TestMockTimeTaskRunner::NowTicks() const {
110 DCHECK(thread_checker_.CalledOnValidThread());
111 return now_ticks_;
112 }
113
114 scoped_ptr<Clock> TestMockTimeTaskRunner::GetMockClock() const {
115 DCHECK(thread_checker_.CalledOnValidThread());
116 return make_scoped_ptr(new MockClock(this));
117 }
118
95 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { 119 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const {
96 DCHECK(thread_checker_.CalledOnValidThread()); 120 DCHECK(thread_checker_.CalledOnValidThread());
97 return make_scoped_ptr(new MockTickClock(this)); 121 return make_scoped_ptr(new MockTickClock(this));
98 } 122 }
99 123
100 bool TestMockTimeTaskRunner::HasPendingTask() const { 124 bool TestMockTimeTaskRunner::HasPendingTask() const {
101 DCHECK(thread_checker_.CalledOnValidThread()); 125 DCHECK(thread_checker_.CalledOnValidThread());
102 return !tasks_.empty(); 126 return !tasks_.empty();
103 } 127 }
104 128
105 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const { 129 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const {
106 DCHECK(thread_checker_.CalledOnValidThread()); 130 DCHECK(thread_checker_.CalledOnValidThread());
107 return tasks_.size(); 131 return tasks_.size();
108 } 132 }
109 133
110 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { 134 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const {
111 DCHECK(thread_checker_.CalledOnValidThread()); 135 DCHECK(thread_checker_.CalledOnValidThread());
112 return tasks_.empty() ? TimeDelta::Max() : tasks_.top().GetTimeToRun() - now_; 136 return tasks_.empty() ? TimeDelta::Max()
137 : tasks_.top().GetTimeToRun() - now_ticks_;
113 } 138 }
114 139
115 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { 140 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const {
116 return thread_checker_.CalledOnValidThread(); 141 return thread_checker_.CalledOnValidThread();
117 } 142 }
118 143
119 bool TestMockTimeTaskRunner::PostDelayedTask( 144 bool TestMockTimeTaskRunner::PostDelayedTask(
120 const tracked_objects::Location& from_here, 145 const tracked_objects::Location& from_here,
121 const Closure& task, 146 const Closure& task,
122 TimeDelta delay) { 147 TimeDelta delay) {
123 base::AutoLock scoped_lock(tasks_lock_); 148 AutoLock scoped_lock(tasks_lock_);
124 tasks_.push( 149 tasks_.push(TestPendingTask(from_here, task, now_ticks_, delay,
125 TestPendingTask(from_here, task, now_, delay, TestPendingTask::NESTABLE)); 150 TestPendingTask::NESTABLE));
126 return true; 151 return true;
127 } 152 }
128 153
129 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( 154 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask(
130 const tracked_objects::Location& from_here, 155 const tracked_objects::Location& from_here,
131 const Closure& task, 156 const Closure& task,
132 TimeDelta delay) { 157 TimeDelta delay) {
133 NOTREACHED(); 158 NOTREACHED();
134 return false; 159 return false;
135 } 160 }
136 161
137 void TestMockTimeTaskRunner::OnBeforeSelectingTask() { 162 void TestMockTimeTaskRunner::OnBeforeSelectingTask() {
138 // Empty default implementation. 163 // Empty default implementation.
139 } 164 }
140 165
141 void TestMockTimeTaskRunner::OnAfterTimePassed() { 166 void TestMockTimeTaskRunner::OnAfterTimePassed() {
142 // Empty default implementation. 167 // Empty default implementation.
143 } 168 }
144 169
145 void TestMockTimeTaskRunner::OnAfterTaskRun() { 170 void TestMockTimeTaskRunner::OnAfterTaskRun() {
146 // Empty default implementation. 171 // Empty default implementation.
147 } 172 }
148 173
149 bool TestMockTimeTaskRunner::DequeueNextTask(const base::TimeTicks& reference, 174 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) {
150 const base::TimeDelta& max_delta, 175 DCHECK_GE(max_delta, TimeDelta());
176 const TimeTicks original_now_ticks = now_ticks_;
177 while (true) {
178 OnBeforeSelectingTask();
179 TestPendingTask task_info;
180 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info))
181 break;
182 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will
183 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not
184 // moving the clock backwards in this case.
185 ForwardClocksUntilTickTime(task_info.GetTimeToRun());
186 task_info.task.Run();
187 OnAfterTaskRun();
188 }
189 }
190
191 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) {
192 if (later_ticks <= now_ticks_)
193 return;
194
195 now_ += later_ticks - now_ticks_;
196 now_ticks_ = later_ticks;
197 OnAfterTimePassed();
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698