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 "base/logging.h" 6 #include "base/memory/scoped_ptr.h" 7 #include "net/quic/congestion_control/paced_sender.h" 8 #include "net/quic/quic_protocol.h" 9 #include "net/quic/test_tools/mock_clock.h" 10 #include "testing/gmock/include/gmock/gmock.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 namespace net { 14 namespace test { 15 16 const int kHundredKBytesPerS = 100; 17 18 class PacedSenderTest : public ::testing::Test { 19 protected: 20 PacedSenderTest() 21 : zero_time_(QuicTime::Delta::Zero()), 22 paced_sender_(new PacedSender( 23 QuicBandwidth::FromKBytesPerSecond(kHundredKBytesPerS), 24 kDefaultMaxPacketSize)) { 25 } 26 27 const QuicTime::Delta zero_time_; 28 MockClock clock_; 29 scoped_ptr<PacedSender> paced_sender_; 30 }; 31 32 TEST_F(PacedSenderTest, Basic) { 33 paced_sender_->UpdateBandwidthEstimate(clock_.Now(), 34 QuicBandwidth::FromKBytesPerSecond(kHundredKBytesPerS * 10)); 35 EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero()); 36 paced_sender_->OnPacketSent(clock_.Now(), kDefaultMaxPacketSize); 37 EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero()); 38 paced_sender_->OnPacketSent(clock_.Now(), kDefaultMaxPacketSize); 39 EXPECT_EQ(static_cast<int64>(kDefaultMaxPacketSize * 2), 40 paced_sender_->TimeUntilSend( 41 clock_.Now(), zero_time_).ToMicroseconds()); 42 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(24)); 43 EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero()); 44 } 45 46 TEST_F(PacedSenderTest, LowRate) { 47 paced_sender_->UpdateBandwidthEstimate(clock_.Now(), 48 QuicBandwidth::FromKBytesPerSecond(kHundredKBytesPerS)); 49 EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero()); 50 paced_sender_->OnPacketSent(clock_.Now(), kDefaultMaxPacketSize); 51 EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero()); 52 paced_sender_->OnPacketSent(clock_.Now(), kDefaultMaxPacketSize); 53 EXPECT_EQ(static_cast<int64>(kDefaultMaxPacketSize * 20), 54 paced_sender_->TimeUntilSend( 55 clock_.Now(), zero_time_).ToMicroseconds()); 56 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(24)); 57 EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero()); 58 } 59 60 TEST_F(PacedSenderTest, HighRate) { 61 QuicBandwidth bandwidth_estimate = QuicBandwidth::FromKBytesPerSecond( 62 kHundredKBytesPerS * 100); 63 paced_sender_->UpdateBandwidthEstimate(clock_.Now(), bandwidth_estimate); 64 EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero()); 65 for (int i = 0; i < 16; ++i) { 66 paced_sender_->OnPacketSent(clock_.Now(), kDefaultMaxPacketSize); 67 EXPECT_TRUE(paced_sender_->TimeUntilSend( 68 clock_.Now(), zero_time_).IsZero()); 69 } 70 paced_sender_->OnPacketSent(clock_.Now(), kDefaultMaxPacketSize); 71 EXPECT_EQ(2040, paced_sender_->TimeUntilSend( 72 clock_.Now(), zero_time_).ToMicroseconds()); 73 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(20400)); 74 EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero()); 75 } 76 77 } // namespace test 78 } // namespace net 79