1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
base / timer / wall_clock_timer_unittest.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/timer/wall_clock_timer.h"
#include <memory>
#include <utility>
#include "base/test/mock_callback.h"
#include "base/test/power_monitor_test.h"
#include "base/test/simple_test_clock.h"
#include "base/test/task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
class WallClockTimerTest : public ::testing::Test {
protected:
// Fast-forwards virtual time by |delta|. If |with_power| is true, both
// |clock_| and |task_environment_| time will be fast-forwarded. Otherwise,
// only |clock_| time will be changed to mimic the behavior when machine is
// suspended.
// Power event will be triggered if |with_power| is set to false.
void FastForwardBy(base::TimeDelta delay, bool with_power = true) {
if (!with_power)
fake_power_monitor_source_.Suspend();
clock_.Advance(delay);
if (with_power) {
task_environment_.FastForwardBy(delay);
} else {
fake_power_monitor_source_.Resume();
task_environment_.RunUntilIdle();
}
}
base::test::ScopedPowerMonitorTestSource fake_power_monitor_source_;
base::test::SingleThreadTaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
base::SimpleTestClock clock_;
};
TEST_F(WallClockTimerTest, PowerResume) {
::testing::StrictMock<base::MockOnceClosure> callback;
// Set up a WallClockTimer that will fire in one minute.
WallClockTimer wall_clock_timer(&clock_,
task_environment_.GetMockTickClock());
constexpr auto delay = base::Minutes(1);
const auto start_time = base::Time::Now();
const auto run_time = start_time + delay;
clock_.SetNow(start_time);
wall_clock_timer.Start(FROM_HERE, run_time, callback.Get());
EXPECT_EQ(wall_clock_timer.desired_run_time(), start_time + delay);
// Pretend that time jumps forward 30 seconds while the machine is suspended.
constexpr auto past_time = base::Seconds(30);
FastForwardBy(past_time, /*with_power=*/false);
// Ensure that the timer has not yet fired.
::testing::Mock::VerifyAndClearExpectations(&callback);
EXPECT_EQ(wall_clock_timer.desired_run_time(), start_time + delay);
// Expect that the timer fires at the desired run time.
EXPECT_CALL(callback, Run());
// Both Time::Now() and |task_environment_| MockTickClock::Now()
// go forward by (|delay| - |past_time|):
FastForwardBy(delay - past_time);
::testing::Mock::VerifyAndClearExpectations(&callback);
EXPECT_FALSE(wall_clock_timer.IsRunning());
}
TEST_F(WallClockTimerTest, UseTimerTwiceInRow) {
::testing::StrictMock<base::MockOnceClosure> first_callback;
::testing::StrictMock<base::MockOnceClosure> second_callback;
const auto start_time = base::Time::Now();
clock_.SetNow(start_time);
// Set up a WallClockTimer that will invoke |first_callback| in one minute.
// Once it's done, it will invoke |second_callback| after the other minute.
WallClockTimer wall_clock_timer(&clock_,
task_environment_.GetMockTickClock());
constexpr auto delay = base::Minutes(1);
wall_clock_timer.Start(FROM_HERE, clock_.Now() + delay, first_callback.Get());
EXPECT_CALL(first_callback, Run())
.WillOnce(::testing::InvokeWithoutArgs(
[this, &wall_clock_timer, &second_callback, delay] {
wall_clock_timer.Start(FROM_HERE, clock_.Now() + delay,
second_callback.Get());
}));
FastForwardBy(delay);
::testing::Mock::VerifyAndClearExpectations(&first_callback);
::testing::Mock::VerifyAndClearExpectations(&second_callback);
// When the |wall_clock_time| is used for the second time, it can still handle
// power suspension properly.
constexpr auto past_time = base::Seconds(30);
FastForwardBy(past_time, /*with_power=*/false);
::testing::Mock::VerifyAndClearExpectations(&second_callback);
EXPECT_CALL(second_callback, Run());
FastForwardBy(delay - past_time);
::testing::Mock::VerifyAndClearExpectations(&second_callback);
}
TEST_F(WallClockTimerTest, Stop) {
::testing::StrictMock<base::MockOnceClosure> callback;
clock_.SetNow(base::Time::Now());
// Set up a WallClockTimer.
WallClockTimer wall_clock_timer(&clock_,
task_environment_.GetMockTickClock());
constexpr auto delay = base::Minutes(1);
wall_clock_timer.Start(FROM_HERE, clock_.Now() + delay, callback.Get());
// After 20 seconds, timer is stopped.
constexpr auto past_time = base::Seconds(20);
FastForwardBy(past_time);
EXPECT_TRUE(wall_clock_timer.IsRunning());
wall_clock_timer.Stop();
EXPECT_FALSE(wall_clock_timer.IsRunning());
// When power is suspends and resumed, timer won't be resumed.
FastForwardBy(past_time, /*with_power=*/false);
EXPECT_FALSE(wall_clock_timer.IsRunning());
// Timer won't fire when desired run time is reached.
FastForwardBy(delay - past_time * 2);
::testing::Mock::VerifyAndClearExpectations(&callback);
}
TEST_F(WallClockTimerTest, RestartRunningTimer) {
::testing::StrictMock<base::MockOnceClosure> first_callback;
::testing::StrictMock<base::MockOnceClosure> second_callback;
constexpr auto delay = base::Minutes(1);
// Set up a WallClockTimer that will invoke |first_callback| in one minute.
clock_.SetNow(base::Time::Now());
WallClockTimer wall_clock_timer(&clock_,
task_environment_.GetMockTickClock());
wall_clock_timer.Start(FROM_HERE, clock_.Now() + delay, first_callback.Get());
// After 30 seconds, replace the timer with |second_callback| with new one
// minute delay.
constexpr auto past_time = delay / 2;
FastForwardBy(past_time);
wall_clock_timer.Start(FROM_HERE, clock_.Now() + delay,
second_callback.Get());
// |first_callback| is due but it won't be called because it's replaced.
FastForwardBy(past_time);
::testing::Mock::VerifyAndClearExpectations(&first_callback);
::testing::Mock::VerifyAndClearExpectations(&second_callback);
// Timer invokes the |second_callback|.
EXPECT_CALL(second_callback, Run());
FastForwardBy(past_time);
::testing::Mock::VerifyAndClearExpectations(&first_callback);
::testing::Mock::VerifyAndClearExpectations(&second_callback);
}
TEST_F(WallClockTimerTest, DoubleStop) {
::testing::StrictMock<base::MockOnceClosure> callback;
clock_.SetNow(base::Time::Now());
// Set up a WallClockTimer.
WallClockTimer wall_clock_timer(&clock_,
task_environment_.GetMockTickClock());
constexpr auto delay = base::Minutes(1);
wall_clock_timer.Start(FROM_HERE, clock_.Now() + delay, callback.Get());
// After 15 seconds, timer is stopped.
constexpr auto past_time = delay / 4;
FastForwardBy(past_time);
EXPECT_TRUE(wall_clock_timer.IsRunning());
wall_clock_timer.Stop();
EXPECT_FALSE(wall_clock_timer.IsRunning());
// And timer is stopped again later. The second stop should be a no-op.
FastForwardBy(past_time);
EXPECT_FALSE(wall_clock_timer.IsRunning());
wall_clock_timer.Stop();
EXPECT_FALSE(wall_clock_timer.IsRunning());
// Timer won't fire after stop.
FastForwardBy(past_time, /*with_power=*/false);
FastForwardBy(delay - past_time * 3);
::testing::Mock::VerifyAndClearExpectations(&callback);
}
// On some platforms, TickClock will never freeze. WallClockTimer are still
// supported on those platforms.
TEST_F(WallClockTimerTest, NonStopTickClock) {
::testing::StrictMock<base::MockOnceClosure> callback;
// Set up a WallClockTimer that will fire in one minute.
WallClockTimer wall_clock_timer(&clock_,
task_environment_.GetMockTickClock());
constexpr auto delay = base::Minutes(1);
const auto start_time = base::Time::Now();
const auto run_time = start_time + delay;
clock_.SetNow(start_time);
wall_clock_timer.Start(FROM_HERE, run_time, callback.Get());
EXPECT_EQ(wall_clock_timer.desired_run_time(), start_time + delay);
// Pretend that time jumps forward 30 seconds while the machine is suspended.
constexpr auto past_time = base::Seconds(30);
// Fastword with both clocks even the power is suspended.
fake_power_monitor_source_.Suspend();
clock_.SetNow(clock_.Now() + past_time);
task_environment_.FastForwardBy(past_time);
fake_power_monitor_source_.Resume();
// Ensure that the timer has not yet fired.
::testing::Mock::VerifyAndClearExpectations(&callback);
EXPECT_EQ(wall_clock_timer.desired_run_time(), start_time + delay);
// Expect that the timer fires at the desired run time.
EXPECT_CALL(callback, Run());
// Both Time::Now() and |task_environment_| MockTickClock::Now()
// go forward by (|delay| - |past_time|):
FastForwardBy(delay - past_time);
::testing::Mock::VerifyAndClearExpectations(&callback);
EXPECT_FALSE(wall_clock_timer.IsRunning());
}
TEST_F(WallClockTimerTest, NonStopTickClockWithLongPause) {
::testing::StrictMock<base::MockOnceClosure> callback;
// Set up a WallClockTimer that will fire in one minute.
WallClockTimer wall_clock_timer(&clock_,
task_environment_.GetMockTickClock());
constexpr auto delay = base::Minutes(1);
const auto start_time = base::Time::Now();
const auto run_time = start_time + delay;
clock_.SetNow(start_time);
wall_clock_timer.Start(FROM_HERE, run_time, callback.Get());
EXPECT_EQ(wall_clock_timer.desired_run_time(), start_time + delay);
// Pretend that time jumps forward 60 seconds while the machine is suspended.
constexpr auto past_time = base::Seconds(60);
// Fastword with both clocks even the power is suspended. Timer fires at the
// moment of power resume.
EXPECT_CALL(callback, Run());
fake_power_monitor_source_.Suspend();
clock_.SetNow(clock_.Now() + past_time);
task_environment_.FastForwardBy(past_time);
fake_power_monitor_source_.Resume();
::testing::Mock::VerifyAndClearExpectations(&callback);
EXPECT_FALSE(wall_clock_timer.IsRunning());
}
} // namespace base