rtt_tracker_unittest.cc (2468B)
1 /* 2 * Copyright (c) 2018 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 11 #include "modules/congestion_controller/pcc/rtt_tracker.h" 12 13 #include "api/transport/network_types.h" 14 #include "api/units/time_delta.h" 15 #include "api/units/timestamp.h" 16 #include "test/gtest.h" 17 18 namespace webrtc { 19 namespace pcc { 20 namespace test { 21 namespace { 22 constexpr TimeDelta kInitialRtt = TimeDelta::Micros(10); 23 constexpr double kAlpha = 0.9; 24 constexpr Timestamp kStartTime = Timestamp::Seconds(0); 25 26 PacketResult GetPacketWithRtt(TimeDelta rtt) { 27 SentPacket packet; 28 packet.send_time = kStartTime; 29 PacketResult packet_result; 30 packet_result.sent_packet = packet; 31 if (rtt.IsFinite()) { 32 packet_result.receive_time = kStartTime + rtt; 33 } else { 34 packet_result.receive_time = Timestamp::PlusInfinity(); 35 } 36 return packet_result; 37 } 38 } // namespace 39 40 TEST(PccRttTrackerTest, InitialValue) { 41 RttTracker tracker{kInitialRtt, kAlpha}; 42 EXPECT_EQ(kInitialRtt, tracker.GetRtt()); 43 for (int i = 0; i < 100; ++i) { 44 tracker.OnPacketsFeedback({GetPacketWithRtt(kInitialRtt)}, 45 kStartTime + kInitialRtt); 46 } 47 EXPECT_EQ(kInitialRtt, tracker.GetRtt()); 48 } 49 50 TEST(PccRttTrackerTest, DoNothingWhenPacketIsLost) { 51 RttTracker tracker{kInitialRtt, kAlpha}; 52 tracker.OnPacketsFeedback({GetPacketWithRtt(TimeDelta::PlusInfinity())}, 53 kStartTime + kInitialRtt); 54 EXPECT_EQ(tracker.GetRtt(), kInitialRtt); 55 } 56 57 TEST(PccRttTrackerTest, ChangeInRtt) { 58 RttTracker tracker{kInitialRtt, kAlpha}; 59 const TimeDelta kNewRtt = TimeDelta::Micros(100); 60 tracker.OnPacketsFeedback({GetPacketWithRtt(kNewRtt)}, kStartTime + kNewRtt); 61 EXPECT_GT(tracker.GetRtt(), kInitialRtt); 62 EXPECT_LE(tracker.GetRtt(), kNewRtt); 63 for (int i = 0; i < 100; ++i) { 64 tracker.OnPacketsFeedback({GetPacketWithRtt(kNewRtt)}, 65 kStartTime + kNewRtt); 66 } 67 const TimeDelta absolute_error = TimeDelta::Micros(1); 68 EXPECT_NEAR(tracker.GetRtt().us(), kNewRtt.us(), absolute_error.us()); 69 EXPECT_LE(tracker.GetRtt(), kNewRtt); 70 } 71 72 } // namespace test 73 } // namespace pcc 74 } // namespace webrtc