blob: 61ca559a5364ae928ff5d8d023700f0d052e4671 [file] [log] [blame]
Irfan Sheriffb2540bb2016-09-12 12:28:54 -07001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10#include <memory>
11
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070012#include "webrtc/base/logging.h"
13#include "webrtc/modules/congestion_controller/probe_controller.h"
14#include "webrtc/modules/pacing/mock/mock_paced_sender.h"
15#include "webrtc/system_wrappers/include/clock.h"
kwibergac9f8762016-09-30 22:29:43 -070016#include "webrtc/test/gmock.h"
17#include "webrtc/test/gtest.h"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070018
19using testing::_;
20using testing::AtLeast;
21using testing::NiceMock;
sergeyu80ed35e2016-11-28 13:11:13 -080022using testing::Return;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070023
24namespace webrtc {
25namespace test {
26
27namespace {
28
29constexpr int kMinBitrateBps = 100;
30constexpr int kStartBitrateBps = 300;
sergeyu07c147d2016-11-03 11:59:50 -070031constexpr int kMaxBitrateBps = 10000;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070032
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070033constexpr int kExponentialProbingTimeoutMs = 5000;
34
sergeyu80ed35e2016-11-28 13:11:13 -080035constexpr int kAlrProbeInterval = 5000;
36
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070037} // namespace
38
39class ProbeControllerTest : public ::testing::Test {
40 protected:
sergeyu80ed35e2016-11-28 13:11:13 -080041 ProbeControllerTest() : clock_(100000000L) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070042 probe_controller_.reset(new ProbeController(&pacer_, &clock_));
43 }
44 ~ProbeControllerTest() override {}
45
46 SimulatedClock clock_;
47 NiceMock<MockPacedSender> pacer_;
48 std::unique_ptr<ProbeController> probe_controller_;
49};
50
51TEST_F(ProbeControllerTest, InitiatesProbingAtStart) {
52 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(AtLeast(2));
53 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
54 kMaxBitrateBps);
55}
56
Sergey Ulanove2b15012016-11-22 16:08:30 -080057TEST_F(ProbeControllerTest, ProbeOnlyWhenNetworkIsUp) {
58 probe_controller_->OnNetworkStateChanged(kNetworkDown);
59 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(0);
60 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
61 kMaxBitrateBps);
62
63 testing::Mock::VerifyAndClearExpectations(&pacer_);
64 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(AtLeast(2));
65 probe_controller_->OnNetworkStateChanged(kNetworkUp);
66}
67
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070068TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncrease) {
69 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(AtLeast(2));
70 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
71 kMaxBitrateBps);
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070072 // Long enough to time out exponential probing.
73 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070074 probe_controller_->SetEstimatedBitrate(kStartBitrateBps);
sergeyu9cef11b2016-12-02 11:03:01 -080075 probe_controller_->Process();
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070076
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070077 EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100, _));
78 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
79 kMaxBitrateBps + 100);
80}
81
82TEST_F(ProbeControllerTest, TestExponentialProbing) {
83 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
84 kMaxBitrateBps);
sergeyu07c147d2016-11-03 11:59:50 -070085
sergeyu9cef11b2016-12-02 11:03:01 -080086 // Repeated probe should only be sent when estimated bitrate climbs above 4 *
87 // kStartBitrateBps = 1200.
88 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(0);
89 probe_controller_->SetEstimatedBitrate(1000);
90 testing::Mock::VerifyAndClearExpectations(&pacer_);
91
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070092 EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800, _));
93 probe_controller_->SetEstimatedBitrate(1800);
94}
95
96TEST_F(ProbeControllerTest, TestExponentialProbingTimeout) {
97 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
98 kMaxBitrateBps);
99
100 // Advance far enough to cause a time out in waiting for probing result.
Irfan Sheriff9b7b7532016-09-16 11:30:44 -0700101 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
sergeyu9cef11b2016-12-02 11:03:01 -0800102 probe_controller_->Process();
103
104 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(0);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700105 probe_controller_->SetEstimatedBitrate(1800);
106}
107
sergeyu80ed35e2016-11-28 13:11:13 -0800108TEST_F(ProbeControllerTest, ProbeAfterEstimateDropInAlr) {
109 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(2);
110 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
111 kMaxBitrateBps);
112 probe_controller_->SetEstimatedBitrate(500);
113 testing::Mock::VerifyAndClearExpectations(&pacer_);
114
115 // When bandwidth estimate drops the controller should send a probe at the
116 // previous bitrate.
117 EXPECT_CALL(pacer_, CreateProbeCluster(500, _)).Times(1);
118 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
119 .WillRepeatedly(
120 Return(rtc::Optional<int64_t>(clock_.TimeInMilliseconds())));
121 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
sergeyu9cef11b2016-12-02 11:03:01 -0800122 probe_controller_->Process();
sergeyu80ed35e2016-11-28 13:11:13 -0800123 probe_controller_->SetEstimatedBitrate(50);
124}
125
126TEST_F(ProbeControllerTest, PeriodicProbing) {
127 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(2);
128 probe_controller_->EnablePeriodicAlrProbing(true);
129 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
130 kMaxBitrateBps);
131 probe_controller_->SetEstimatedBitrate(500);
132 testing::Mock::VerifyAndClearExpectations(&pacer_);
133
134 int64_t start_time = clock_.TimeInMilliseconds();
135
136 // Expect the controller to send a new probe after 5s has passed.
137 EXPECT_CALL(pacer_, CreateProbeCluster(1000, _)).Times(1);
138 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
139 .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time)));
140 clock_.AdvanceTimeMilliseconds(5000);
141 probe_controller_->Process();
142 probe_controller_->SetEstimatedBitrate(500);
143 testing::Mock::VerifyAndClearExpectations(&pacer_);
144
145 // The following probe should be sent at 10s into ALR.
146 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(0);
147 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
148 .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time)));
149 clock_.AdvanceTimeMilliseconds(4000);
150 probe_controller_->Process();
151 probe_controller_->SetEstimatedBitrate(500);
152 testing::Mock::VerifyAndClearExpectations(&pacer_);
153
154 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(1);
155 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
156 .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time)));
157 clock_.AdvanceTimeMilliseconds(1000);
158 probe_controller_->Process();
159 probe_controller_->SetEstimatedBitrate(500);
160 testing::Mock::VerifyAndClearExpectations(&pacer_);
161}
162
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700163} // namespace test
164} // namespace webrtc