blob: a7006387cfdb16a5815a4d318fa784ff78063071 [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"
danakjdb9ae7942020-11-11 16:01:3510#include "base/callback_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"
Gabriel Charetted87f10f2022-03-31 00:44:2214#include "base/time/time.h"
[email protected]afb84fc2014-05-29 01:47:5315#include "testing/gtest/include/gtest/gtest.h"
16
17namespace cc {
18namespace {
19
20class TestNotifier : public DelayedUniqueNotifier {
21 public:
22 TestNotifier(base::SequencedTaskRunner* task_runner,
kylechar4bb144d2019-01-11 20:42:0723 base::RepeatingClosure closure,
[email protected]afb84fc2014-05-29 01:47:5324 const base::TimeDelta& delay)
kylechar4bb144d2019-01-11 20:42:0725 : DelayedUniqueNotifier(task_runner, std::move(closure), delay) {}
Chris Watkinsf6353292017-12-04 02:36:0526 ~TestNotifier() override = default;
[email protected]afb84fc2014-05-29 01:47:5327
28 // Overridden from DelayedUniqueNotifier:
dcheng716bedf2014-10-21 09:51:0829 base::TimeTicks Now() const override { return now_; }
[email protected]afb84fc2014-05-29 01:47:5330
31 void SetNow(base::TimeTicks now) { now_ = now; }
32
33 private:
34 base::TimeTicks now_;
35};
36
37class DelayedUniqueNotifierTest : public testing::Test {
38 public:
39 DelayedUniqueNotifierTest() : notification_count_(0) {}
40
danakjaeb95062014-11-14 01:35:3641 void SetUp() override {
[email protected]afb84fc2014-05-29 01:47:5342 notification_count_ = 0;
kylechar96f3eba2017-09-25 20:23:5643 task_runner_ = base::MakeRefCounted<base::TestSimpleTaskRunner>();
[email protected]afb84fc2014-05-29 01:47:5344 }
45
46 void Notify() { ++notification_count_; }
47
48 int NotificationCount() const { return notification_count_; }
49
Brett Wilson55ff1475e2017-09-26 00:28:4850 base::circular_deque<base::TestPendingTask> TakePendingTasks() {
tzik2d1f91c2016-10-04 03:58:0551 return task_runner_->TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:5352 }
53
54 protected:
55 int notification_count_;
56 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
57};
58
[email protected]afb84fc2014-05-29 01:47:5359TEST_F(DelayedUniqueNotifierTest, SmallDelay) {
Peter Kastinge5a38ed2021-10-02 03:06:3560 base::TimeDelta delay = base::Microseconds(20);
kylechar4bb144d2019-01-11 20:42:0761 TestNotifier notifier(task_runner_.get(),
62 base::BindRepeating(&DelayedUniqueNotifierTest::Notify,
63 base::Unretained(this)),
64 delay);
[email protected]afb84fc2014-05-29 01:47:5365
66 EXPECT_EQ(0, NotificationCount());
67
68 // Basic schedule for |delay| from now (now: 30, run time: 50).
Peter Kastinge5a38ed2021-10-02 03:06:3569 base::TimeTicks schedule_time = base::TimeTicks() + base::Microseconds(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).
Peter Kastinge5a38ed2021-10-02 03:06:3594 notifier.SetNow(notifier.Now() + base::Microseconds(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).
Peter Kastinge5a38ed2021-10-02 03:06:35108 notifier.SetNow(notifier.Now() + base::Microseconds(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) {
Peter Kastinge5a38ed2021-10-02 03:06:35119 base::TimeDelta delay = base::Microseconds(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.
Peter Kastinge5a38ed2021-10-02 03:06:35132 schedule_time = notifier.Now() + base::Microseconds(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.
Peter Kastinge5a38ed2021-10-02 03:06:35147 schedule_time = notifier.Now() + base::Microseconds(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) {
Peter Kastinge5a38ed2021-10-02 03:06:35161 base::TimeDelta delay = base::Microseconds(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.
Peter Kastinge5a38ed2021-10-02 03:06:35170 base::TimeTicks schedule_time = notifier.Now() + base::Microseconds(10);
[email protected]afb84fc2014-05-29 01:47:53171 notifier.SetNow(schedule_time);
172 notifier.Schedule();
[email protected]0a1028722014-05-30 10:08:03173 EXPECT_TRUE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53174
175 // Cancel the run.
176 notifier.Cancel();
[email protected]0a1028722014-05-30 10:08:03177 EXPECT_FALSE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53178
Brett Wilson55ff1475e2017-09-26 00:28:48179 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
[email protected]afb84fc2014-05-29 01:47:53180
181 ASSERT_EQ(1u, tasks.size());
182 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
183
184 // Time to run, but a canceled task!
tzika6f0007a2017-01-27 04:01:11185 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53186 EXPECT_EQ(0, NotificationCount());
[email protected]0a1028722014-05-30 10:08:03187 EXPECT_FALSE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53188
189 tasks = TakePendingTasks();
190 EXPECT_EQ(0u, tasks.size());
191
192 notifier.Schedule();
[email protected]0a1028722014-05-30 10:08:03193 EXPECT_TRUE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53194 tasks = TakePendingTasks();
195
196 ASSERT_EQ(1u, tasks.size());
197 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
198
199 // Advance the time.
200 notifier.SetNow(notifier.Now() + delay);
201
[email protected]0a1028722014-05-30 10:08:03202 // This should run since it wasn't canceled.
tzika6f0007a2017-01-27 04:01:11203 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53204 EXPECT_EQ(1, NotificationCount());
[email protected]0a1028722014-05-30 10:08:03205 EXPECT_FALSE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53206
[email protected]0a1028722014-05-30 10:08:03207 for (int i = 0; i < 10; ++i) {
[email protected]afb84fc2014-05-29 01:47:53208 notifier.Schedule();
[email protected]0a1028722014-05-30 10:08:03209 EXPECT_TRUE(notifier.HasPendingNotification());
210 notifier.Cancel();
211 EXPECT_FALSE(notifier.HasPendingNotification());
212 }
[email protected]afb84fc2014-05-29 01:47:53213
[email protected]afb84fc2014-05-29 01:47:53214 tasks = TakePendingTasks();
215
216 ASSERT_EQ(1u, tasks.size());
217 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun());
218
219 // Time to run, but a canceled task!
220 notifier.SetNow(notifier.Now() + delay);
tzika6f0007a2017-01-27 04:01:11221 std::move(tasks[0].task).Run();
[email protected]afb84fc2014-05-29 01:47:53222 EXPECT_EQ(1, NotificationCount());
223
224 tasks = TakePendingTasks();
225 EXPECT_EQ(0u, tasks.size());
[email protected]0a1028722014-05-30 10:08:03226 EXPECT_FALSE(notifier.HasPendingNotification());
[email protected]afb84fc2014-05-29 01:47:53227}
228
danakj1a3c3172014-10-29 18:24:49229TEST_F(DelayedUniqueNotifierTest, ShutdownWithScheduledTask) {
Peter Kastinge5a38ed2021-10-02 03:06:35230 base::TimeDelta delay = base::Microseconds(20);
kylechar4bb144d2019-01-11 20:42:07231 TestNotifier notifier(task_runner_.get(),
232 base::BindRepeating(&DelayedUniqueNotifierTest::Notify,
233 base::Unretained(this)),
234 delay);
danakj1a3c3172014-10-29 18:24:49235
236 EXPECT_EQ(0, NotificationCount());
237
238 // Schedule for |delay| seconds from now.
Peter Kastinge5a38ed2021-10-02 03:06:35239 base::TimeTicks schedule_time = notifier.Now() + base::Microseconds(10);
danakj1a3c3172014-10-29 18:24:49240 notifier.SetNow(schedule_time);
241 notifier.Schedule();
242 EXPECT_TRUE(notifier.HasPendingNotification());
243
244 // Shutdown the notifier.
245 notifier.Shutdown();
246
247 // The task is still there, but...
Brett Wilson55ff1475e2017-09-26 00:28:48248 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
danakj1a3c3172014-10-29 18:24:49249 ASSERT_EQ(1u, tasks.size());
250
251 // Running the task after shutdown does nothing since it's cancelled.
tzika6f0007a2017-01-27 04:01:11252 std::move(tasks[0].task).Run();
danakj1a3c3172014-10-29 18:24:49253 EXPECT_EQ(0, NotificationCount());
254
255 tasks = TakePendingTasks();
256 EXPECT_EQ(0u, tasks.size());
257
258 // We are no longer able to schedule tasks.
259 notifier.Schedule();
260 tasks = TakePendingTasks();
261 ASSERT_EQ(0u, tasks.size());
262
263 // Verify after the scheduled time happens there is still no task.
264 notifier.SetNow(notifier.Now() + delay);
265 tasks = TakePendingTasks();
266 ASSERT_EQ(0u, tasks.size());
267}
268
269TEST_F(DelayedUniqueNotifierTest, ShutdownPreventsSchedule) {
Peter Kastinge5a38ed2021-10-02 03:06:35270 base::TimeDelta delay = base::Microseconds(20);
kylechar4bb144d2019-01-11 20:42:07271 TestNotifier notifier(task_runner_.get(),
272 base::BindRepeating(&DelayedUniqueNotifierTest::Notify,
273 base::Unretained(this)),
274 delay);
danakj1a3c3172014-10-29 18:24:49275
276 EXPECT_EQ(0, NotificationCount());
277
278 // Schedule for |delay| seconds from now.
Peter Kastinge5a38ed2021-10-02 03:06:35279 base::TimeTicks schedule_time = notifier.Now() + base::Microseconds(10);
danakj1a3c3172014-10-29 18:24:49280 notifier.SetNow(schedule_time);
281
282 // Shutdown the notifier.
283 notifier.Shutdown();
284
285 // Scheduling a task no longer does anything.
286 notifier.Schedule();
Brett Wilson55ff1475e2017-09-26 00:28:48287 base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks();
danakj1a3c3172014-10-29 18:24:49288 ASSERT_EQ(0u, tasks.size());
289
290 // Verify after the scheduled time happens there is still no task.
291 notifier.SetNow(notifier.Now() + delay);
292 tasks = TakePendingTasks();
293 ASSERT_EQ(0u, tasks.size());
294}
295
[email protected]afb84fc2014-05-29 01:47:53296} // namespace
297} // namespace cc