blob: 102328049c66c577169e1dad052c7004553c80ee [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"
Sadrul Habib Chowdhury92b874b2020-09-10 09:12:066
7#include <utility>
8
[email protected]afb84fc2014-05-29 01:47:539#include "base/bind.h"
10#include "base/bind_helpers.h"
Brett Wilson55ff1475e2017-09-26 00:28:4811#include "base/containers/circular_deque.h"
[email protected]afb84fc2014-05-29 01:47:5312#include "base/test/test_pending_task.h"
13#include "base/test/test_simple_task_runner.h"
[email protected]afb84fc2014-05-29 01:47:5314#include "testing/gtest/include/gtest/gtest.h"
15
16namespace cc {
17namespace {
18
19class TestNotifier : public DelayedUniqueNotifier {
20 public:
21 TestNotifier(base::SequencedTaskRunner* task_runner,
kylechar4bb144d2019-01-11 20:42:0722 base::RepeatingClosure closure,
[email protected]afb84fc2014-05-29 01:47:5323 const base::TimeDelta& delay)
kylechar4bb144d2019-01-11 20:42:0724 : DelayedUniqueNotifier(task_runner, std::move(closure), delay) {}
Chris Watkinsf6353292017-12-04 02:36:0525 ~TestNotifier() override = default;
[email protected]afb84fc2014-05-29 01:47:5326
27 // Overridden from DelayedUniqueNotifier:
dcheng716bedf2014-10-21 09:51:0828 base::TimeTicks Now() const override { return now_; }
[email protected]afb84fc2014-05-29 01:47:5329
30 void SetNow(base::TimeTicks now) { now_ = now; }
31
32 private:
33 base::TimeTicks now_;
34};
35
36class DelayedUniqueNotifierTest : public testing::Test {
37 public:
38 DelayedUniqueNotifierTest() : notification_count_(0) {}
39
danakjaeb95062014-11-14 01:35:3640 void SetUp() override {
[email protected]afb84fc2014-05-29 01:47:5341 notification_count_ = 0;
kylechar96f3eba2017-09-25 20:23:5642 task_runner_ = base::MakeRefCounted<base::TestSimpleTaskRunner>();
[email protected]afb84fc2014-05-29 01:47:5343 }
44
45 void Notify() { ++notification_count_; }
46
47 int NotificationCount() const { return notification_count_; }
48
Brett Wilson55ff1475e2017-09-26 00:28:4849 base::circular_deque<base::TestPendingTask> TakePendingTasks() {
tzik2d1f91c2016-10-04 03:58:0550 return task_runner_->TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:5351 }
52
53 protected:
54 int notification_count_;
55 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
56};
57
[email protected]afb84fc2014-05-29 01:47:5358TEST_F(DelayedUniqueNotifierTest, SmallDelay) {
Yuri Wiitalad0611762017-07-22 02:33:2159 base::TimeDelta delay = base::TimeDelta::FromMicroseconds(20);
kylechar4bb144d2019-01-11 20:42:0760 TestNotifier notifier(task_runner_.get(),
61 base::BindRepeating(&DelayedUniqueNotifierTest::Notify,
62 base::Unretained(this)),
63 delay);
[email protected]afb84fc2014-05-29 01:47:5364
65 EXPECT_EQ(0, NotificationCount());
66
67 // Basic schedule for |delay| from now (now: 30, run time: 50).
68 base::TimeTicks schedule_time =
Yuri Wiitalad0611762017-07-22 02:33:2169 base::TimeTicks() + base::TimeDelta::FromMicroseconds(30);
[email protected]afb84fc2014-05-29 01:47:5370
71 notifier.SetNow(schedule_time);
72 notifier.Schedule();
73
Brett Wilson55ff1475e2017-09-26 00:28:4874 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:5375
76 ASSERT_EQ(1u, tasks.size());
77 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
78
79 // It's not yet time to run, so we expect no notifications.
tzika6f0007a2017-01-27 04:01:1180 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:5381 EXPECT_EQ(0, NotificationCount());
82
83 tasks = TakePendingTasks();
84
85 ASSERT_EQ(1u, tasks.size());
86 // Now the time should be delay minus whatever the value of now happens to be
87 // (now: 30, run time: 50).
88 base::TimeTicks scheduled_run_time = notifier.Now() + delay;
89 base::TimeTicks scheduled_delay =
90 base::TimeTicks() + (scheduled_run_time - notifier.Now());
91 EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun());
92
93 // Move closer to the run time (time: 49, run time: 50).
Yuri Wiitalad0611762017-07-22 02:33:2194 notifier.SetNow(notifier.Now() + base::TimeDelta::FromMicroseconds(19));
[email protected]afb84fc2014-05-29 01:47:5395
96 // It's not yet time to run, so we expect no notifications.
tzika6f0007a2017-01-27 04:01:1197 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:5398 EXPECT_EQ(0, NotificationCount());
99
100 tasks = TakePendingTasks();
101 ASSERT_EQ(1u, tasks.size());
102
103 // Now the time should be delay minus whatever the value of now happens to be.
104 scheduled_delay = base::TimeTicks() + (scheduled_run_time - notifier.Now());
105 EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun());
106
107 // Move to exactly the run time (time: 50, run time: 50).
Yuri Wiitalad0611762017-07-22 02:33:21108 notifier.SetNow(notifier.Now() + base::TimeDelta::FromMicroseconds(1));
[email protected]afb84fc2014-05-29 01:47:53109
110 // It's time to run!
tzika6f0007a2017-01-27 04:01:11111 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53112 EXPECT_EQ(1, NotificationCount());
113
114 tasks = TakePendingTasks();
115 EXPECT_EQ(0u, tasks.size());
116}
117
118TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) {
Yuri Wiitalad0611762017-07-22 02:33:21119 base::TimeDelta delay = base::TimeDelta::FromMicroseconds(20);
kylechar4bb144d2019-01-11 20:42:07120 TestNotifier notifier(task_runner_.get(),
121 base::BindRepeating(&DelayedUniqueNotifierTest::Notify,
122 base::Unretained(this)),
123 delay);
[email protected]afb84fc2014-05-29 01:47:53124
125 base::TimeTicks schedule_time;
126 // Move time 19 units forward and reschedule, expecting that we still need to
127 // run in |delay| time and we don't get a notification.
128 for (int i = 0; i < 10; ++i) {
129 EXPECT_EQ(0, NotificationCount());
130
131 // Move time forward 19 units.
Yuri Wiitalad0611762017-07-22 02:33:21132 schedule_time = notifier.Now() + base::TimeDelta::FromMicroseconds(19);
[email protected]afb84fc2014-05-29 01:47:53133 notifier.SetNow(schedule_time);
134 notifier.Schedule();
135
Brett Wilson55ff1475e2017-09-26 00:28:48136 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:53137
138 ASSERT_EQ(1u, tasks.size());
139 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
140
141 // It's not yet time to run, so we expect no notifications.
tzika6f0007a2017-01-27 04:01:11142 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53143 EXPECT_EQ(0, NotificationCount());
144 }
145
146 // Move time forward 20 units, expecting a notification.
Yuri Wiitalad0611762017-07-22 02:33:21147 schedule_time = notifier.Now() + base::TimeDelta::FromMicroseconds(20);
[email protected]afb84fc2014-05-29 01:47:53148 notifier.SetNow(schedule_time);
149
Brett Wilson55ff1475e2017-09-26 00:28:48150 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:53151
152 ASSERT_EQ(1u, tasks.size());
153 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
154
155 // Time to run!
tzika6f0007a2017-01-27 04:01:11156 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53157 EXPECT_EQ(1, NotificationCount());
158}
159
[email protected]0a1028722014-05-30 10:08:03160TEST_F(DelayedUniqueNotifierTest, CancelAndHasPendingNotification) {
Yuri Wiitalad0611762017-07-22 02:33:21161 base::TimeDelta delay = base::TimeDelta::FromMicroseconds(20);
kylechar4bb144d2019-01-11 20:42:07162 TestNotifier notifier(task_runner_.get(),
163 base::BindRepeating(&DelayedUniqueNotifierTest::Notify,
164 base::Unretained(this)),
165 delay);
[email protected]afb84fc2014-05-29 01:47:53166
167 EXPECT_EQ(0, NotificationCount());
168
169 // Schedule for |delay| seconds from now.
170 base::TimeTicks schedule_time =
Yuri Wiitalad0611762017-07-22 02:33:21171 notifier.Now() + base::TimeDelta::FromMicroseconds(10);
[email protected]afb84fc2014-05-29 01:47:53172 notifier.SetNow(schedule_time);
173 notifier.Schedule();
[email protected]0a1028722014-05-30 10:08:03174 EXPECT_TRUE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53175
176 // Cancel the run.
177 notifier.Cancel();
[email protected]0a1028722014-05-30 10:08:03178 EXPECT_FALSE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53179
Brett Wilson55ff1475e2017-09-26 00:28:48180 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:53181
182 ASSERT_EQ(1u, tasks.size());
183 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
184
185 // Time to run, but a canceled task!
tzika6f0007a2017-01-27 04:01:11186 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53187 EXPECT_EQ(0, NotificationCount());
[email protected]0a1028722014-05-30 10:08:03188 EXPECT_FALSE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53189
190 tasks = TakePendingTasks();
191 EXPECT_EQ(0u, tasks.size());
192
193 notifier.Schedule();
[email protected]0a1028722014-05-30 10:08:03194 EXPECT_TRUE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53195 tasks = TakePendingTasks();
196
197 ASSERT_EQ(1u, tasks.size());
198 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
199
200 // Advance the time.
201 notifier.SetNow(notifier.Now() + delay);
202
[email protected]0a1028722014-05-30 10:08:03203 // This should run since it wasn't canceled.
tzika6f0007a2017-01-27 04:01:11204 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53205 EXPECT_EQ(1, NotificationCount());
[email protected]0a1028722014-05-30 10:08:03206 EXPECT_FALSE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53207
[email protected]0a1028722014-05-30 10:08:03208 for (int i = 0; i < 10; ++i) {
[email protected]afb84fc2014-05-29 01:47:53209 notifier.Schedule();
[email protected]0a1028722014-05-30 10:08:03210 EXPECT_TRUE(notifier.HasPendingNotification());
211 notifier.Cancel();
212 EXPECT_FALSE(notifier.HasPendingNotification());
213 }
[email protected]afb84fc2014-05-29 01:47:53214
[email protected]afb84fc2014-05-29 01:47:53215 tasks = TakePendingTasks();
216
217 ASSERT_EQ(1u, tasks.size());
218 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
219
220 // Time to run, but a canceled task!
221 notifier.SetNow(notifier.Now() + delay);
tzika6f0007a2017-01-27 04:01:11222 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53223 EXPECT_EQ(1, NotificationCount());
224
225 tasks = TakePendingTasks();
226 EXPECT_EQ(0u, tasks.size());
[email protected]0a1028722014-05-30 10:08:03227 EXPECT_FALSE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53228}
229
danakj1a3c3172014-10-29 18:24:49230TEST_F(DelayedUniqueNotifierTest, ShutdownWithScheduledTask) {
Yuri Wiitalad0611762017-07-22 02:33:21231 base::TimeDelta delay = base::TimeDelta::FromMicroseconds(20);
kylechar4bb144d2019-01-11 20:42:07232 TestNotifier notifier(task_runner_.get(),
233 base::BindRepeating(&DelayedUniqueNotifierTest::Notify,
234 base::Unretained(this)),
235 delay);
danakj1a3c3172014-10-29 18:24:49236
237 EXPECT_EQ(0, NotificationCount());
238
239 // Schedule for |delay| seconds from now.
240 base::TimeTicks schedule_time =
Yuri Wiitalad0611762017-07-22 02:33:21241 notifier.Now() + base::TimeDelta::FromMicroseconds(10);
danakj1a3c3172014-10-29 18:24:49242 notifier.SetNow(schedule_time);
243 notifier.Schedule();
244 EXPECT_TRUE(notifier.HasPendingNotification());
245
246 // Shutdown the notifier.
247 notifier.Shutdown();
248
249 // The task is still there, but...
Brett Wilson55ff1475e2017-09-26 00:28:48250 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
danakj1a3c3172014-10-29 18:24:49251 ASSERT_EQ(1u, tasks.size());
252
253 // Running the task after shutdown does nothing since it's cancelled.
tzika6f0007a2017-01-27 04:01:11254 std::move(tasks[0].task).Run();
danakj1a3c3172014-10-29 18:24:49255 EXPECT_EQ(0, NotificationCount());
256
257 tasks = TakePendingTasks();
258 EXPECT_EQ(0u, tasks.size());
259
260 // We are no longer able to schedule tasks.
261 notifier.Schedule();
262 tasks = TakePendingTasks();
263 ASSERT_EQ(0u, tasks.size());
264
265 // Verify after the scheduled time happens there is still no task.
266 notifier.SetNow(notifier.Now() + delay);
267 tasks = TakePendingTasks();
268 ASSERT_EQ(0u, tasks.size());
269}
270
271TEST_F(DelayedUniqueNotifierTest, ShutdownPreventsSchedule) {
Yuri Wiitalad0611762017-07-22 02:33:21272 base::TimeDelta delay = base::TimeDelta::FromMicroseconds(20);
kylechar4bb144d2019-01-11 20:42:07273 TestNotifier notifier(task_runner_.get(),
274 base::BindRepeating(&DelayedUniqueNotifierTest::Notify,
275 base::Unretained(this)),
276 delay);
danakj1a3c3172014-10-29 18:24:49277
278 EXPECT_EQ(0, NotificationCount());
279
280 // Schedule for |delay| seconds from now.
281 base::TimeTicks schedule_time =
Yuri Wiitalad0611762017-07-22 02:33:21282 notifier.Now() + base::TimeDelta::FromMicroseconds(10);
danakj1a3c3172014-10-29 18:24:49283 notifier.SetNow(schedule_time);
284
285 // Shutdown the notifier.
286 notifier.Shutdown();
287
288 // Scheduling a task no longer does anything.
289 notifier.Schedule();
Brett Wilson55ff1475e2017-09-26 00:28:48290 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
danakj1a3c3172014-10-29 18:24:49291 ASSERT_EQ(0u, tasks.size());
292
293 // Verify after the scheduled time happens there is still no task.
294 notifier.SetNow(notifier.Now() + delay);
295 tasks = TakePendingTasks();
296 ASSERT_EQ(0u, tasks.size());
297}
298
[email protected]afb84fc2014-05-29 01:47:53299} // namespace
300} // namespace cc