| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "components/timers/alarm_timer.h" | 12 #include "components/timers/alarm_timer.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 // Most of these tests have been lifted right out of timer_unittest.cc with only | 15 // Most of these tests have been lifted right out of timer_unittest.cc with only |
| 16 // cosmetic changes (like replacing calls to MessageLoop::current()->Run() with | 16 // cosmetic changes (like replacing calls to MessageLoop::current()->Run() with |
| 17 // a RunLoop). We want the AlarmTimer to be a drop-in replacement for the | 17 // a RunLoop). We want the AlarmTimer to be a drop-in replacement for the |
| 18 // regular Timer so it should pass the same tests as the Timer class. | 18 // regular Timer so it should pass the same tests as the Timer class. |
| 19 // | 19 // |
| 20 // The only new tests are the .*ConcurrentResetAndTimerFired tests, which test | 20 // The only new tests are the .*ConcurrentResetAndTimerFired tests, which test |
| 21 // that race conditions that can come up in the AlarmTimer::Delegate are | 21 // that race conditions that can come up in the AlarmTimer::Delegate are |
| 22 // properly handled. | 22 // properly handled. |
| 23 namespace timers { | 23 namespace timers { |
| 24 namespace { | 24 namespace { |
| 25 // The message loops on which each timer should be tested. | 25 // The message loops on which each timer should be tested. |
| 26 const base::MessageLoop::Type testing_message_loops[] = { | 26 const base::MessageLoop::Type testing_message_loops[] = { |
| 27 base::MessageLoop::TYPE_DEFAULT, | 27 base::MessageLoop::TYPE_DEFAULT, |
| 28 base::MessageLoop::TYPE_IO, | 28 base::MessageLoop::TYPE_IO, |
| 29 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. | 29 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. |
| 30 base::MessageLoop::TYPE_UI, | 30 base::MessageLoop::TYPE_UI, |
| 31 #endif | 31 #endif |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 const int kNumTestingMessageLoops = arraysize(testing_message_loops); | 34 const int kNumTestingMessageLoops = arraysize(testing_message_loops); |
| 35 const base::TimeDelta kTenMilliseconds = base::TimeDelta::FromMilliseconds(10); | 35 const base::TimeDelta kTenMilliseconds = base::TimeDelta::FromMilliseconds(10); |
| 36 | 36 |
| 37 class OneShotAlarmTimerTester { | 37 class OneShotAlarmTimerTester { |
| 38 public: | 38 public: |
| 39 OneShotAlarmTimerTester(bool* did_run, base::TimeDelta delay) | 39 OneShotAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
| 40 : did_run_(did_run), | 40 : did_run_(did_run), |
| 41 delay_(delay), | 41 delay_(delay), |
| 42 timer_(new timers::AlarmTimer(false, false)) { | 42 timer_(new timers::OneShotAlarmTimer()) {} |
| 43 } | |
| 44 void Start() { | 43 void Start() { |
| 45 timer_->Start(FROM_HERE, | 44 timer_->Start(FROM_HERE, delay_, base::Bind(&OneShotAlarmTimerTester::Run, |
| 46 delay_, | 45 base::Unretained(this))); |
| 47 base::Bind(&OneShotAlarmTimerTester::Run, | |
| 48 base::Unretained(this))); | |
| 49 } | 46 } |
| 50 | 47 |
| 51 private: | 48 private: |
| 52 void Run() { | 49 void Run() { |
| 53 *did_run_ = true; | 50 *did_run_ = true; |
| 54 | 51 |
| 55 base::MessageLoop::current()->PostTask( | 52 base::MessageLoop::current()->PostTask( |
| 56 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 53 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 57 } | 54 } |
| 58 | 55 |
| 59 bool* did_run_; | 56 bool* did_run_; |
| 60 const base::TimeDelta delay_; | 57 const base::TimeDelta delay_; |
| 61 scoped_ptr<timers::AlarmTimer> timer_; | 58 scoped_ptr<timers::OneShotAlarmTimer> timer_; |
| 62 | 59 |
| 63 DISALLOW_COPY_AND_ASSIGN(OneShotAlarmTimerTester); | 60 DISALLOW_COPY_AND_ASSIGN(OneShotAlarmTimerTester); |
| 64 }; | 61 }; |
| 65 | 62 |
| 66 class OneShotSelfDeletingAlarmTimerTester { | 63 class OneShotSelfDeletingAlarmTimerTester { |
| 67 public: | 64 public: |
| 68 OneShotSelfDeletingAlarmTimerTester(bool* did_run, base::TimeDelta delay) | 65 OneShotSelfDeletingAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
| 69 : did_run_(did_run), | 66 : did_run_(did_run), |
| 70 delay_(delay), | 67 delay_(delay), |
| 71 timer_(new timers::AlarmTimer(false, false)) { | 68 timer_(new timers::OneShotAlarmTimer()) {} |
| 72 } | |
| 73 void Start() { | 69 void Start() { |
| 74 timer_->Start(FROM_HERE, | 70 timer_->Start(FROM_HERE, delay_, |
| 75 delay_, | |
| 76 base::Bind(&OneShotSelfDeletingAlarmTimerTester::Run, | 71 base::Bind(&OneShotSelfDeletingAlarmTimerTester::Run, |
| 77 base::Unretained(this))); | 72 base::Unretained(this))); |
| 78 } | 73 } |
| 79 | 74 |
| 80 private: | 75 private: |
| 81 void Run() { | 76 void Run() { |
| 82 *did_run_ = true; | 77 *did_run_ = true; |
| 83 timer_.reset(); | 78 timer_.reset(); |
| 84 | 79 |
| 85 base::MessageLoop::current()->PostTask( | 80 base::MessageLoop::current()->PostTask( |
| 86 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 81 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 87 } | 82 } |
| 88 | 83 |
| 89 bool* did_run_; | 84 bool* did_run_; |
| 90 const base::TimeDelta delay_; | 85 const base::TimeDelta delay_; |
| 91 scoped_ptr<timers::AlarmTimer> timer_; | 86 scoped_ptr<timers::OneShotAlarmTimer> timer_; |
| 92 | 87 |
| 93 DISALLOW_COPY_AND_ASSIGN(OneShotSelfDeletingAlarmTimerTester); | 88 DISALLOW_COPY_AND_ASSIGN(OneShotSelfDeletingAlarmTimerTester); |
| 94 }; | 89 }; |
| 95 | 90 |
| 96 class RepeatingAlarmTimerTester { | 91 class RepeatingAlarmTimerTester { |
| 97 public: | 92 public: |
| 98 RepeatingAlarmTimerTester(bool* did_run, base::TimeDelta delay) | 93 RepeatingAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
| 99 : did_run_(did_run), | 94 : did_run_(did_run), |
| 100 delay_(delay), | 95 delay_(delay), |
| 101 counter_(10), | 96 counter_(10), |
| 102 timer_(new timers::AlarmTimer(true, true)) { | 97 timer_(new timers::RepeatingAlarmTimer()) {} |
| 103 } | |
| 104 void Start() { | 98 void Start() { |
| 105 timer_->Start(FROM_HERE, | 99 timer_->Start(FROM_HERE, delay_, base::Bind(&RepeatingAlarmTimerTester::Run, |
| 106 delay_, | 100 base::Unretained(this))); |
| 107 base::Bind(&RepeatingAlarmTimerTester::Run, | |
| 108 base::Unretained(this))); | |
| 109 } | 101 } |
| 110 | 102 |
| 111 private: | 103 private: |
| 112 void Run() { | 104 void Run() { |
| 113 if (--counter_ == 0) { | 105 if (--counter_ == 0) { |
| 114 *did_run_ = true; | 106 *did_run_ = true; |
| 115 timer_->Stop(); | 107 timer_->Stop(); |
| 116 | 108 |
| 117 base::MessageLoop::current()->PostTask( | 109 base::MessageLoop::current()->PostTask( |
| 118 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 110 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 119 } | 111 } |
| 120 } | 112 } |
| 121 | 113 |
| 122 bool* did_run_; | 114 bool* did_run_; |
| 123 const base::TimeDelta delay_; | 115 const base::TimeDelta delay_; |
| 124 int counter_; | 116 int counter_; |
| 125 scoped_ptr<timers::AlarmTimer> timer_; | 117 scoped_ptr<timers::RepeatingAlarmTimer> timer_; |
| 126 | 118 |
| 127 DISALLOW_COPY_AND_ASSIGN(RepeatingAlarmTimerTester); | 119 DISALLOW_COPY_AND_ASSIGN(RepeatingAlarmTimerTester); |
| 128 }; | 120 }; |
| 129 | 121 |
| 130 void RunTest_OneShotAlarmTimer(base::MessageLoop::Type message_loop_type) { | 122 void RunTest_OneShotAlarmTimer(base::MessageLoop::Type message_loop_type) { |
| 131 base::MessageLoop loop(message_loop_type); | 123 base::MessageLoop loop(message_loop_type); |
| 132 | 124 |
| 133 bool did_run = false; | 125 bool did_run = false; |
| 134 OneShotAlarmTimerTester f(&did_run, kTenMilliseconds); | 126 OneShotAlarmTimerTester f(&did_run, kTenMilliseconds); |
| 135 f.Start(); | 127 f.Start(); |
| 136 | 128 |
| 137 base::RunLoop().Run(); | 129 base::RunLoop().Run(); |
| 138 | 130 |
| 139 EXPECT_TRUE(did_run); | 131 EXPECT_TRUE(did_run); |
| 140 } | 132 } |
| 141 | 133 |
| 142 void RunTest_OneShotAlarmTimer_Cancel( | 134 void RunTest_OneShotAlarmTimer_Cancel( |
| 143 base::MessageLoop::Type message_loop_type) { | 135 base::MessageLoop::Type message_loop_type) { |
| 144 base::MessageLoop loop(message_loop_type); | 136 base::MessageLoop loop(message_loop_type); |
| 145 | 137 |
| 146 bool did_run_a = false; | 138 bool did_run_a = false; |
| 147 OneShotAlarmTimerTester* a = new OneShotAlarmTimerTester(&did_run_a, | 139 OneShotAlarmTimerTester* a = |
| 148 kTenMilliseconds); | 140 new OneShotAlarmTimerTester(&did_run_a, kTenMilliseconds); |
| 149 | 141 |
| 150 // This should run before the timer expires. | 142 // This should run before the timer expires. |
| 151 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); | 143 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
| 152 | 144 |
| 153 // Now start the timer. | 145 // Now start the timer. |
| 154 a->Start(); | 146 a->Start(); |
| 155 | 147 |
| 156 bool did_run_b = false; | 148 bool did_run_b = false; |
| 157 OneShotAlarmTimerTester b(&did_run_b, kTenMilliseconds); | 149 OneShotAlarmTimerTester b(&did_run_b, kTenMilliseconds); |
| 158 b.Start(); | 150 b.Start(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 183 bool did_run = false; | 175 bool did_run = false; |
| 184 RepeatingAlarmTimerTester f(&did_run, delay); | 176 RepeatingAlarmTimerTester f(&did_run, delay); |
| 185 f.Start(); | 177 f.Start(); |
| 186 | 178 |
| 187 base::RunLoop().Run(); | 179 base::RunLoop().Run(); |
| 188 | 180 |
| 189 EXPECT_TRUE(did_run); | 181 EXPECT_TRUE(did_run); |
| 190 } | 182 } |
| 191 | 183 |
| 192 void RunTest_RepeatingAlarmTimer_Cancel( | 184 void RunTest_RepeatingAlarmTimer_Cancel( |
| 193 base::MessageLoop::Type message_loop_type, const base::TimeDelta& delay) { | 185 base::MessageLoop::Type message_loop_type, |
| 186 const base::TimeDelta& delay) { |
| 194 base::MessageLoop loop(message_loop_type); | 187 base::MessageLoop loop(message_loop_type); |
| 195 | 188 |
| 196 bool did_run_a = false; | 189 bool did_run_a = false; |
| 197 RepeatingAlarmTimerTester* a = new RepeatingAlarmTimerTester(&did_run_a, | 190 RepeatingAlarmTimerTester* a = |
| 198 delay); | 191 new RepeatingAlarmTimerTester(&did_run_a, delay); |
| 199 | 192 |
| 200 // This should run before the timer expires. | 193 // This should run before the timer expires. |
| 201 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); | 194 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
| 202 | 195 |
| 203 // Now start the timer. | 196 // Now start the timer. |
| 204 a->Start(); | 197 a->Start(); |
| 205 | 198 |
| 206 bool did_run_b = false; | 199 bool did_run_b = false; |
| 207 RepeatingAlarmTimerTester b(&did_run_b, delay); | 200 RepeatingAlarmTimerTester b(&did_run_b, delay); |
| 208 b.Start(); | 201 b.Start(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 234 // If underlying timer does not handle this properly, we will crash or fail | 227 // If underlying timer does not handle this properly, we will crash or fail |
| 235 // in full page heap environment. | 228 // in full page heap environment. |
| 236 TEST(AlarmTimerTest, OneShotSelfDeletingAlarmTimer) { | 229 TEST(AlarmTimerTest, OneShotSelfDeletingAlarmTimer) { |
| 237 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 230 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 238 RunTest_OneShotSelfDeletingAlarmTimer(testing_message_loops[i]); | 231 RunTest_OneShotSelfDeletingAlarmTimer(testing_message_loops[i]); |
| 239 } | 232 } |
| 240 } | 233 } |
| 241 | 234 |
| 242 TEST(AlarmTimerTest, RepeatingAlarmTimer) { | 235 TEST(AlarmTimerTest, RepeatingAlarmTimer) { |
| 243 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 236 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 244 RunTest_RepeatingAlarmTimer(testing_message_loops[i], | 237 RunTest_RepeatingAlarmTimer(testing_message_loops[i], kTenMilliseconds); |
| 245 kTenMilliseconds); | |
| 246 } | 238 } |
| 247 } | 239 } |
| 248 | 240 |
| 249 TEST(AlarmTimerTest, RepeatingAlarmTimer_Cancel) { | 241 TEST(AlarmTimerTest, RepeatingAlarmTimer_Cancel) { |
| 250 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 242 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 251 RunTest_RepeatingAlarmTimer_Cancel(testing_message_loops[i], | 243 RunTest_RepeatingAlarmTimer_Cancel(testing_message_loops[i], |
| 252 kTenMilliseconds); | 244 kTenMilliseconds); |
| 253 } | 245 } |
| 254 } | 246 } |
| 255 | 247 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 276 { | 268 { |
| 277 OneShotAlarmTimerTester a(&did_run, kTenMilliseconds); | 269 OneShotAlarmTimerTester a(&did_run, kTenMilliseconds); |
| 278 OneShotAlarmTimerTester b(&did_run, kTenMilliseconds); | 270 OneShotAlarmTimerTester b(&did_run, kTenMilliseconds); |
| 279 OneShotAlarmTimerTester c(&did_run, kTenMilliseconds); | 271 OneShotAlarmTimerTester c(&did_run, kTenMilliseconds); |
| 280 OneShotAlarmTimerTester d(&did_run, kTenMilliseconds); | 272 OneShotAlarmTimerTester d(&did_run, kTenMilliseconds); |
| 281 { | 273 { |
| 282 base::MessageLoop loop; | 274 base::MessageLoop loop; |
| 283 a.Start(); | 275 a.Start(); |
| 284 b.Start(); | 276 b.Start(); |
| 285 } // MessageLoop destructs by falling out of scope. | 277 } // MessageLoop destructs by falling out of scope. |
| 286 } // OneShotTimers destruct. SHOULD NOT CRASH, of course. | 278 } // OneShotTimers destruct. SHOULD NOT CRASH, of course. |
| 287 | 279 |
| 288 EXPECT_FALSE(did_run); | 280 EXPECT_FALSE(did_run); |
| 289 } | 281 } |
| 290 | 282 |
| 291 TEST(AlarmTimerTest, NonRepeatIsRunning) { | 283 TEST(AlarmTimerTest, NonRepeatIsRunning) { |
| 292 { | 284 { |
| 293 base::MessageLoop loop; | 285 base::MessageLoop loop; |
| 294 timers::AlarmTimer timer(false, false); | 286 timers::OneShotAlarmTimer timer; |
| 295 EXPECT_FALSE(timer.IsRunning()); | 287 EXPECT_FALSE(timer.IsRunning()); |
| 296 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), | 288 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), |
| 297 base::Bind(&base::DoNothing)); | 289 base::Bind(&base::DoNothing)); |
| 298 EXPECT_TRUE(timer.IsRunning()); | 290 EXPECT_TRUE(timer.IsRunning()); |
| 299 timer.Stop(); | 291 timer.Stop(); |
| 300 EXPECT_FALSE(timer.IsRunning()); | 292 EXPECT_FALSE(timer.IsRunning()); |
| 301 EXPECT_TRUE(timer.user_task().is_null()); | 293 EXPECT_TRUE(timer.user_task().is_null()); |
| 302 } | 294 } |
| 303 | 295 |
| 304 { | 296 { |
| 305 timers::AlarmTimer timer(true, false); | 297 timers::SimpleAlarmTimer timer; |
| 306 base::MessageLoop loop; | 298 base::MessageLoop loop; |
| 307 EXPECT_FALSE(timer.IsRunning()); | 299 EXPECT_FALSE(timer.IsRunning()); |
| 308 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), | 300 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), |
| 309 base::Bind(&base::DoNothing)); | 301 base::Bind(&base::DoNothing)); |
| 310 EXPECT_TRUE(timer.IsRunning()); | 302 EXPECT_TRUE(timer.IsRunning()); |
| 311 timer.Stop(); | 303 timer.Stop(); |
| 312 EXPECT_FALSE(timer.IsRunning()); | 304 EXPECT_FALSE(timer.IsRunning()); |
| 313 ASSERT_FALSE(timer.user_task().is_null()); | 305 ASSERT_FALSE(timer.user_task().is_null()); |
| 314 timer.Reset(); | 306 timer.Reset(); |
| 315 EXPECT_TRUE(timer.IsRunning()); | 307 EXPECT_TRUE(timer.IsRunning()); |
| 316 } | 308 } |
| 317 } | 309 } |
| 318 | 310 |
| 319 TEST(AlarmTimerTest, NonRepeatMessageLoopDeath) { | 311 TEST(AlarmTimerTest, NonRepeatMessageLoopDeath) { |
| 320 timers::AlarmTimer timer(false, false); | 312 timers::OneShotAlarmTimer timer; |
| 321 { | 313 { |
| 322 base::MessageLoop loop; | 314 base::MessageLoop loop; |
| 323 EXPECT_FALSE(timer.IsRunning()); | 315 EXPECT_FALSE(timer.IsRunning()); |
| 324 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), | 316 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), |
| 325 base::Bind(&base::DoNothing)); | 317 base::Bind(&base::DoNothing)); |
| 326 EXPECT_TRUE(timer.IsRunning()); | 318 EXPECT_TRUE(timer.IsRunning()); |
| 327 } | 319 } |
| 328 EXPECT_FALSE(timer.IsRunning()); | 320 EXPECT_FALSE(timer.IsRunning()); |
| 329 EXPECT_TRUE(timer.user_task().is_null()); | 321 EXPECT_TRUE(timer.user_task().is_null()); |
| 330 } | 322 } |
| 331 | 323 |
| 332 TEST(AlarmTimerTest, RetainRepeatIsRunning) { | 324 TEST(AlarmTimerTest, RetainRepeatIsRunning) { |
| 333 base::MessageLoop loop; | 325 base::MessageLoop loop; |
| 334 timers::AlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), | 326 timers::RepeatingAlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), |
| 335 base::Bind(&base::DoNothing), true); | 327 base::Bind(&base::DoNothing)); |
| 336 EXPECT_FALSE(timer.IsRunning()); | 328 EXPECT_FALSE(timer.IsRunning()); |
| 337 timer.Reset(); | 329 timer.Reset(); |
| 338 EXPECT_TRUE(timer.IsRunning()); | 330 EXPECT_TRUE(timer.IsRunning()); |
| 339 timer.Stop(); | 331 timer.Stop(); |
| 340 EXPECT_FALSE(timer.IsRunning()); | 332 EXPECT_FALSE(timer.IsRunning()); |
| 341 timer.Reset(); | 333 timer.Reset(); |
| 342 EXPECT_TRUE(timer.IsRunning()); | 334 EXPECT_TRUE(timer.IsRunning()); |
| 343 } | 335 } |
| 344 | 336 |
| 345 TEST(AlarmTimerTest, RetainNonRepeatIsRunning) { | 337 TEST(AlarmTimerTest, RetainNonRepeatIsRunning) { |
| 346 base::MessageLoop loop; | 338 base::MessageLoop loop; |
| 347 timers::AlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), | 339 timers::SimpleAlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), |
| 348 base::Bind(&base::DoNothing), false); | 340 base::Bind(&base::DoNothing)); |
| 349 EXPECT_FALSE(timer.IsRunning()); | 341 EXPECT_FALSE(timer.IsRunning()); |
| 350 timer.Reset(); | 342 timer.Reset(); |
| 351 EXPECT_TRUE(timer.IsRunning()); | 343 EXPECT_TRUE(timer.IsRunning()); |
| 352 timer.Stop(); | 344 timer.Stop(); |
| 353 EXPECT_FALSE(timer.IsRunning()); | 345 EXPECT_FALSE(timer.IsRunning()); |
| 354 timer.Reset(); | 346 timer.Reset(); |
| 355 EXPECT_TRUE(timer.IsRunning()); | 347 EXPECT_TRUE(timer.IsRunning()); |
| 356 } | 348 } |
| 357 | 349 |
| 358 namespace { | 350 namespace { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 374 void SetCallbackHappened2() { | 366 void SetCallbackHappened2() { |
| 375 g_callback_happened2 = true; | 367 g_callback_happened2 = true; |
| 376 base::MessageLoop::current()->PostTask( | 368 base::MessageLoop::current()->PostTask( |
| 377 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 369 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 378 } | 370 } |
| 379 | 371 |
| 380 TEST(AlarmTimerTest, ContinuationStopStart) { | 372 TEST(AlarmTimerTest, ContinuationStopStart) { |
| 381 { | 373 { |
| 382 ClearAllCallbackHappened(); | 374 ClearAllCallbackHappened(); |
| 383 base::MessageLoop loop; | 375 base::MessageLoop loop; |
| 384 timers::AlarmTimer timer(false, false); | 376 timers::OneShotAlarmTimer timer; |
| 385 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), | 377 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), |
| 386 base::Bind(&SetCallbackHappened1)); | 378 base::Bind(&SetCallbackHappened1)); |
| 387 timer.Stop(); | 379 timer.Stop(); |
| 388 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(40), | 380 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(40), |
| 389 base::Bind(&SetCallbackHappened2)); | 381 base::Bind(&SetCallbackHappened2)); |
| 390 base::RunLoop().Run(); | 382 base::RunLoop().Run(); |
| 391 EXPECT_FALSE(g_callback_happened1); | 383 EXPECT_FALSE(g_callback_happened1); |
| 392 EXPECT_TRUE(g_callback_happened2); | 384 EXPECT_TRUE(g_callback_happened2); |
| 393 } | 385 } |
| 394 } | 386 } |
| 395 | 387 |
| 396 TEST(AlarmTimerTest, ContinuationReset) { | 388 TEST(AlarmTimerTest, ContinuationReset) { |
| 397 { | 389 { |
| 398 ClearAllCallbackHappened(); | 390 ClearAllCallbackHappened(); |
| 399 base::MessageLoop loop; | 391 base::MessageLoop loop; |
| 400 timers::AlarmTimer timer(false, false); | 392 timers::OneShotAlarmTimer timer; |
| 401 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), | 393 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), |
| 402 base::Bind(&SetCallbackHappened1)); | 394 base::Bind(&SetCallbackHappened1)); |
| 403 timer.Reset(); | 395 timer.Reset(); |
| 404 // Since Reset happened before task ran, the user_task must not be cleared: | 396 // Since Reset happened before task ran, the user_task must not be cleared: |
| 405 ASSERT_FALSE(timer.user_task().is_null()); | 397 ASSERT_FALSE(timer.user_task().is_null()); |
| 406 base::RunLoop().Run(); | 398 base::RunLoop().Run(); |
| 407 EXPECT_TRUE(g_callback_happened1); | 399 EXPECT_TRUE(g_callback_happened1); |
| 408 } | 400 } |
| 409 } | 401 } |
| 410 | 402 |
| 411 } // namespace | 403 } // namespace |
| 412 | 404 |
| 413 | |
| 414 namespace { | 405 namespace { |
| 415 void TimerRanCallback(bool* did_run) { | 406 void TimerRanCallback(bool* did_run) { |
| 416 *did_run = true; | 407 *did_run = true; |
| 417 | 408 |
| 418 base::MessageLoop::current()->PostTask( | 409 base::MessageLoop::current()->PostTask( |
| 419 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 410 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 420 } | 411 } |
| 421 | 412 |
| 422 void RunTest_OneShotTimerConcurrentResetAndTimerFired( | 413 void RunTest_OneShotTimerConcurrentResetAndTimerFired( |
| 423 base::MessageLoop::Type message_loop_type) { | 414 base::MessageLoop::Type message_loop_type) { |
| 424 base::MessageLoop loop(message_loop_type); | 415 base::MessageLoop loop(message_loop_type); |
| 425 | 416 |
| 426 timers::AlarmTimer timer(false, false); | 417 timers::OneShotAlarmTimer timer; |
| 427 bool did_run = false; | 418 bool did_run = false; |
| 428 | 419 |
| 429 timer.Start( | 420 timer.Start(FROM_HERE, kTenMilliseconds, |
| 430 FROM_HERE, kTenMilliseconds, base::Bind(&TimerRanCallback, &did_run)); | 421 base::Bind(&TimerRanCallback, &did_run)); |
| 431 | 422 |
| 432 // Sleep twice as long as the timer to ensure that the timer task gets queued. | 423 // Sleep twice as long as the timer to ensure that the timer task gets queued. |
| 433 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); | 424 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); |
| 434 | 425 |
| 435 // Now reset the timer. This is attempting to simulate the timer firing and | 426 // Now reset the timer. This is attempting to simulate the timer firing and |
| 436 // being reset at the same time. The previously queued task should be | 427 // being reset at the same time. The previously queued task should be |
| 437 // removed. | 428 // removed. |
| 438 timer.Reset(); | 429 timer.Reset(); |
| 439 | 430 |
| 440 base::RunLoop().RunUntilIdle(); | 431 base::RunLoop().RunUntilIdle(); |
| 441 EXPECT_FALSE(did_run); | 432 EXPECT_FALSE(did_run); |
| 442 | 433 |
| 443 // If the previous check failed, running the message loop again will hang the | 434 // If the previous check failed, running the message loop again will hang the |
| 444 // test so we only do it if the callback has not run yet. | 435 // test so we only do it if the callback has not run yet. |
| 445 if (!did_run) { | 436 if (!did_run) { |
| 446 base::RunLoop().Run(); | 437 base::RunLoop().Run(); |
| 447 EXPECT_TRUE(did_run); | 438 EXPECT_TRUE(did_run); |
| 448 } | 439 } |
| 449 } | 440 } |
| 450 | 441 |
| 451 void RunTest_RepeatingTimerConcurrentResetAndTimerFired( | 442 void RunTest_RepeatingTimerConcurrentResetAndTimerFired( |
| 452 base::MessageLoop::Type message_loop_type) { | 443 base::MessageLoop::Type message_loop_type) { |
| 453 base::MessageLoop loop(message_loop_type); | 444 base::MessageLoop loop(message_loop_type); |
| 454 | 445 |
| 455 timers::AlarmTimer timer(true, true); | 446 timers::RepeatingAlarmTimer timer; |
| 456 bool did_run = false; | 447 bool did_run = false; |
| 457 | 448 |
| 458 timer.Start( | 449 timer.Start(FROM_HERE, kTenMilliseconds, |
| 459 FROM_HERE, kTenMilliseconds, base::Bind(&TimerRanCallback, &did_run)); | 450 base::Bind(&TimerRanCallback, &did_run)); |
| 460 | 451 |
| 461 // Sleep more that three times as long as the timer duration to ensure that | 452 // Sleep more that three times as long as the timer duration to ensure that |
| 462 // multiple tasks get queued. | 453 // multiple tasks get queued. |
| 463 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(35)); | 454 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(35)); |
| 464 | 455 |
| 465 // Now reset the timer. This is attempting to simulate a very busy message | 456 // Now reset the timer. This is attempting to simulate a very busy message |
| 466 // loop where multiple tasks get queued but the timer gets reset before any of | 457 // loop where multiple tasks get queued but the timer gets reset before any of |
| 467 // them have a chance to run. | 458 // them have a chance to run. |
| 468 timer.Reset(); | 459 timer.Reset(); |
| 469 | 460 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 487 TEST(AlarmTimerTest, RepeatingTimerConcurrentResetAndTimerFired) { | 478 TEST(AlarmTimerTest, RepeatingTimerConcurrentResetAndTimerFired) { |
| 488 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 479 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 489 RunTest_RepeatingTimerConcurrentResetAndTimerFired( | 480 RunTest_RepeatingTimerConcurrentResetAndTimerFired( |
| 490 testing_message_loops[i]); | 481 testing_message_loops[i]); |
| 491 } | 482 } |
| 492 } | 483 } |
| 493 | 484 |
| 494 } // namespace | 485 } // namespace |
| 495 | 486 |
| 496 } // namespace timers | 487 } // namespace timers |
| OLD | NEW |