Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 5 | #include "base/timer/timer.h" |
| 6 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
Avi Drissman | 63e1f99 | 2023-01-13 18:54:43 | [diff] [blame] | 11 | #include "base/functional/bind.h" |
| 12 | #include "base/functional/callback.h" |
| 13 | #include "base/functional/callback_helpers.h" |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 15 | #include "base/run_loop.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 16 | #include "base/task/sequenced_task_runner.h" |
Guido Urdaneta | ef4e9194 | 2020-11-09 15:06:24 | [diff] [blame] | 17 | #include "base/test/bind.h" |
Etienne Pierre-doray | 1a0edc7 | 2022-01-14 15:11:44 | [diff] [blame] | 18 | #include "base/test/mock_callback.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 19 | #include "base/test/task_environment.h" |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 20 | #include "base/test/test_simple_task_runner.h" |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 21 | #include "base/time/tick_clock.h" |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 22 | #include "base/time/time.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 23 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 24 | #include "testing/gtest/include/gtest/gtest.h" |
| 25 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 26 | namespace base { |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 27 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 28 | namespace { |
| 29 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 30 | constexpr TimeDelta kTestDelay = Seconds(10); |
| 31 | constexpr TimeDelta kLongTestDelay = Minutes(10); |
| 32 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 33 | // The main thread types on which each timer should be tested. |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 34 | const test::TaskEnvironment::MainThreadType testing_main_threads[] = { |
| 35 | test::TaskEnvironment::MainThreadType::DEFAULT, |
| 36 | test::TaskEnvironment::MainThreadType::IO, |
Xiaohan Wang | 7940de2 | 2022-01-15 14:47:55 | [diff] [blame] | 37 | #if !BUILDFLAG(IS_IOS) // iOS does not allow direct running of the UI loop. |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 38 | test::TaskEnvironment::MainThreadType::UI, |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 39 | #endif |
| 40 | }; |
| 41 | |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 42 | class Receiver { |
| 43 | public: |
Peter Kasting | 6218bbad | 2025-01-10 11:26:40 | [diff] [blame] | 44 | Receiver() = default; |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 45 | void OnCalled() { count_++; } |
| 46 | bool WasCalled() { return count_ > 0; } |
| 47 | int TimesCalled() { return count_; } |
| 48 | |
| 49 | private: |
Peter Kasting | 811504a7 | 2025-01-09 03:18:50 | [diff] [blame] | 50 | int count_ = 0; |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 51 | }; |
| 52 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 53 | // Basic test with same setup as RunTest_OneShotTimers_Cancel below to confirm |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 54 | // that |timer| would be fired in that test if it wasn't for the deletion. |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 55 | void RunTest_OneShotTimers( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 56 | test::TaskEnvironment::MainThreadType main_thread_type) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 57 | test::TaskEnvironment task_environment( |
| 58 | test::TaskEnvironment::TimeSource::MOCK_TIME, main_thread_type); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 59 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 60 | Receiver receiver; |
| 61 | OneShotTimer timer; |
| 62 | timer.Start(FROM_HERE, kTestDelay, |
| 63 | BindOnce(&Receiver::OnCalled, Unretained(&receiver))); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 64 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 65 | task_environment.FastForwardBy(kTestDelay); |
| 66 | EXPECT_TRUE(receiver.WasCalled()); |
Etienne Pierre-doray | 9c3d17d8 | 2022-05-12 22:05:29 | [diff] [blame] | 67 | EXPECT_FALSE(timer.IsRunning()); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 68 | } |
| 69 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 70 | void RunTest_OneShotTimers_Cancel( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 71 | test::TaskEnvironment::MainThreadType main_thread_type) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 72 | test::TaskEnvironment task_environment( |
| 73 | test::TaskEnvironment::TimeSource::MOCK_TIME, main_thread_type); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 74 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 75 | Receiver receiver; |
| 76 | auto timer = std::make_unique<OneShotTimer>(); |
| 77 | auto* timer_ptr = timer.get(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 78 | |
| 79 | // This should run before the timer expires. |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 80 | SequencedTaskRunner::GetCurrentDefault()->DeleteSoon(FROM_HERE, |
| 81 | std::move(timer)); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 82 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 83 | timer_ptr->Start(FROM_HERE, kTestDelay, |
| 84 | BindOnce(&Receiver::OnCalled, Unretained(&receiver))); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 85 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 86 | task_environment.FastForwardBy(kTestDelay); |
| 87 | EXPECT_FALSE(receiver.WasCalled()); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 88 | } |
| 89 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 90 | void RunTest_OneShotSelfDeletingTimer( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 91 | test::TaskEnvironment::MainThreadType main_thread_type) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 92 | test::TaskEnvironment task_environment( |
| 93 | test::TaskEnvironment::TimeSource::MOCK_TIME, main_thread_type); |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 94 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 95 | Receiver receiver; |
| 96 | auto timer = std::make_unique<OneShotTimer>(); |
| 97 | auto* timer_ptr = timer.get(); |
| 98 | |
| 99 | timer_ptr->Start( |
| 100 | FROM_HERE, kTestDelay, |
| 101 | BindLambdaForTesting([&receiver, timer = std::move(timer)]() mutable { |
| 102 | receiver.OnCalled(); |
Etienne Pierre-doray | 9c3d17d8 | 2022-05-12 22:05:29 | [diff] [blame] | 103 | EXPECT_FALSE(timer->IsRunning()); |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 104 | timer.reset(); |
| 105 | })); |
| 106 | |
| 107 | task_environment.FastForwardBy(kTestDelay); |
| 108 | EXPECT_TRUE(receiver.WasCalled()); |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 109 | } |
| 110 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 111 | void RunTest_RepeatingTimer( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 112 | test::TaskEnvironment::MainThreadType main_thread_type, |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 113 | const TimeDelta& delay) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 114 | test::TaskEnvironment task_environment( |
| 115 | test::TaskEnvironment::TimeSource::MOCK_TIME, main_thread_type); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 116 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 117 | Receiver receiver; |
| 118 | RepeatingTimer timer; |
| 119 | timer.Start(FROM_HERE, kTestDelay, |
| 120 | BindRepeating(&Receiver::OnCalled, Unretained(&receiver))); |
| 121 | |
| 122 | task_environment.FastForwardBy(20 * kTestDelay); |
| 123 | EXPECT_EQ(receiver.TimesCalled(), 20); |
Etienne Pierre-doray | 9c3d17d8 | 2022-05-12 22:05:29 | [diff] [blame] | 124 | EXPECT_TRUE(timer.IsRunning()); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 125 | } |
| 126 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 127 | void RunTest_RepeatingTimer_Cancel( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 128 | test::TaskEnvironment::MainThreadType main_thread_type, |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 129 | const TimeDelta& delay) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 130 | test::TaskEnvironment task_environment( |
| 131 | test::TaskEnvironment::TimeSource::MOCK_TIME, main_thread_type); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 132 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 133 | Receiver receiver; |
| 134 | auto timer = std::make_unique<RepeatingTimer>(); |
| 135 | auto* timer_ptr = timer.get(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 136 | |
| 137 | // This should run before the timer expires. |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 138 | SequencedTaskRunner::GetCurrentDefault()->DeleteSoon(FROM_HERE, |
| 139 | std::move(timer)); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 140 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 141 | timer_ptr->Start(FROM_HERE, delay, |
| 142 | BindRepeating(&Receiver::OnCalled, Unretained(&receiver))); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 143 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 144 | task_environment.FastForwardBy(delay); |
| 145 | EXPECT_FALSE(receiver.WasCalled()); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 146 | } |
| 147 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 148 | void RunTest_DelayTimer_NoCall( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 149 | test::TaskEnvironment::MainThreadType main_thread_type) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 150 | test::TaskEnvironment task_environment( |
| 151 | test::TaskEnvironment::TimeSource::MOCK_TIME, main_thread_type); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 152 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 153 | Receiver receiver; |
| 154 | DelayTimer timer(FROM_HERE, kTestDelay, &receiver, &Receiver::OnCalled); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 155 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 156 | task_environment.FastForwardBy(kTestDelay); |
| 157 | EXPECT_FALSE(receiver.WasCalled()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 158 | } |
| 159 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 160 | void RunTest_DelayTimer_OneCall( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 161 | test::TaskEnvironment::MainThreadType main_thread_type) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 162 | test::TaskEnvironment task_environment( |
| 163 | test::TaskEnvironment::TimeSource::MOCK_TIME, main_thread_type); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 164 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 165 | Receiver receiver; |
| 166 | DelayTimer timer(FROM_HERE, kTestDelay, &receiver, &Receiver::OnCalled); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 167 | timer.Reset(); |
| 168 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 169 | task_environment.FastForwardBy(kTestDelay); |
| 170 | EXPECT_TRUE(receiver.WasCalled()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 171 | } |
| 172 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 173 | void RunTest_DelayTimer_Reset( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 174 | test::TaskEnvironment::MainThreadType main_thread_type) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 175 | test::TaskEnvironment task_environment( |
| 176 | test::TaskEnvironment::TimeSource::MOCK_TIME, main_thread_type); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 177 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 178 | Receiver receiver; |
| 179 | DelayTimer timer(FROM_HERE, kTestDelay, &receiver, &Receiver::OnCalled); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 180 | timer.Reset(); |
| 181 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 182 | // Fast-forward by a delay smaller than the timer delay. The timer will not |
| 183 | // fire. |
| 184 | task_environment.FastForwardBy(kTestDelay / 2); |
| 185 | EXPECT_FALSE(receiver.WasCalled()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 186 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 187 | // Postpone the fire time. |
| 188 | timer.Reset(); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 189 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 190 | // Verify that the timer does not fire at its original fire time. |
| 191 | task_environment.FastForwardBy(kTestDelay / 2); |
| 192 | EXPECT_FALSE(receiver.WasCalled()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 193 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 194 | // Fast-forward by the timer delay. The timer will fire. |
| 195 | task_environment.FastForwardBy(kTestDelay / 2); |
| 196 | EXPECT_TRUE(receiver.WasCalled()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 197 | } |
| 198 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 199 | void RunTest_DelayTimer_Deleted( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 200 | test::TaskEnvironment::MainThreadType main_thread_type) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 201 | test::TaskEnvironment task_environment( |
| 202 | test::TaskEnvironment::TimeSource::MOCK_TIME, main_thread_type); |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 203 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 204 | Receiver receiver; |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 205 | |
| 206 | { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 207 | DelayTimer timer(FROM_HERE, kTestDelay, &receiver, &Receiver::OnCalled); |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 208 | timer.Reset(); |
| 209 | } |
| 210 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 211 | // Because the timer was deleted, it will never fire. |
| 212 | task_environment.FastForwardBy(kTestDelay); |
| 213 | EXPECT_FALSE(receiver.WasCalled()); |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 216 | } // namespace |
| 217 | |
| 218 | //----------------------------------------------------------------------------- |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 219 | // Each test is run against each type of main thread. That way we are sure |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 220 | // that timers work properly in all configurations. |
| 221 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 222 | class TimerTestWithThreadType |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 223 | : public testing::TestWithParam<test::TaskEnvironment::MainThreadType> {}; |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 224 | |
| 225 | TEST_P(TimerTestWithThreadType, OneShotTimers) { |
| 226 | RunTest_OneShotTimers(GetParam()); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 227 | } |
| 228 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 229 | TEST_P(TimerTestWithThreadType, OneShotTimers_Cancel) { |
| 230 | RunTest_OneShotTimers_Cancel(GetParam()); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 233 | // If underline timer does not handle properly, we will crash or fail |
[email protected] | 64e95e1 | 2011-08-17 17:41:02 | [diff] [blame] | 234 | // in full page heap environment. |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 235 | TEST_P(TimerTestWithThreadType, OneShotSelfDeletingTimer) { |
| 236 | RunTest_OneShotSelfDeletingTimer(GetParam()); |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 237 | } |
| 238 | |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 239 | TEST(TimerTest, OneShotTimer_CustomTaskRunner) { |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 240 | auto task_runner = base::MakeRefCounted<TestSimpleTaskRunner>(); |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 241 | |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 242 | OneShotTimer timer; |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 243 | |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 244 | bool task_ran = false; |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 245 | |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 246 | // The timer will use the TestSimpleTaskRunner to schedule its delays. |
| 247 | timer.SetTaskRunner(task_runner); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 248 | timer.Start(FROM_HERE, Days(1), |
Sorin Jianu | ad4cc623 | 2024-10-08 19:06:01 | [diff] [blame] | 249 | BindLambdaForTesting([&] { task_ran = true; })); |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 250 | |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 251 | EXPECT_FALSE(task_ran); |
| 252 | EXPECT_TRUE(task_runner->HasPendingTask()); |
| 253 | |
| 254 | task_runner->RunPendingTasks(); |
| 255 | |
| 256 | EXPECT_TRUE(task_ran); |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 257 | } |
| 258 | |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 259 | TEST(TimerTest, OneShotTimerWithTickClock) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 260 | test::TaskEnvironment task_environment( |
| 261 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 262 | Receiver receiver; |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 263 | OneShotTimer timer(task_environment.GetMockTickClock()); |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 264 | timer.Start(FROM_HERE, kTestDelay, |
tzik | f3336c9 | 2018-07-25 03:15:50 | [diff] [blame] | 265 | BindOnce(&Receiver::OnCalled, Unretained(&receiver))); |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 266 | task_environment.FastForwardBy(kTestDelay); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 267 | EXPECT_TRUE(receiver.WasCalled()); |
Etienne Pierre-doray | 9c3d17d8 | 2022-05-12 22:05:29 | [diff] [blame] | 268 | EXPECT_FALSE(timer.IsRunning()); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 269 | } |
| 270 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 271 | TEST_P(TimerTestWithThreadType, RepeatingTimer) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 272 | RunTest_RepeatingTimer(GetParam(), kTestDelay); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 273 | } |
| 274 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 275 | TEST_P(TimerTestWithThreadType, RepeatingTimer_Cancel) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 276 | RunTest_RepeatingTimer_Cancel(GetParam(), kTestDelay); |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 277 | } |
| 278 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 279 | TEST_P(TimerTestWithThreadType, RepeatingTimerZeroDelay) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 280 | RunTest_RepeatingTimer(GetParam(), Seconds(0)); |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 281 | } |
| 282 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 283 | TEST_P(TimerTestWithThreadType, RepeatingTimerZeroDelay_Cancel) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 284 | RunTest_RepeatingTimer_Cancel(GetParam(), Seconds(0)); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 285 | } |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 286 | |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 287 | TEST(TimerTest, RepeatingTimerWithTickClock) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 288 | test::TaskEnvironment task_environment( |
| 289 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 290 | Receiver receiver; |
| 291 | const int expected_times_called = 10; |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 292 | RepeatingTimer timer(task_environment.GetMockTickClock()); |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 293 | timer.Start(FROM_HERE, kTestDelay, |
tzik | f3336c9 | 2018-07-25 03:15:50 | [diff] [blame] | 294 | BindRepeating(&Receiver::OnCalled, Unretained(&receiver))); |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 295 | task_environment.FastForwardBy(expected_times_called * kTestDelay); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 296 | timer.Stop(); |
| 297 | EXPECT_EQ(expected_times_called, receiver.TimesCalled()); |
| 298 | } |
| 299 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 300 | TEST_P(TimerTestWithThreadType, DelayTimer_NoCall) { |
| 301 | RunTest_DelayTimer_NoCall(GetParam()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 302 | } |
| 303 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 304 | TEST_P(TimerTestWithThreadType, DelayTimer_OneCall) { |
| 305 | RunTest_DelayTimer_OneCall(GetParam()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 306 | } |
| 307 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 308 | TEST_P(TimerTestWithThreadType, DelayTimer_Reset) { |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 309 | RunTest_DelayTimer_Reset(GetParam()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 310 | } |
| 311 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 312 | TEST_P(TimerTestWithThreadType, DelayTimer_Deleted) { |
| 313 | RunTest_DelayTimer_Deleted(GetParam()); |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 314 | } |
| 315 | |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 316 | TEST(TimerTest, DelayTimerWithTickClock) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 317 | test::TaskEnvironment task_environment( |
| 318 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 319 | Receiver receiver; |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 320 | DelayTimer timer(FROM_HERE, kTestDelay, &receiver, &Receiver::OnCalled, |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 321 | task_environment.GetMockTickClock()); |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 322 | task_environment.FastForwardBy(kTestDelay - Microseconds(1)); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 323 | EXPECT_FALSE(receiver.WasCalled()); |
| 324 | timer.Reset(); |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 325 | task_environment.FastForwardBy(kTestDelay - Microseconds(1)); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 326 | EXPECT_FALSE(receiver.WasCalled()); |
| 327 | timer.Reset(); |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 328 | task_environment.FastForwardBy(kTestDelay); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 329 | EXPECT_TRUE(receiver.WasCalled()); |
| 330 | } |
| 331 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 332 | TEST(TimerTest, TaskEnvironmentShutdown) { |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 333 | // This test is designed to verify that shutdown of the |
| 334 | // message loop does not cause crashes if there were pending |
| 335 | // timers not yet fired. It may only trigger exceptions |
[email protected] | 64e95e1 | 2011-08-17 17:41:02 | [diff] [blame] | 336 | // if debug heap checking is enabled. |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 337 | Receiver receiver; |
| 338 | OneShotTimer timer; |
| 339 | |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 340 | { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 341 | test::TaskEnvironment task_environment; |
| 342 | timer.Start(FROM_HERE, kTestDelay, |
| 343 | BindOnce(&Receiver::OnCalled, Unretained(&receiver))); |
| 344 | } // Task environment destructs by falling out of scope. |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 345 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 346 | EXPECT_FALSE(receiver.WasCalled()); |
| 347 | // Timer destruct. SHOULD NOT CRASH, of course. |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 348 | } |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 349 | |
Etienne Pierre-doray | 9c3d17d8 | 2022-05-12 22:05:29 | [diff] [blame] | 350 | TEST(TimerTest, TaskEnvironmentSelfOwningTimer) { |
| 351 | // This test verifies that a timer does not cause crashes if |
| 352 | // |Timer::user_task_| owns the timer. The test may only trigger exceptions if |
| 353 | // debug heap checking is enabled. |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 354 | |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 355 | auto timer = std::make_unique<OneShotTimer>(); |
| 356 | auto* timer_ptr = timer.get(); |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 357 | |
Etienne Pierre-doray | 9c3d17d8 | 2022-05-12 22:05:29 | [diff] [blame] | 358 | test::TaskEnvironment task_environment( |
| 359 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 360 | |
| 361 | timer_ptr->Start(FROM_HERE, kTestDelay, |
| 362 | BindLambdaForTesting([timer = std::move(timer)]() {})); |
Etienne Pierre-doray | 9c3d17d8 | 2022-05-12 22:05:29 | [diff] [blame] | 363 | // |Timer::user_task_| owns sole reference to |timer|. Both will be destroyed |
| 364 | // once the task ran. SHOULD NOT CRASH. |
| 365 | task_environment.FastForwardUntilNoTasksRemain(); |
| 366 | } |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 367 | |
Etienne Pierre-doray | 9c3d17d8 | 2022-05-12 22:05:29 | [diff] [blame] | 368 | TEST(TimerTest, TaskEnvironmentSelfOwningTimerStopped) { |
| 369 | // This test verifies that a timer does not cause crashes when stopped if |
| 370 | // |Timer::user_task_| owns the timer. The test may only trigger exceptions if |
| 371 | // debug heap checking is enabled. |
| 372 | |
| 373 | auto timer = std::make_unique<OneShotTimer>(); |
| 374 | auto* timer_ptr = timer.get(); |
| 375 | |
| 376 | test::TaskEnvironment task_environment( |
| 377 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 378 | |
| 379 | timer_ptr->Start(FROM_HERE, kTestDelay, |
| 380 | BindLambdaForTesting([timer = std::move(timer)]() { |
| 381 | // Stop destroys |Timer::user_task_| which owns sole |
| 382 | // reference to |timer|. SHOULD NOT CRASH. |
| 383 | timer->Stop(); |
| 384 | })); |
| 385 | task_environment.FastForwardUntilNoTasksRemain(); |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 388 | TEST(TimerTest, NonRepeatIsRunning) { |
| 389 | { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 390 | test::TaskEnvironment task_environment; |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 391 | OneShotTimer timer; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 392 | EXPECT_FALSE(timer.IsRunning()); |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 393 | timer.Start(FROM_HERE, kTestDelay, DoNothing()); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 394 | EXPECT_TRUE(timer.IsRunning()); |
| 395 | timer.Stop(); |
| 396 | EXPECT_FALSE(timer.IsRunning()); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | { |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 400 | RetainingOneShotTimer timer; |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 401 | test::TaskEnvironment task_environment; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 402 | EXPECT_FALSE(timer.IsRunning()); |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 403 | timer.Start(FROM_HERE, kTestDelay, DoNothing()); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 404 | EXPECT_TRUE(timer.IsRunning()); |
| 405 | timer.Stop(); |
| 406 | EXPECT_FALSE(timer.IsRunning()); |
| 407 | ASSERT_FALSE(timer.user_task().is_null()); |
| 408 | timer.Reset(); |
| 409 | EXPECT_TRUE(timer.IsRunning()); |
| 410 | } |
| 411 | } |
| 412 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 413 | TEST(TimerTest, NonRepeatTaskEnvironmentDeath) { |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 414 | OneShotTimer timer; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 415 | { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 416 | test::TaskEnvironment task_environment; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 417 | EXPECT_FALSE(timer.IsRunning()); |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 418 | timer.Start(FROM_HERE, kTestDelay, DoNothing()); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 419 | EXPECT_TRUE(timer.IsRunning()); |
| 420 | } |
| 421 | EXPECT_FALSE(timer.IsRunning()); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | TEST(TimerTest, RetainRepeatIsRunning) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 425 | test::TaskEnvironment task_environment; |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 426 | RepeatingTimer timer(FROM_HERE, kTestDelay, DoNothing()); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 427 | EXPECT_FALSE(timer.IsRunning()); |
| 428 | timer.Reset(); |
| 429 | EXPECT_TRUE(timer.IsRunning()); |
| 430 | timer.Stop(); |
| 431 | EXPECT_FALSE(timer.IsRunning()); |
| 432 | timer.Reset(); |
| 433 | EXPECT_TRUE(timer.IsRunning()); |
| 434 | } |
| 435 | |
| 436 | TEST(TimerTest, RetainNonRepeatIsRunning) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 437 | test::TaskEnvironment task_environment; |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 438 | RetainingOneShotTimer timer(FROM_HERE, kTestDelay, DoNothing()); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 439 | EXPECT_FALSE(timer.IsRunning()); |
| 440 | timer.Reset(); |
| 441 | EXPECT_TRUE(timer.IsRunning()); |
| 442 | timer.Stop(); |
| 443 | EXPECT_FALSE(timer.IsRunning()); |
| 444 | timer.Reset(); |
| 445 | EXPECT_TRUE(timer.IsRunning()); |
| 446 | } |
| 447 | |
gab | 4e844bb | 2017-06-01 16:23:36 | [diff] [blame] | 448 | //----------------------------------------------------------------------------- |
| 449 | |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 450 | TEST(TimerTest, ContinuationStopStart) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 451 | test::TaskEnvironment task_environment( |
| 452 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 453 | |
| 454 | Receiver receiver1; |
| 455 | Receiver receiver2; |
| 456 | OneShotTimer timer; |
| 457 | timer.Start(FROM_HERE, kTestDelay, |
| 458 | BindOnce(&Receiver::OnCalled, Unretained(&receiver1))); |
| 459 | timer.Stop(); |
| 460 | timer.Start(FROM_HERE, kLongTestDelay, |
| 461 | BindOnce(&Receiver::OnCalled, Unretained(&receiver2))); |
| 462 | task_environment.FastForwardBy(kLongTestDelay); |
| 463 | EXPECT_FALSE(receiver1.WasCalled()); |
| 464 | EXPECT_TRUE(receiver2.WasCalled()); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | TEST(TimerTest, ContinuationReset) { |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 468 | test::TaskEnvironment task_environment( |
| 469 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 470 | |
| 471 | Receiver receiver; |
| 472 | OneShotTimer timer; |
| 473 | timer.Start(FROM_HERE, kTestDelay, |
| 474 | BindOnce(&Receiver::OnCalled, Unretained(&receiver))); |
| 475 | timer.Reset(); |
| 476 | // // Since Reset happened before task ran, the user_task must not be |
| 477 | // cleared: ASSERT_FALSE(timer.user_task().is_null()); |
| 478 | task_environment.FastForwardBy(kTestDelay); |
| 479 | EXPECT_TRUE(receiver.WasCalled()); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 480 | } |
| 481 | |
Patrick Monette | 57d1c18d | 2021-04-22 00:50:44 | [diff] [blame] | 482 | TEST(TimerTest, AbandonedTaskIsCancelled) { |
| 483 | test::TaskEnvironment task_environment( |
| 484 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 485 | OneShotTimer timer; |
| 486 | |
| 487 | // Start a timer. There will be a pending task on the current sequence. |
Patrick Monette | 46682393 | 2022-01-10 20:33:52 | [diff] [blame] | 488 | timer.Start(FROM_HERE, kTestDelay, base::DoNothing()); |
Patrick Monette | 57d1c18d | 2021-04-22 00:50:44 | [diff] [blame] | 489 | EXPECT_EQ(1u, task_environment.GetPendingMainThreadTaskCount()); |
| 490 | |
Alex Attar | 467b2ce | 2024-11-04 14:50:28 | [diff] [blame] | 491 | // After Stop(), the task is correctly treated as cancelled. |
| 492 | timer.Stop(); |
Patrick Monette | 57d1c18d | 2021-04-22 00:50:44 | [diff] [blame] | 493 | EXPECT_EQ(0u, task_environment.GetPendingMainThreadTaskCount()); |
Etienne Pierre-doray | 9c3d17d8 | 2022-05-12 22:05:29 | [diff] [blame] | 494 | EXPECT_FALSE(timer.IsRunning()); |
Patrick Monette | 57d1c18d | 2021-04-22 00:50:44 | [diff] [blame] | 495 | } |
| 496 | |
Etienne Pierre-doray | 1a0edc7 | 2022-01-14 15:11:44 | [diff] [blame] | 497 | TEST(TimerTest, DeadlineTimer) { |
| 498 | test::TaskEnvironment task_environment( |
| 499 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 500 | RunLoop run_loop; |
| 501 | DeadlineTimer timer; |
| 502 | TimeTicks start = TimeTicks::Now(); |
| 503 | |
| 504 | timer.Start(FROM_HERE, start + Seconds(5), run_loop.QuitClosure()); |
| 505 | run_loop.Run(); |
| 506 | EXPECT_EQ(start + Seconds(5), TimeTicks::Now()); |
| 507 | } |
| 508 | |
| 509 | TEST(TimerTest, DeadlineTimerCancel) { |
| 510 | test::TaskEnvironment task_environment( |
| 511 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 512 | RunLoop run_loop; |
| 513 | DeadlineTimer timer; |
| 514 | TimeTicks start = TimeTicks::Now(); |
| 515 | |
| 516 | MockRepeatingCallback<void()> callback; |
| 517 | timer.Start(FROM_HERE, start + Seconds(5), callback.Get()); |
| 518 | |
| 519 | EXPECT_CALL(callback, Run()).Times(0); |
| 520 | timer.Stop(); |
| 521 | task_environment.FastForwardBy(Seconds(5)); |
| 522 | EXPECT_EQ(start + Seconds(5), TimeTicks::Now()); |
| 523 | } |
| 524 | |
Etienne Pierre-doray | 9c3d17d8 | 2022-05-12 22:05:29 | [diff] [blame] | 525 | TEST(TimerTest, DeadlineTimerTaskDestructed) { |
| 526 | test::TaskEnvironment task_environment( |
| 527 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 528 | RunLoop run_loop; |
| 529 | DeadlineTimer timer; |
| 530 | TimeTicks start = TimeTicks::Now(); |
| 531 | |
| 532 | MockRepeatingCallback<void()> destructed; |
| 533 | ScopedClosureRunner scoped_closure(destructed.Get()); |
| 534 | timer.Start(FROM_HERE, start + Seconds(5), |
| 535 | BindOnce([](ScopedClosureRunner) {}, std::move(scoped_closure))); |
| 536 | |
| 537 | EXPECT_CALL(destructed, Run()); |
| 538 | timer.Stop(); |
| 539 | testing::Mock::VerifyAndClearExpectations(&destructed); |
| 540 | } |
| 541 | |
Etienne Pierre-doray | feab1c2 | 2022-12-01 15:31:05 | [diff] [blame] | 542 | TEST(TimerTest, DeadlineTimerStartTwice) { |
| 543 | test::TaskEnvironment task_environment( |
| 544 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 545 | DeadlineTimer timer; |
| 546 | TimeTicks start = TimeTicks::Now(); |
| 547 | |
| 548 | RunLoop run_loop; |
| 549 | timer.Start(FROM_HERE, start + Seconds(5), run_loop.QuitClosure()); |
| 550 | timer.Start(FROM_HERE, start + Seconds(10), run_loop.QuitClosure()); |
| 551 | run_loop.Run(); |
| 552 | EXPECT_EQ(start + Seconds(10), TimeTicks::Now()); |
| 553 | } |
| 554 | |
Etienne Pierre-doray | c765461 | 2022-06-08 22:23:32 | [diff] [blame] | 555 | TEST(TimerTest, MetronomeTimer) { |
| 556 | test::TaskEnvironment task_environment( |
| 557 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 558 | MetronomeTimer timer; |
| 559 | TimeTicks start = TimeTicks::Now(); |
| 560 | |
| 561 | // Ensure the run_loop.Run() below doesn't straddle over multiple ticks. |
| 562 | task_environment.AdvanceClock( |
| 563 | start.SnappedToNextTick(TimeTicks(), Seconds(5)) - start); |
| 564 | start = TimeTicks::Now(); |
| 565 | |
| 566 | RunLoop run_loop; |
| 567 | timer.Start(FROM_HERE, Seconds(5), run_loop.QuitClosure()); |
| 568 | run_loop.Run(); |
| 569 | EXPECT_EQ(start + Seconds(5), TimeTicks::Now()); |
| 570 | } |
| 571 | |
| 572 | TEST(TimerTest, MetronomeTimerCustomPhase) { |
| 573 | test::TaskEnvironment task_environment( |
| 574 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 575 | RunLoop run_loop; |
| 576 | MetronomeTimer timer; |
| 577 | TimeTicks start = TimeTicks::Now(); |
| 578 | |
| 579 | timer.Start(FROM_HERE, Seconds(5), run_loop.QuitClosure(), start); |
| 580 | run_loop.Run(); |
| 581 | EXPECT_EQ(start + Seconds(5), TimeTicks::Now()); |
| 582 | } |
| 583 | |
| 584 | TEST(TimerTest, MetronomeTimerReset) { |
| 585 | test::TaskEnvironment task_environment( |
| 586 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 587 | RunLoop run_loop; |
| 588 | TimeTicks start = TimeTicks::Now(); |
| 589 | MetronomeTimer timer(FROM_HERE, Seconds(5), run_loop.QuitClosure(), start); |
| 590 | |
| 591 | timer.Reset(); |
| 592 | run_loop.Run(); |
| 593 | EXPECT_EQ(start + Seconds(5), TimeTicks::Now()); |
| 594 | } |
| 595 | |
| 596 | TEST(TimerTest, MetronomeTimerStartTwice) { |
| 597 | test::TaskEnvironment task_environment( |
| 598 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 599 | MetronomeTimer timer; |
| 600 | TimeTicks start = TimeTicks::Now(); |
| 601 | |
| 602 | { |
| 603 | RunLoop run_loop; |
| 604 | timer.Start(FROM_HERE, Seconds(4), run_loop.QuitClosure(), start); |
| 605 | run_loop.Run(); |
| 606 | } |
| 607 | EXPECT_EQ(start + Seconds(4), TimeTicks::Now()); |
| 608 | |
| 609 | { |
| 610 | RunLoop run_loop; |
| 611 | timer.Start(FROM_HERE, Seconds(2), run_loop.QuitClosure(), start); |
| 612 | run_loop.Run(); |
| 613 | } |
| 614 | EXPECT_EQ(start + Seconds(6), TimeTicks::Now()); |
| 615 | } |
| 616 | |
| 617 | TEST(TimerTest, MetronomeTimerMultiple) { |
| 618 | test::TaskEnvironment task_environment( |
| 619 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 620 | MetronomeTimer timer; |
| 621 | TimeTicks start = TimeTicks::Now(); |
| 622 | |
| 623 | // Ensure the subsequent FastForwardBy() don't straddle over multiple ticks. |
| 624 | task_environment.AdvanceClock( |
| 625 | start.SnappedToNextTick(TimeTicks(), Seconds(5)) - start); |
| 626 | |
| 627 | MockRepeatingCallback<void()> callback; |
| 628 | timer.Start(FROM_HERE, Seconds(5), callback.Get()); |
| 629 | |
| 630 | // The first tick is skipped because it is too close. Ticks at 5s and 10s. |
| 631 | EXPECT_CALL(callback, Run()).Times(2); |
| 632 | task_environment.FastForwardBy(Seconds(10)); |
| 633 | |
| 634 | EXPECT_CALL(callback, Run()).Times(2); |
| 635 | // Ticks at 15s and 25s, while 20s is missed. |
| 636 | task_environment.AdvanceClock(Seconds(12)); |
| 637 | task_environment.FastForwardBy(Seconds(3)); |
| 638 | } |
| 639 | |
| 640 | TEST(TimerTest, MetronomeTimerCancel) { |
| 641 | test::TaskEnvironment task_environment( |
| 642 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 643 | RunLoop run_loop; |
| 644 | MetronomeTimer timer; |
| 645 | TimeTicks start = TimeTicks::Now(); |
| 646 | |
| 647 | MockRepeatingCallback<void()> callback; |
| 648 | timer.Start(FROM_HERE, Seconds(5), callback.Get()); |
| 649 | |
| 650 | EXPECT_CALL(callback, Run()).Times(0); |
| 651 | timer.Stop(); |
| 652 | task_environment.FastForwardBy(Seconds(5)); |
| 653 | EXPECT_EQ(start + Seconds(5), TimeTicks::Now()); |
| 654 | } |
| 655 | |
Ilia Samsonov | a2885f7 | 2019-11-20 22:16:58 | [diff] [blame] | 656 | INSTANTIATE_TEST_SUITE_P(All, |
Victor Costan | 033b9ac | 2019-01-29 00:52:16 | [diff] [blame] | 657 | TimerTestWithThreadType, |
| 658 | testing::ValuesIn(testing_main_threads)); |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 659 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 660 | } // namespace base |