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