1 // Copyright 2013 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.
5 #include "base/test/simple_test_tick_clock.h"
6 #include "media/cast/cast_defines.h"
7 #include "media/cast/congestion_control/congestion_control.h"
8 #include "testing/gtest/include/gtest/gtest.h"
13 static const uint32 kMaxBitrateConfigured
= 5000000;
14 static const uint32 kMinBitrateConfigured
= 500000;
15 static const uint32 kStartBitrate
= 2000000;
16 static const int64 kStartMillisecond
= GG_INT64_C(12345678900000);
17 static const int64 kRttMs
= 20;
18 static const int64 kAckRateMs
= 33;
19 static const int64 kNackRateMs
= 10;
21 class CongestionControlTest
: public ::testing::Test
{
23 CongestionControlTest()
24 : congestion_control_(&testing_clock_
,
25 kDefaultCongestionControlBackOff
,
26 kMaxBitrateConfigured
,
27 kMinBitrateConfigured
,
29 testing_clock_
.Advance(
30 base::TimeDelta::FromMilliseconds(kStartMillisecond
));
33 base::SimpleTestTickClock testing_clock_
;
34 CongestionControl congestion_control_
;
37 TEST_F(CongestionControlTest
, Max
) {
38 uint32 new_bitrate
= 0;
39 base::TimeDelta rtt
= base::TimeDelta::FromMilliseconds(kRttMs
);
40 base::TimeDelta ack_rate
= base::TimeDelta::FromMilliseconds(kAckRateMs
);
41 EXPECT_FALSE(congestion_control_
.OnAck(rtt
, &new_bitrate
));
43 uint32 expected_increase_bitrate
= 0;
45 // Expected time is 5 seconds. 500000 - 2000000 = 5 * 1500 * 8 * (1000 / 20).
46 for (int i
= 0; i
< 151; ++i
) {
47 testing_clock_
.Advance(ack_rate
);
48 EXPECT_TRUE(congestion_control_
.OnAck(rtt
, &new_bitrate
));
49 expected_increase_bitrate
+= 1500 * 8 * kAckRateMs
/ kRttMs
;
50 EXPECT_EQ(kStartBitrate
+ expected_increase_bitrate
, new_bitrate
);
52 testing_clock_
.Advance(ack_rate
);
53 EXPECT_TRUE(congestion_control_
.OnAck(rtt
, &new_bitrate
));
54 EXPECT_EQ(kMaxBitrateConfigured
, new_bitrate
);
57 TEST_F(CongestionControlTest
, Min
) {
58 uint32 new_bitrate
= 0;
59 base::TimeDelta rtt
= base::TimeDelta::FromMilliseconds(kRttMs
);
60 base::TimeDelta ack_rate
= base::TimeDelta::FromMilliseconds(kAckRateMs
);
61 EXPECT_FALSE(congestion_control_
.OnNack(rtt
, &new_bitrate
));
63 uint32 expected_decrease_bitrate
= kStartBitrate
;
65 // Expected number is 10. 2000 * 0.875^10 <= 500.
66 for (int i
= 0; i
< 10; ++i
) {
67 testing_clock_
.Advance(ack_rate
);
68 EXPECT_TRUE(congestion_control_
.OnNack(rtt
, &new_bitrate
));
69 expected_decrease_bitrate
= static_cast<uint32
>(
70 expected_decrease_bitrate
* kDefaultCongestionControlBackOff
);
71 EXPECT_EQ(expected_decrease_bitrate
, new_bitrate
);
73 testing_clock_
.Advance(ack_rate
);
74 EXPECT_TRUE(congestion_control_
.OnNack(rtt
, &new_bitrate
));
75 EXPECT_EQ(kMinBitrateConfigured
, new_bitrate
);
78 TEST_F(CongestionControlTest
, Timing
) {
79 base::TimeDelta rtt
= base::TimeDelta::FromMilliseconds(kRttMs
);
80 base::TimeDelta ack_rate
= base::TimeDelta::FromMilliseconds(kAckRateMs
);
81 uint32 new_bitrate
= 0;
82 uint32 expected_bitrate
= kStartBitrate
;
84 EXPECT_FALSE(congestion_control_
.OnAck(rtt
, &new_bitrate
));
86 testing_clock_
.Advance(ack_rate
);
87 EXPECT_TRUE(congestion_control_
.OnAck(rtt
, &new_bitrate
));
88 expected_bitrate
+= 1500 * 8 * kAckRateMs
/ kRttMs
;
89 EXPECT_EQ(expected_bitrate
, new_bitrate
);
91 // We should back immediately.
92 EXPECT_TRUE(congestion_control_
.OnNack(rtt
, &new_bitrate
));
93 expected_bitrate
= static_cast<uint32
>(
94 expected_bitrate
* kDefaultCongestionControlBackOff
);
95 EXPECT_EQ(expected_bitrate
, new_bitrate
);
97 // Less than one RTT have passed don't back again.
98 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(10));
99 EXPECT_FALSE(congestion_control_
.OnNack(rtt
, &new_bitrate
));
101 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(10));
102 EXPECT_TRUE(congestion_control_
.OnNack(rtt
, &new_bitrate
));
103 expected_bitrate
= static_cast<uint32
>(
104 expected_bitrate
* kDefaultCongestionControlBackOff
);
105 EXPECT_EQ(expected_bitrate
, new_bitrate
);
107 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(10));
108 EXPECT_FALSE(congestion_control_
.OnAck(rtt
, &new_bitrate
));
109 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(10));
110 EXPECT_TRUE(congestion_control_
.OnAck(rtt
, &new_bitrate
));
111 expected_bitrate
+= 1500 * 8 * 20 / kRttMs
;
112 EXPECT_EQ(expected_bitrate
, new_bitrate
);
114 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(10));
115 EXPECT_FALSE(congestion_control_
.OnAck(rtt
, &new_bitrate
));
116 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(10));
117 EXPECT_TRUE(congestion_control_
.OnAck(rtt
, &new_bitrate
));
118 expected_bitrate
+= 1500 * 8 * 20 / kRttMs
;
119 EXPECT_EQ(expected_bitrate
, new_bitrate
);
121 // Test long elapsed time (300 ms).
122 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(300));
123 EXPECT_TRUE(congestion_control_
.OnAck(rtt
, &new_bitrate
));
124 expected_bitrate
+= 1500 * 8 * 100 / kRttMs
;
125 EXPECT_EQ(expected_bitrate
, new_bitrate
);
127 // Test many short elapsed time (1 ms).
128 for (int i
= 0; i
< 19; ++i
) {
129 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(1));
130 EXPECT_FALSE(congestion_control_
.OnAck(rtt
, &new_bitrate
));
132 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(1));
133 EXPECT_TRUE(congestion_control_
.OnAck(rtt
, &new_bitrate
));
134 expected_bitrate
+= 1500 * 8 * 20 / kRttMs
;
135 EXPECT_EQ(expected_bitrate
, new_bitrate
);