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/leaky_bucket.h"
      8 #include "net/quic/test_tools/mock_clock.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 namespace net {
     12 namespace test {
     13 
     14 class LeakyBucketTest : public ::testing::Test {
     15  protected:
     16   virtual void SetUp() {
     17     leaky_bucket_.reset(new LeakyBucket(QuicBandwidth::Zero()));
     18   }
     19   MockClock clock_;
     20   scoped_ptr<LeakyBucket> leaky_bucket_;
     21 };
     22 
     23 TEST_F(LeakyBucketTest, Basic) {
     24   QuicBandwidth draining_rate = QuicBandwidth::FromBytesPerSecond(200000);
     25   leaky_bucket_->SetDrainingRate(clock_.Now(), draining_rate);
     26   leaky_bucket_->Add(clock_.Now(), 2000);
     27   EXPECT_EQ(2000u, leaky_bucket_->BytesPending(clock_.Now()));
     28   EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10),
     29             leaky_bucket_->TimeRemaining(clock_.Now()));
     30   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
     31   EXPECT_EQ(1000u, leaky_bucket_->BytesPending(clock_.Now()));
     32   EXPECT_EQ(QuicTime::Delta::FromMilliseconds(5),
     33             leaky_bucket_->TimeRemaining(clock_.Now()));
     34   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
     35   EXPECT_EQ(0u, leaky_bucket_->BytesPending(clock_.Now()));
     36   EXPECT_TRUE(leaky_bucket_->TimeRemaining(clock_.Now()).IsZero());
     37   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
     38   EXPECT_EQ(0u, leaky_bucket_->BytesPending(clock_.Now()));
     39   EXPECT_TRUE(leaky_bucket_->TimeRemaining(clock_.Now()).IsZero());
     40   leaky_bucket_->Add(clock_.Now(), 2000);
     41   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(11));
     42   EXPECT_EQ(0u, leaky_bucket_->BytesPending(clock_.Now()));
     43   EXPECT_TRUE(leaky_bucket_->TimeRemaining(clock_.Now()).IsZero());
     44   leaky_bucket_->Add(clock_.Now(), 2000);
     45   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
     46   leaky_bucket_->Add(clock_.Now(), 2000);
     47   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
     48   EXPECT_EQ(2000u, leaky_bucket_->BytesPending(clock_.Now()));
     49   EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10),
     50             leaky_bucket_->TimeRemaining(clock_.Now()));
     51   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
     52   EXPECT_EQ(0u, leaky_bucket_->BytesPending(clock_.Now()));
     53   EXPECT_TRUE(leaky_bucket_->TimeRemaining(clock_.Now()).IsZero());
     54 }
     55 
     56 TEST_F(LeakyBucketTest, ChangeDrainRate) {
     57   QuicBandwidth draining_rate = QuicBandwidth::FromBytesPerSecond(200000);
     58   leaky_bucket_->SetDrainingRate(clock_.Now(), draining_rate);
     59   leaky_bucket_->Add(clock_.Now(), 2000);
     60   EXPECT_EQ(2000u, leaky_bucket_->BytesPending(clock_.Now()));
     61   EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10),
     62             leaky_bucket_->TimeRemaining(clock_.Now()));
     63   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
     64   EXPECT_EQ(1000u, leaky_bucket_->BytesPending(clock_.Now()));
     65   EXPECT_EQ(QuicTime::Delta::FromMilliseconds(5),
     66             leaky_bucket_->TimeRemaining(clock_.Now()));
     67   draining_rate = draining_rate.Scale(0.5f);  // Cut drain rate in half.
     68   leaky_bucket_->SetDrainingRate(clock_.Now(), draining_rate);
     69   EXPECT_EQ(1000u, leaky_bucket_->BytesPending(clock_.Now()));
     70   EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10),
     71             leaky_bucket_->TimeRemaining(clock_.Now()));
     72 }
     73 
     74 }  // namespace test
     75 }  // namespace net
     76