| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 | |
| 5 #include "net/quic/quic_alarm.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "testing/gmock/include/gmock/gmock.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 using testing::Return; | |
| 12 using testing::Invoke; | |
| 13 | |
| 14 namespace net { | |
| 15 namespace test { | |
| 16 namespace { | |
| 17 | |
| 18 class MockDelegate : public QuicAlarm::Delegate { | |
| 19 public: | |
| 20 MOCK_METHOD0(OnAlarm, void()); | |
| 21 }; | |
| 22 | |
| 23 class DestructiveDelegate : public QuicAlarm::Delegate { | |
| 24 public: | |
| 25 DestructiveDelegate() : alarm_(nullptr) {} | |
| 26 | |
| 27 void set_alarm(QuicAlarm* alarm) { alarm_ = alarm; } | |
| 28 | |
| 29 void OnAlarm() override { | |
| 30 DCHECK(alarm_); | |
| 31 delete alarm_; | |
| 32 } | |
| 33 | |
| 34 private: | |
| 35 QuicAlarm* alarm_; | |
| 36 }; | |
| 37 | |
| 38 class TestAlarm : public QuicAlarm { | |
| 39 public: | |
| 40 explicit TestAlarm(QuicAlarm::Delegate* delegate) | |
| 41 : QuicAlarm(QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)) {} | |
| 42 | |
| 43 bool scheduled() const { return scheduled_; } | |
| 44 | |
| 45 void FireAlarm() { | |
| 46 scheduled_ = false; | |
| 47 Fire(); | |
| 48 } | |
| 49 | |
| 50 protected: | |
| 51 void SetImpl() override { | |
| 52 DCHECK(deadline().IsInitialized()); | |
| 53 scheduled_ = true; | |
| 54 } | |
| 55 | |
| 56 void CancelImpl() override { | |
| 57 DCHECK(!deadline().IsInitialized()); | |
| 58 scheduled_ = false; | |
| 59 } | |
| 60 | |
| 61 private: | |
| 62 bool scheduled_; | |
| 63 }; | |
| 64 | |
| 65 class DestructiveAlarm : public QuicAlarm { | |
| 66 public: | |
| 67 explicit DestructiveAlarm(DestructiveDelegate* delegate) | |
| 68 : QuicAlarm(QuicArenaScopedPtr<DestructiveDelegate>(delegate)) {} | |
| 69 | |
| 70 void FireAlarm() { Fire(); } | |
| 71 | |
| 72 protected: | |
| 73 void SetImpl() override {} | |
| 74 | |
| 75 void CancelImpl() override {} | |
| 76 }; | |
| 77 | |
| 78 class QuicAlarmTest : public ::testing::Test { | |
| 79 public: | |
| 80 QuicAlarmTest() | |
| 81 : delegate_(new MockDelegate()), | |
| 82 alarm_(delegate_), | |
| 83 deadline_(QuicTime::Zero() + QuicTime::Delta::FromSeconds(7)), | |
| 84 deadline2_(QuicTime::Zero() + QuicTime::Delta::FromSeconds(14)), | |
| 85 new_deadline_(QuicTime::Zero()) {} | |
| 86 | |
| 87 void ResetAlarm() { alarm_.Set(new_deadline_); } | |
| 88 | |
| 89 MockDelegate* delegate_; // not owned | |
| 90 TestAlarm alarm_; | |
| 91 QuicTime deadline_; | |
| 92 QuicTime deadline2_; | |
| 93 QuicTime new_deadline_; | |
| 94 }; | |
| 95 | |
| 96 TEST_F(QuicAlarmTest, IsSet) { | |
| 97 EXPECT_FALSE(alarm_.IsSet()); | |
| 98 } | |
| 99 | |
| 100 TEST_F(QuicAlarmTest, Set) { | |
| 101 QuicTime deadline = QuicTime::Zero() + QuicTime::Delta::FromSeconds(7); | |
| 102 alarm_.Set(deadline); | |
| 103 EXPECT_TRUE(alarm_.IsSet()); | |
| 104 EXPECT_TRUE(alarm_.scheduled()); | |
| 105 EXPECT_EQ(deadline, alarm_.deadline()); | |
| 106 } | |
| 107 | |
| 108 TEST_F(QuicAlarmTest, Cancel) { | |
| 109 QuicTime deadline = QuicTime::Zero() + QuicTime::Delta::FromSeconds(7); | |
| 110 alarm_.Set(deadline); | |
| 111 alarm_.Cancel(); | |
| 112 EXPECT_FALSE(alarm_.IsSet()); | |
| 113 EXPECT_FALSE(alarm_.scheduled()); | |
| 114 EXPECT_EQ(QuicTime::Zero(), alarm_.deadline()); | |
| 115 } | |
| 116 | |
| 117 TEST_F(QuicAlarmTest, Update) { | |
| 118 QuicTime deadline = QuicTime::Zero() + QuicTime::Delta::FromSeconds(7); | |
| 119 alarm_.Set(deadline); | |
| 120 QuicTime new_deadline = QuicTime::Zero() + QuicTime::Delta::FromSeconds(8); | |
| 121 alarm_.Update(new_deadline, QuicTime::Delta::Zero()); | |
| 122 EXPECT_TRUE(alarm_.IsSet()); | |
| 123 EXPECT_TRUE(alarm_.scheduled()); | |
| 124 EXPECT_EQ(new_deadline, alarm_.deadline()); | |
| 125 } | |
| 126 | |
| 127 TEST_F(QuicAlarmTest, UpdateWithZero) { | |
| 128 QuicTime deadline = QuicTime::Zero() + QuicTime::Delta::FromSeconds(7); | |
| 129 alarm_.Set(deadline); | |
| 130 alarm_.Update(QuicTime::Zero(), QuicTime::Delta::Zero()); | |
| 131 EXPECT_FALSE(alarm_.IsSet()); | |
| 132 EXPECT_FALSE(alarm_.scheduled()); | |
| 133 EXPECT_EQ(QuicTime::Zero(), alarm_.deadline()); | |
| 134 } | |
| 135 | |
| 136 TEST_F(QuicAlarmTest, Fire) { | |
| 137 QuicTime deadline = QuicTime::Zero() + QuicTime::Delta::FromSeconds(7); | |
| 138 alarm_.Set(deadline); | |
| 139 alarm_.FireAlarm(); | |
| 140 EXPECT_FALSE(alarm_.IsSet()); | |
| 141 EXPECT_FALSE(alarm_.scheduled()); | |
| 142 EXPECT_EQ(QuicTime::Zero(), alarm_.deadline()); | |
| 143 } | |
| 144 | |
| 145 TEST_F(QuicAlarmTest, FireAndResetViaSet) { | |
| 146 alarm_.Set(deadline_); | |
| 147 new_deadline_ = deadline2_; | |
| 148 EXPECT_CALL(*delegate_, OnAlarm()) | |
| 149 .WillOnce(Invoke(this, &QuicAlarmTest::ResetAlarm)); | |
| 150 alarm_.FireAlarm(); | |
| 151 EXPECT_TRUE(alarm_.IsSet()); | |
| 152 EXPECT_TRUE(alarm_.scheduled()); | |
| 153 EXPECT_EQ(deadline2_, alarm_.deadline()); | |
| 154 } | |
| 155 | |
| 156 TEST_F(QuicAlarmTest, FireDestroysAlarm) { | |
| 157 DestructiveDelegate* delegate(new DestructiveDelegate); | |
| 158 DestructiveAlarm* alarm = new DestructiveAlarm(delegate); | |
| 159 delegate->set_alarm(alarm); | |
| 160 QuicTime deadline = QuicTime::Zero() + QuicTime::Delta::FromSeconds(7); | |
| 161 alarm->Set(deadline); | |
| 162 // This should not crash, even though it will destroy alarm. | |
| 163 alarm->FireAlarm(); | |
| 164 } | |
| 165 | |
| 166 } // namespace | |
| 167 } // namespace test | |
| 168 } // namespace net | |
| OLD | NEW |