- add sources.
[platform/framework/web/crosswalk.git] / src / media / cast / congestion_control / congestion_control_unittest.cc
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.
4
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"
9
10 namespace media {
11 namespace cast {
12
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
20 class CongestionControlTest : public ::testing::Test {
21  protected:
22   CongestionControlTest()
23       : congestion_control_(&testing_clock_,
24                             kDefaultCongestionControlBackOff,
25                             kMaxBitrateConfigured,
26                             kMinBitrateConfigured,
27                             kStartBitrate) {
28     testing_clock_.Advance(
29         base::TimeDelta::FromMilliseconds(kStartMillisecond));
30   }
31
32   base::SimpleTestTickClock testing_clock_;
33   CongestionControl congestion_control_;
34 };
35
36 TEST_F(CongestionControlTest, Max) {
37   uint32 new_bitrate = 0;
38   base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(kRttMs);
39   base::TimeDelta ack_rate = base::TimeDelta::FromMilliseconds(kAckRateMs);
40   EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
41
42   uint32 expected_increase_bitrate = 0;
43
44   // Expected time is 5 seconds. 500000 - 2000000 = 5 * 1500 * 8 * (1000 / 20).
45   for (int i = 0; i < 151; ++i) {
46     testing_clock_.Advance(ack_rate);
47     EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
48     expected_increase_bitrate += 1500 * 8 * kAckRateMs / kRttMs;
49     EXPECT_EQ(kStartBitrate + expected_increase_bitrate, new_bitrate);
50   }
51   testing_clock_.Advance(ack_rate);
52   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
53   EXPECT_EQ(kMaxBitrateConfigured, new_bitrate);
54 }
55
56 TEST_F(CongestionControlTest, Min) {
57   uint32 new_bitrate = 0;
58   base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(kRttMs);
59   base::TimeDelta ack_rate = base::TimeDelta::FromMilliseconds(kAckRateMs);
60   EXPECT_FALSE(congestion_control_.OnNack(rtt, &new_bitrate));
61
62   uint32 expected_decrease_bitrate = kStartBitrate;
63
64   // Expected number is 10. 2000 * 0.875^10 <= 500.
65   for (int i = 0; i < 10; ++i) {
66     testing_clock_.Advance(ack_rate);
67      EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
68      expected_decrease_bitrate = static_cast<uint32>(
69          expected_decrease_bitrate * kDefaultCongestionControlBackOff);
70      EXPECT_EQ(expected_decrease_bitrate, new_bitrate);
71    }
72    testing_clock_.Advance(ack_rate);
73    EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
74    EXPECT_EQ(kMinBitrateConfigured, new_bitrate);
75 }
76
77 TEST_F(CongestionControlTest, Timing) {
78   base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(kRttMs);
79   base::TimeDelta ack_rate = base::TimeDelta::FromMilliseconds(kAckRateMs);
80   uint32 new_bitrate = 0;
81   uint32 expected_bitrate = kStartBitrate;
82
83   EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
84
85   testing_clock_.Advance(ack_rate);
86   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
87   expected_bitrate += 1500 * 8 * kAckRateMs / kRttMs;
88   EXPECT_EQ(expected_bitrate, new_bitrate);
89
90   // We should back immediately.
91   EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
92   expected_bitrate = static_cast<uint32>(
93       expected_bitrate * kDefaultCongestionControlBackOff);
94   EXPECT_EQ(expected_bitrate, new_bitrate);
95
96   // Less than one RTT have passed don't back again.
97   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
98   EXPECT_FALSE(congestion_control_.OnNack(rtt, &new_bitrate));
99
100   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
101   EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
102   expected_bitrate = static_cast<uint32>(
103       expected_bitrate * kDefaultCongestionControlBackOff);
104   EXPECT_EQ(expected_bitrate, new_bitrate);
105
106   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
107   EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
108   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
109   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
110   expected_bitrate += 1500 * 8 * 20 / kRttMs;
111   EXPECT_EQ(expected_bitrate, new_bitrate);
112
113   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
114   EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
115   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
116   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
117   expected_bitrate += 1500 * 8 * 20 / kRttMs;
118   EXPECT_EQ(expected_bitrate, new_bitrate);
119
120   // Test long elapsed time (300 ms).
121   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(300));
122   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
123   expected_bitrate += 1500 * 8 * 100 / kRttMs;
124   EXPECT_EQ(expected_bitrate, new_bitrate);
125
126   // Test many short elapsed time (1 ms).
127   for (int i = 0; i < 19; ++i) {
128     testing_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
129     EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
130   }
131   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
132   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
133   expected_bitrate += 1500 * 8 * 20 / kRttMs;
134   EXPECT_EQ(expected_bitrate, new_bitrate);
135 }
136
137 }  // namespace cast
138 }  // namespace media