| OLD | NEW | 
|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "net/quic/quic_alarm.h" | 5 #include "net/quic/quic_alarm.h" | 
| 6 | 6 | 
| 7 #include "base/logging.h" | 7 #include "base/logging.h" | 
| 8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" | 
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" | 
| 10 | 10 | 
| 11 using testing::Return; | 11 using testing::Return; | 
| 12 using testing::Invoke; | 12 using testing::Invoke; | 
| 13 | 13 | 
| 14 namespace net { | 14 namespace net { | 
| 15 namespace test { | 15 namespace test { | 
| 16 namespace { | 16 namespace { | 
| 17 | 17 | 
| 18 class MockDelegate : public QuicAlarm::Delegate { | 18 class MockDelegate : public QuicAlarm::Delegate { | 
| 19  public: | 19  public: | 
| 20   MOCK_METHOD0(OnAlarm, QuicTime()); | 20   MOCK_METHOD0(OnAlarm, void()); | 
| 21 }; | 21 }; | 
| 22 | 22 | 
| 23 class DestructiveDelegate : public QuicAlarm::Delegate { | 23 class DestructiveDelegate : public QuicAlarm::Delegate { | 
| 24  public: | 24  public: | 
| 25   DestructiveDelegate() : alarm_(nullptr) {} | 25   DestructiveDelegate() : alarm_(nullptr) {} | 
| 26 | 26 | 
| 27   void set_alarm(QuicAlarm* alarm) { alarm_ = alarm; } | 27   void set_alarm(QuicAlarm* alarm) { alarm_ = alarm; } | 
| 28 | 28 | 
| 29   QuicTime OnAlarm() override { | 29   void OnAlarm() override { | 
| 30     DCHECK(alarm_); | 30     DCHECK(alarm_); | 
| 31     delete alarm_; | 31     delete alarm_; | 
| 32     return QuicTime::Zero(); |  | 
| 33   } | 32   } | 
| 34 | 33 | 
| 35  private: | 34  private: | 
| 36   QuicAlarm* alarm_; | 35   QuicAlarm* alarm_; | 
| 37 }; | 36 }; | 
| 38 | 37 | 
| 39 class TestAlarm : public QuicAlarm { | 38 class TestAlarm : public QuicAlarm { | 
| 40  public: | 39  public: | 
| 41   explicit TestAlarm(QuicAlarm::Delegate* delegate) | 40   explicit TestAlarm(QuicAlarm::Delegate* delegate) | 
| 42       : QuicAlarm(QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)) {} | 41       : QuicAlarm(QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)) {} | 
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 130   alarm_.Set(deadline); | 129   alarm_.Set(deadline); | 
| 131   alarm_.Update(QuicTime::Zero(), QuicTime::Delta::Zero()); | 130   alarm_.Update(QuicTime::Zero(), QuicTime::Delta::Zero()); | 
| 132   EXPECT_FALSE(alarm_.IsSet()); | 131   EXPECT_FALSE(alarm_.IsSet()); | 
| 133   EXPECT_FALSE(alarm_.scheduled()); | 132   EXPECT_FALSE(alarm_.scheduled()); | 
| 134   EXPECT_EQ(QuicTime::Zero(), alarm_.deadline()); | 133   EXPECT_EQ(QuicTime::Zero(), alarm_.deadline()); | 
| 135 } | 134 } | 
| 136 | 135 | 
| 137 TEST_F(QuicAlarmTest, Fire) { | 136 TEST_F(QuicAlarmTest, Fire) { | 
| 138   QuicTime deadline = QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7)); | 137   QuicTime deadline = QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7)); | 
| 139   alarm_.Set(deadline); | 138   alarm_.Set(deadline); | 
| 140   EXPECT_CALL(*delegate_, OnAlarm()).WillOnce(Return(QuicTime::Zero())); |  | 
| 141   alarm_.FireAlarm(); | 139   alarm_.FireAlarm(); | 
| 142   EXPECT_FALSE(alarm_.IsSet()); | 140   EXPECT_FALSE(alarm_.IsSet()); | 
| 143   EXPECT_FALSE(alarm_.scheduled()); | 141   EXPECT_FALSE(alarm_.scheduled()); | 
| 144   EXPECT_EQ(QuicTime::Zero(), alarm_.deadline()); | 142   EXPECT_EQ(QuicTime::Zero(), alarm_.deadline()); | 
| 145 } | 143 } | 
| 146 | 144 | 
| 147 TEST_F(QuicAlarmTest, FireAndResetViaReturn) { |  | 
| 148   alarm_.Set(deadline_); |  | 
| 149   EXPECT_CALL(*delegate_, OnAlarm()).WillOnce(Return(deadline2_)); |  | 
| 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, FireAndResetViaSet) { | 145 TEST_F(QuicAlarmTest, FireAndResetViaSet) { | 
| 157   alarm_.Set(deadline_); | 146   alarm_.Set(deadline_); | 
| 158   new_deadline_ = deadline2_; | 147   new_deadline_ = deadline2_; | 
| 159   EXPECT_CALL(*delegate_, OnAlarm()) | 148   EXPECT_CALL(*delegate_, OnAlarm()) | 
| 160       .WillOnce(DoAll(Invoke(this, &QuicAlarmTest::ResetAlarm), | 149       .WillOnce(Invoke(this, &QuicAlarmTest::ResetAlarm)); | 
| 161                       Return(QuicTime::Zero()))); |  | 
| 162   alarm_.FireAlarm(); | 150   alarm_.FireAlarm(); | 
| 163   EXPECT_TRUE(alarm_.IsSet()); | 151   EXPECT_TRUE(alarm_.IsSet()); | 
| 164   EXPECT_TRUE(alarm_.scheduled()); | 152   EXPECT_TRUE(alarm_.scheduled()); | 
| 165   EXPECT_EQ(deadline2_, alarm_.deadline()); | 153   EXPECT_EQ(deadline2_, alarm_.deadline()); | 
| 166 } | 154 } | 
| 167 | 155 | 
| 168 TEST_F(QuicAlarmTest, FireDestroysAlarm) { | 156 TEST_F(QuicAlarmTest, FireDestroysAlarm) { | 
| 169   DestructiveDelegate* delegate(new DestructiveDelegate); | 157   DestructiveDelegate* delegate(new DestructiveDelegate); | 
| 170   DestructiveAlarm* alarm = new DestructiveAlarm(delegate); | 158   DestructiveAlarm* alarm = new DestructiveAlarm(delegate); | 
| 171   delegate->set_alarm(alarm); | 159   delegate->set_alarm(alarm); | 
| 172   QuicTime deadline = QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7)); | 160   QuicTime deadline = QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7)); | 
| 173   alarm->Set(deadline); | 161   alarm->Set(deadline); | 
| 174   // This should not crash, even though it will destroy alarm. | 162   // This should not crash, even though it will destroy alarm. | 
| 175   alarm->FireAlarm(); | 163   alarm->FireAlarm(); | 
| 176 } | 164 } | 
| 177 | 165 | 
| 178 }  // namespace | 166 }  // namespace | 
| 179 }  // namespace test | 167 }  // namespace test | 
| 180 }  // namespace net | 168 }  // namespace net | 
| OLD | NEW | 
|---|