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; |
| 22 | |
| 23 | namespace webrtc { |
| 24 | namespace test { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | constexpr int kMinBitrateBps = 100; |
| 29 | constexpr int kStartBitrateBps = 300; |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 30 | constexpr int kMaxBitrateBps = 10000; |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 31 | |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 32 | constexpr int kExponentialProbingTimeoutMs = 5000; |
| 33 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 34 | } // namespace |
| 35 | |
| 36 | class ProbeControllerTest : public ::testing::Test { |
| 37 | protected: |
| 38 | ProbeControllerTest() : clock_(0) { |
| 39 | probe_controller_.reset(new ProbeController(&pacer_, &clock_)); |
| 40 | } |
| 41 | ~ProbeControllerTest() override {} |
| 42 | |
| 43 | SimulatedClock clock_; |
| 44 | NiceMock<MockPacedSender> pacer_; |
| 45 | std::unique_ptr<ProbeController> probe_controller_; |
| 46 | }; |
| 47 | |
| 48 | TEST_F(ProbeControllerTest, InitiatesProbingAtStart) { |
| 49 | EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(AtLeast(2)); |
| 50 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 51 | kMaxBitrateBps); |
| 52 | } |
| 53 | |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame^] | 54 | TEST_F(ProbeControllerTest, ProbeOnlyWhenNetworkIsUp) { |
| 55 | probe_controller_->OnNetworkStateChanged(kNetworkDown); |
| 56 | EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(0); |
| 57 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 58 | kMaxBitrateBps); |
| 59 | |
| 60 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 61 | EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(AtLeast(2)); |
| 62 | probe_controller_->OnNetworkStateChanged(kNetworkUp); |
| 63 | } |
| 64 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 65 | TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncrease) { |
| 66 | EXPECT_CALL(pacer_, CreateProbeCluster(_, _)).Times(AtLeast(2)); |
| 67 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 68 | kMaxBitrateBps); |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 69 | // Long enough to time out exponential probing. |
| 70 | clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 71 | probe_controller_->SetEstimatedBitrate(kStartBitrateBps); |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 72 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 73 | EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100, _)); |
| 74 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 75 | kMaxBitrateBps + 100); |
| 76 | } |
| 77 | |
| 78 | TEST_F(ProbeControllerTest, TestExponentialProbing) { |
| 79 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 80 | kMaxBitrateBps); |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 81 | |
| 82 | |
| 83 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 84 | EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800, _)); |
| 85 | probe_controller_->SetEstimatedBitrate(1800); |
| 86 | } |
| 87 | |
| 88 | TEST_F(ProbeControllerTest, TestExponentialProbingTimeout) { |
| 89 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 90 | kMaxBitrateBps); |
| 91 | |
| 92 | // 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] | 93 | clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 94 | EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800, _)).Times(0); |
| 95 | probe_controller_->SetEstimatedBitrate(1800); |
| 96 | } |
| 97 | |
| 98 | } // namespace test |
| 99 | } // namespace webrtc |