blob: 3c43cfebbbb89f2929c1c9c22c79ae2b4ad69c87 [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);
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070075
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070076 EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100, _));
77 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
78 kMaxBitrateBps + 100);
79}
80
81TEST_F(ProbeControllerTest, TestExponentialProbing) {
82 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
83 kMaxBitrateBps);
sergeyu07c147d2016-11-03 11:59:50 -070084
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070085 EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800, _));
86 probe_controller_->SetEstimatedBitrate(1800);
87}
88
89TEST_F(ProbeControllerTest, TestExponentialProbingTimeout) {
90 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
91 kMaxBitrateBps);
92
93 // Advance far enough to cause a time out in waiting for probing result.
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070094 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070095 EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800, _)).Times(0);
96 probe_controller_->SetEstimatedBitrate(1800);
97}
98
sergeyu80ed35e2016-11-28 13:11:13 -080099TEST_F(ProbeControllerTest, ProbeAfterEstimateDropInAlr) {
100 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(2);
101 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
102 kMaxBitrateBps);
103 probe_controller_->SetEstimatedBitrate(500);
104 testing::Mock::VerifyAndClearExpectations(&pacer_);
105
106 // When bandwidth estimate drops the controller should send a probe at the
107 // previous bitrate.
108 EXPECT_CALL(pacer_, CreateProbeCluster(500, _)).Times(1);
109 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
110 .WillRepeatedly(
111 Return(rtc::Optional<int64_t>(clock_.TimeInMilliseconds())));
112 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
113 probe_controller_->SetEstimatedBitrate(50);
114}
115
116TEST_F(ProbeControllerTest, PeriodicProbing) {
117 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(2);
118 probe_controller_->EnablePeriodicAlrProbing(true);
119 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
120 kMaxBitrateBps);
121 probe_controller_->SetEstimatedBitrate(500);
122 testing::Mock::VerifyAndClearExpectations(&pacer_);
123
124 int64_t start_time = clock_.TimeInMilliseconds();
125
126 // Expect the controller to send a new probe after 5s has passed.
127 EXPECT_CALL(pacer_, CreateProbeCluster(1000, _)).Times(1);
128 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
129 .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time)));
130 clock_.AdvanceTimeMilliseconds(5000);
131 probe_controller_->Process();
132 probe_controller_->SetEstimatedBitrate(500);
133 testing::Mock::VerifyAndClearExpectations(&pacer_);
134
135 // The following probe should be sent at 10s into ALR.
136 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(0);
137 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
138 .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time)));
139 clock_.AdvanceTimeMilliseconds(4000);
140 probe_controller_->Process();
141 probe_controller_->SetEstimatedBitrate(500);
142 testing::Mock::VerifyAndClearExpectations(&pacer_);
143
144 EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(1);
145 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
146 .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time)));
147 clock_.AdvanceTimeMilliseconds(1000);
148 probe_controller_->Process();
149 probe_controller_->SetEstimatedBitrate(500);
150 testing::Mock::VerifyAndClearExpectations(&pacer_);
151}
152
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700153} // namespace test
154} // namespace webrtc