1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/congestion_control/time_loss_algorithm.h"
9 #include "base/logging.h"
10 #include "base/stl_util.h"
11 #include "net/quic/congestion_control/rtt_stats.h"
12 #include "net/quic/quic_ack_notifier_manager.h"
13 #include "net/quic/quic_unacked_packet_map.h"
14 #include "net/quic/test_tools/mock_clock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
23 // Default packet length.
24 const uint32 kDefaultLength
= 1000;
26 class TimeLossAlgorithmTest
: public ::testing::Test
{
28 TimeLossAlgorithmTest() : unacked_packets_(&ack_notifier_manager_
) {
29 rtt_stats_
.UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
30 QuicTime::Delta::Zero(),
34 ~TimeLossAlgorithmTest() override
{
35 STLDeleteElements(&packets_
);
38 void SendDataPacket(QuicPacketSequenceNumber sequence_number
) {
39 packets_
.push_back(new QuicEncryptedPacket(nullptr, kDefaultLength
));
40 SerializedPacket
packet(
41 sequence_number
, PACKET_1BYTE_SEQUENCE_NUMBER
, packets_
.back(), 0,
42 new RetransmittableFrames(ENCRYPTION_NONE
), false, false);
43 unacked_packets_
.AddSentPacket(packet
, 0, NOT_RETRANSMISSION
, clock_
.Now(),
47 void VerifyLosses(QuicPacketSequenceNumber largest_observed
,
48 QuicPacketSequenceNumber
* losses_expected
,
50 SequenceNumberSet lost_packets
=
51 loss_algorithm_
.DetectLostPackets(
52 unacked_packets_
, clock_
.Now(), largest_observed
, rtt_stats_
);
53 EXPECT_EQ(num_losses
, lost_packets
.size());
54 for (size_t i
= 0; i
< num_losses
; ++i
) {
55 EXPECT_TRUE(ContainsKey(lost_packets
, losses_expected
[i
]));
59 vector
<QuicEncryptedPacket
*> packets_
;
60 AckNotifierManager ack_notifier_manager_
;
61 QuicUnackedPacketMap unacked_packets_
;
62 TimeLossAlgorithm loss_algorithm_
;
67 TEST_F(TimeLossAlgorithmTest
, NoLossFor500Nacks
) {
68 const size_t kNumSentPackets
= 5;
69 // Transmit 5 packets.
70 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
73 unacked_packets_
.RemoveFromInFlight(2);
74 for (size_t i
= 1; i
< 500; ++i
) {
75 unacked_packets_
.NackPacket(1, i
);
76 VerifyLosses(2, nullptr, 0);
78 EXPECT_EQ(rtt_stats_
.smoothed_rtt().Multiply(1.25),
79 loss_algorithm_
.GetLossTimeout().Subtract(clock_
.Now()));
82 TEST_F(TimeLossAlgorithmTest
, NoLossUntilTimeout
) {
83 const size_t kNumSentPackets
= 10;
84 // Transmit 10 packets at 1/10th an RTT interval.
85 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
87 clock_
.AdvanceTime(rtt_stats_
.smoothed_rtt().Multiply(0.1));
89 // Expect the timer to not be set.
90 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_
.GetLossTimeout());
91 // The packet should not be lost until 1.25 RTTs pass.
92 unacked_packets_
.NackPacket(1, 1);
93 unacked_packets_
.RemoveFromInFlight(2);
94 VerifyLosses(2, nullptr, 0);
95 // Expect the timer to be set to 0.25 RTT's in the future.
96 EXPECT_EQ(rtt_stats_
.smoothed_rtt().Multiply(0.25),
97 loss_algorithm_
.GetLossTimeout().Subtract(clock_
.Now()));
98 unacked_packets_
.NackPacket(1, 5);
99 VerifyLosses(2, nullptr, 0);
100 clock_
.AdvanceTime(rtt_stats_
.smoothed_rtt().Multiply(0.25));
101 QuicPacketSequenceNumber lost
[] = { 1 };
102 VerifyLosses(2, lost
, arraysize(lost
));
103 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_
.GetLossTimeout());
106 TEST_F(TimeLossAlgorithmTest
, NoLossWithoutNack
) {
107 const size_t kNumSentPackets
= 10;
108 // Transmit 10 packets at 1/10th an RTT interval.
109 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
111 clock_
.AdvanceTime(rtt_stats_
.smoothed_rtt().Multiply(0.1));
113 // Expect the timer to not be set.
114 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_
.GetLossTimeout());
115 // The packet should not be lost without a nack.
116 unacked_packets_
.RemoveFromInFlight(1);
117 VerifyLosses(1, nullptr, 0);
118 // The timer should still not be set.
119 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_
.GetLossTimeout());
120 clock_
.AdvanceTime(rtt_stats_
.smoothed_rtt().Multiply(0.25));
121 VerifyLosses(1, nullptr, 0);
122 clock_
.AdvanceTime(rtt_stats_
.smoothed_rtt());
123 VerifyLosses(1, nullptr, 0);
125 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_
.GetLossTimeout());
128 TEST_F(TimeLossAlgorithmTest
, MultipleLossesAtOnce
) {
129 const size_t kNumSentPackets
= 10;
130 // Transmit 10 packets at once and then go forward an RTT.
131 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
134 clock_
.AdvanceTime(rtt_stats_
.smoothed_rtt());
135 // Expect the timer to not be set.
136 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_
.GetLossTimeout());
137 // The packet should not be lost until 1.25 RTTs pass.
138 for (size_t i
= 1; i
< kNumSentPackets
; ++i
) {
139 unacked_packets_
.NackPacket(i
, 1);
141 unacked_packets_
.RemoveFromInFlight(10);
142 VerifyLosses(10, nullptr, 0);
143 // Expect the timer to be set to 0.25 RTT's in the future.
144 EXPECT_EQ(rtt_stats_
.smoothed_rtt().Multiply(0.25),
145 loss_algorithm_
.GetLossTimeout().Subtract(clock_
.Now()));
146 clock_
.AdvanceTime(rtt_stats_
.smoothed_rtt().Multiply(0.25));
147 QuicPacketSequenceNumber lost
[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
148 VerifyLosses(10, lost
, arraysize(lost
));
149 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_
.GetLossTimeout());