Avi Drissman | 3f7a9d8 | 2022-09-08 20:55:42 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 5 | #include "cc/base/delayed_unique_notifier.h" |
Sadrul Habib Chowdhury | 92b874b | 2020-09-10 09:12:06 | [diff] [blame] | 6 | |
| 7 | #include <utility> |
| 8 | |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 9 | #include "base/containers/circular_deque.h" |
Avi Drissman | 453cf7a7 | 2023-01-07 00:52:17 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
| 11 | #include "base/functional/callback_helpers.h" |
Sean Maher | e672a66 | 2023-01-09 21:42:28 | [diff] [blame] | 12 | #include "base/task/sequenced_task_runner.h" |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 13 | #include "base/test/test_pending_task.h" |
| 14 | #include "base/test/test_simple_task_runner.h" |
Gabriel Charette | d87f10f | 2022-03-31 00:44:22 | [diff] [blame] | 15 | #include "base/time/time.h" |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace cc { |
| 19 | namespace { |
| 20 | |
| 21 | class TestNotifier : public DelayedUniqueNotifier { |
| 22 | public: |
| 23 | TestNotifier(base::SequencedTaskRunner* task_runner, |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 24 | base::RepeatingClosure closure, |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 25 | const base::TimeDelta& delay) |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 26 | : DelayedUniqueNotifier(task_runner, std::move(closure), delay) {} |
Chris Watkins | f635329 | 2017-12-04 02:36:05 | [diff] [blame] | 27 | ~TestNotifier() override = default; |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 28 | |
| 29 | // Overridden from DelayedUniqueNotifier: |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 30 | base::TimeTicks Now() const override { return now_; } |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 31 | |
| 32 | void SetNow(base::TimeTicks now) { now_ = now; } |
| 33 | |
| 34 | private: |
| 35 | base::TimeTicks now_; |
| 36 | }; |
| 37 | |
| 38 | class DelayedUniqueNotifierTest : public testing::Test { |
| 39 | public: |
| 40 | DelayedUniqueNotifierTest() : notification_count_(0) {} |
| 41 | |
danakj | aeb9506 | 2014-11-14 01:35:36 | [diff] [blame] | 42 | void SetUp() override { |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 43 | notification_count_ = 0; |
kylechar | 96f3eba | 2017-09-25 20:23:56 | [diff] [blame] | 44 | task_runner_ = base::MakeRefCounted<base::TestSimpleTaskRunner>(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void Notify() { ++notification_count_; } |
| 48 | |
| 49 | int NotificationCount() const { return notification_count_; } |
| 50 | |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 51 | base::circular_deque<base::TestPendingTask> TakePendingTasks() { |
tzik | 2d1f91c | 2016-10-04 03:58:05 | [diff] [blame] | 52 | return task_runner_->TakePendingTasks(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | protected: |
| 56 | int notification_count_; |
| 57 | scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 58 | }; |
| 59 | |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 60 | TEST_F(DelayedUniqueNotifierTest, SmallDelay) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 61 | base::TimeDelta delay = base::Microseconds(20); |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 62 | TestNotifier notifier(task_runner_.get(), |
| 63 | base::BindRepeating(&DelayedUniqueNotifierTest::Notify, |
| 64 | base::Unretained(this)), |
| 65 | delay); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 66 | |
| 67 | EXPECT_EQ(0, NotificationCount()); |
| 68 | |
| 69 | // Basic schedule for |delay| from now (now: 30, run time: 50). |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 70 | base::TimeTicks schedule_time = base::TimeTicks() + base::Microseconds(30); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 71 | |
| 72 | notifier.SetNow(schedule_time); |
| 73 | notifier.Schedule(); |
| 74 | |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 75 | base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 76 | |
| 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. |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 81 | std::move(tasks[0].task).Run(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 82 | 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 Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 95 | notifier.SetNow(notifier.Now() + base::Microseconds(19)); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 96 | |
| 97 | // It's not yet time to run, so we expect no notifications. |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 98 | std::move(tasks[0].task).Run(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 99 | 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 Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 109 | notifier.SetNow(notifier.Now() + base::Microseconds(1)); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 110 | |
| 111 | // It's time to run! |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 112 | std::move(tasks[0].task).Run(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 113 | EXPECT_EQ(1, NotificationCount()); |
| 114 | |
| 115 | tasks = TakePendingTasks(); |
| 116 | EXPECT_EQ(0u, tasks.size()); |
| 117 | } |
| 118 | |
| 119 | TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 120 | base::TimeDelta delay = base::Microseconds(20); |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 121 | TestNotifier notifier(task_runner_.get(), |
| 122 | base::BindRepeating(&DelayedUniqueNotifierTest::Notify, |
| 123 | base::Unretained(this)), |
| 124 | delay); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 125 | |
| 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 Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 133 | schedule_time = notifier.Now() + base::Microseconds(19); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 134 | notifier.SetNow(schedule_time); |
| 135 | notifier.Schedule(); |
| 136 | |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 137 | base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 138 | |
| 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. |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 143 | std::move(tasks[0].task).Run(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 144 | EXPECT_EQ(0, NotificationCount()); |
| 145 | } |
| 146 | |
| 147 | // Move time forward 20 units, expecting a notification. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 148 | schedule_time = notifier.Now() + base::Microseconds(20); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 149 | notifier.SetNow(schedule_time); |
| 150 | |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 151 | base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 152 | |
| 153 | ASSERT_EQ(1u, tasks.size()); |
| 154 | EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
| 155 | |
| 156 | // Time to run! |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 157 | std::move(tasks[0].task).Run(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 158 | EXPECT_EQ(1, NotificationCount()); |
| 159 | } |
| 160 | |
[email protected] | 0a102872 | 2014-05-30 10:08:03 | [diff] [blame] | 161 | TEST_F(DelayedUniqueNotifierTest, CancelAndHasPendingNotification) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 162 | base::TimeDelta delay = base::Microseconds(20); |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 163 | TestNotifier notifier(task_runner_.get(), |
| 164 | base::BindRepeating(&DelayedUniqueNotifierTest::Notify, |
| 165 | base::Unretained(this)), |
| 166 | delay); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 167 | |
| 168 | EXPECT_EQ(0, NotificationCount()); |
| 169 | |
| 170 | // Schedule for |delay| seconds from now. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 171 | base::TimeTicks schedule_time = notifier.Now() + base::Microseconds(10); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 172 | notifier.SetNow(schedule_time); |
| 173 | notifier.Schedule(); |
[email protected] | 0a102872 | 2014-05-30 10:08:03 | [diff] [blame] | 174 | EXPECT_TRUE(notifier.HasPendingNotification()); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 175 | |
| 176 | // Cancel the run. |
| 177 | notifier.Cancel(); |
[email protected] | 0a102872 | 2014-05-30 10:08:03 | [diff] [blame] | 178 | EXPECT_FALSE(notifier.HasPendingNotification()); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 179 | |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 180 | base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 181 | |
| 182 | ASSERT_EQ(1u, tasks.size()); |
| 183 | EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
| 184 | |
| 185 | // Time to run, but a canceled task! |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 186 | std::move(tasks[0].task).Run(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 187 | EXPECT_EQ(0, NotificationCount()); |
[email protected] | 0a102872 | 2014-05-30 10:08:03 | [diff] [blame] | 188 | EXPECT_FALSE(notifier.HasPendingNotification()); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 189 | |
| 190 | tasks = TakePendingTasks(); |
| 191 | EXPECT_EQ(0u, tasks.size()); |
| 192 | |
| 193 | notifier.Schedule(); |
[email protected] | 0a102872 | 2014-05-30 10:08:03 | [diff] [blame] | 194 | EXPECT_TRUE(notifier.HasPendingNotification()); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 195 | 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] | 0a102872 | 2014-05-30 10:08:03 | [diff] [blame] | 203 | // This should run since it wasn't canceled. |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 204 | std::move(tasks[0].task).Run(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 205 | EXPECT_EQ(1, NotificationCount()); |
[email protected] | 0a102872 | 2014-05-30 10:08:03 | [diff] [blame] | 206 | EXPECT_FALSE(notifier.HasPendingNotification()); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 207 | |
[email protected] | 0a102872 | 2014-05-30 10:08:03 | [diff] [blame] | 208 | for (int i = 0; i < 10; ++i) { |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 209 | notifier.Schedule(); |
[email protected] | 0a102872 | 2014-05-30 10:08:03 | [diff] [blame] | 210 | EXPECT_TRUE(notifier.HasPendingNotification()); |
| 211 | notifier.Cancel(); |
| 212 | EXPECT_FALSE(notifier.HasPendingNotification()); |
| 213 | } |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 214 | |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 215 | 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); |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 222 | std::move(tasks[0].task).Run(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 223 | EXPECT_EQ(1, NotificationCount()); |
| 224 | |
| 225 | tasks = TakePendingTasks(); |
| 226 | EXPECT_EQ(0u, tasks.size()); |
[email protected] | 0a102872 | 2014-05-30 10:08:03 | [diff] [blame] | 227 | EXPECT_FALSE(notifier.HasPendingNotification()); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 228 | } |
| 229 | |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 230 | TEST_F(DelayedUniqueNotifierTest, ShutdownWithScheduledTask) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 231 | base::TimeDelta delay = base::Microseconds(20); |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 232 | TestNotifier notifier(task_runner_.get(), |
| 233 | base::BindRepeating(&DelayedUniqueNotifierTest::Notify, |
| 234 | base::Unretained(this)), |
| 235 | delay); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 236 | |
| 237 | EXPECT_EQ(0, NotificationCount()); |
| 238 | |
| 239 | // Schedule for |delay| seconds from now. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 240 | base::TimeTicks schedule_time = notifier.Now() + base::Microseconds(10); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 241 | 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 Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 249 | base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks(); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 250 | ASSERT_EQ(1u, tasks.size()); |
| 251 | |
| 252 | // Running the task after shutdown does nothing since it's cancelled. |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 253 | std::move(tasks[0].task).Run(); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 254 | 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 | |
| 270 | TEST_F(DelayedUniqueNotifierTest, ShutdownPreventsSchedule) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 271 | base::TimeDelta delay = base::Microseconds(20); |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 272 | TestNotifier notifier(task_runner_.get(), |
| 273 | base::BindRepeating(&DelayedUniqueNotifierTest::Notify, |
| 274 | base::Unretained(this)), |
| 275 | delay); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 276 | |
| 277 | EXPECT_EQ(0, NotificationCount()); |
| 278 | |
| 279 | // Schedule for |delay| seconds from now. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 280 | base::TimeTicks schedule_time = notifier.Now() + base::Microseconds(10); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 281 | 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 Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 288 | base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks(); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 289 | 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] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 297 | } // namespace |
| 298 | } // namespace cc |