[email protected] | a67fc9e1 | 2012-02-14 00:29:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
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 | |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 11 | #include "base/bind.h" |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 12 | #include "base/callback.h" |
danakj | db9ae794 | 2020-11-11 16:01:35 | [diff] [blame] | 13 | #include "base/callback_helpers.h" |
Lei Zhang | b7f2622 | 2021-06-15 18:45:47 | [diff] [blame] | 14 | #include "base/cxx17_backports.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 15 | #include "base/macros.h" |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 16 | #include "base/memory/ptr_util.h" |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 18 | #include "base/run_loop.h" |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 19 | #include "base/sequenced_task_runner.h" |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 20 | #include "base/synchronization/waitable_event.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 21 | #include "base/task/post_task.h" |
Guido Urdaneta | ef4e9194 | 2020-11-09 15:06:24 | [diff] [blame] | 22 | #include "base/test/bind.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 23 | #include "base/test/task_environment.h" |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 24 | #include "base/test/test_mock_time_task_runner.h" |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 25 | #include "base/test/test_simple_task_runner.h" |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 26 | #include "base/threading/platform_thread.h" |
| 27 | #include "base/threading/sequenced_task_runner_handle.h" |
| 28 | #include "base/threading/thread.h" |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 29 | #include "base/time/tick_clock.h" |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 30 | #include "base/time/time.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 31 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | #include "testing/gtest/include/gtest/gtest.h" |
| 33 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 34 | namespace base { |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 35 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 36 | namespace { |
| 37 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 38 | // The main thread types on which each timer should be tested. |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 39 | const test::TaskEnvironment::MainThreadType testing_main_threads[] = { |
| 40 | test::TaskEnvironment::MainThreadType::DEFAULT, |
| 41 | test::TaskEnvironment::MainThreadType::IO, |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 42 | #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 43 | test::TaskEnvironment::MainThreadType::UI, |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 44 | #endif |
| 45 | }; |
| 46 | |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 47 | class Receiver { |
| 48 | public: |
| 49 | Receiver() : count_(0) {} |
| 50 | void OnCalled() { count_++; } |
| 51 | bool WasCalled() { return count_ > 0; } |
| 52 | int TimesCalled() { return count_; } |
| 53 | |
| 54 | private: |
| 55 | int count_; |
| 56 | }; |
| 57 | |
ahest | 68c9f10 | 2016-12-13 11:34:38 | [diff] [blame] | 58 | // A basic helper class that can start a one-shot timer and signal a |
| 59 | // WaitableEvent when this timer fires. |
| 60 | class OneShotTimerTesterBase { |
| 61 | public: |
| 62 | // |did_run|, if provided, will be signaled when Run() fires. |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 63 | explicit OneShotTimerTesterBase(WaitableEvent* did_run = nullptr, |
| 64 | const TimeDelta& delay = Milliseconds(10)) |
ahest | 68c9f10 | 2016-12-13 11:34:38 | [diff] [blame] | 65 | : did_run_(did_run), delay_(delay) {} |
| 66 | |
Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 67 | OneShotTimerTesterBase(const OneShotTimerTesterBase&) = delete; |
| 68 | OneShotTimerTesterBase& operator=(const OneShotTimerTesterBase&) = delete; |
| 69 | |
ahest | 68c9f10 | 2016-12-13 11:34:38 | [diff] [blame] | 70 | virtual ~OneShotTimerTesterBase() = default; |
| 71 | |
| 72 | void Start() { |
| 73 | started_time_ = TimeTicks::Now(); |
| 74 | timer_->Start(FROM_HERE, delay_, this, &OneShotTimerTesterBase::Run); |
| 75 | } |
| 76 | |
| 77 | bool IsRunning() { return timer_->IsRunning(); } |
| 78 | |
| 79 | TimeTicks started_time() const { return started_time_; } |
| 80 | TimeDelta delay() const { return delay_; } |
| 81 | |
| 82 | protected: |
| 83 | virtual void Run() { |
| 84 | if (did_run_) { |
| 85 | EXPECT_FALSE(did_run_->IsSignaled()); |
| 86 | did_run_->Signal(); |
| 87 | } |
| 88 | } |
| 89 | |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 90 | std::unique_ptr<OneShotTimer> timer_ = std::make_unique<OneShotTimer>(); |
ahest | 68c9f10 | 2016-12-13 11:34:38 | [diff] [blame] | 91 | |
| 92 | private: |
| 93 | WaitableEvent* const did_run_; |
| 94 | const TimeDelta delay_; |
| 95 | TimeTicks started_time_; |
ahest | 68c9f10 | 2016-12-13 11:34:38 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | // Extends functionality of OneShotTimerTesterBase with the abilities to wait |
| 99 | // until the timer fires and to change task runner for the timer. |
| 100 | class OneShotTimerTester : public OneShotTimerTesterBase { |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 101 | public: |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 102 | // |did_run|, if provided, will be signaled when Run() fires. |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 103 | explicit OneShotTimerTester(WaitableEvent* did_run = nullptr, |
| 104 | const TimeDelta& delay = Milliseconds(10)) |
ahest | 68c9f10 | 2016-12-13 11:34:38 | [diff] [blame] | 105 | : OneShotTimerTesterBase(did_run, delay), |
| 106 | quit_closure_(run_loop_.QuitClosure()) {} |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 107 | |
Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 108 | OneShotTimerTester(const OneShotTimerTester&) = delete; |
| 109 | OneShotTimerTester& operator=(const OneShotTimerTester&) = delete; |
| 110 | |
ahest | 68c9f10 | 2016-12-13 11:34:38 | [diff] [blame] | 111 | ~OneShotTimerTester() override = default; |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 112 | |
gab | 4e844bb | 2017-06-01 16:23:36 | [diff] [blame] | 113 | void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) { |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 114 | timer_->SetTaskRunner(std::move(task_runner)); |
| 115 | |
| 116 | // Run() will be invoked on |task_runner| but |run_loop_|'s QuitClosure |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 117 | // needs to run on this thread (where the task environment lives). |
tzik | f3336c9 | 2018-07-25 03:15:50 | [diff] [blame] | 118 | quit_closure_ = BindOnce(IgnoreResult(&SequencedTaskRunner::PostTask), |
| 119 | SequencedTaskRunnerHandle::Get(), FROM_HERE, |
| 120 | run_loop_.QuitClosure()); |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 121 | } |
| 122 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 123 | // Blocks until Run() executes and confirms that Run() didn't fire before |
| 124 | // |delay_| expired. |
| 125 | void WaitAndConfirmTimerFiredAfterDelay() { |
| 126 | run_loop_.Run(); |
| 127 | |
ahest | 68c9f10 | 2016-12-13 11:34:38 | [diff] [blame] | 128 | EXPECT_NE(TimeTicks(), started_time()); |
| 129 | EXPECT_GE(TimeTicks::Now() - started_time(), delay()); |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 130 | } |
| 131 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 132 | protected: |
| 133 | // Overridable method to do things on Run() before signaling events/closures |
| 134 | // managed by this helper. |
| 135 | virtual void OnRun() {} |
| 136 | |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 137 | private: |
ahest | 68c9f10 | 2016-12-13 11:34:38 | [diff] [blame] | 138 | void Run() override { |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 139 | OnRun(); |
ahest | 68c9f10 | 2016-12-13 11:34:38 | [diff] [blame] | 140 | OneShotTimerTesterBase::Run(); |
tzik | f3336c9 | 2018-07-25 03:15:50 | [diff] [blame] | 141 | std::move(quit_closure_).Run(); |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 142 | } |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 143 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 144 | RunLoop run_loop_; |
tzik | f3336c9 | 2018-07-25 03:15:50 | [diff] [blame] | 145 | OnceClosure quit_closure_; |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 146 | }; |
| 147 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 148 | class OneShotSelfDeletingTimerTester : public OneShotTimerTester { |
| 149 | protected: |
| 150 | void OnRun() override { timer_.reset(); } |
| 151 | }; |
| 152 | |
| 153 | constexpr int kNumRepeats = 10; |
| 154 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 155 | class RepeatingTimerTester { |
| 156 | public: |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 157 | explicit RepeatingTimerTester(WaitableEvent* did_run, const TimeDelta& delay) |
| 158 | : counter_(kNumRepeats), |
| 159 | quit_closure_(run_loop_.QuitClosure()), |
| 160 | did_run_(did_run), |
| 161 | delay_(delay) {} |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 162 | |
Peter Boström | 75cd3c0 | 2021-09-28 15:23:18 | [diff] [blame] | 163 | RepeatingTimerTester(const RepeatingTimerTester&) = delete; |
| 164 | RepeatingTimerTester& operator=(const RepeatingTimerTester&) = delete; |
| 165 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 166 | void Start() { |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 167 | started_time_ = TimeTicks::Now(); |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 168 | timer_.Start(FROM_HERE, delay_, this, &RepeatingTimerTester::Run); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 169 | } |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 170 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 171 | void WaitAndConfirmTimerFiredRepeatedlyAfterDelay() { |
| 172 | run_loop_.Run(); |
| 173 | |
| 174 | EXPECT_NE(TimeTicks(), started_time_); |
| 175 | EXPECT_GE(TimeTicks::Now() - started_time_, kNumRepeats * delay_); |
| 176 | } |
| 177 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 178 | private: |
| 179 | void Run() { |
| 180 | if (--counter_ == 0) { |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 181 | if (did_run_) { |
| 182 | EXPECT_FALSE(did_run_->IsSignaled()); |
| 183 | did_run_->Signal(); |
| 184 | } |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 185 | timer_.Stop(); |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 186 | quit_closure_.Run(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 187 | } |
| 188 | } |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 189 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 190 | RepeatingTimer timer_; |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 191 | int counter_; |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 192 | |
| 193 | RunLoop run_loop_; |
kylechar | 83fb51e5 | 2019-03-14 15:30:43 | [diff] [blame] | 194 | RepeatingClosure quit_closure_; |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 195 | WaitableEvent* const did_run_; |
| 196 | |
| 197 | const TimeDelta delay_; |
| 198 | TimeTicks started_time_; |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 199 | }; |
| 200 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 201 | // Basic test with same setup as RunTest_OneShotTimers_Cancel below to confirm |
| 202 | // that |did_run_a| would be signaled in that test if it wasn't for the |
| 203 | // deletion. |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 204 | void RunTest_OneShotTimers( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 205 | test::TaskEnvironment::MainThreadType main_thread_type) { |
| 206 | test::TaskEnvironment task_environment(main_thread_type); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 207 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 208 | WaitableEvent did_run_a(WaitableEvent::ResetPolicy::MANUAL, |
| 209 | WaitableEvent::InitialState::NOT_SIGNALED); |
| 210 | OneShotTimerTester a(&did_run_a); |
| 211 | a.Start(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 212 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 213 | OneShotTimerTester b; |
| 214 | b.Start(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 215 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 216 | b.WaitAndConfirmTimerFiredAfterDelay(); |
| 217 | |
| 218 | EXPECT_TRUE(did_run_a.IsSignaled()); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 219 | } |
| 220 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 221 | void RunTest_OneShotTimers_Cancel( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 222 | test::TaskEnvironment::MainThreadType main_thread_type) { |
| 223 | test::TaskEnvironment task_environment(main_thread_type); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 224 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 225 | WaitableEvent did_run_a(WaitableEvent::ResetPolicy::MANUAL, |
| 226 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 227 | OneShotTimerTester* a = new OneShotTimerTester(&did_run_a); |
| 228 | |
| 229 | // This should run before the timer expires. |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 230 | SequencedTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 231 | |
| 232 | // Now start the timer. |
| 233 | a->Start(); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 234 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 235 | OneShotTimerTester b; |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 236 | b.Start(); |
| 237 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 238 | b.WaitAndConfirmTimerFiredAfterDelay(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 239 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 240 | EXPECT_FALSE(did_run_a.IsSignaled()); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 241 | } |
| 242 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 243 | void RunTest_OneShotSelfDeletingTimer( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 244 | test::TaskEnvironment::MainThreadType main_thread_type) { |
| 245 | test::TaskEnvironment task_environment(main_thread_type); |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 246 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 247 | OneShotSelfDeletingTimerTester f; |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 248 | f.Start(); |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 249 | f.WaitAndConfirmTimerFiredAfterDelay(); |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 250 | } |
| 251 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 252 | void RunTest_RepeatingTimer( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 253 | test::TaskEnvironment::MainThreadType main_thread_type, |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 254 | const TimeDelta& delay) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 255 | test::TaskEnvironment task_environment(main_thread_type); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 256 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 257 | RepeatingTimerTester f(nullptr, delay); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 258 | f.Start(); |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 259 | f.WaitAndConfirmTimerFiredRepeatedlyAfterDelay(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 260 | } |
| 261 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 262 | void RunTest_RepeatingTimer_Cancel( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 263 | test::TaskEnvironment::MainThreadType main_thread_type, |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 264 | const TimeDelta& delay) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 265 | test::TaskEnvironment task_environment(main_thread_type); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 266 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 267 | WaitableEvent did_run_a(WaitableEvent::ResetPolicy::MANUAL, |
| 268 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 269 | RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a, delay); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 270 | |
| 271 | // This should run before the timer expires. |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 272 | SequencedTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 273 | |
| 274 | // Now start the timer. |
| 275 | a->Start(); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 276 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 277 | RepeatingTimerTester b(nullptr, delay); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 278 | b.Start(); |
| 279 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 280 | b.WaitAndConfirmTimerFiredRepeatedlyAfterDelay(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 281 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 282 | // |a| should not have fired despite |b| starting after it on the same |
| 283 | // sequence and being complete by now. |
| 284 | EXPECT_FALSE(did_run_a.IsSignaled()); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 285 | } |
| 286 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 287 | class DelayTimerTarget { |
| 288 | public: |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 289 | bool signaled() const { return signaled_; } |
| 290 | |
| 291 | void Signal() { |
| 292 | ASSERT_FALSE(signaled_); |
| 293 | signaled_ = true; |
| 294 | } |
| 295 | |
| 296 | private: |
hashimoto | 42a14e86 | 2015-01-22 15:21:41 | [diff] [blame] | 297 | bool signaled_ = false; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 298 | }; |
| 299 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 300 | void RunTest_DelayTimer_NoCall( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 301 | test::TaskEnvironment::MainThreadType main_thread_type) { |
| 302 | test::TaskEnvironment task_environment(main_thread_type); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 303 | |
| 304 | // If Delay is never called, the timer shouldn't go off. |
| 305 | DelayTimerTarget target; |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 306 | DelayTimer timer(FROM_HERE, Milliseconds(1), &target, |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 307 | &DelayTimerTarget::Signal); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 308 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 309 | OneShotTimerTester tester; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 310 | tester.Start(); |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 311 | tester.WaitAndConfirmTimerFiredAfterDelay(); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 312 | |
| 313 | ASSERT_FALSE(target.signaled()); |
| 314 | } |
| 315 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 316 | void RunTest_DelayTimer_OneCall( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 317 | test::TaskEnvironment::MainThreadType main_thread_type) { |
| 318 | test::TaskEnvironment task_environment(main_thread_type); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 319 | |
| 320 | DelayTimerTarget target; |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 321 | DelayTimer timer(FROM_HERE, Milliseconds(1), &target, |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 322 | &DelayTimerTarget::Signal); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 323 | timer.Reset(); |
| 324 | |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 325 | OneShotTimerTester tester(nullptr, Milliseconds(100)); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 326 | tester.Start(); |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 327 | tester.WaitAndConfirmTimerFiredAfterDelay(); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 328 | |
| 329 | ASSERT_TRUE(target.signaled()); |
| 330 | } |
| 331 | |
| 332 | struct ResetHelper { |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 333 | ResetHelper(DelayTimer* timer, DelayTimerTarget* target) |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 334 | : timer_(timer), target_(target) {} |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 335 | |
| 336 | void Reset() { |
| 337 | ASSERT_FALSE(target_->signaled()); |
| 338 | timer_->Reset(); |
| 339 | } |
| 340 | |
| 341 | private: |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 342 | DelayTimer* const timer_; |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 343 | DelayTimerTarget* const target_; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 344 | }; |
| 345 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 346 | void RunTest_DelayTimer_Reset( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 347 | test::TaskEnvironment::MainThreadType main_thread_type) { |
| 348 | test::TaskEnvironment task_environment(main_thread_type); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 349 | |
| 350 | // If Delay is never called, the timer shouldn't go off. |
| 351 | DelayTimerTarget target; |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 352 | DelayTimer timer(FROM_HERE, Milliseconds(50), &target, |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 353 | &DelayTimerTarget::Signal); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 354 | timer.Reset(); |
| 355 | |
| 356 | ResetHelper reset_helper(&timer, &target); |
| 357 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 358 | OneShotTimer timers[20]; |
Avi Drissman | e3b70bf | 2019-01-04 19:50:22 | [diff] [blame] | 359 | for (size_t i = 0; i < base::size(timers); ++i) { |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 360 | timers[i].Start(FROM_HERE, Milliseconds(i * 10), &reset_helper, |
| 361 | &ResetHelper::Reset); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 362 | } |
| 363 | |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 364 | OneShotTimerTester tester(nullptr, Milliseconds(300)); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 365 | tester.Start(); |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 366 | tester.WaitAndConfirmTimerFiredAfterDelay(); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 367 | |
| 368 | ASSERT_TRUE(target.signaled()); |
| 369 | } |
| 370 | |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 371 | class DelayTimerFatalTarget { |
| 372 | public: |
| 373 | void Signal() { |
| 374 | ASSERT_TRUE(false); |
| 375 | } |
| 376 | }; |
| 377 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 378 | void RunTest_DelayTimer_Deleted( |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 379 | test::TaskEnvironment::MainThreadType main_thread_type) { |
| 380 | test::TaskEnvironment task_environment(main_thread_type); |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 381 | |
| 382 | DelayTimerFatalTarget target; |
| 383 | |
| 384 | { |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 385 | DelayTimer timer(FROM_HERE, Milliseconds(50), &target, |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 386 | &DelayTimerFatalTarget::Signal); |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 387 | timer.Reset(); |
| 388 | } |
| 389 | |
| 390 | // When the timer is deleted, the DelayTimerFatalTarget should never be |
| 391 | // called. |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 392 | PlatformThread::Sleep(Milliseconds(100)); |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 393 | } |
| 394 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 395 | } // namespace |
| 396 | |
| 397 | //----------------------------------------------------------------------------- |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 398 | // 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] | 399 | // that timers work properly in all configurations. |
| 400 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 401 | class TimerTestWithThreadType |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 402 | : public testing::TestWithParam<test::TaskEnvironment::MainThreadType> {}; |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 403 | |
| 404 | TEST_P(TimerTestWithThreadType, OneShotTimers) { |
| 405 | RunTest_OneShotTimers(GetParam()); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 406 | } |
| 407 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 408 | TEST_P(TimerTestWithThreadType, OneShotTimers_Cancel) { |
| 409 | RunTest_OneShotTimers_Cancel(GetParam()); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 410 | } |
| 411 | |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 412 | // If underline timer does not handle properly, we will crash or fail |
[email protected] | 64e95e1 | 2011-08-17 17:41:02 | [diff] [blame] | 413 | // in full page heap environment. |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 414 | TEST_P(TimerTestWithThreadType, OneShotSelfDeletingTimer) { |
| 415 | RunTest_OneShotSelfDeletingTimer(GetParam()); |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 416 | } |
| 417 | |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 418 | TEST(TimerTest, OneShotTimer_CustomTaskRunner) { |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 419 | auto task_runner = base::MakeRefCounted<TestSimpleTaskRunner>(); |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 420 | |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 421 | OneShotTimer timer; |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 422 | |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 423 | bool task_ran = false; |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 424 | |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 425 | // The timer will use the TestSimpleTaskRunner to schedule its delays. |
| 426 | timer.SetTaskRunner(task_runner); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 427 | timer.Start(FROM_HERE, Days(1), |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 428 | BindLambdaForTesting([&]() { task_ran = true; })); |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 429 | |
Gabriel Charette | e92ccc0 | 2019-01-19 14:13:05 | [diff] [blame] | 430 | EXPECT_FALSE(task_ran); |
| 431 | EXPECT_TRUE(task_runner->HasPendingTask()); |
| 432 | |
| 433 | task_runner->RunPendingTasks(); |
| 434 | |
| 435 | EXPECT_TRUE(task_ran); |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 436 | } |
| 437 | |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 438 | TEST(TimerTest, OneShotTimerWithTickClock) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 439 | test::TaskEnvironment task_environment( |
| 440 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 441 | Receiver receiver; |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 442 | OneShotTimer timer(task_environment.GetMockTickClock()); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 443 | timer.Start(FROM_HERE, Seconds(1), |
tzik | f3336c9 | 2018-07-25 03:15:50 | [diff] [blame] | 444 | BindOnce(&Receiver::OnCalled, Unretained(&receiver))); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 445 | task_environment.FastForwardBy(Seconds(1)); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 446 | EXPECT_TRUE(receiver.WasCalled()); |
| 447 | } |
| 448 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 449 | TEST_P(TimerTestWithThreadType, RepeatingTimer) { |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 450 | RunTest_RepeatingTimer(GetParam(), Milliseconds(10)); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 451 | } |
| 452 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 453 | TEST_P(TimerTestWithThreadType, RepeatingTimer_Cancel) { |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 454 | RunTest_RepeatingTimer_Cancel(GetParam(), Milliseconds(10)); |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 455 | } |
| 456 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 457 | TEST_P(TimerTestWithThreadType, RepeatingTimerZeroDelay) { |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 458 | RunTest_RepeatingTimer(GetParam(), Milliseconds(0)); |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 459 | } |
| 460 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 461 | TEST_P(TimerTestWithThreadType, RepeatingTimerZeroDelay_Cancel) { |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 462 | RunTest_RepeatingTimer_Cancel(GetParam(), Milliseconds(0)); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 463 | } |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 464 | |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 465 | TEST(TimerTest, RepeatingTimerWithTickClock) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 466 | test::TaskEnvironment task_environment( |
| 467 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 468 | Receiver receiver; |
| 469 | const int expected_times_called = 10; |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 470 | RepeatingTimer timer(task_environment.GetMockTickClock()); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 471 | timer.Start(FROM_HERE, Seconds(1), |
tzik | f3336c9 | 2018-07-25 03:15:50 | [diff] [blame] | 472 | BindRepeating(&Receiver::OnCalled, Unretained(&receiver))); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 473 | task_environment.FastForwardBy(Seconds(expected_times_called)); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 474 | timer.Stop(); |
| 475 | EXPECT_EQ(expected_times_called, receiver.TimesCalled()); |
| 476 | } |
| 477 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 478 | TEST_P(TimerTestWithThreadType, DelayTimer_NoCall) { |
| 479 | RunTest_DelayTimer_NoCall(GetParam()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 480 | } |
| 481 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 482 | TEST_P(TimerTestWithThreadType, DelayTimer_OneCall) { |
| 483 | RunTest_DelayTimer_OneCall(GetParam()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 484 | } |
| 485 | |
[email protected] | 471d7d6 | 2009-10-16 15:26:16 | [diff] [blame] | 486 | // It's flaky on the buildbot, http://crbug.com/25038. |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 487 | TEST_P(TimerTestWithThreadType, DISABLED_DelayTimer_Reset) { |
| 488 | RunTest_DelayTimer_Reset(GetParam()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 489 | } |
| 490 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 491 | TEST_P(TimerTestWithThreadType, DelayTimer_Deleted) { |
| 492 | RunTest_DelayTimer_Deleted(GetParam()); |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 493 | } |
| 494 | |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 495 | TEST(TimerTest, DelayTimerWithTickClock) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 496 | test::TaskEnvironment task_environment( |
| 497 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 498 | Receiver receiver; |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 499 | DelayTimer timer(FROM_HERE, Seconds(1), &receiver, &Receiver::OnCalled, |
| 500 | task_environment.GetMockTickClock()); |
| 501 | task_environment.FastForwardBy(Milliseconds(999)); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 502 | EXPECT_FALSE(receiver.WasCalled()); |
| 503 | timer.Reset(); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 504 | task_environment.FastForwardBy(Milliseconds(999)); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 505 | EXPECT_FALSE(receiver.WasCalled()); |
| 506 | timer.Reset(); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 507 | task_environment.FastForwardBy(Seconds(1)); |
jameswest | 6e0c8d2 | 2016-11-15 05:19:54 | [diff] [blame] | 508 | EXPECT_TRUE(receiver.WasCalled()); |
| 509 | } |
| 510 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 511 | TEST(TimerTest, TaskEnvironmentShutdown) { |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 512 | // This test is designed to verify that shutdown of the |
| 513 | // message loop does not cause crashes if there were pending |
| 514 | // timers not yet fired. It may only trigger exceptions |
[email protected] | 64e95e1 | 2011-08-17 17:41:02 | [diff] [blame] | 515 | // if debug heap checking is enabled. |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 516 | WaitableEvent did_run(WaitableEvent::ResetPolicy::MANUAL, |
| 517 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 518 | { |
ahest | 68c9f10 | 2016-12-13 11:34:38 | [diff] [blame] | 519 | OneShotTimerTesterBase a(&did_run); |
| 520 | OneShotTimerTesterBase b(&did_run); |
| 521 | OneShotTimerTesterBase c(&did_run); |
| 522 | OneShotTimerTesterBase d(&did_run); |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 523 | { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 524 | test::TaskEnvironment task_environment; |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 525 | a.Start(); |
| 526 | b.Start(); |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 527 | } // Task environment destructs by falling out of scope. |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 528 | } // OneShotTimers destruct. SHOULD NOT CRASH, of course. |
| 529 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 530 | EXPECT_FALSE(did_run.IsSignaled()); |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 531 | } |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 532 | |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 533 | // Ref counted class which owns a Timer. The class passes a reference to itself |
| 534 | // via the |user_task| parameter in Timer::Start(). |Timer::user_task_| might |
| 535 | // end up holding the last reference to the class. |
| 536 | class OneShotSelfOwningTimerTester |
| 537 | : public RefCounted<OneShotSelfOwningTimerTester> { |
| 538 | public: |
| 539 | OneShotSelfOwningTimerTester() = default; |
| 540 | |
Peter Boström | 75cd3c0 | 2021-09-28 15:23:18 | [diff] [blame] | 541 | OneShotSelfOwningTimerTester(const OneShotSelfOwningTimerTester&) = delete; |
| 542 | OneShotSelfOwningTimerTester& operator=(const OneShotSelfOwningTimerTester&) = |
| 543 | delete; |
| 544 | |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 545 | void StartTimer() { |
| 546 | // Start timer with long delay in order to test the timer getting destroyed |
| 547 | // while a timer task is still pending. |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 548 | timer_.Start(FROM_HERE, Days(1), |
tzik | f3336c9 | 2018-07-25 03:15:50 | [diff] [blame] | 549 | BindOnce(&OneShotSelfOwningTimerTester::Run, this)); |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | private: |
| 553 | friend class RefCounted<OneShotSelfOwningTimerTester>; |
| 554 | ~OneShotSelfOwningTimerTester() = default; |
| 555 | |
| 556 | void Run() { |
| 557 | ADD_FAILURE() << "Timer unexpectedly fired."; |
| 558 | } |
| 559 | |
| 560 | OneShotTimer timer_; |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 561 | }; |
| 562 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 563 | TEST(TimerTest, TaskEnvironmentShutdownSelfOwningTimer) { |
| 564 | // This test verifies that shutdown of the task environment does not cause |
| 565 | // crashes if there is a pending timer not yet fired and |Timer::user_task_| |
| 566 | // owns the timer. The test may only trigger exceptions if debug heap checking |
| 567 | // is enabled. |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 568 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 569 | test::TaskEnvironment task_environment; |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 570 | scoped_refptr<OneShotSelfOwningTimerTester> tester = |
| 571 | new OneShotSelfOwningTimerTester(); |
| 572 | |
| 573 | std::move(tester)->StartTimer(); |
| 574 | // |Timer::user_task_| owns sole reference to |tester|. |
| 575 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 576 | // Task environment destructs by falling out of scope. SHOULD NOT CRASH. |
pkotwicz | 4a286ed | 2017-01-17 20:45:26 | [diff] [blame] | 577 | } |
| 578 | |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 579 | void TimerTestCallback() { |
| 580 | } |
| 581 | |
| 582 | TEST(TimerTest, NonRepeatIsRunning) { |
| 583 | { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 584 | test::TaskEnvironment task_environment; |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 585 | OneShotTimer timer; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 586 | EXPECT_FALSE(timer.IsRunning()); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 587 | timer.Start(FROM_HERE, Days(1), BindOnce(&TimerTestCallback)); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 588 | EXPECT_TRUE(timer.IsRunning()); |
| 589 | timer.Stop(); |
| 590 | EXPECT_FALSE(timer.IsRunning()); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | { |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 594 | RetainingOneShotTimer timer; |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 595 | test::TaskEnvironment task_environment; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 596 | EXPECT_FALSE(timer.IsRunning()); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 597 | timer.Start(FROM_HERE, Days(1), BindRepeating(&TimerTestCallback)); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 598 | EXPECT_TRUE(timer.IsRunning()); |
| 599 | timer.Stop(); |
| 600 | EXPECT_FALSE(timer.IsRunning()); |
| 601 | ASSERT_FALSE(timer.user_task().is_null()); |
| 602 | timer.Reset(); |
| 603 | EXPECT_TRUE(timer.IsRunning()); |
| 604 | } |
| 605 | } |
| 606 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 607 | TEST(TimerTest, NonRepeatTaskEnvironmentDeath) { |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 608 | OneShotTimer timer; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 609 | { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 610 | test::TaskEnvironment task_environment; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 611 | EXPECT_FALSE(timer.IsRunning()); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 612 | timer.Start(FROM_HERE, Days(1), BindOnce(&TimerTestCallback)); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 613 | EXPECT_TRUE(timer.IsRunning()); |
| 614 | } |
| 615 | EXPECT_FALSE(timer.IsRunning()); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | TEST(TimerTest, RetainRepeatIsRunning) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 619 | test::TaskEnvironment task_environment; |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 620 | RepeatingTimer timer(FROM_HERE, Days(1), BindRepeating(&TimerTestCallback)); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 621 | EXPECT_FALSE(timer.IsRunning()); |
| 622 | timer.Reset(); |
| 623 | EXPECT_TRUE(timer.IsRunning()); |
| 624 | timer.Stop(); |
| 625 | EXPECT_FALSE(timer.IsRunning()); |
| 626 | timer.Reset(); |
| 627 | EXPECT_TRUE(timer.IsRunning()); |
| 628 | } |
| 629 | |
| 630 | TEST(TimerTest, RetainNonRepeatIsRunning) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 631 | test::TaskEnvironment task_environment; |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 632 | RetainingOneShotTimer timer(FROM_HERE, Days(1), |
tzik | f3336c9 | 2018-07-25 03:15:50 | [diff] [blame] | 633 | BindRepeating(&TimerTestCallback)); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 634 | EXPECT_FALSE(timer.IsRunning()); |
| 635 | timer.Reset(); |
| 636 | EXPECT_TRUE(timer.IsRunning()); |
| 637 | timer.Stop(); |
| 638 | EXPECT_FALSE(timer.IsRunning()); |
| 639 | timer.Reset(); |
| 640 | EXPECT_TRUE(timer.IsRunning()); |
| 641 | } |
| 642 | |
gab | 4e844bb | 2017-06-01 16:23:36 | [diff] [blame] | 643 | //----------------------------------------------------------------------------- |
| 644 | |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 645 | namespace { |
| 646 | |
| 647 | bool g_callback_happened1 = false; |
| 648 | bool g_callback_happened2 = false; |
| 649 | |
| 650 | void ClearAllCallbackHappened() { |
| 651 | g_callback_happened1 = false; |
| 652 | g_callback_happened2 = false; |
| 653 | } |
| 654 | |
| 655 | void SetCallbackHappened1() { |
| 656 | g_callback_happened1 = true; |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 657 | RunLoop::QuitCurrentWhenIdleDeprecated(); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | void SetCallbackHappened2() { |
| 661 | g_callback_happened2 = true; |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 662 | RunLoop::QuitCurrentWhenIdleDeprecated(); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 663 | } |
| 664 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 665 | } // namespace |
| 666 | |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 667 | TEST(TimerTest, ContinuationStopStart) { |
| 668 | { |
| 669 | ClearAllCallbackHappened(); |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 670 | test::TaskEnvironment task_environment; |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 671 | OneShotTimer timer; |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 672 | timer.Start(FROM_HERE, Milliseconds(10), BindOnce(&SetCallbackHappened1)); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 673 | timer.Stop(); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 674 | timer.Start(FROM_HERE, Milliseconds(40), BindOnce(&SetCallbackHappened2)); |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 675 | RunLoop().Run(); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 676 | EXPECT_FALSE(g_callback_happened1); |
| 677 | EXPECT_TRUE(g_callback_happened2); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | TEST(TimerTest, ContinuationReset) { |
| 682 | { |
| 683 | ClearAllCallbackHappened(); |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 684 | test::TaskEnvironment task_environment; |
tzik | d93bb086 | 2018-07-19 11:54:14 | [diff] [blame] | 685 | OneShotTimer timer; |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 686 | timer.Start(FROM_HERE, Milliseconds(10), BindOnce(&SetCallbackHappened1)); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 687 | timer.Reset(); |
tzik | f3336c9 | 2018-07-25 03:15:50 | [diff] [blame] | 688 | // // Since Reset happened before task ran, the user_task must not be |
| 689 | // cleared: ASSERT_FALSE(timer.user_task().is_null()); |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 690 | RunLoop().Run(); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 691 | EXPECT_TRUE(g_callback_happened1); |
| 692 | } |
| 693 | } |
| 694 | |
Patrick Monette | 57d1c18d | 2021-04-22 00:50:44 | [diff] [blame] | 695 | TEST(TimerTest, AbandonedTaskIsCancelled) { |
| 696 | test::TaskEnvironment task_environment( |
| 697 | test::TaskEnvironment::TimeSource::MOCK_TIME); |
| 698 | OneShotTimer timer; |
| 699 | |
| 700 | // Start a timer. There will be a pending task on the current sequence. |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame^] | 701 | timer.Start(FROM_HERE, Seconds(5), base::DoNothing()); |
Patrick Monette | 57d1c18d | 2021-04-22 00:50:44 | [diff] [blame] | 702 | EXPECT_EQ(1u, task_environment.GetPendingMainThreadTaskCount()); |
| 703 | |
| 704 | // After AbandonAndStop(), the task is correctly treated as cancelled. |
| 705 | timer.AbandonAndStop(); |
| 706 | EXPECT_EQ(0u, task_environment.GetPendingMainThreadTaskCount()); |
| 707 | } |
| 708 | |
Ilia Samsonov | a2885f7 | 2019-11-20 22:16:58 | [diff] [blame] | 709 | INSTANTIATE_TEST_SUITE_P(All, |
Victor Costan | 033b9ac | 2019-01-29 00:52:16 | [diff] [blame] | 710 | TimerTestWithThreadType, |
| 711 | testing::ValuesIn(testing_main_threads)); |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 712 | |
gab | 175490c1 | 2016-11-28 23:01:33 | [diff] [blame] | 713 | } // namespace base |