| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/timer/timer.h" | 5 #include "base/timer/timer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 public: | 47 public: |
| 48 Receiver() : count_(0) {} | 48 Receiver() : count_(0) {} |
| 49 void OnCalled() { count_++; } | 49 void OnCalled() { count_++; } |
| 50 bool WasCalled() { return count_ > 0; } | 50 bool WasCalled() { return count_ > 0; } |
| 51 int TimesCalled() { return count_; } | 51 int TimesCalled() { return count_; } |
| 52 | 52 |
| 53 private: | 53 private: |
| 54 int count_; | 54 int count_; |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 class OneShotTimerTester { | 57 // A basic helper class that can start a one-shot timer and signal a |
| 58 // WaitableEvent when this timer fires. |
| 59 class OneShotTimerTesterBase { |
| 60 public: |
| 61 // |did_run|, if provided, will be signaled when Run() fires. |
| 62 explicit OneShotTimerTesterBase( |
| 63 WaitableEvent* did_run = nullptr, |
| 64 const TimeDelta& delay = TimeDelta::FromMilliseconds(10)) |
| 65 : did_run_(did_run), delay_(delay) {} |
| 66 |
| 67 virtual ~OneShotTimerTesterBase() = default; |
| 68 |
| 69 void Start() { |
| 70 started_time_ = TimeTicks::Now(); |
| 71 timer_->Start(FROM_HERE, delay_, this, &OneShotTimerTesterBase::Run); |
| 72 } |
| 73 |
| 74 bool IsRunning() { return timer_->IsRunning(); } |
| 75 |
| 76 TimeTicks started_time() const { return started_time_; } |
| 77 TimeDelta delay() const { return delay_; } |
| 78 |
| 79 protected: |
| 80 virtual void Run() { |
| 81 if (did_run_) { |
| 82 EXPECT_FALSE(did_run_->IsSignaled()); |
| 83 did_run_->Signal(); |
| 84 } |
| 85 } |
| 86 |
| 87 std::unique_ptr<OneShotTimer> timer_ = MakeUnique<OneShotTimer>(); |
| 88 |
| 89 private: |
| 90 WaitableEvent* const did_run_; |
| 91 const TimeDelta delay_; |
| 92 TimeTicks started_time_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(OneShotTimerTesterBase); |
| 95 }; |
| 96 |
| 97 // Extends functionality of OneShotTimerTesterBase with the abilities to wait |
| 98 // until the timer fires and to change task runner for the timer. |
| 99 class OneShotTimerTester : public OneShotTimerTesterBase { |
| 58 public: | 100 public: |
| 59 // |did_run|, if provided, will be signaled when Run() fires. | 101 // |did_run|, if provided, will be signaled when Run() fires. |
| 60 explicit OneShotTimerTester( | 102 explicit OneShotTimerTester( |
| 61 WaitableEvent* did_run = nullptr, | 103 WaitableEvent* did_run = nullptr, |
| 62 const TimeDelta& delay = TimeDelta::FromMilliseconds(10)) | 104 const TimeDelta& delay = TimeDelta::FromMilliseconds(10)) |
| 63 : quit_closure_(run_loop_.QuitClosure()), | 105 : OneShotTimerTesterBase(did_run, delay), |
| 64 did_run_(did_run), | 106 quit_closure_(run_loop_.QuitClosure()) {} |
| 65 delay_(delay) {} | |
| 66 | 107 |
| 67 virtual ~OneShotTimerTester() = default; | 108 ~OneShotTimerTester() override = default; |
| 68 | 109 |
| 69 void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) { | 110 void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) { |
| 70 timer_->SetTaskRunner(std::move(task_runner)); | 111 timer_->SetTaskRunner(std::move(task_runner)); |
| 71 | 112 |
| 72 // Run() will be invoked on |task_runner| but |run_loop_|'s QuitClosure | 113 // Run() will be invoked on |task_runner| but |run_loop_|'s QuitClosure |
| 73 // needs to run on this thread (where the MessageLoop lives). | 114 // needs to run on this thread (where the MessageLoop lives). |
| 74 quit_closure_ = | 115 quit_closure_ = |
| 75 Bind(IgnoreResult(&SingleThreadTaskRunner::PostTask), | 116 Bind(IgnoreResult(&SingleThreadTaskRunner::PostTask), |
| 76 ThreadTaskRunnerHandle::Get(), FROM_HERE, run_loop_.QuitClosure()); | 117 ThreadTaskRunnerHandle::Get(), FROM_HERE, run_loop_.QuitClosure()); |
| 77 } | 118 } |
| 78 | 119 |
| 79 void Start() { | |
| 80 started_time_ = TimeTicks::Now(); | |
| 81 timer_->Start(FROM_HERE, delay_, this, &OneShotTimerTester::Run); | |
| 82 } | |
| 83 | |
| 84 // Blocks until Run() executes and confirms that Run() didn't fire before | 120 // Blocks until Run() executes and confirms that Run() didn't fire before |
| 85 // |delay_| expired. | 121 // |delay_| expired. |
| 86 void WaitAndConfirmTimerFiredAfterDelay() { | 122 void WaitAndConfirmTimerFiredAfterDelay() { |
| 87 run_loop_.Run(); | 123 run_loop_.Run(); |
| 88 | 124 |
| 89 EXPECT_NE(TimeTicks(), started_time_); | 125 EXPECT_NE(TimeTicks(), started_time()); |
| 90 EXPECT_GE(TimeTicks::Now() - started_time_, delay_); | 126 EXPECT_GE(TimeTicks::Now() - started_time(), delay()); |
| 91 } | 127 } |
| 92 | 128 |
| 93 bool IsRunning() { return timer_->IsRunning(); } | |
| 94 | |
| 95 protected: | 129 protected: |
| 96 // Overridable method to do things on Run() before signaling events/closures | 130 // Overridable method to do things on Run() before signaling events/closures |
| 97 // managed by this helper. | 131 // managed by this helper. |
| 98 virtual void OnRun() {} | 132 virtual void OnRun() {} |
| 99 | 133 |
| 100 std::unique_ptr<OneShotTimer> timer_ = MakeUnique<OneShotTimer>(); | |
| 101 | |
| 102 private: | 134 private: |
| 103 void Run() { | 135 void Run() override { |
| 104 OnRun(); | 136 OnRun(); |
| 105 if (did_run_) { | 137 OneShotTimerTesterBase::Run(); |
| 106 EXPECT_FALSE(did_run_->IsSignaled()); | |
| 107 did_run_->Signal(); | |
| 108 } | |
| 109 quit_closure_.Run(); | 138 quit_closure_.Run(); |
| 110 } | 139 } |
| 111 | 140 |
| 112 RunLoop run_loop_; | 141 RunLoop run_loop_; |
| 113 Closure quit_closure_; | 142 Closure quit_closure_; |
| 114 WaitableEvent* const did_run_; | |
| 115 | |
| 116 const TimeDelta delay_; | |
| 117 TimeTicks started_time_; | |
| 118 | 143 |
| 119 DISALLOW_COPY_AND_ASSIGN(OneShotTimerTester); | 144 DISALLOW_COPY_AND_ASSIGN(OneShotTimerTester); |
| 120 }; | 145 }; |
| 121 | 146 |
| 122 class OneShotSelfDeletingTimerTester : public OneShotTimerTester { | 147 class OneShotSelfDeletingTimerTester : public OneShotTimerTester { |
| 123 protected: | 148 protected: |
| 124 void OnRun() override { timer_.reset(); } | 149 void OnRun() override { timer_.reset(); } |
| 125 }; | 150 }; |
| 126 | 151 |
| 127 constexpr int kNumRepeats = 10; | 152 constexpr int kNumRepeats = 10; |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 } | 535 } |
| 511 | 536 |
| 512 TEST(TimerTest, MessageLoopShutdown) { | 537 TEST(TimerTest, MessageLoopShutdown) { |
| 513 // This test is designed to verify that shutdown of the | 538 // This test is designed to verify that shutdown of the |
| 514 // message loop does not cause crashes if there were pending | 539 // message loop does not cause crashes if there were pending |
| 515 // timers not yet fired. It may only trigger exceptions | 540 // timers not yet fired. It may only trigger exceptions |
| 516 // if debug heap checking is enabled. | 541 // if debug heap checking is enabled. |
| 517 WaitableEvent did_run(WaitableEvent::ResetPolicy::MANUAL, | 542 WaitableEvent did_run(WaitableEvent::ResetPolicy::MANUAL, |
| 518 WaitableEvent::InitialState::NOT_SIGNALED); | 543 WaitableEvent::InitialState::NOT_SIGNALED); |
| 519 { | 544 { |
| 520 OneShotTimerTester a(&did_run); | 545 OneShotTimerTesterBase a(&did_run); |
| 521 OneShotTimerTester b(&did_run); | 546 OneShotTimerTesterBase b(&did_run); |
| 522 OneShotTimerTester c(&did_run); | 547 OneShotTimerTesterBase c(&did_run); |
| 523 OneShotTimerTester d(&did_run); | 548 OneShotTimerTesterBase d(&did_run); |
| 524 { | 549 { |
| 525 MessageLoop loop; | 550 MessageLoop loop; |
| 526 a.Start(); | 551 a.Start(); |
| 527 b.Start(); | 552 b.Start(); |
| 528 } // MessageLoop destructs by falling out of scope. | 553 } // MessageLoop destructs by falling out of scope. |
| 529 } // OneShotTimers destruct. SHOULD NOT CRASH, of course. | 554 } // OneShotTimers destruct. SHOULD NOT CRASH, of course. |
| 530 | 555 |
| 531 EXPECT_FALSE(did_run.IsSignaled()); | 556 EXPECT_FALSE(did_run.IsSignaled()); |
| 532 } | 557 } |
| 533 | 558 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 Bind(&SetCallbackHappened1)); | 670 Bind(&SetCallbackHappened1)); |
| 646 timer.Reset(); | 671 timer.Reset(); |
| 647 // Since Reset happened before task ran, the user_task must not be cleared: | 672 // Since Reset happened before task ran, the user_task must not be cleared: |
| 648 ASSERT_FALSE(timer.user_task().is_null()); | 673 ASSERT_FALSE(timer.user_task().is_null()); |
| 649 RunLoop().Run(); | 674 RunLoop().Run(); |
| 650 EXPECT_TRUE(g_callback_happened1); | 675 EXPECT_TRUE(g_callback_happened1); |
| 651 } | 676 } |
| 652 } | 677 } |
| 653 | 678 |
| 654 } // namespace base | 679 } // namespace base |
| OLD | NEW |