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/quic_unacked_packet_map.h"
7 #include "net/quic/test_tools/quic_test_utils.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 // Default packet length.
15 const uint32 kDefaultAckLength
= 50;
16 const uint32 kDefaultLength
= 1000;
18 class QuicUnackedPacketMapTest
: public ::testing::Test
{
20 QuicUnackedPacketMapTest()
21 : now_(QuicTime::Zero().Add(QuicTime::Delta::FromMilliseconds(1000))) {
24 SerializedPacket
CreateRetransmittablePacket(
25 QuicPacketSequenceNumber sequence_number
) {
26 return SerializedPacket(sequence_number
, PACKET_1BYTE_SEQUENCE_NUMBER
, NULL
,
27 0, new RetransmittableFrames());
30 SerializedPacket
CreateNonRetransmittablePacket(
31 QuicPacketSequenceNumber sequence_number
) {
32 return SerializedPacket(
33 sequence_number
, PACKET_1BYTE_SEQUENCE_NUMBER
, NULL
, 0, NULL
);
36 void VerifyInFlightPackets(QuicPacketSequenceNumber
* packets
,
38 if (num_packets
== 0) {
39 EXPECT_FALSE(unacked_packets_
.HasInFlightPackets());
40 EXPECT_FALSE(unacked_packets_
.HasMultipleInFlightPackets());
43 if (num_packets
== 1) {
44 EXPECT_TRUE(unacked_packets_
.HasInFlightPackets());
45 EXPECT_FALSE(unacked_packets_
.HasMultipleInFlightPackets());
46 ASSERT_TRUE(unacked_packets_
.IsUnacked(packets
[0]));
47 EXPECT_TRUE(unacked_packets_
.GetTransmissionInfo(packets
[0]).in_flight
);
49 for (size_t i
= 0; i
< num_packets
; ++i
) {
50 ASSERT_TRUE(unacked_packets_
.IsUnacked(packets
[i
]));
51 EXPECT_TRUE(unacked_packets_
.GetTransmissionInfo(packets
[i
]).in_flight
);
53 size_t in_flight_count
= 0;
54 for (QuicUnackedPacketMap::const_iterator it
= unacked_packets_
.begin();
55 it
!= unacked_packets_
.end(); ++it
) {
56 if (it
->second
.in_flight
) {
60 EXPECT_EQ(num_packets
, in_flight_count
);
63 void VerifyUnackedPackets(QuicPacketSequenceNumber
* packets
,
65 if (num_packets
== 0) {
66 EXPECT_FALSE(unacked_packets_
.HasUnackedPackets());
67 EXPECT_FALSE(unacked_packets_
.HasUnackedRetransmittableFrames());
70 EXPECT_TRUE(unacked_packets_
.HasUnackedPackets());
71 for (size_t i
= 0; i
< num_packets
; ++i
) {
72 EXPECT_TRUE(unacked_packets_
.IsUnacked(packets
[i
])) << packets
[i
];
74 size_t unacked_count
= 0;
75 for (QuicUnackedPacketMap::const_iterator it
= unacked_packets_
.begin();
76 it
!= unacked_packets_
.end(); ++it
) {
79 EXPECT_EQ(num_packets
, unacked_count
);
82 void VerifyRetransmittablePackets(QuicPacketSequenceNumber
* packets
,
84 size_t num_retransmittable_packets
= 0;
85 for (QuicUnackedPacketMap::const_iterator it
= unacked_packets_
.begin();
86 it
!= unacked_packets_
.end(); ++it
) {
87 if (it
->second
.retransmittable_frames
!= NULL
) {
88 ++num_retransmittable_packets
;
91 EXPECT_EQ(num_packets
, num_retransmittable_packets
);
92 for (size_t i
= 0; i
< num_packets
; ++i
) {
93 EXPECT_TRUE(unacked_packets_
.HasRetransmittableFrames(packets
[i
]))
94 << " packets[" << i
<< "]:" << packets
[i
];
98 QuicUnackedPacketMap unacked_packets_
;
102 TEST_F(QuicUnackedPacketMapTest
, RttOnly
) {
103 // Acks are only tracked for RTT measurement purposes.
104 unacked_packets_
.AddPacket(CreateNonRetransmittablePacket(1));
105 unacked_packets_
.SetSent(1, now_
, kDefaultAckLength
, false);
107 QuicPacketSequenceNumber unacked
[] = { 1 };
108 VerifyUnackedPackets(unacked
, arraysize(unacked
));
109 VerifyInFlightPackets(NULL
, 0);
110 VerifyRetransmittablePackets(NULL
, 0);
112 unacked_packets_
.IncreaseLargestObserved(1);
113 VerifyUnackedPackets(NULL
, 0);
114 VerifyInFlightPackets(NULL
, 0);
115 VerifyRetransmittablePackets(NULL
, 0);
118 TEST_F(QuicUnackedPacketMapTest
, RetransmittableInflightAndRtt
) {
119 // Simulate a retransmittable packet being sent and acked.
120 unacked_packets_
.AddPacket(CreateRetransmittablePacket(1));
121 unacked_packets_
.SetSent(1, now_
, kDefaultLength
, true);
123 QuicPacketSequenceNumber unacked
[] = { 1 };
124 VerifyUnackedPackets(unacked
, arraysize(unacked
));
125 VerifyInFlightPackets(unacked
, arraysize(unacked
));
126 VerifyRetransmittablePackets(unacked
, arraysize(unacked
));
128 unacked_packets_
.RemoveRetransmittability(1);
129 VerifyUnackedPackets(unacked
, arraysize(unacked
));
130 VerifyInFlightPackets(unacked
, arraysize(unacked
));
131 VerifyRetransmittablePackets(NULL
, 0);
133 unacked_packets_
.IncreaseLargestObserved(1);
134 VerifyUnackedPackets(unacked
, arraysize(unacked
));
135 VerifyInFlightPackets(unacked
, arraysize(unacked
));
136 VerifyRetransmittablePackets(NULL
, 0);
138 unacked_packets_
.RemoveFromInFlight(1);
139 VerifyUnackedPackets(NULL
, 0);
140 VerifyInFlightPackets(NULL
, 0);
141 VerifyRetransmittablePackets(NULL
, 0);
144 TEST_F(QuicUnackedPacketMapTest
, RetransmittedPacket
) {
145 // Simulate a retransmittable packet being sent, retransmitted, and the first
146 // transmission being acked.
147 unacked_packets_
.AddPacket(CreateRetransmittablePacket(1));
148 unacked_packets_
.SetSent(1, now_
, kDefaultLength
, true);
149 unacked_packets_
.OnRetransmittedPacket(1, 2, LOSS_RETRANSMISSION
);
150 unacked_packets_
.SetSent(2, now_
, kDefaultLength
, true);
152 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
153 VerifyUnackedPackets(unacked
, arraysize(unacked
));
154 VerifyInFlightPackets(unacked
, arraysize(unacked
));
155 QuicPacketSequenceNumber retransmittable
[] = { 2 };
156 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
158 unacked_packets_
.RemoveRetransmittability(1);
159 VerifyUnackedPackets(unacked
, arraysize(unacked
));
160 VerifyInFlightPackets(unacked
, arraysize(unacked
));
161 VerifyRetransmittablePackets(NULL
, 0);
163 unacked_packets_
.IncreaseLargestObserved(2);
164 VerifyUnackedPackets(unacked
, arraysize(unacked
));
165 VerifyInFlightPackets(unacked
, arraysize(unacked
));
166 VerifyRetransmittablePackets(NULL
, 0);
168 unacked_packets_
.RemoveFromInFlight(2);
169 QuicPacketSequenceNumber unacked2
[] = { 1 };
170 VerifyUnackedPackets(unacked
, arraysize(unacked2
));
171 VerifyInFlightPackets(unacked
, arraysize(unacked2
));
172 VerifyRetransmittablePackets(NULL
, 0);
174 unacked_packets_
.RemoveFromInFlight(1);
175 VerifyUnackedPackets(NULL
, 0);
176 VerifyInFlightPackets(NULL
, 0);
177 VerifyRetransmittablePackets(NULL
, 0);
180 TEST_F(QuicUnackedPacketMapTest
, RetransmitThreeTimes
) {
181 // Simulate a retransmittable packet being sent and retransmitted twice.
182 unacked_packets_
.AddPacket(CreateRetransmittablePacket(1));
183 unacked_packets_
.SetSent(1, now_
, kDefaultLength
, true);
184 unacked_packets_
.AddPacket(CreateRetransmittablePacket(2));
185 unacked_packets_
.SetSent(2, now_
, kDefaultLength
, true);
187 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
188 VerifyUnackedPackets(unacked
, arraysize(unacked
));
189 VerifyInFlightPackets(unacked
, arraysize(unacked
));
190 QuicPacketSequenceNumber retransmittable
[] = { 1, 2 };
191 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
193 // Early retransmit 1 as 3 and send new data as 4.
194 unacked_packets_
.IncreaseLargestObserved(2);
195 unacked_packets_
.RemoveFromInFlight(2);
196 unacked_packets_
.RemoveRetransmittability(2);
197 unacked_packets_
.RemoveFromInFlight(1);
198 unacked_packets_
.OnRetransmittedPacket(1, 3, LOSS_RETRANSMISSION
);
199 unacked_packets_
.SetSent(3, now_
, kDefaultLength
, true);
200 unacked_packets_
.AddPacket(CreateRetransmittablePacket(4));
201 unacked_packets_
.SetSent(4, now_
, kDefaultLength
, true);
203 QuicPacketSequenceNumber unacked2
[] = { 1, 3, 4 };
204 VerifyUnackedPackets(unacked2
, arraysize(unacked2
));
205 QuicPacketSequenceNumber pending2
[] = { 3, 4, };
206 VerifyInFlightPackets(pending2
, arraysize(pending2
));
207 QuicPacketSequenceNumber retransmittable2
[] = { 3, 4 };
208 VerifyRetransmittablePackets(retransmittable2
, arraysize(retransmittable2
));
210 // Early retransmit 3 (formerly 1) as 5, and remove 1 from unacked.
211 unacked_packets_
.IncreaseLargestObserved(4);
212 unacked_packets_
.RemoveFromInFlight(4);
213 unacked_packets_
.RemoveRetransmittability(4);
214 unacked_packets_
.OnRetransmittedPacket(3, 5, LOSS_RETRANSMISSION
);
215 unacked_packets_
.SetSent(5, now_
, kDefaultLength
, true);
216 unacked_packets_
.AddPacket(CreateRetransmittablePacket(6));
217 unacked_packets_
.SetSent(6, now_
, kDefaultLength
, true);
219 QuicPacketSequenceNumber unacked3
[] = { 3, 5, 6 };
220 VerifyUnackedPackets(unacked3
, arraysize(unacked3
));
221 QuicPacketSequenceNumber pending3
[] = { 3, 5, 6 };
222 VerifyInFlightPackets(pending3
, arraysize(pending3
));
223 QuicPacketSequenceNumber retransmittable3
[] = { 5, 6 };
224 VerifyRetransmittablePackets(retransmittable3
, arraysize(retransmittable3
));
226 // Early retransmit 5 as 7 and ensure in flight packet 3 is not removed.
227 unacked_packets_
.IncreaseLargestObserved(6);
228 unacked_packets_
.RemoveFromInFlight(6);
229 unacked_packets_
.RemoveRetransmittability(6);
230 unacked_packets_
.OnRetransmittedPacket(5, 7, LOSS_RETRANSMISSION
);
231 unacked_packets_
.SetSent(7, now_
, kDefaultLength
, true);
233 QuicPacketSequenceNumber unacked4
[] = { 3, 5, 7 };
234 VerifyUnackedPackets(unacked4
, arraysize(unacked4
));
235 QuicPacketSequenceNumber pending4
[] = { 3, 5, 7 };
236 VerifyInFlightPackets(pending4
, arraysize(pending4
));
237 QuicPacketSequenceNumber retransmittable4
[] = { 7 };
238 VerifyRetransmittablePackets(retransmittable4
, arraysize(retransmittable4
));