| 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, QuicTime()); | |
| 21 }; | |
| 22 | |
| 23 class TestAlarm : public QuicAlarm { | |
| 24 public: | |
| 25 explicit TestAlarm(QuicAlarm::Delegate* delegate) : QuicAlarm(delegate) {} | |
| 26 | |
| 27 bool scheduled() const { return scheduled_; } | |
| 28 | |
| 29 void FireAlarm() { | |
| 30 scheduled_ = false; | |
| 31 Fire(); | |
| 32 } | |
| 33 | |
| 34 protected: | |
| 35 void SetImpl() override { | |
| 36 DCHECK(deadline().IsInitialized()); | |
| 37 scheduled_ = true; | |
| 38 } | |
| 39 | |
| 40 void CancelImpl() override { | |
| 41 DCHECK(!deadline().IsInitialized()); | |
| 42 scheduled_ = false; | |
| 43 } | |
| 44 | |
| 45 private: | |
| 46 bool scheduled_; | |
| 47 }; | |
| 48 | |
| 49 class QuicAlarmTest : public ::testing::Test { | |
| 50 public: | |
| 51 QuicAlarmTest() | |
| 52 : delegate_(new MockDelegate()), | |
| 53 alarm_(delegate_), | |
| 54 deadline_(QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7))), | |
| 55 deadline2_(QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(14))), | |
| 56 new_deadline_(QuicTime::Zero()) { | |
| 57 } | |
| 58 | |
| 59 void ResetAlarm() { | |
| 60 alarm_.Set(new_deadline_); | |
| 61 } | |
| 62 | |
| 63 MockDelegate* delegate_; // not owned | |
| 64 TestAlarm alarm_; | |
| 65 QuicTime deadline_; | |
| 66 QuicTime deadline2_; | |
| 67 QuicTime new_deadline_; | |
| 68 }; | |
| 69 | |
| 70 TEST_F(QuicAlarmTest, IsSet) { | |
| 71 EXPECT_FALSE(alarm_.IsSet()); | |
| 72 } | |
| 73 | |
| 74 TEST_F(QuicAlarmTest, Set) { | |
| 75 QuicTime deadline = QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7)); | |
| 76 alarm_.Set(deadline); | |
| 77 EXPECT_TRUE(alarm_.IsSet()); | |
| 78 EXPECT_TRUE(alarm_.scheduled()); | |
| 79 EXPECT_EQ(deadline, alarm_.deadline()); | |
| 80 } | |
| 81 | |
| 82 TEST_F(QuicAlarmTest, Cancel) { | |
| 83 QuicTime deadline = QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7)); | |
| 84 alarm_.Set(deadline); | |
| 85 alarm_.Cancel(); | |
| 86 EXPECT_FALSE(alarm_.IsSet()); | |
| 87 EXPECT_FALSE(alarm_.scheduled()); | |
| 88 EXPECT_EQ(QuicTime::Zero(), alarm_.deadline()); | |
| 89 } | |
| 90 | |
| 91 TEST_F(QuicAlarmTest, Update) { | |
| 92 QuicTime deadline = QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7)); | |
| 93 alarm_.Set(deadline); | |
| 94 QuicTime new_deadline = QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(8)); | |
| 95 alarm_.Update(new_deadline, QuicTime::Delta::Zero()); | |
| 96 EXPECT_TRUE(alarm_.IsSet()); | |
| 97 EXPECT_TRUE(alarm_.scheduled()); | |
| 98 EXPECT_EQ(new_deadline, alarm_.deadline()); | |
| 99 } | |
| 100 | |
| 101 TEST_F(QuicAlarmTest, UpdateWithZero) { | |
| 102 QuicTime deadline = QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7)); | |
| 103 alarm_.Set(deadline); | |
| 104 alarm_.Update(QuicTime::Zero(), QuicTime::Delta::Zero()); | |
| 105 EXPECT_FALSE(alarm_.IsSet()); | |
| 106 EXPECT_FALSE(alarm_.scheduled()); | |
| 107 EXPECT_EQ(QuicTime::Zero(), alarm_.deadline()); | |
| 108 } | |
| 109 | |
| 110 TEST_F(QuicAlarmTest, Fire) { | |
| 111 QuicTime deadline = QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7)); | |
| 112 alarm_.Set(deadline); | |
| 113 EXPECT_CALL(*delegate_, OnAlarm()).WillOnce(Return(QuicTime::Zero())); | |
| 114 alarm_.FireAlarm(); | |
| 115 EXPECT_FALSE(alarm_.IsSet()); | |
| 116 EXPECT_FALSE(alarm_.scheduled()); | |
| 117 EXPECT_EQ(QuicTime::Zero(), alarm_.deadline()); | |
| 118 } | |
| 119 | |
| 120 TEST_F(QuicAlarmTest, FireAndResetViaReturn) { | |
| 121 alarm_.Set(deadline_); | |
| 122 EXPECT_CALL(*delegate_, OnAlarm()).WillOnce(Return(deadline2_)); | |
| 123 alarm_.FireAlarm(); | |
| 124 EXPECT_TRUE(alarm_.IsSet()); | |
| 125 EXPECT_TRUE(alarm_.scheduled()); | |
| 126 EXPECT_EQ(deadline2_, alarm_.deadline()); | |
| 127 } | |
| 128 | |
| 129 TEST_F(QuicAlarmTest, FireAndResetViaSet) { | |
| 130 alarm_.Set(deadline_); | |
| 131 new_deadline_ = deadline2_; | |
| 132 EXPECT_CALL(*delegate_, OnAlarm()).WillOnce(DoAll( | |
| 133 Invoke(this, &QuicAlarmTest::ResetAlarm), | |
| 134 Return(QuicTime::Zero()))); | |
| 135 alarm_.FireAlarm(); | |
| 136 EXPECT_TRUE(alarm_.IsSet()); | |
| 137 EXPECT_TRUE(alarm_.scheduled()); | |
| 138 EXPECT_EQ(deadline2_, alarm_.deadline()); | |
| 139 } | |
| 140 | |
| 141 } // namespace | |
| 142 } // namespace test | |
| 143 } // namespace net | |
| OLD | NEW |