[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" |
| 10 | #include "base/bind_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" |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace cc { |
| 17 | namespace { |
| 18 | |
| 19 | class TestNotifier : public DelayedUniqueNotifier { |
| 20 | public: |
| 21 | TestNotifier(base::SequencedTaskRunner* task_runner, |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 22 | base::RepeatingClosure closure, |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 23 | const base::TimeDelta& delay) |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 24 | : DelayedUniqueNotifier(task_runner, std::move(closure), delay) {} |
Chris Watkins | f635329 | 2017-12-04 02:36:05 | [diff] [blame] | 25 | ~TestNotifier() override = default; |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 26 | |
| 27 | // Overridden from DelayedUniqueNotifier: |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 28 | base::TimeTicks Now() const override { return now_; } |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 29 | |
| 30 | void SetNow(base::TimeTicks now) { now_ = now; } |
| 31 | |
| 32 | private: |
| 33 | base::TimeTicks now_; |
| 34 | }; |
| 35 | |
| 36 | class DelayedUniqueNotifierTest : public testing::Test { |
| 37 | public: |
| 38 | DelayedUniqueNotifierTest() : notification_count_(0) {} |
| 39 | |
danakj | aeb9506 | 2014-11-14 01:35:36 | [diff] [blame] | 40 | void SetUp() override { |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 41 | notification_count_ = 0; |
kylechar | 96f3eba | 2017-09-25 20:23:56 | [diff] [blame] | 42 | task_runner_ = base::MakeRefCounted<base::TestSimpleTaskRunner>(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void Notify() { ++notification_count_; } |
| 46 | |
| 47 | int NotificationCount() const { return notification_count_; } |
| 48 | |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 49 | base::circular_deque<base::TestPendingTask> TakePendingTasks() { |
tzik | 2d1f91c | 2016-10-04 03:58:05 | [diff] [blame] | 50 | return task_runner_->TakePendingTasks(); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | protected: |
| 54 | int notification_count_; |
| 55 | scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 56 | }; |
| 57 | |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 58 | TEST_F(DelayedUniqueNotifierTest, SmallDelay) { |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 59 | base::TimeDelta delay = base::TimeDelta::FromMicroseconds(20); |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 60 | TestNotifier notifier(task_runner_.get(), |
| 61 | base::BindRepeating(&DelayedUniqueNotifierTest::Notify, |
| 62 | base::Unretained(this)), |
| 63 | delay); |
[email protected] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 64 | |
| 65 | EXPECT_EQ(0, NotificationCount()); |
| 66 | |
| 67 | // Basic schedule for |delay| from now (now: 30, run time: 50). |
| 68 | base::TimeTicks schedule_time = |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 69 | base::TimeTicks() + base::TimeDelta::FromMicroseconds(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). |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 94 | notifier.SetNow(notifier.Now() + base::TimeDelta::FromMicroseconds(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). |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 108 | notifier.SetNow(notifier.Now() + base::TimeDelta::FromMicroseconds(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) { |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 119 | base::TimeDelta delay = base::TimeDelta::FromMicroseconds(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. |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 132 | schedule_time = notifier.Now() + base::TimeDelta::FromMicroseconds(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. |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 147 | schedule_time = notifier.Now() + base::TimeDelta::FromMicroseconds(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) { |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 161 | base::TimeDelta delay = base::TimeDelta::FromMicroseconds(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. |
| 170 | base::TimeTicks schedule_time = |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 171 | notifier.Now() + base::TimeDelta::FromMicroseconds(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) { |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 231 | base::TimeDelta delay = base::TimeDelta::FromMicroseconds(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. |
| 240 | base::TimeTicks schedule_time = |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 241 | notifier.Now() + base::TimeDelta::FromMicroseconds(10); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 242 | 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 Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 250 | base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks(); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 251 | ASSERT_EQ(1u, tasks.size()); |
| 252 | |
| 253 | // Running the task after shutdown does nothing since it's cancelled. |
tzik | a6f0007a | 2017-01-27 04:01:11 | [diff] [blame] | 254 | std::move(tasks[0].task).Run(); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 255 | 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 | |
| 271 | TEST_F(DelayedUniqueNotifierTest, ShutdownPreventsSchedule) { |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 272 | base::TimeDelta delay = base::TimeDelta::FromMicroseconds(20); |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 273 | TestNotifier notifier(task_runner_.get(), |
| 274 | base::BindRepeating(&DelayedUniqueNotifierTest::Notify, |
| 275 | base::Unretained(this)), |
| 276 | delay); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 277 | |
| 278 | EXPECT_EQ(0, NotificationCount()); |
| 279 | |
| 280 | // Schedule for |delay| seconds from now. |
| 281 | base::TimeTicks schedule_time = |
Yuri Wiitala | d061176 | 2017-07-22 02:33:21 | [diff] [blame] | 282 | notifier.Now() + base::TimeDelta::FromMicroseconds(10); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 283 | 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 Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 290 | base::circular_deque<base::TestPendingTask> tasks = TakePendingTasks(); |
danakj | 1a3c317 | 2014-10-29 18:24:49 | [diff] [blame] | 291 | 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] | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 299 | } // namespace |
| 300 | } // namespace cc |