Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 1 | /* |
| 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 Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 12 | #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" |
kwiberg | ac9f876 | 2016-09-30 22:29:43 -0700 | [diff] [blame] | 16 | #include "webrtc/test/gmock.h" |
| 17 | #include "webrtc/test/gtest.h" |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 18 | |
| 19 | using testing::_; |
| 20 | using testing::AtLeast; |
| 21 | using testing::NiceMock; |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 22 | using testing::Return; |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | namespace test { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | constexpr int kMinBitrateBps = 100; |
| 30 | constexpr int kStartBitrateBps = 300; |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 31 | constexpr int kMaxBitrateBps = 10000; |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 32 | |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 33 | constexpr int kExponentialProbingTimeoutMs = 5000; |
| 34 | |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 35 | constexpr int kAlrProbeInterval = 5000; |
| 36 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 37 | } // namespace |
| 38 | |
| 39 | class ProbeControllerTest : public ::testing::Test { |
| 40 | protected: |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 41 | ProbeControllerTest() : clock_(100000000L) { |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 42 | 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 | |
| 51 | TEST_F(ProbeControllerTest, InitiatesProbingAtStart) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 52 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 53 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 54 | kMaxBitrateBps); |
| 55 | } |
| 56 | |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 57 | TEST_F(ProbeControllerTest, ProbeOnlyWhenNetworkIsUp) { |
| 58 | probe_controller_->OnNetworkStateChanged(kNetworkDown); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 59 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 60 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 61 | kMaxBitrateBps); |
| 62 | |
| 63 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 64 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2)); |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 65 | probe_controller_->OnNetworkStateChanged(kNetworkUp); |
| 66 | } |
| 67 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 68 | TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncrease) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 69 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 70 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 71 | kMaxBitrateBps); |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 72 | // Long enough to time out exponential probing. |
| 73 | clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 74 | probe_controller_->SetEstimatedBitrate(kStartBitrateBps); |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 75 | probe_controller_->Process(); |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 76 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 77 | EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 78 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 79 | kMaxBitrateBps + 100); |
| 80 | } |
| 81 | |
| 82 | TEST_F(ProbeControllerTest, TestExponentialProbing) { |
| 83 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 84 | kMaxBitrateBps); |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 85 | |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 86 | // Repeated probe should only be sent when estimated bitrate climbs above |
| 87 | // 0.7 * 6 * kStartBitrateBps = 1260. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 88 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 89 | probe_controller_->SetEstimatedBitrate(1000); |
| 90 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 91 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 92 | EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 93 | probe_controller_->SetEstimatedBitrate(1800); |
| 94 | } |
| 95 | |
| 96 | TEST_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 Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 101 | clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs); |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 102 | probe_controller_->Process(); |
| 103 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 104 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 105 | probe_controller_->SetEstimatedBitrate(1800); |
| 106 | } |
| 107 | |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 108 | TEST_F(ProbeControllerTest, ProbeAfterEstimateDropInAlr) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 109 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 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. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 117 | EXPECT_CALL(pacer_, CreateProbeCluster(500)).Times(1); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 118 | EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime()) |
| 119 | .WillRepeatedly( |
| 120 | Return(rtc::Optional<int64_t>(clock_.TimeInMilliseconds()))); |
| 121 | clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1); |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 122 | probe_controller_->Process(); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 123 | probe_controller_->SetEstimatedBitrate(50); |
| 124 | } |
| 125 | |
| 126 | TEST_F(ProbeControllerTest, PeriodicProbing) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 127 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 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. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 137 | EXPECT_CALL(pacer_, CreateProbeCluster(1000)).Times(1); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 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. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 146 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 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 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 154 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(1); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 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 | |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 163 | TEST_F(ProbeControllerTest, TestExponentialProbingOverflow) { |
| 164 | const int64_t kMbpsMultiplier = 1000000; |
| 165 | probe_controller_->SetBitrates(kMinBitrateBps, 10 * kMbpsMultiplier, |
| 166 | 100 * kMbpsMultiplier); |
| 167 | |
| 168 | // Verify that probe bitrate is capped at the specified max bitrate |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 169 | EXPECT_CALL(pacer_, CreateProbeCluster(100 * kMbpsMultiplier)); |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 170 | probe_controller_->SetEstimatedBitrate(60 * kMbpsMultiplier); |
| 171 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 172 | |
| 173 | // Verify that repeated probes aren't sent. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 174 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 175 | probe_controller_->SetEstimatedBitrate(100 * kMbpsMultiplier); |
| 176 | } |
| 177 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 178 | } // namespace test |
| 179 | } // namespace webrtc |