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

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

Issue 823143004: Add TestMockTimeTaskRunner in base/test. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add synchronization for PostTask(). Created 5 years, 11 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
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/test/test_mock_time_task_runner.h"
6
7 #include "base/callback.h"
bartfab (slow) 2015/01/08 14:14:34 Nit: Do you actually need this? You do not seem to
engedy 2015/01/08 16:41:25 Apparently not. Removed.
8
9 namespace base {
10
11 namespace {
12
13 // TickClock that always returns the then-current mock time of |task_runner| as
14 // the current time.
15 class CorrespondingMockTickClock : public TickClock {
bartfab (slow) 2015/01/08 14:14:34 Nit: I am not sure "Corresponding" is a useful par
engedy 2015/01/08 16:41:25 Removed that part. Done.
16 public:
17 explicit CorrespondingMockTickClock(
18 scoped_refptr<const TestMockTimeTaskRunner> task_runner);
bartfab (slow) 2015/01/08 14:14:34 Nit: #include "base/memory/ref_counted.h"
engedy 2015/01/08 16:41:25 Done.
19 virtual ~CorrespondingMockTickClock();
20
21 // TickClock:
22 virtual TimeTicks NowTicks() override;
23
24 private:
25 scoped_refptr<const TestMockTimeTaskRunner> task_runner_;
26
27 DISALLOW_COPY_AND_ASSIGN(CorrespondingMockTickClock);
28 };
29
30 CorrespondingMockTickClock::CorrespondingMockTickClock(
31 scoped_refptr<const TestMockTimeTaskRunner> task_runner)
32 : task_runner_(task_runner) {
33 }
34
35 CorrespondingMockTickClock::~CorrespondingMockTickClock() {
36 }
37
38 TimeTicks CorrespondingMockTickClock::NowTicks() {
39 return task_runner_->GetCurrentMockTime();
40 }
41
42 } // namespace
43
44 bool TestMockTimeTaskRunner::TemporalOrder::operator()(
45 const TestPendingTask& first_task,
46 const TestPendingTask& second_task) const {
47 return first_task.GetTimeToRun() > second_task.GetTimeToRun();
48 }
49
50 TestMockTimeTaskRunner::TestMockTimeTaskRunner() {
51 }
52
53 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() {
54 }
55
56 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) {
57 DCHECK(thread_checker_.CalledOnValidThread());
bartfab (slow) 2015/01/08 14:14:34 Nit: #include "base/logging.h"
engedy 2015/01/08 16:41:25 Done.
58
59 OnBeforeSelectingTask();
60
61 base::TimeTicks original_now = now_;
bartfab (slow) 2015/01/08 14:14:34 Nit: const.
engedy 2015/01/08 16:41:25 Done.
62 TestPendingTask task_info;
63 while (DequeueNextTask(original_now, delta, &task_info)) {
64 if (task_info.GetTimeToRun() - now_ > base::TimeDelta()) {
65 now_ = task_info.GetTimeToRun();
66 OnAfterTimePasses();
67 }
68
69 task_info.task.Run();
70
71 OnAfterRunningTask();
72 OnBeforeSelectingTask();
73 }
74
75 if (!delta.is_max() && now_ - original_now < delta) {
76 now_ = original_now + delta;
77 OnAfterTimePasses();
78 }
79 }
80
81 void TestMockTimeTaskRunner::RunUntilIdle() {
82 FastForwardBy(TimeDelta());
83 }
84
85 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() {
86 FastForwardBy(TimeDelta::Max());
87 }
88
89 TimeTicks TestMockTimeTaskRunner::GetCurrentMockTime() const {
90 DCHECK(thread_checker_.CalledOnValidThread());
91 return now_;
92 }
93
94 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const {
95 DCHECK(thread_checker_.CalledOnValidThread());
96 return make_scoped_ptr(new CorrespondingMockTickClock(this));
97 }
98
99 bool TestMockTimeTaskRunner::HasPendingTask() const {
100 DCHECK(thread_checker_.CalledOnValidThread());
101 return !tasks_.empty();
102 }
103
104 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const {
105 DCHECK(thread_checker_.CalledOnValidThread());
106 return tasks_.empty() ? TimeDelta::Max() : tasks_.top().GetTimeToRun() - now_;
107 }
108
109 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const {
110 DCHECK(thread_checker_.CalledOnValidThread());
bartfab (slow) 2015/01/08 14:14:34 Nit: You could actually trivially make this method
engedy 2015/01/08 16:41:25 Nice. Done.
111 return true;
112 }
113
114 bool TestMockTimeTaskRunner::PostDelayedTask(
115 const tracked_objects::Location& from_here,
116 const Closure& task,
117 TimeDelta delay) {
118 base::AutoLock scoped_lock(tasks_lock_);
119 tasks_.push(
120 TestPendingTask(from_here, task, now_, delay, TestPendingTask::NESTABLE));
121 return true;
122 }
123
124 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask(
125 const tracked_objects::Location& from_here,
126 const Closure& task,
127 TimeDelta delay) {
128 NOTREACHED();
129 return false;
130 }
131
132 void TestMockTimeTaskRunner::OnBeforeSelectingTask() {
133 // Empty default implementation.
134 }
135
136 void TestMockTimeTaskRunner::OnAfterTimePasses() {
137 // Empty default implementation.
138 }
139
140 void TestMockTimeTaskRunner::OnAfterRunningTask() {
141 // Empty default implementation.
142 }
143
144 bool TestMockTimeTaskRunner::DequeueNextTask(const base::TimeTicks& reference,
145 const base::TimeDelta& max_delta,
146 TestPendingTask* next_task) {
147 base::AutoLock scoped_lock(tasks_lock_);
148 if (!tasks_.empty() &&
149 (tasks_.top().GetTimeToRun() - reference) <= max_delta) {
150 *next_task = tasks_.top();
151 tasks_.pop();
152 return true;
153 }
154 return false;
155 }
156
157 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698