blob: c75daf53f100de387d088c300475ca55c6cfb675 [file] [log] [blame]
Avi Drissman3f7a9d82022-09-08 20:55:421// Copyright 2014 The Chromium Authors
[email protected]afb84fc2014-05-29 01:47:532// 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
Brett Wilson55ff1475e2017-09-26 00:28:489#include "base/containers/circular_deque.h"
Avi Drissman453cf7a72023-01-07 00:52:1710#include "base/functional/bind.h"
11#include "base/functional/callback_helpers.h"
Sean Mahere672a662023-01-09 21:42:2812#include "base/task/sequenced_task_runner.h"
[email protected]afb84fc2014-05-29 01:47:5313#include "base/test/test_pending_task.h"
14#include "base/test/test_simple_task_runner.h"
Gabriel Charetted87f10f2022-03-31 00:44:2215#include "base/time/time.h"
[email protected]afb84fc2014-05-29 01:47:5316#include "testing/gtest/include/gtest/gtest.h"
17
18namespace cc {
19namespace {
20
21class TestNotifier : public DelayedUniqueNotifier {
22 public:
23 TestNotifier(base::SequencedTaskRunner* task_runner,
kylechar4bb144d2019-01-11 20:42:0724 base::RepeatingClosure closure,
[email protected]afb84fc2014-05-29 01:47:5325 const base::TimeDelta& delay)
kylechar4bb144d2019-01-11 20:42:0726 : DelayedUniqueNotifier(task_runner, std::move(closure), delay) {}
Chris Watkinsf6353292017-12-04 02:36:0527 ~TestNotifier() override = default;
[email protected]afb84fc2014-05-29 01:47:5328
29 // Overridden from DelayedUniqueNotifier:
dcheng716bedf2014-10-21 09:51:0830 base::TimeTicks Now() const override { return now_; }
[email protected]afb84fc2014-05-29 01:47:5331
32 void SetNow(base::TimeTicks now) { now_ = now; }
33
34 private:
35 base::TimeTicks now_;
36};
37
38class DelayedUniqueNotifierTest : public testing::Test {
39 public:
40 DelayedUniqueNotifierTest() : notification_count_(0) {}
41
danakjaeb95062014-11-14 01:35:3642 void SetUp() override {
[email protected]afb84fc2014-05-29 01:47:5343 notification_count_ = 0;
kylechar96f3eba2017-09-25 20:23:5644 task_runner_ = base::MakeRefCounted<base::TestSimpleTaskRunner>();
[email protected]afb84fc2014-05-29 01:47:5345 }
46
47 void Notify() { ++notification_count_; }
48
49 int NotificationCount() const { return notification_count_; }
50
Brett Wilson55ff1475e2017-09-26 00:28:4851 base::circular_deque<base::TestPendingTask> TakePendingTasks() {
tzik2d1f91c2016-10-04 03:58:0552 return task_runner_->TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:5353 }
54
55 protected:
56 int notification_count_;
57 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
58};
59
[email protected]afb84fc2014-05-29 01:47:5360TEST_F(DelayedUniqueNotifierTest, SmallDelay) {
Peter Kastinge5a38ed2021-10-02 03:06:3561 base::TimeDelta delay = base::Microseconds(20);
kylechar4bb144d2019-01-11 20:42:0762 TestNotifier notifier(task_runner_.get(),
63 base::BindRepeating(&DelayedUniqueNotifierTest::Notify,
64 base::Unretained(this)),
65 delay);
[email protected]afb84fc2014-05-29 01:47:5366
67 EXPECT_EQ(0, NotificationCount());
68
69 // Basic schedule for |delay| from now (now: 30, run time: 50).
Peter Kastinge5a38ed2021-10-02 03:06:3570 base::TimeTicks schedule_time = base::TimeTicks() + base::Microseconds(30);
[email protected]afb84fc2014-05-29 01:47:5371
72 notifier.SetNow(schedule_time);
73 notifier.Schedule();
74
Brett Wilson55ff1475e2017-09-26 00:28:4875 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:5376
77 ASSERT_EQ(1u, tasks.size());
78 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
79
80 // It's not yet time to run, so we expect no notifications.
tzika6f0007a2017-01-27 04:01:1181 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:5382 EXPECT_EQ(0, NotificationCount());
83
84 tasks = TakePendingTasks();
85
86 ASSERT_EQ(1u, tasks.size());
87 // Now the time should be delay minus whatever the value of now happens to be
88 // (now: 30, run time: 50).
89 base::TimeTicks scheduled_run_time = notifier.Now() + delay;
90 base::TimeTicks scheduled_delay =
91 base::TimeTicks() + (scheduled_run_time - notifier.Now());
92 EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun());
93
94 // Move closer to the run time (time: 49, run time: 50).
Peter Kastinge5a38ed2021-10-02 03:06:3595 notifier.SetNow(notifier.Now() + base::Microseconds(19));
[email protected]afb84fc2014-05-29 01:47:5396
97 // It's not yet time to run, so we expect no notifications.
tzika6f0007a2017-01-27 04:01:1198 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:5399 EXPECT_EQ(0, NotificationCount());
100
101 tasks = TakePendingTasks();
102 ASSERT_EQ(1u, tasks.size());
103
104 // Now the time should be delay minus whatever the value of now happens to be.
105 scheduled_delay = base::TimeTicks() + (scheduled_run_time - notifier.Now());
106 EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun());
107
108 // Move to exactly the run time (time: 50, run time: 50).
Peter Kastinge5a38ed2021-10-02 03:06:35109 notifier.SetNow(notifier.Now() + base::Microseconds(1));
[email protected]afb84fc2014-05-29 01:47:53110
111 // It's time to run!
tzika6f0007a2017-01-27 04:01:11112 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53113 EXPECT_EQ(1, NotificationCount());
114
115 tasks = TakePendingTasks();
116 EXPECT_EQ(0u, tasks.size());
117}
118
119TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) {
Peter Kastinge5a38ed2021-10-02 03:06:35120 base::TimeDelta delay = base::Microseconds(20);
kylechar4bb144d2019-01-11 20:42:07121 TestNotifier notifier(task_runner_.get(),
122 base::BindRepeating(&DelayedUniqueNotifierTest::Notify,
123 base::Unretained(this)),
124 delay);
[email protected]afb84fc2014-05-29 01:47:53125
126 base::TimeTicks schedule_time;
127 // Move time 19 units forward and reschedule, expecting that we still need to
128 // run in |delay| time and we don't get a notification.
129 for (int i = 0; i < 10; ++i) {
130 EXPECT_EQ(0, NotificationCount());
131
132 // Move time forward 19 units.
Peter Kastinge5a38ed2021-10-02 03:06:35133 schedule_time = notifier.Now() + base::Microseconds(19);
[email protected]afb84fc2014-05-29 01:47:53134 notifier.SetNow(schedule_time);
135 notifier.Schedule();
136
Brett Wilson55ff1475e2017-09-26 00:28:48137 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:53138
139 ASSERT_EQ(1u, tasks.size());
140 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
141
142 // It's not yet time to run, so we expect no notifications.
tzika6f0007a2017-01-27 04:01:11143 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53144 EXPECT_EQ(0, NotificationCount());
145 }
146
147 // Move time forward 20 units, expecting a notification.
Peter Kastinge5a38ed2021-10-02 03:06:35148 schedule_time = notifier.Now() + base::Microseconds(20);
[email protected]afb84fc2014-05-29 01:47:53149 notifier.SetNow(schedule_time);
150
Brett Wilson55ff1475e2017-09-26 00:28:48151 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:53152
153 ASSERT_EQ(1u, tasks.size());
154 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
155
156 // Time to run!
tzika6f0007a2017-01-27 04:01:11157 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53158 EXPECT_EQ(1, NotificationCount());
159}
160
[email protected]0a1028722014-05-30 10:08:03161TEST_F(DelayedUniqueNotifierTest, CancelAndHasPendingNotification) {
Peter Kastinge5a38ed2021-10-02 03:06:35162 base::TimeDelta delay = base::Microseconds(20);
kylechar4bb144d2019-01-11 20:42:07163 TestNotifier notifier(task_runner_.get(),
164 base::BindRepeating(&DelayedUniqueNotifierTest::Notify,
165 base::Unretained(this)),
166 delay);
[email protected]afb84fc2014-05-29 01:47:53167
168 EXPECT_EQ(0, NotificationCount());
169
170 // Schedule for |delay| seconds from now.
Peter Kastinge5a38ed2021-10-02 03:06:35171 base::TimeTicks schedule_time = notifier.Now() + base::Microseconds(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) {
Peter Kastinge5a38ed2021-10-02 03:06:35231 base::TimeDelta delay = base::Microseconds(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.
Peter Kastinge5a38ed2021-10-02 03:06:35240 base::TimeTicks schedule_time = notifier.Now() + base::Microseconds(10);
danakj1a3c3172014-10-29 18:24:49241 notifier.SetNow(schedule_time);
242 notifier.Schedule();
243 EXPECT_TRUE(notifier.HasPendingNotification());
244
245 // Shutdown the notifier.
246 notifier.Shutdown();
247
248 // The task is still there, but...
Brett Wilson55ff1475e2017-09-26 00:28:48249 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
danakj1a3c3172014-10-29 18:24:49250 ASSERT_EQ(1u, tasks.size());
251
252 // Running the task after shutdown does nothing since it's cancelled.
tzika6f0007a2017-01-27 04:01:11253 std::move(tasks[0].task).Run();
danakj1a3c3172014-10-29 18:24:49254 EXPECT_EQ(0, NotificationCount());
255
256 tasks = TakePendingTasks();
257 EXPECT_EQ(0u, tasks.size());
258
259 // We are no longer able to schedule tasks.
260 notifier.Schedule();
261 tasks = TakePendingTasks();
262 ASSERT_EQ(0u, tasks.size());
263
264 // Verify after the scheduled time happens there is still no task.
265 notifier.SetNow(notifier.Now() + delay);
266 tasks = TakePendingTasks();
267 ASSERT_EQ(0u, tasks.size());
268}
269
270TEST_F(DelayedUniqueNotifierTest, ShutdownPreventsSchedule) {
Peter Kastinge5a38ed2021-10-02 03:06:35271 base::TimeDelta delay = base::Microseconds(20);
kylechar4bb144d2019-01-11 20:42:07272 TestNotifier notifier(task_runner_.get(),
273 base::BindRepeating(&DelayedUniqueNotifierTest::Notify,
274 base::Unretained(this)),
275 delay);
danakj1a3c3172014-10-29 18:24:49276
277 EXPECT_EQ(0, NotificationCount());
278
279 // Schedule for |delay| seconds from now.
Peter Kastinge5a38ed2021-10-02 03:06:35280 base::TimeTicks schedule_time = notifier.Now() + base::Microseconds(10);
danakj1a3c3172014-10-29 18:24:49281 notifier.SetNow(schedule_time);
282
283 // Shutdown the notifier.
284 notifier.Shutdown();
285
286 // Scheduling a task no longer does anything.
287 notifier.Schedule();
Brett Wilson55ff1475e2017-09-26 00:28:48288 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
danakj1a3c3172014-10-29 18:24:49289 ASSERT_EQ(0u, tasks.size());
290
291 // Verify after the scheduled time happens there is still no task.
292 notifier.SetNow(notifier.Now() + delay);
293 tasks = TakePendingTasks();
294 ASSERT_EQ(0u, tasks.size());
295}
296
[email protected]afb84fc2014-05-29 01:47:53297} // namespace
298} // namespace cc