Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [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 | |
| 5 | #include "base/cancelable_callback.h" |
| 6 | |
kuznetsovs | b020bcd8 | 2016-04-13 13:01:47 | [diff] [blame] | 7 | #include <memory> |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 8 | #include <optional> |
kuznetsovs | b020bcd8 | 2016-04-13 13:01:47 | [diff] [blame] | 9 | |
Avi Drissman | 63e1f99 | 2023-01-13 18:54:43 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
| 11 | #include "base/functional/callback_helpers.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 12 | #include "base/location.h" |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 14 | #include "base/run_loop.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 15 | #include "base/task/single_thread_task_runner.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 16 | #include "base/test/task_environment.h" |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | namespace base { |
| 20 | namespace { |
| 21 | |
| 22 | class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> { |
| 23 | private: |
| 24 | friend class RefCountedThreadSafe<TestRefCounted>; |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 25 | ~TestRefCounted() = default; |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 26 | }; |
| 27 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 28 | void Increment(int* count) { |
| 29 | (*count)++; |
| 30 | } |
| 31 | void IncrementBy(int* count, int n) { |
| 32 | (*count) += n; |
| 33 | } |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 34 | void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {} |
| 35 | |
kuznetsovs | b020bcd8 | 2016-04-13 13:01:47 | [diff] [blame] | 36 | void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) { |
| 37 | *value = *result; |
| 38 | } |
| 39 | |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 40 | // Cancel(). |
| 41 | // - Callback can be run multiple times. |
| 42 | // - After Cancel(), Run() completes but has no effect. |
| 43 | TEST(CancelableCallbackTest, Cancel) { |
| 44 | int count = 0; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 45 | CancelableRepeatingClosure cancelable( |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 46 | BindRepeating(&Increment, Unretained(&count))); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 47 | |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 48 | RepeatingClosure callback = cancelable.callback(); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 49 | callback.Run(); |
| 50 | EXPECT_EQ(1, count); |
| 51 | |
| 52 | callback.Run(); |
| 53 | EXPECT_EQ(2, count); |
| 54 | |
| 55 | cancelable.Cancel(); |
| 56 | callback.Run(); |
| 57 | EXPECT_EQ(2, count); |
| 58 | } |
| 59 | |
| 60 | // Cancel() called multiple times. |
| 61 | // - Cancel() cancels all copies of the wrapped callback. |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 62 | // - Calling Cancel() more than once has no effect. |
| 63 | // - After Cancel(), callback() returns a null callback. |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 64 | TEST(CancelableCallbackTest, MultipleCancel) { |
| 65 | int count = 0; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 66 | CancelableRepeatingClosure cancelable( |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 67 | BindRepeating(&Increment, Unretained(&count))); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 68 | |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 69 | RepeatingClosure callback1 = cancelable.callback(); |
| 70 | RepeatingClosure callback2 = cancelable.callback(); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 71 | cancelable.Cancel(); |
| 72 | |
| 73 | callback1.Run(); |
| 74 | EXPECT_EQ(0, count); |
| 75 | |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 76 | callback2.Run(); |
| 77 | EXPECT_EQ(0, count); |
| 78 | |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 79 | // Calling Cancel() again has no effect. |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 80 | cancelable.Cancel(); |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 81 | |
| 82 | // callback() of a cancelled callback is null. |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 83 | RepeatingClosure callback3 = cancelable.callback(); |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 84 | EXPECT_TRUE(callback3.is_null()); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 85 | } |
| 86 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 87 | // CancelableRepeatingCallback destroyed before callback is run. |
| 88 | // - Destruction of CancelableRepeatingCallback cancels outstanding callbacks. |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 89 | TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) { |
| 90 | int count = 0; |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 91 | RepeatingClosure callback; |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 92 | |
| 93 | { |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 94 | CancelableRepeatingClosure cancelable( |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 95 | BindRepeating(&Increment, Unretained(&count))); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 96 | |
| 97 | callback = cancelable.callback(); |
| 98 | callback.Run(); |
| 99 | EXPECT_EQ(1, count); |
| 100 | } |
| 101 | |
| 102 | callback.Run(); |
| 103 | EXPECT_EQ(1, count); |
| 104 | } |
| 105 | |
| 106 | // Cancel() called on bound closure with a RefCounted parameter. |
| 107 | // - Cancel drops wrapped callback (and, implicitly, its bound arguments). |
| 108 | TEST(CancelableCallbackTest, CancelDropsCallback) { |
| 109 | scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted; |
| 110 | EXPECT_TRUE(ref_counted->HasOneRef()); |
| 111 | |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 112 | CancelableOnceClosure cancelable(BindOnce(RefCountedParam, ref_counted)); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 113 | EXPECT_FALSE(cancelable.IsCancelled()); |
| 114 | EXPECT_TRUE(ref_counted.get()); |
| 115 | EXPECT_FALSE(ref_counted->HasOneRef()); |
| 116 | |
| 117 | // There is only one reference to |ref_counted| after the Cancel(). |
| 118 | cancelable.Cancel(); |
| 119 | EXPECT_TRUE(cancelable.IsCancelled()); |
| 120 | EXPECT_TRUE(ref_counted.get()); |
| 121 | EXPECT_TRUE(ref_counted->HasOneRef()); |
| 122 | } |
| 123 | |
| 124 | // Reset(). |
| 125 | // - Reset() replaces the existing wrapped callback with a new callback. |
| 126 | // - Reset() deactivates outstanding callbacks. |
| 127 | TEST(CancelableCallbackTest, Reset) { |
| 128 | int count = 0; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 129 | CancelableRepeatingClosure cancelable( |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 130 | BindRepeating(&Increment, Unretained(&count))); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 131 | |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 132 | RepeatingClosure callback = cancelable.callback(); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 133 | callback.Run(); |
| 134 | EXPECT_EQ(1, count); |
| 135 | |
| 136 | callback.Run(); |
| 137 | EXPECT_EQ(2, count); |
| 138 | |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 139 | cancelable.Reset(BindRepeating(&IncrementBy, Unretained(&count), 3)); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 140 | EXPECT_FALSE(cancelable.IsCancelled()); |
| 141 | |
| 142 | // The stale copy of the cancelable callback is non-null. |
| 143 | ASSERT_FALSE(callback.is_null()); |
| 144 | |
| 145 | // The stale copy of the cancelable callback is no longer active. |
| 146 | callback.Run(); |
| 147 | EXPECT_EQ(2, count); |
| 148 | |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 149 | RepeatingClosure callback2 = cancelable.callback(); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 150 | ASSERT_FALSE(callback2.is_null()); |
| 151 | |
| 152 | callback2.Run(); |
| 153 | EXPECT_EQ(5, count); |
| 154 | } |
| 155 | |
| 156 | // IsCanceled(). |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 157 | // - Cancel() transforms the CancelableOnceCallback into a cancelled state. |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 158 | TEST(CancelableCallbackTest, IsNull) { |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 159 | CancelableOnceClosure cancelable; |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 160 | EXPECT_TRUE(cancelable.IsCancelled()); |
| 161 | |
| 162 | int count = 0; |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 163 | cancelable.Reset(BindOnce(&Increment, Unretained(&count))); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 164 | EXPECT_FALSE(cancelable.IsCancelled()); |
| 165 | |
| 166 | cancelable.Cancel(); |
| 167 | EXPECT_TRUE(cancelable.IsCancelled()); |
| 168 | } |
| 169 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 170 | // CancelableRepeatingCallback posted to a task environment with PostTask. |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 171 | // - Posted callbacks can be cancelled. |
Peter Kasting | 4075371 | 2023-12-22 01:22:05 | [diff] [blame] | 172 | // - Chained callbacks from `.Then()` still run on cancelled callbacks. |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 173 | TEST(CancelableCallbackTest, PostTask) { |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 174 | test::SingleThreadTaskEnvironment task_environment; |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 175 | |
| 176 | int count = 0; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 177 | CancelableRepeatingClosure cancelable( |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 178 | BindRepeating(&Increment, Unretained(&count))); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 179 | |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 180 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, |
| 181 | cancelable.callback()); |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 182 | RunLoop().RunUntilIdle(); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 183 | |
| 184 | EXPECT_EQ(1, count); |
| 185 | |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 186 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, |
| 187 | cancelable.callback()); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 188 | |
Peter Kasting | 4075371 | 2023-12-22 01:22:05 | [diff] [blame] | 189 | // Cancel before running the task. |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 190 | cancelable.Cancel(); |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 191 | RunLoop().RunUntilIdle(); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 192 | |
| 193 | // Callback never ran due to cancellation; count is the same. |
| 194 | EXPECT_EQ(1, count); |
Peter Kasting | 4075371 | 2023-12-22 01:22:05 | [diff] [blame] | 195 | |
| 196 | // Chain a callback to the cancelable callback. |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 197 | cancelable.Reset(BindRepeating(&Increment, Unretained(&count))); |
Peter Kasting | 4075371 | 2023-12-22 01:22:05 | [diff] [blame] | 198 | SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 199 | FROM_HERE, cancelable.callback().Then( |
| 200 | BindRepeating(&IncrementBy, Unretained(&count), 2))); |
Peter Kasting | 4075371 | 2023-12-22 01:22:05 | [diff] [blame] | 201 | |
| 202 | // Cancel before running the task. |
| 203 | cancelable.Cancel(); |
| 204 | RunLoop().RunUntilIdle(); |
| 205 | |
| 206 | // Callback never ran due to cancellation, but chained callback still should |
| 207 | // have. Count should increase by exactly two. |
| 208 | EXPECT_EQ(3, count); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 209 | } |
| 210 | |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 211 | // CancelableRepeatingCallback posted to a task environment with |
| 212 | // PostTaskAndReply. |
| 213 | // - Posted callbacks can be cancelled. |
| 214 | TEST(CancelableCallbackTest, PostTaskAndReply) { |
| 215 | std::optional<test::SingleThreadTaskEnvironment> task_environment; |
| 216 | task_environment.emplace(); |
| 217 | |
| 218 | int count = 0; |
| 219 | CancelableRepeatingClosure cancelable_reply( |
| 220 | BindRepeating(&Increment, Unretained(&count))); |
| 221 | |
| 222 | std::optional<RunLoop> loop; |
| 223 | loop.emplace(); |
| 224 | SingleThreadTaskRunner::GetCurrentDefault()->PostTaskAndReply( |
| 225 | FROM_HERE, DoNothing(), |
| 226 | cancelable_reply.callback().Then(loop->QuitClosure())); |
| 227 | loop->Run(); |
| 228 | |
| 229 | EXPECT_EQ(1, count); |
| 230 | |
| 231 | loop.emplace(); |
| 232 | SingleThreadTaskRunner::GetCurrentDefault()->PostTaskAndReply( |
| 233 | FROM_HERE, DoNothing(), |
| 234 | cancelable_reply.callback().Then(loop->QuitClosure())); |
| 235 | |
| 236 | // Cancel before running the tasks. |
| 237 | cancelable_reply.Cancel(); |
| 238 | loop->Run(); |
| 239 | |
| 240 | // Callback never ran due to cancellation; count is the same. Note that |
| 241 | // QuitClosure() is still invoked because chained callbacks via Then() get |
| 242 | // invoked even if the first callback is cancelled. |
| 243 | EXPECT_EQ(1, count); |
| 244 | |
| 245 | // Post it again to exercise a shutdown-like scenario. |
| 246 | cancelable_reply.Reset(BindRepeating(&Increment, Unretained(&count))); |
| 247 | |
| 248 | SingleThreadTaskRunner::GetCurrentDefault()->PostTaskAndReply( |
| 249 | FROM_HERE, DoNothing(), cancelable_reply.callback()); |
| 250 | task_environment.reset(); |
| 251 | |
| 252 | // Callback never ran due to task runner shutdown; count is the same. |
| 253 | EXPECT_EQ(1, count); |
| 254 | } |
| 255 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 256 | // CancelableRepeatingCallback can be used with move-only types. |
kuznetsovs | b020bcd8 | 2016-04-13 13:01:47 | [diff] [blame] | 257 | TEST(CancelableCallbackTest, MoveOnlyType) { |
| 258 | const int kExpectedResult = 42; |
| 259 | |
| 260 | int result = 0; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 261 | CancelableRepeatingCallback<void(std::unique_ptr<int>)> cb( |
Mikel Astiz | 239acc17 | 2024-01-11 14:16:26 | [diff] [blame] | 262 | BindRepeating(&OnMoveOnlyReceived, Unretained(&result))); |
David Benjamin | dd43629 | 2018-10-11 16:28:00 | [diff] [blame] | 263 | cb.callback().Run(std::make_unique<int>(kExpectedResult)); |
kuznetsovs | b020bcd8 | 2016-04-13 13:01:47 | [diff] [blame] | 264 | |
| 265 | EXPECT_EQ(kExpectedResult, result); |
| 266 | } |
| 267 | |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 268 | } // namespace |
| 269 | } // namespace base |