chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 5 | #include <sys/timerfd.h> |
| 6 | |
dcheng | 7822a7f | 2016-04-26 09:35:02 +0900 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/bind_helpers.h" |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 11 | #include "base/files/file_descriptor_watcher_posix.h" |
fdoray | c674174 | 2016-06-16 01:01:03 +0900 | [diff] [blame] | 12 | #include "base/location.h" |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 13 | #include "base/macros.h" |
| 14 | #include "base/message_loop/message_loop.h" |
| 15 | #include "base/run_loop.h" |
fdoray | c674174 | 2016-06-16 01:01:03 +0900 | [diff] [blame] | 16 | #include "base/single_thread_task_runner.h" |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 17 | #include "base/threading/platform_thread.h" |
fdoray | c674174 | 2016-06-16 01:01:03 +0900 | [diff] [blame] | 18 | #include "base/threading/thread_task_runner_handle.h" |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 19 | #include "base/time/time.h" |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 20 | #include "components/timers/alarm_timer_chromeos.h" |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
| 23 | // Most of these tests have been lifted right out of timer_unittest.cc with only |
| 24 | // cosmetic changes (like replacing calls to MessageLoop::current()->Run() with |
| 25 | // a RunLoop). We want the AlarmTimer to be a drop-in replacement for the |
| 26 | // regular Timer so it should pass the same tests as the Timer class. |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 27 | namespace timers { |
| 28 | namespace { |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 29 | const base::TimeDelta kTenMilliseconds = base::TimeDelta::FromMilliseconds(10); |
| 30 | |
| 31 | class OneShotAlarmTimerTester { |
| 32 | public: |
| 33 | OneShotAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
| 34 | : did_run_(did_run), |
| 35 | delay_(delay), |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 36 | timer_(new timers::OneShotAlarmTimer()) {} |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 37 | void Start() { |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 38 | timer_->Start(FROM_HERE, delay_, base::Bind(&OneShotAlarmTimerTester::Run, |
| 39 | base::Unretained(this))); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | private: |
| 43 | void Run() { |
| 44 | *did_run_ = true; |
| 45 | |
fdoray | c674174 | 2016-06-16 01:01:03 +0900 | [diff] [blame] | 46 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 47 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 48 | } |
| 49 | |
| 50 | bool* did_run_; |
| 51 | const base::TimeDelta delay_; |
dcheng | 7822a7f | 2016-04-26 09:35:02 +0900 | [diff] [blame] | 52 | std::unique_ptr<timers::OneShotAlarmTimer> timer_; |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 53 | |
| 54 | DISALLOW_COPY_AND_ASSIGN(OneShotAlarmTimerTester); |
| 55 | }; |
| 56 | |
| 57 | class OneShotSelfDeletingAlarmTimerTester { |
| 58 | public: |
| 59 | OneShotSelfDeletingAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
| 60 | : did_run_(did_run), |
| 61 | delay_(delay), |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 62 | timer_(new timers::OneShotAlarmTimer()) {} |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 63 | void Start() { |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 64 | timer_->Start(FROM_HERE, delay_, |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 65 | base::Bind(&OneShotSelfDeletingAlarmTimerTester::Run, |
| 66 | base::Unretained(this))); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | void Run() { |
| 71 | *did_run_ = true; |
| 72 | timer_.reset(); |
| 73 | |
fdoray | c674174 | 2016-06-16 01:01:03 +0900 | [diff] [blame] | 74 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 75 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 76 | } |
| 77 | |
| 78 | bool* did_run_; |
| 79 | const base::TimeDelta delay_; |
dcheng | 7822a7f | 2016-04-26 09:35:02 +0900 | [diff] [blame] | 80 | std::unique_ptr<timers::OneShotAlarmTimer> timer_; |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 81 | |
| 82 | DISALLOW_COPY_AND_ASSIGN(OneShotSelfDeletingAlarmTimerTester); |
| 83 | }; |
| 84 | |
| 85 | class RepeatingAlarmTimerTester { |
| 86 | public: |
| 87 | RepeatingAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
| 88 | : did_run_(did_run), |
| 89 | delay_(delay), |
| 90 | counter_(10), |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 91 | timer_(new timers::RepeatingAlarmTimer()) {} |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 92 | void Start() { |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 93 | timer_->Start(FROM_HERE, delay_, base::Bind(&RepeatingAlarmTimerTester::Run, |
| 94 | base::Unretained(this))); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | private: |
| 98 | void Run() { |
| 99 | if (--counter_ == 0) { |
| 100 | *did_run_ = true; |
| 101 | timer_->Stop(); |
| 102 | |
fdoray | c674174 | 2016-06-16 01:01:03 +0900 | [diff] [blame] | 103 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 104 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | bool* did_run_; |
| 109 | const base::TimeDelta delay_; |
| 110 | int counter_; |
dcheng | 7822a7f | 2016-04-26 09:35:02 +0900 | [diff] [blame] | 111 | std::unique_ptr<timers::RepeatingAlarmTimer> timer_; |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 112 | |
| 113 | DISALLOW_COPY_AND_ASSIGN(RepeatingAlarmTimerTester); |
| 114 | }; |
| 115 | |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 116 | } // namespace |
| 117 | |
| 118 | //----------------------------------------------------------------------------- |
| 119 | // Each test is run against each type of MessageLoop. That way we are sure |
| 120 | // that timers work properly in all configurations. |
| 121 | |
| 122 | TEST(AlarmTimerTest, OneShotAlarmTimer) { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 123 | base::MessageLoopForIO loop; |
| 124 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 125 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 126 | bool did_run = false; |
| 127 | OneShotAlarmTimerTester f(&did_run, kTenMilliseconds); |
| 128 | f.Start(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 129 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 130 | base::RunLoop().Run(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 131 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 132 | EXPECT_TRUE(did_run); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | TEST(AlarmTimerTest, OneShotAlarmTimer_Cancel) { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 136 | base::MessageLoopForIO loop; |
| 137 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 138 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 139 | bool did_run_a = false; |
| 140 | OneShotAlarmTimerTester* a = |
| 141 | new OneShotAlarmTimerTester(&did_run_a, kTenMilliseconds); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 142 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 143 | // This should run before the timer expires. |
| 144 | base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 145 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 146 | // Now start the timer. |
| 147 | a->Start(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 148 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 149 | bool did_run_b = false; |
| 150 | OneShotAlarmTimerTester b(&did_run_b, kTenMilliseconds); |
| 151 | b.Start(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 152 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 153 | base::RunLoop().Run(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 154 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 155 | EXPECT_FALSE(did_run_a); |
| 156 | EXPECT_TRUE(did_run_b); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // If underlying timer does not handle this properly, we will crash or fail |
| 160 | // in full page heap environment. |
| 161 | TEST(AlarmTimerTest, OneShotSelfDeletingAlarmTimer) { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 162 | base::MessageLoopForIO loop; |
| 163 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 164 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 165 | bool did_run = false; |
| 166 | OneShotSelfDeletingAlarmTimerTester f(&did_run, kTenMilliseconds); |
| 167 | f.Start(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 168 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 169 | base::RunLoop().Run(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 170 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 171 | EXPECT_TRUE(did_run); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | TEST(AlarmTimerTest, RepeatingAlarmTimer) { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 175 | base::MessageLoopForIO loop; |
| 176 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 177 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 178 | bool did_run = false; |
| 179 | RepeatingAlarmTimerTester f(&did_run, kTenMilliseconds); |
| 180 | f.Start(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 181 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 182 | base::RunLoop().Run(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 183 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 184 | EXPECT_TRUE(did_run); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | TEST(AlarmTimerTest, RepeatingAlarmTimer_Cancel) { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 188 | base::MessageLoopForIO loop; |
| 189 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 190 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 191 | bool did_run_a = false; |
| 192 | RepeatingAlarmTimerTester* a = |
| 193 | new RepeatingAlarmTimerTester(&did_run_a, kTenMilliseconds); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 194 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 195 | // This should run before the timer expires. |
| 196 | base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 197 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 198 | // Now start the timer. |
| 199 | a->Start(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 200 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 201 | bool did_run_b = false; |
| 202 | RepeatingAlarmTimerTester b(&did_run_b, kTenMilliseconds); |
| 203 | b.Start(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 204 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 205 | base::RunLoop().Run(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 206 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 207 | EXPECT_FALSE(did_run_a); |
| 208 | EXPECT_TRUE(did_run_b); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay) { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 212 | base::MessageLoopForIO loop; |
| 213 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 214 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 215 | bool did_run = false; |
| 216 | RepeatingAlarmTimerTester f(&did_run, base::TimeDelta()); |
| 217 | f.Start(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 218 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 219 | base::RunLoop().Run(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 220 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 221 | EXPECT_TRUE(did_run); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay_Cancel) { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 225 | base::MessageLoopForIO loop; |
| 226 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 227 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 228 | bool did_run_a = false; |
| 229 | RepeatingAlarmTimerTester* a = |
| 230 | new RepeatingAlarmTimerTester(&did_run_a, base::TimeDelta()); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 231 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 232 | // This should run before the timer expires. |
| 233 | base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 234 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 235 | // Now start the timer. |
| 236 | a->Start(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 237 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 238 | bool did_run_b = false; |
| 239 | RepeatingAlarmTimerTester b(&did_run_b, base::TimeDelta()); |
| 240 | b.Start(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 241 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 242 | base::RunLoop().Run(); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 243 | |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 244 | EXPECT_FALSE(did_run_a); |
| 245 | EXPECT_TRUE(did_run_b); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | TEST(AlarmTimerTest, MessageLoopShutdown) { |
| 249 | // This test is designed to verify that shutdown of the |
| 250 | // message loop does not cause crashes if there were pending |
| 251 | // timers not yet fired. It may only trigger exceptions |
| 252 | // if debug heap checking is enabled. |
| 253 | bool did_run = false; |
| 254 | { |
Gyuyoung Kim | f2bab8d | 2018-01-23 22:34:37 +0900 | [diff] [blame] | 255 | auto loop = std::make_unique<base::MessageLoopForIO>(); |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 256 | auto file_descriptor_watcher = |
Gyuyoung Kim | f2bab8d | 2018-01-23 22:34:37 +0900 | [diff] [blame] | 257 | std::make_unique<base::FileDescriptorWatcher>(loop.get()); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 258 | OneShotAlarmTimerTester a(&did_run, kTenMilliseconds); |
| 259 | OneShotAlarmTimerTester b(&did_run, kTenMilliseconds); |
| 260 | OneShotAlarmTimerTester c(&did_run, kTenMilliseconds); |
| 261 | OneShotAlarmTimerTester d(&did_run, kTenMilliseconds); |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 262 | |
| 263 | a.Start(); |
| 264 | b.Start(); |
| 265 | |
| 266 | // Allow FileDescriptorWatcher to start watching the timers. Without this, |
| 267 | // tasks posted by FileDescriptorWatcher::WatchReadable() are leaked. |
| 268 | base::RunLoop().RunUntilIdle(); |
| 269 | |
| 270 | // MessageLoop and FileDescriptorWatcher destruct. |
| 271 | file_descriptor_watcher.reset(); |
| 272 | loop.reset(); |
| 273 | } // OneShotTimers destruct. SHOULD NOT CRASH, of course. |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 274 | |
| 275 | EXPECT_FALSE(did_run); |
| 276 | } |
| 277 | |
| 278 | TEST(AlarmTimerTest, NonRepeatIsRunning) { |
| 279 | { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 280 | base::MessageLoopForIO loop; |
| 281 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 282 | timers::OneShotAlarmTimer timer; |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 283 | EXPECT_FALSE(timer.IsRunning()); |
Peter Kasting | 24efe5e | 2018-02-24 09:03:01 +0900 | [diff] [blame^] | 284 | timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), base::DoNothing()); |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 285 | |
| 286 | // Allow FileDescriptorWatcher to start watching the timer. Without this, a |
| 287 | // task posted by FileDescriptorWatcher::WatchReadable() is leaked. |
| 288 | base::RunLoop().RunUntilIdle(); |
| 289 | |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 290 | EXPECT_TRUE(timer.IsRunning()); |
| 291 | timer.Stop(); |
| 292 | EXPECT_FALSE(timer.IsRunning()); |
| 293 | EXPECT_TRUE(timer.user_task().is_null()); |
| 294 | } |
| 295 | |
| 296 | { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 297 | base::MessageLoopForIO loop; |
| 298 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
chirantan | c88389f | 2015-04-08 05:43:11 +0900 | [diff] [blame] | 299 | timers::SimpleAlarmTimer timer; |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 300 | EXPECT_FALSE(timer.IsRunning()); |
Peter Kasting | 24efe5e | 2018-02-24 09:03:01 +0900 | [diff] [blame^] | 301 | timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), base::DoNothing()); |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 302 | |
| 303 | // Allow FileDescriptorWatcher to start watching the timer. Without this, a |
| 304 | // task posted by FileDescriptorWatcher::WatchReadable() is leaked. |
| 305 | base::RunLoop().RunUntilIdle(); |
| 306 | |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 307 | EXPECT_TRUE(timer.IsRunning()); |
| 308 | timer.Stop(); |
| 309 | EXPECT_FALSE(timer.IsRunning()); |
| 310 | ASSERT_FALSE(timer.user_task().is_null()); |
| 311 | timer.Reset(); |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 312 | base::RunLoop().RunUntilIdle(); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 313 | EXPECT_TRUE(timer.IsRunning()); |
| 314 | } |
| 315 | } |
| 316 | |
nasko | d3fd0db | 2016-10-07 06:22:32 +0900 | [diff] [blame] | 317 | TEST(AlarmTimerTest, RetainRepeatIsRunning) { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 318 | base::MessageLoopForIO loop; |
| 319 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
| 320 | timers::RepeatingAlarmTimer timer; |
nasko | d3fd0db | 2016-10-07 06:22:32 +0900 | [diff] [blame] | 321 | EXPECT_FALSE(timer.IsRunning()); |
Peter Kasting | 24efe5e | 2018-02-24 09:03:01 +0900 | [diff] [blame^] | 322 | timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), base::DoNothing()); |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 323 | |
| 324 | // Allow FileDescriptorWatcher to start watching the timer. Without this, a |
| 325 | // task posted by FileDescriptorWatcher::WatchReadable() is leaked. |
| 326 | base::RunLoop().RunUntilIdle(); |
| 327 | |
| 328 | EXPECT_TRUE(timer.IsRunning()); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 329 | timer.Reset(); |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 330 | base::RunLoop().RunUntilIdle(); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 331 | EXPECT_TRUE(timer.IsRunning()); |
| 332 | timer.Stop(); |
| 333 | EXPECT_FALSE(timer.IsRunning()); |
| 334 | timer.Reset(); |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 335 | base::RunLoop().RunUntilIdle(); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 336 | EXPECT_TRUE(timer.IsRunning()); |
| 337 | } |
| 338 | |
| 339 | TEST(AlarmTimerTest, RetainNonRepeatIsRunning) { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 340 | base::MessageLoopForIO loop; |
| 341 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
| 342 | timers::SimpleAlarmTimer timer; |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 343 | EXPECT_FALSE(timer.IsRunning()); |
Peter Kasting | 24efe5e | 2018-02-24 09:03:01 +0900 | [diff] [blame^] | 344 | timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), base::DoNothing()); |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 345 | |
| 346 | // Allow FileDescriptorWatcher to start watching the timer. Without this, a |
| 347 | // task posted by FileDescriptorWatcher::WatchReadable() is leaked. |
| 348 | base::RunLoop().RunUntilIdle(); |
| 349 | |
| 350 | EXPECT_TRUE(timer.IsRunning()); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 351 | timer.Reset(); |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 352 | base::RunLoop().RunUntilIdle(); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 353 | EXPECT_TRUE(timer.IsRunning()); |
| 354 | timer.Stop(); |
| 355 | EXPECT_FALSE(timer.IsRunning()); |
| 356 | timer.Reset(); |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 357 | base::RunLoop().RunUntilIdle(); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 358 | EXPECT_TRUE(timer.IsRunning()); |
| 359 | } |
| 360 | |
| 361 | namespace { |
| 362 | |
| 363 | bool g_callback_happened1 = false; |
| 364 | bool g_callback_happened2 = false; |
| 365 | |
| 366 | void ClearAllCallbackHappened() { |
| 367 | g_callback_happened1 = false; |
| 368 | g_callback_happened2 = false; |
| 369 | } |
| 370 | |
| 371 | void SetCallbackHappened1() { |
| 372 | g_callback_happened1 = true; |
fdoray | c674174 | 2016-06-16 01:01:03 +0900 | [diff] [blame] | 373 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 374 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 375 | } |
| 376 | |
| 377 | void SetCallbackHappened2() { |
| 378 | g_callback_happened2 = true; |
fdoray | c674174 | 2016-06-16 01:01:03 +0900 | [diff] [blame] | 379 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 380 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 381 | } |
| 382 | |
| 383 | TEST(AlarmTimerTest, ContinuationStopStart) { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 384 | ClearAllCallbackHappened(); |
| 385 | base::MessageLoopForIO loop; |
| 386 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
| 387 | timers::OneShotAlarmTimer timer; |
| 388 | timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), |
| 389 | base::Bind(&SetCallbackHappened1)); |
| 390 | timer.Stop(); |
| 391 | timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(40), |
| 392 | base::Bind(&SetCallbackHappened2)); |
| 393 | base::RunLoop().Run(); |
| 394 | EXPECT_FALSE(g_callback_happened1); |
| 395 | EXPECT_TRUE(g_callback_happened2); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | TEST(AlarmTimerTest, ContinuationReset) { |
fdoray | 5db5217 | 2016-10-12 02:31:13 +0900 | [diff] [blame] | 399 | ClearAllCallbackHappened(); |
| 400 | base::MessageLoopForIO loop; |
| 401 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
| 402 | timers::OneShotAlarmTimer timer; |
| 403 | timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), |
| 404 | base::Bind(&SetCallbackHappened1)); |
| 405 | timer.Reset(); |
| 406 | // Since Reset happened before task ran, the user_task must not be cleared: |
| 407 | ASSERT_FALSE(timer.user_task().is_null()); |
| 408 | base::RunLoop().Run(); |
| 409 | EXPECT_TRUE(g_callback_happened1); |
| 410 | } |
| 411 | |
| 412 | // Verify that no crash occurs if a timer is deleted while its callback is |
| 413 | // running. |
| 414 | TEST(AlarmTimerTest, DeleteTimerWhileCallbackIsRunning) { |
| 415 | base::MessageLoopForIO loop; |
| 416 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
| 417 | base::RunLoop run_loop; |
| 418 | |
| 419 | // Will be deleted by the callback. |
| 420 | timers::OneShotAlarmTimer* timer = new timers::OneShotAlarmTimer; |
| 421 | |
| 422 | timer->Start( |
| 423 | FROM_HERE, base::TimeDelta::FromMilliseconds(10), |
| 424 | base::Bind( |
| 425 | [](timers::OneShotAlarmTimer* timer, base::RunLoop* run_loop) { |
| 426 | delete timer; |
| 427 | run_loop->Quit(); |
| 428 | }, |
| 429 | timer, &run_loop)); |
| 430 | run_loop.Run(); |
| 431 | } |
| 432 | |
| 433 | // Verify that no crash occurs if a zero-delay timer is deleted while its |
| 434 | // callback is running. |
| 435 | TEST(AlarmTimerTest, DeleteTimerWhileCallbackIsRunningZeroDelay) { |
| 436 | base::MessageLoopForIO loop; |
| 437 | base::FileDescriptorWatcher file_descriptor_watcher(&loop); |
| 438 | base::RunLoop run_loop; |
| 439 | |
| 440 | // Will be deleted by the callback. |
| 441 | timers::OneShotAlarmTimer* timer = new timers::OneShotAlarmTimer; |
| 442 | |
| 443 | timer->Start( |
| 444 | FROM_HERE, base::TimeDelta(), |
| 445 | base::Bind( |
| 446 | [](timers::OneShotAlarmTimer* timer, base::RunLoop* run_loop) { |
| 447 | delete timer; |
| 448 | run_loop->Quit(); |
| 449 | }, |
| 450 | timer, &run_loop)); |
| 451 | run_loop.Run(); |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | } // namespace |
chirantan | a435157 | 2014-10-25 04:44:42 +0900 | [diff] [blame] | 455 | } // namespace timers |