blob: 8731cb0ba735788c990b024b7a0a9a9de50ab2b1 [file] [log] [blame]
[email protected]afb84fc2014-05-29 01:47:531// 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
Brett Wilson55ff1475e2017-09-26 00:28:485#include "cc/base/delayed_unique_notifier.h"
[email protected]afb84fc2014-05-29 01:47:536#include "base/bind.h"
7#include "base/bind_helpers.h"
Brett Wilson55ff1475e2017-09-26 00:28:488#include "base/containers/circular_deque.h"
[email protected]afb84fc2014-05-29 01:47:539#include "base/test/test_pending_task.h"
10#include "base/test/test_simple_task_runner.h"
[email protected]afb84fc2014-05-29 01:47:5311#include "testing/gtest/include/gtest/gtest.h"
12
13namespace cc {
14namespace {
15
16class TestNotifier : public DelayedUniqueNotifier {
17 public:
18 TestNotifier(base::SequencedTaskRunner* task_runner,
19 const base::Closure& closure,
20 const base::TimeDelta& delay)
21 : DelayedUniqueNotifier(task_runner, closure, delay) {}
Chris Watkinsf6353292017-12-04 02:36:0522 ~TestNotifier() override = default;
[email protected]afb84fc2014-05-29 01:47:5323
24 // Overridden from DelayedUniqueNotifier:
dcheng716bedf2014-10-21 09:51:0825 base::TimeTicks Now() const override { return now_; }
[email protected]afb84fc2014-05-29 01:47:5326
27 void SetNow(base::TimeTicks now) { now_ = now; }
28
29 private:
30 base::TimeTicks now_;
31};
32
33class DelayedUniqueNotifierTest : public testing::Test {
34 public:
35 DelayedUniqueNotifierTest() : notification_count_(0) {}
36
danakjaeb95062014-11-14 01:35:3637 void SetUp() override {
[email protected]afb84fc2014-05-29 01:47:5338 notification_count_ = 0;
kylechar96f3eba2017-09-25 20:23:5639 task_runner_ = base::MakeRefCounted<base::TestSimpleTaskRunner>();
[email protected]afb84fc2014-05-29 01:47:5340 }
41
42 void Notify() { ++notification_count_; }
43
44 int NotificationCount() const { return notification_count_; }
45
Brett Wilson55ff1475e2017-09-26 00:28:4846 base::circular_deque<base::TestPendingTask> TakePendingTasks() {
tzik2d1f91c2016-10-04 03:58:0547 return task_runner_->TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:5348 }
49
50 protected:
51 int notification_count_;
52 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
53};
54
55TEST_F(DelayedUniqueNotifierTest, ZeroDelay) {
Yuri Wiitalad0611762017-07-22 02:33:2156 base::TimeDelta delay; // Zero delay.
[email protected]afb84fc2014-05-29 01:47:5357 TestNotifier notifier(
dcheng80bc5e3c2014-08-26 03:54:1858 task_runner_.get(),
[email protected]afb84fc2014-05-29 01:47:5359 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)),
60 delay);
61
62 EXPECT_EQ(0, NotificationCount());
63
64 // Basic schedule for |delay| from now.
65 base::TimeTicks schedule_time =
Yuri Wiitalad0611762017-07-22 02:33:2166 base::TimeTicks() + base::TimeDelta::FromMicroseconds(10);
[email protected]afb84fc2014-05-29 01:47:5367
68 notifier.SetNow(schedule_time);
69 notifier.Schedule();
70
Brett Wilson55ff1475e2017-09-26 00:28:4871 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:5372 ASSERT_EQ(1u, tasks.size());
73 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
74
tzika6f0007a2017-01-27 04:01:1175 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:5376 EXPECT_EQ(1, NotificationCount());
77
78 // 5 schedules should result in only one run.
79 for (int i = 0; i < 5; ++i)
80 notifier.Schedule();
81
82 tasks = TakePendingTasks();
83 ASSERT_EQ(1u, tasks.size());
84 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
85
tzika6f0007a2017-01-27 04:01:1186 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:5387 EXPECT_EQ(2, NotificationCount());
88}
89
90TEST_F(DelayedUniqueNotifierTest, SmallDelay) {
Yuri Wiitalad0611762017-07-22 02:33:2191 base::TimeDelta delay = base::TimeDelta::FromMicroseconds(20);
[email protected]afb84fc2014-05-29 01:47:5392 TestNotifier notifier(
dcheng80bc5e3c2014-08-26 03:54:1893 task_runner_.get(),
[email protected]afb84fc2014-05-29 01:47:5394 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)),
95 delay);
96
97 EXPECT_EQ(0, NotificationCount());
98
99 // Basic schedule for |delay| from now (now: 30, run time: 50).
100 base::TimeTicks schedule_time =
Yuri Wiitalad0611762017-07-22 02:33:21101 base::TimeTicks() + base::TimeDelta::FromMicroseconds(30);
[email protected]afb84fc2014-05-29 01:47:53102
103 notifier.SetNow(schedule_time);
104 notifier.Schedule();
105
Brett Wilson55ff1475e2017-09-26 00:28:48106 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:53107
108 ASSERT_EQ(1u, tasks.size());
109 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
110
111 // It's not yet time to run, so we expect no notifications.
tzika6f0007a2017-01-27 04:01:11112 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53113 EXPECT_EQ(0, NotificationCount());
114
115 tasks = TakePendingTasks();
116
117 ASSERT_EQ(1u, tasks.size());
118 // Now the time should be delay minus whatever the value of now happens to be
119 // (now: 30, run time: 50).
120 base::TimeTicks scheduled_run_time = notifier.Now() + delay;
121 base::TimeTicks scheduled_delay =
122 base::TimeTicks() + (scheduled_run_time - notifier.Now());
123 EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun());
124
125 // Move closer to the run time (time: 49, run time: 50).
Yuri Wiitalad0611762017-07-22 02:33:21126 notifier.SetNow(notifier.Now() + base::TimeDelta::FromMicroseconds(19));
[email protected]afb84fc2014-05-29 01:47:53127
128 // It's not yet time to run, so we expect no notifications.
tzika6f0007a2017-01-27 04:01:11129 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53130 EXPECT_EQ(0, NotificationCount());
131
132 tasks = TakePendingTasks();
133 ASSERT_EQ(1u, tasks.size());
134
135 // Now the time should be delay minus whatever the value of now happens to be.
136 scheduled_delay = base::TimeTicks() + (scheduled_run_time - notifier.Now());
137 EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun());
138
139 // Move to exactly the run time (time: 50, run time: 50).
Yuri Wiitalad0611762017-07-22 02:33:21140 notifier.SetNow(notifier.Now() + base::TimeDelta::FromMicroseconds(1));
[email protected]afb84fc2014-05-29 01:47:53141
142 // It's time to run!
tzika6f0007a2017-01-27 04:01:11143 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53144 EXPECT_EQ(1, NotificationCount());
145
146 tasks = TakePendingTasks();
147 EXPECT_EQ(0u, tasks.size());
148}
149
150TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) {
Yuri Wiitalad0611762017-07-22 02:33:21151 base::TimeDelta delay = base::TimeDelta::FromMicroseconds(20);
[email protected]afb84fc2014-05-29 01:47:53152 TestNotifier notifier(
dcheng80bc5e3c2014-08-26 03:54:18153 task_runner_.get(),
[email protected]afb84fc2014-05-29 01:47:53154 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)),
155 delay);
156
157 base::TimeTicks schedule_time;
158 // Move time 19 units forward and reschedule, expecting that we still need to
159 // run in |delay| time and we don't get a notification.
160 for (int i = 0; i < 10; ++i) {
161 EXPECT_EQ(0, NotificationCount());
162
163 // Move time forward 19 units.
Yuri Wiitalad0611762017-07-22 02:33:21164 schedule_time = notifier.Now() + base::TimeDelta::FromMicroseconds(19);
[email protected]afb84fc2014-05-29 01:47:53165 notifier.SetNow(schedule_time);
166 notifier.Schedule();
167
Brett Wilson55ff1475e2017-09-26 00:28:48168 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:53169
170 ASSERT_EQ(1u, tasks.size());
171 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
172
173 // It's not yet time to run, so we expect no notifications.
tzika6f0007a2017-01-27 04:01:11174 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53175 EXPECT_EQ(0, NotificationCount());
176 }
177
178 // Move time forward 20 units, expecting a notification.
Yuri Wiitalad0611762017-07-22 02:33:21179 schedule_time = notifier.Now() + base::TimeDelta::FromMicroseconds(20);
[email protected]afb84fc2014-05-29 01:47:53180 notifier.SetNow(schedule_time);
181
Brett Wilson55ff1475e2017-09-26 00:28:48182 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:53183
184 ASSERT_EQ(1u, tasks.size());
185 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
186
187 // Time to run!
tzika6f0007a2017-01-27 04:01:11188 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53189 EXPECT_EQ(1, NotificationCount());
190}
191
[email protected]0a1028722014-05-30 10:08:03192TEST_F(DelayedUniqueNotifierTest, CancelAndHasPendingNotification) {
Yuri Wiitalad0611762017-07-22 02:33:21193 base::TimeDelta delay = base::TimeDelta::FromMicroseconds(20);
[email protected]afb84fc2014-05-29 01:47:53194 TestNotifier notifier(
dcheng80bc5e3c2014-08-26 03:54:18195 task_runner_.get(),
[email protected]afb84fc2014-05-29 01:47:53196 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)),
197 delay);
198
199 EXPECT_EQ(0, NotificationCount());
200
201 // Schedule for |delay| seconds from now.
202 base::TimeTicks schedule_time =
Yuri Wiitalad0611762017-07-22 02:33:21203 notifier.Now() + base::TimeDelta::FromMicroseconds(10);
[email protected]afb84fc2014-05-29 01:47:53204 notifier.SetNow(schedule_time);
205 notifier.Schedule();
[email protected]0a1028722014-05-30 10:08:03206 EXPECT_TRUE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53207
208 // Cancel the run.
209 notifier.Cancel();
[email protected]0a1028722014-05-30 10:08:03210 EXPECT_FALSE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53211
Brett Wilson55ff1475e2017-09-26 00:28:48212 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:53213
214 ASSERT_EQ(1u, tasks.size());
215 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
216
217 // Time to run, but a canceled task!
tzika6f0007a2017-01-27 04:01:11218 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53219 EXPECT_EQ(0, NotificationCount());
[email protected]0a1028722014-05-30 10:08:03220 EXPECT_FALSE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53221
222 tasks = TakePendingTasks();
223 EXPECT_EQ(0u, tasks.size());
224
225 notifier.Schedule();
[email protected]0a1028722014-05-30 10:08:03226 EXPECT_TRUE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53227 tasks = TakePendingTasks();
228
229 ASSERT_EQ(1u, tasks.size());
230 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
231
232 // Advance the time.
233 notifier.SetNow(notifier.Now() + delay);
234
[email protected]0a1028722014-05-30 10:08:03235 // This should run since it wasn't canceled.
tzika6f0007a2017-01-27 04:01:11236 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53237 EXPECT_EQ(1, NotificationCount());
[email protected]0a1028722014-05-30 10:08:03238 EXPECT_FALSE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53239
[email protected]0a1028722014-05-30 10:08:03240 for (int i = 0; i < 10; ++i) {
[email protected]afb84fc2014-05-29 01:47:53241 notifier.Schedule();
[email protected]0a1028722014-05-30 10:08:03242 EXPECT_TRUE(notifier.HasPendingNotification());
243 notifier.Cancel();
244 EXPECT_FALSE(notifier.HasPendingNotification());
245 }
[email protected]afb84fc2014-05-29 01:47:53246
[email protected]afb84fc2014-05-29 01:47:53247 tasks = TakePendingTasks();
248
249 ASSERT_EQ(1u, tasks.size());
250 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
251
252 // Time to run, but a canceled task!
253 notifier.SetNow(notifier.Now() + delay);
tzika6f0007a2017-01-27 04:01:11254 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53255 EXPECT_EQ(1, NotificationCount());
256
257 tasks = TakePendingTasks();
258 EXPECT_EQ(0u, tasks.size());
[email protected]0a1028722014-05-30 10:08:03259 EXPECT_FALSE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53260}
261
danakj1a3c3172014-10-29 18:24:49262TEST_F(DelayedUniqueNotifierTest, ShutdownWithScheduledTask) {
Yuri Wiitalad0611762017-07-22 02:33:21263 base::TimeDelta delay = base::TimeDelta::FromMicroseconds(20);
danakj1a3c3172014-10-29 18:24:49264 TestNotifier notifier(
265 task_runner_.get(),
266 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)),
267 delay);
268
269 EXPECT_EQ(0, NotificationCount());
270
271 // Schedule for |delay| seconds from now.
272 base::TimeTicks schedule_time =
Yuri Wiitalad0611762017-07-22 02:33:21273 notifier.Now() + base::TimeDelta::FromMicroseconds(10);
danakj1a3c3172014-10-29 18:24:49274 notifier.SetNow(schedule_time);
275 notifier.Schedule();
276 EXPECT_TRUE(notifier.HasPendingNotification());
277
278 // Shutdown the notifier.
279 notifier.Shutdown();
280
281 // The task is still there, but...
Brett Wilson55ff1475e2017-09-26 00:28:48282 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
danakj1a3c3172014-10-29 18:24:49283 ASSERT_EQ(1u, tasks.size());
284
285 // Running the task after shutdown does nothing since it's cancelled.
tzika6f0007a2017-01-27 04:01:11286 std::move(tasks[0].task).Run();
danakj1a3c3172014-10-29 18:24:49287 EXPECT_EQ(0, NotificationCount());
288
289 tasks = TakePendingTasks();
290 EXPECT_EQ(0u, tasks.size());
291
292 // We are no longer able to schedule tasks.
293 notifier.Schedule();
294 tasks = TakePendingTasks();
295 ASSERT_EQ(0u, tasks.size());
296
297 // Verify after the scheduled time happens there is still no task.
298 notifier.SetNow(notifier.Now() + delay);
299 tasks = TakePendingTasks();
300 ASSERT_EQ(0u, tasks.size());
301}
302
303TEST_F(DelayedUniqueNotifierTest, ShutdownPreventsSchedule) {
Yuri Wiitalad0611762017-07-22 02:33:21304 base::TimeDelta delay = base::TimeDelta::FromMicroseconds(20);
danakj1a3c3172014-10-29 18:24:49305 TestNotifier notifier(
306 task_runner_.get(),
307 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)),
308 delay);
309
310 EXPECT_EQ(0, NotificationCount());
311
312 // Schedule for |delay| seconds from now.
313 base::TimeTicks schedule_time =
Yuri Wiitalad0611762017-07-22 02:33:21314 notifier.Now() + base::TimeDelta::FromMicroseconds(10);
danakj1a3c3172014-10-29 18:24:49315 notifier.SetNow(schedule_time);
316
317 // Shutdown the notifier.
318 notifier.Shutdown();
319
320 // Scheduling a task no longer does anything.
321 notifier.Schedule();
Brett Wilson55ff1475e2017-09-26 00:28:48322 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
danakj1a3c3172014-10-29 18:24:49323 ASSERT_EQ(0u, tasks.size());
324
325 // Verify after the scheduled time happens there is still no task.
326 notifier.SetNow(notifier.Now() + delay);
327 tasks = TakePendingTasks();
328 ASSERT_EQ(0u, tasks.size());
329}
330
[email protected]afb84fc2014-05-29 01:47:53331} // namespace
332} // namespace cc