Home | History | Annotate | Download | only in congestion_control
      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   }
     25 
     26   const QuicTime::Delta zero_time_;
     27   MockClock clock_;
     28   scoped_ptr<PacedSender> paced_sender_;
     29 };
     30 
     31 TEST_F(PacedSenderTest, Basic) {
     32   paced_sender_->UpdateBandwidthEstimate(clock_.Now(),
     33       QuicBandwidth::FromKBytesPerSecond(kHundredKBytesPerS * 10));
     34   EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero());
     35   paced_sender_->SentPacket(clock_.Now(), kMaxPacketSize);
     36   EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero());
     37   paced_sender_->SentPacket(clock_.Now(), kMaxPacketSize);
     38   EXPECT_EQ(static_cast<int64>(kMaxPacketSize * 2),
     39             paced_sender_->TimeUntilSend(
     40                 clock_.Now(), zero_time_).ToMicroseconds());
     41   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(24));
     42   EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero());
     43 }
     44 
     45 TEST_F(PacedSenderTest, LowRate) {
     46   paced_sender_->UpdateBandwidthEstimate(clock_.Now(),
     47       QuicBandwidth::FromKBytesPerSecond(kHundredKBytesPerS));
     48   EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero());
     49   paced_sender_->SentPacket(clock_.Now(), kMaxPacketSize);
     50   EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero());
     51   paced_sender_->SentPacket(clock_.Now(), kMaxPacketSize);
     52   EXPECT_EQ(static_cast<int64>(kMaxPacketSize * 20),
     53             paced_sender_->TimeUntilSend(
     54                 clock_.Now(), zero_time_).ToMicroseconds());
     55   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(24));
     56   EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero());
     57 }
     58 
     59 TEST_F(PacedSenderTest, HighRate) {
     60   QuicBandwidth bandwidth_estimate = QuicBandwidth::FromKBytesPerSecond(
     61       kHundredKBytesPerS * 100);
     62   paced_sender_->UpdateBandwidthEstimate(clock_.Now(), bandwidth_estimate);
     63   EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero());
     64   for (int i = 0; i < 16; ++i) {
     65     paced_sender_->SentPacket(clock_.Now(), kMaxPacketSize);
     66     EXPECT_TRUE(paced_sender_->TimeUntilSend(
     67         clock_.Now(), zero_time_).IsZero());
     68   }
     69   paced_sender_->SentPacket(clock_.Now(), kMaxPacketSize);
     70   EXPECT_EQ(2040, paced_sender_->TimeUntilSend(
     71       clock_.Now(), zero_time_).ToMicroseconds());
     72   clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(20400));
     73   EXPECT_TRUE(paced_sender_->TimeUntilSend(clock_.Now(), zero_time_).IsZero());
     74 }
     75 
     76 }  // namespace test
     77 }  // namespace net
     78