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 3 // found in the LICENSE file. 4 5 #include "net/tools/quic/quic_epoll_connection_helper.h" 6 7 #include "net/quic/crypto/quic_random.h" 8 #include "net/tools/quic/test_tools/mock_epoll_server.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 using net::tools::test::MockEpollServer; 12 13 namespace net { 14 namespace tools { 15 namespace test { 16 namespace { 17 18 class TestDelegate : public QuicAlarm::Delegate { 19 public: 20 TestDelegate() : fired_(false) {} 21 22 virtual QuicTime OnAlarm() OVERRIDE { 23 fired_ = true; 24 return QuicTime::Zero(); 25 } 26 27 bool fired() const { return fired_; } 28 29 private: 30 bool fired_; 31 }; 32 33 class QuicEpollConnectionHelperTest : public ::testing::Test { 34 protected: 35 QuicEpollConnectionHelperTest() : helper_(&epoll_server_) {} 36 37 MockEpollServer epoll_server_; 38 QuicEpollConnectionHelper helper_; 39 }; 40 41 TEST_F(QuicEpollConnectionHelperTest, GetClock) { 42 const QuicClock* clock = helper_.GetClock(); 43 QuicTime start = clock->Now(); 44 45 QuicTime::Delta delta = QuicTime::Delta::FromMilliseconds(5); 46 epoll_server_.AdvanceBy(delta.ToMicroseconds()); 47 48 EXPECT_EQ(start.Add(delta), clock->Now()); 49 } 50 51 TEST_F(QuicEpollConnectionHelperTest, GetRandomGenerator) { 52 QuicRandom* random = helper_.GetRandomGenerator(); 53 EXPECT_EQ(QuicRandom::GetInstance(), random); 54 } 55 56 TEST_F(QuicEpollConnectionHelperTest, CreateAlarm) { 57 TestDelegate* delegate = new TestDelegate(); 58 scoped_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate)); 59 60 const QuicClock* clock = helper_.GetClock(); 61 QuicTime start = clock->Now(); 62 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); 63 alarm->Set(start.Add(delta)); 64 65 epoll_server_.AdvanceByAndCallCallbacks(delta.ToMicroseconds()); 66 EXPECT_EQ(start.Add(delta), clock->Now()); 67 } 68 69 TEST_F(QuicEpollConnectionHelperTest, CreateAlarmAndCancel) { 70 TestDelegate* delegate = new TestDelegate(); 71 scoped_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate)); 72 73 const QuicClock* clock = helper_.GetClock(); 74 QuicTime start = clock->Now(); 75 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); 76 alarm->Set(start.Add(delta)); 77 alarm->Cancel(); 78 79 epoll_server_.AdvanceByExactlyAndCallCallbacks(delta.ToMicroseconds()); 80 EXPECT_EQ(start.Add(delta), clock->Now()); 81 EXPECT_FALSE(delegate->fired()); 82 } 83 84 TEST_F(QuicEpollConnectionHelperTest, CreateAlarmAndReset) { 85 TestDelegate* delegate = new TestDelegate(); 86 scoped_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate)); 87 88 const QuicClock* clock = helper_.GetClock(); 89 QuicTime start = clock->Now(); 90 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); 91 alarm->Set(clock->Now().Add(delta)); 92 alarm->Cancel(); 93 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3); 94 alarm->Set(clock->Now().Add(new_delta)); 95 96 epoll_server_.AdvanceByExactlyAndCallCallbacks(delta.ToMicroseconds()); 97 EXPECT_EQ(start.Add(delta), clock->Now()); 98 EXPECT_FALSE(delegate->fired()); 99 100 epoll_server_.AdvanceByExactlyAndCallCallbacks( 101 new_delta.Subtract(delta).ToMicroseconds()); 102 EXPECT_EQ(start.Add(new_delta), clock->Now()); 103 EXPECT_TRUE(delegate->fired()); 104 } 105 106 } // namespace 107 } // namespace test 108 } // namespace tools 109 } // namespace net 110