summaryrefslogtreecommitdiffstats
path: root/net/quic/congestion_control/leaky_bucket_test.cc
blob: 89ab5dbe67a6ded8f199603994b672af795c6606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "net/quic/congestion_control/leaky_bucket.h"
#include "net/quic/test_tools/mock_clock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {
namespace test {

class LeakyBucketTest : public ::testing::Test {
 protected:
  virtual void SetUp() {
    leaky_bucket_.reset(new LeakyBucket(&clock_, QuicBandwidth::Zero()));
  }
  MockClock clock_;
  scoped_ptr<LeakyBucket> leaky_bucket_;
};

TEST_F(LeakyBucketTest, Basic) {
  QuicBandwidth draining_rate = QuicBandwidth::FromBytesPerSecond(200000);
  leaky_bucket_->SetDrainingRate(draining_rate);
  leaky_bucket_->Add(2000);
  EXPECT_EQ(2000u, leaky_bucket_->BytesPending());
  EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10),
            leaky_bucket_->TimeRemaining());
  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
  EXPECT_EQ(1000u, leaky_bucket_->BytesPending());
  EXPECT_EQ(QuicTime::Delta::FromMilliseconds(5),
            leaky_bucket_->TimeRemaining());
  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
  EXPECT_EQ(0u, leaky_bucket_->BytesPending());
  EXPECT_TRUE(leaky_bucket_->TimeRemaining().IsZero());
  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
  EXPECT_EQ(0u, leaky_bucket_->BytesPending());
  EXPECT_TRUE(leaky_bucket_->TimeRemaining().IsZero());
  leaky_bucket_->Add(2000);
  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(11));
  EXPECT_EQ(0u, leaky_bucket_->BytesPending());
  EXPECT_TRUE(leaky_bucket_->TimeRemaining().IsZero());
  leaky_bucket_->Add(2000);
  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
  leaky_bucket_->Add(2000);
  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
  EXPECT_EQ(2000u, leaky_bucket_->BytesPending());
  EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10),
            leaky_bucket_->TimeRemaining());
  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
  EXPECT_EQ(0u, leaky_bucket_->BytesPending());
  EXPECT_TRUE(leaky_bucket_->TimeRemaining().IsZero());
}

TEST_F(LeakyBucketTest, ChangeDrainRate) {
  QuicBandwidth draining_rate = QuicBandwidth::FromBytesPerSecond(200000);
  leaky_bucket_->SetDrainingRate(draining_rate);
  leaky_bucket_->Add(2000);
  EXPECT_EQ(2000u, leaky_bucket_->BytesPending());
  EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10),
            leaky_bucket_->TimeRemaining());
  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
  EXPECT_EQ(1000u, leaky_bucket_->BytesPending());
  EXPECT_EQ(QuicTime::Delta::FromMilliseconds(5),
            leaky_bucket_->TimeRemaining());
  draining_rate = draining_rate.Scale(0.5f);  // Cut drain rate in half.
  leaky_bucket_->SetDrainingRate(draining_rate);
  EXPECT_EQ(1000u, leaky_bucket_->BytesPending());
  EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10),
            leaky_bucket_->TimeRemaining());
}

}  // namespace test
}  // namespace net