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) { |
| 52 | EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(AtLeast(2)); |
| 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); |
| 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 Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 68 | TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncrease) { |
| 69 | EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(AtLeast(2)); |
| 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); |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 75 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 76 | EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100, _)); |
| 77 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 78 | kMaxBitrateBps + 100); |
| 79 | } |
| 80 | |
| 81 | TEST_F(ProbeControllerTest, TestExponentialProbing) { |
| 82 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 83 | kMaxBitrateBps); |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 84 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 85 | EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800, _)); |
| 86 | probe_controller_->SetEstimatedBitrate(1800); |
| 87 | } |
| 88 | |
| 89 | TEST_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 Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 94 | clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 95 | EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800, _)).Times(0); |
| 96 | probe_controller_->SetEstimatedBitrate(1800); |
| 97 | } |
| 98 | |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 99 | TEST_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 | |
| 116 | TEST_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 Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 153 | } // namespace test |
| 154 | } // namespace webrtc |