summaryrefslogtreecommitdiff
path: root/media/cast/congestion_control/congestion_control_unittest.cc
blob: 60c38b45ba1932f076d77d71c530d62c252cef6b (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2013 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/test/simple_test_tick_clock.h"
#include "media/cast/cast_defines.h"
#include "media/cast/congestion_control/congestion_control.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {
namespace cast {

static const uint32 kMaxBitrateConfigured = 5000000;
static const uint32 kMinBitrateConfigured = 500000;
static const uint32 kStartBitrate = 2000000;
static const int64 kStartMillisecond = GG_INT64_C(12345678900000);
static const int64 kRttMs = 20;
static const int64 kAckRateMs = 33;

class CongestionControlTest : public ::testing::Test {
 protected:
  CongestionControlTest()
      : congestion_control_(&testing_clock_,
                            kDefaultCongestionControlBackOff,
                            kMaxBitrateConfigured,
                            kMinBitrateConfigured,
                            kStartBitrate) {
    testing_clock_.Advance(
        base::TimeDelta::FromMilliseconds(kStartMillisecond));
  }

  base::SimpleTestTickClock testing_clock_;
  CongestionControl congestion_control_;
};

TEST_F(CongestionControlTest, Max) {
  uint32 new_bitrate = 0;
  base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(kRttMs);
  base::TimeDelta ack_rate = base::TimeDelta::FromMilliseconds(kAckRateMs);
  EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));

  uint32 expected_increase_bitrate = 0;

  // Expected time is 5 seconds. 500000 - 2000000 = 5 * 1500 * 8 * (1000 / 20).
  for (int i = 0; i < 151; ++i) {
    testing_clock_.Advance(ack_rate);
    EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
    expected_increase_bitrate += 1500 * 8 * kAckRateMs / kRttMs;
    EXPECT_EQ(kStartBitrate + expected_increase_bitrate, new_bitrate);
  }
  testing_clock_.Advance(ack_rate);
  EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
  EXPECT_EQ(kMaxBitrateConfigured, new_bitrate);
}

TEST_F(CongestionControlTest, Min) {
  uint32 new_bitrate = 0;
  base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(kRttMs);
  base::TimeDelta ack_rate = base::TimeDelta::FromMilliseconds(kAckRateMs);
  EXPECT_FALSE(congestion_control_.OnNack(rtt, &new_bitrate));

  uint32 expected_decrease_bitrate = kStartBitrate;

  // Expected number is 10. 2000 * 0.875^10 <= 500.
  for (int i = 0; i < 10; ++i) {
    testing_clock_.Advance(ack_rate);
     EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
     expected_decrease_bitrate = static_cast<uint32>(
         expected_decrease_bitrate * kDefaultCongestionControlBackOff);
     EXPECT_EQ(expected_decrease_bitrate, new_bitrate);
   }
   testing_clock_.Advance(ack_rate);
   EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
   EXPECT_EQ(kMinBitrateConfigured, new_bitrate);
}

TEST_F(CongestionControlTest, Timing) {
  base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(kRttMs);
  base::TimeDelta ack_rate = base::TimeDelta::FromMilliseconds(kAckRateMs);
  uint32 new_bitrate = 0;
  uint32 expected_bitrate = kStartBitrate;

  EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));

  testing_clock_.Advance(ack_rate);
  EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
  expected_bitrate += 1500 * 8 * kAckRateMs / kRttMs;
  EXPECT_EQ(expected_bitrate, new_bitrate);

  // We should back immediately.
  EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
  expected_bitrate = static_cast<uint32>(
      expected_bitrate * kDefaultCongestionControlBackOff);
  EXPECT_EQ(expected_bitrate, new_bitrate);

  // Less than one RTT have passed don't back again.
  testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
  EXPECT_FALSE(congestion_control_.OnNack(rtt, &new_bitrate));

  testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
  EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
  expected_bitrate = static_cast<uint32>(
      expected_bitrate * kDefaultCongestionControlBackOff);
  EXPECT_EQ(expected_bitrate, new_bitrate);

  testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
  EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
  testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
  EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
  expected_bitrate += 1500 * 8 * 20 / kRttMs;
  EXPECT_EQ(expected_bitrate, new_bitrate);

  testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
  EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
  testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
  EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
  expected_bitrate += 1500 * 8 * 20 / kRttMs;
  EXPECT_EQ(expected_bitrate, new_bitrate);

  // Test long elapsed time (300 ms).
  testing_clock_.Advance(base::TimeDelta::FromMilliseconds(300));
  EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
  expected_bitrate += 1500 * 8 * 100 / kRttMs;
  EXPECT_EQ(expected_bitrate, new_bitrate);

  // Test many short elapsed time (1 ms).
  for (int i = 0; i < 19; ++i) {
    testing_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
    EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
  }
  testing_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
  EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
  expected_bitrate += 1500 * 8 * 20 / kRttMs;
  EXPECT_EQ(expected_bitrate, new_bitrate);
}

}  // namespace cast
}  // namespace media