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