1 // Copyright 2013 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_sent_packet_manager.h"
7 #include "base/stl_util.h"
8 #include "net/quic/quic_flags.h"
9 #include "net/quic/test_tools/quic_config_peer.h"
10 #include "net/quic/test_tools/quic_sent_packet_manager_peer.h"
11 #include "net/quic/test_tools/quic_test_utils.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
16 using testing::AnyNumber
;
17 using testing::ElementsAre
;
19 using testing::Pointwise
;
20 using testing::Return
;
21 using testing::StrictMock
;
28 // Default packet length.
29 const uint32 kDefaultLength
= 1000;
31 // Matcher to check the key of the key-value pair it receives as first argument
32 // equals its second argument.
34 return std::tr1::get
<0>(arg
).first
== std::tr1::get
<1>(arg
);
37 class MockDebugDelegate
: public QuicSentPacketManager::DebugDelegate
{
39 MOCK_METHOD2(OnSpuriousPacketRetransmition
,
40 void(TransmissionType transmission_type
,
41 QuicByteCount byte_size
));
44 class QuicSentPacketManagerTest
: public ::testing::TestWithParam
<bool> {
46 QuicSentPacketManagerTest()
47 : manager_(true, &clock_
, &stats_
, kCubic
, kNack
),
48 send_algorithm_(new StrictMock
<MockSendAlgorithm
>),
49 network_change_visitor_(new StrictMock
<MockNetworkChangeVisitor
>) {
50 QuicSentPacketManagerPeer::SetSendAlgorithm(&manager_
, send_algorithm_
);
51 // Disable tail loss probes for most tests.
52 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 0);
53 // Advance the time 1s so the send times are never QuicTime::Zero.
54 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
55 manager_
.set_network_change_visitor(network_change_visitor_
.get());
57 EXPECT_CALL(*send_algorithm_
, HasReliableBandwidthEstimate())
59 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate())
61 .WillRepeatedly(Return(QuicBandwidth::Zero()));
62 EXPECT_CALL(*send_algorithm_
, InSlowStart()).Times(AnyNumber());
63 EXPECT_CALL(*send_algorithm_
, InRecovery()).Times(AnyNumber());
66 virtual ~QuicSentPacketManagerTest() OVERRIDE
{
67 STLDeleteElements(&packets_
);
70 QuicByteCount
BytesInFlight() {
71 return QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
);
73 void VerifyUnackedPackets(QuicPacketSequenceNumber
* packets
,
75 if (num_packets
== 0) {
76 EXPECT_FALSE(manager_
.HasUnackedPackets());
77 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
82 EXPECT_TRUE(manager_
.HasUnackedPackets());
83 EXPECT_EQ(packets
[0], manager_
.GetLeastUnackedSentPacket());
84 for (size_t i
= 0; i
< num_packets
; ++i
) {
85 EXPECT_TRUE(manager_
.IsUnacked(packets
[i
])) << packets
[i
];
89 void VerifyRetransmittablePackets(QuicPacketSequenceNumber
* packets
,
91 EXPECT_EQ(num_packets
,
92 QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
94 for (size_t i
= 0; i
< num_packets
; ++i
) {
95 EXPECT_TRUE(manager_
.HasRetransmittableFrames(packets
[i
]))
96 << " packets[" << i
<< "]:" << packets
[i
];
100 void ExpectAck(QuicPacketSequenceNumber largest_observed
) {
101 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(
102 true, _
, ElementsAre(Pair(largest_observed
, _
)), _
));
103 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
104 .WillOnce(Return(100 * kDefaultTCPMSS
));
105 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
108 void ExpectUpdatedRtt(QuicPacketSequenceNumber largest_observed
) {
109 EXPECT_CALL(*send_algorithm_
,
110 OnCongestionEvent(true, _
, _
, _
));
111 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
112 .WillOnce(Return(100 * kDefaultTCPMSS
));
113 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
116 void ExpectAckAndLoss(bool rtt_updated
,
117 QuicPacketSequenceNumber largest_observed
,
118 QuicPacketSequenceNumber lost_packet
) {
119 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(
120 rtt_updated
, _
, ElementsAre(Pair(largest_observed
, _
)),
121 ElementsAre(Pair(lost_packet
, _
))));
122 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
123 .WillOnce(Return(100 * kDefaultTCPMSS
));
124 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
127 // |packets_acked| and |packets_lost| should be in sequence number order.
128 void ExpectAcksAndLosses(bool rtt_updated
,
129 QuicPacketSequenceNumber
* packets_acked
,
130 size_t num_packets_acked
,
131 QuicPacketSequenceNumber
* packets_lost
,
132 size_t num_packets_lost
) {
133 vector
<QuicPacketSequenceNumber
> ack_vector
;
134 for (size_t i
= 0; i
< num_packets_acked
; ++i
) {
135 ack_vector
.push_back(packets_acked
[i
]);
137 vector
<QuicPacketSequenceNumber
> lost_vector
;
138 for (size_t i
= 0; i
< num_packets_lost
; ++i
) {
139 lost_vector
.push_back(packets_lost
[i
]);
141 EXPECT_CALL(*send_algorithm_
,
142 OnCongestionEvent(rtt_updated
, _
,
143 Pointwise(KeyEq(), ack_vector
),
144 Pointwise(KeyEq(), lost_vector
)));
145 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
146 .WillRepeatedly(Return(100 * kDefaultTCPMSS
));
147 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
)).
151 // Retransmits a packet as though it was a TLP retransmission, because TLP
152 // leaves the |old_sequence_number| pending.
153 // TODO(ianswett): Test with transmission types besides TLP.
154 void RetransmitPacket(QuicPacketSequenceNumber old_sequence_number
,
155 QuicPacketSequenceNumber new_sequence_number
) {
156 QuicSentPacketManagerPeer::MarkForRetransmission(
157 &manager_
, old_sequence_number
, TLP_RETRANSMISSION
);
158 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
159 QuicSentPacketManager::PendingRetransmission next_retransmission
=
160 manager_
.NextPendingRetransmission();
161 EXPECT_EQ(old_sequence_number
, next_retransmission
.sequence_number
);
162 EXPECT_EQ(TLP_RETRANSMISSION
,
163 next_retransmission
.transmission_type
);
164 manager_
.OnRetransmittedPacket(old_sequence_number
,
165 new_sequence_number
);
166 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(
167 &manager_
, new_sequence_number
));
170 void RetransmitAndSendPacket(QuicPacketSequenceNumber old_sequence_number
,
171 QuicPacketSequenceNumber new_sequence_number
) {
172 RetransmitPacket(old_sequence_number
, new_sequence_number
);
174 EXPECT_CALL(*send_algorithm_
,
175 OnPacketSent(_
, BytesInFlight(), new_sequence_number
,
176 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
177 .WillOnce(Return(true));
178 manager_
.OnPacketSent(new_sequence_number
,
182 HAS_RETRANSMITTABLE_DATA
);
185 SerializedPacket
CreateDataPacket(QuicPacketSequenceNumber sequence_number
) {
186 return CreatePacket(sequence_number
, true);
189 SerializedPacket
CreatePacket(QuicPacketSequenceNumber sequence_number
,
190 bool retransmittable
) {
191 packets_
.push_back(QuicPacket::NewDataPacket(
192 NULL
, kDefaultLength
, false, PACKET_8BYTE_CONNECTION_ID
, false,
193 PACKET_6BYTE_SEQUENCE_NUMBER
));
194 return SerializedPacket(
195 sequence_number
, PACKET_6BYTE_SEQUENCE_NUMBER
,
197 retransmittable
? new RetransmittableFrames() : NULL
);
200 SerializedPacket
CreateFecPacket(QuicPacketSequenceNumber sequence_number
) {
201 packets_
.push_back(QuicPacket::NewFecPacket(
202 NULL
, kDefaultLength
, false, PACKET_8BYTE_CONNECTION_ID
, false,
203 PACKET_6BYTE_SEQUENCE_NUMBER
));
204 return SerializedPacket(sequence_number
, PACKET_6BYTE_SEQUENCE_NUMBER
,
205 packets_
.back(), 0u, NULL
);
208 void SendDataPacket(QuicPacketSequenceNumber sequence_number
) {
209 EXPECT_CALL(*send_algorithm_
,
210 OnPacketSent(_
, BytesInFlight(), sequence_number
, _
, _
))
211 .Times(1).WillOnce(Return(true));
212 SerializedPacket
packet(CreateDataPacket(sequence_number
));
213 manager_
.OnSerializedPacket(packet
);
214 manager_
.OnPacketSent(sequence_number
, clock_
.Now(),
215 packet
.packet
->length(), NOT_RETRANSMISSION
,
216 HAS_RETRANSMITTABLE_DATA
);
219 void SendCryptoPacket(QuicPacketSequenceNumber sequence_number
) {
220 EXPECT_CALL(*send_algorithm_
,
221 OnPacketSent(_
, BytesInFlight(), sequence_number
,
222 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
223 .Times(1).WillOnce(Return(true));
224 SerializedPacket
packet(CreateDataPacket(sequence_number
));
225 packet
.retransmittable_frames
->AddStreamFrame(
226 new QuicStreamFrame(1, false, 0, IOVector()));
227 packet
.retransmittable_frames
->set_encryption_level(ENCRYPTION_NONE
);
228 manager_
.OnSerializedPacket(packet
);
229 manager_
.OnPacketSent(sequence_number
, clock_
.ApproximateNow(),
230 packet
.packet
->length(), NOT_RETRANSMISSION
,
231 HAS_RETRANSMITTABLE_DATA
);
234 void SendFecPacket(QuicPacketSequenceNumber sequence_number
) {
235 EXPECT_CALL(*send_algorithm_
,
236 OnPacketSent(_
, BytesInFlight(), sequence_number
,
237 kDefaultLength
, NO_RETRANSMITTABLE_DATA
))
238 .Times(1).WillOnce(Return(true));
239 SerializedPacket
packet(CreateFecPacket(sequence_number
));
240 manager_
.OnSerializedPacket(packet
);
241 manager_
.OnPacketSent(sequence_number
, clock_
.ApproximateNow(),
242 packet
.packet
->length(), NOT_RETRANSMISSION
,
243 NO_RETRANSMITTABLE_DATA
);
246 void SendAckPacket(QuicPacketSequenceNumber sequence_number
) {
247 EXPECT_CALL(*send_algorithm_
,
248 OnPacketSent(_
, BytesInFlight(), sequence_number
,
249 kDefaultLength
, NO_RETRANSMITTABLE_DATA
))
250 .Times(1).WillOnce(Return(false));
251 SerializedPacket
packet(CreatePacket(sequence_number
, false));
252 manager_
.OnSerializedPacket(packet
);
253 manager_
.OnPacketSent(sequence_number
, clock_
.Now(),
254 packet
.packet
->length(), NOT_RETRANSMISSION
,
255 NO_RETRANSMITTABLE_DATA
);
258 // Based on QuicConnection's WritePendingRetransmissions.
259 void RetransmitNextPacket(
260 QuicPacketSequenceNumber retransmission_sequence_number
) {
261 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
262 EXPECT_CALL(*send_algorithm_
,
263 OnPacketSent(_
, _
, retransmission_sequence_number
,
264 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
265 .Times(1).WillOnce(Return(true));
266 const QuicSentPacketManager::PendingRetransmission pending
=
267 manager_
.NextPendingRetransmission();
268 manager_
.OnRetransmittedPacket(pending
.sequence_number
,
269 retransmission_sequence_number
);
270 manager_
.OnPacketSent(retransmission_sequence_number
, clock_
.Now(),
271 kDefaultLength
, pending
.transmission_type
,
272 HAS_RETRANSMITTABLE_DATA
);
275 QuicSentPacketManager manager_
;
276 vector
<QuicPacket
*> packets_
;
278 QuicConnectionStats stats_
;
279 MockSendAlgorithm
* send_algorithm_
;
280 scoped_ptr
<MockNetworkChangeVisitor
> network_change_visitor_
;
283 TEST_F(QuicSentPacketManagerTest
, IsUnacked
) {
284 VerifyUnackedPackets(NULL
, 0);
286 SerializedPacket
serialized_packet(CreateDataPacket(1));
288 manager_
.OnSerializedPacket(serialized_packet
);
290 QuicPacketSequenceNumber unacked
[] = { 1 };
291 VerifyUnackedPackets(unacked
, arraysize(unacked
));
292 QuicPacketSequenceNumber retransmittable
[] = { 1 };
293 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
296 TEST_F(QuicSentPacketManagerTest
, IsUnAckedRetransmit
) {
298 RetransmitPacket(1, 2);
300 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(&manager_
, 2));
301 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
302 VerifyUnackedPackets(unacked
, arraysize(unacked
));
303 QuicPacketSequenceNumber retransmittable
[] = { 2 };
304 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
307 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAck
) {
309 RetransmitAndSendPacket(1, 2);
312 QuicAckFrame ack_frame
;
313 ack_frame
.largest_observed
= 2;
314 ack_frame
.missing_packets
.insert(1);
316 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
318 // Packet 1 is unacked, pending, but not retransmittable.
319 QuicPacketSequenceNumber unacked
[] = { 1 };
320 VerifyUnackedPackets(unacked
, arraysize(unacked
));
321 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
322 VerifyRetransmittablePackets(NULL
, 0);
325 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAckBeforeSend
) {
327 QuicSentPacketManagerPeer::MarkForRetransmission(
328 &manager_
, 1, TLP_RETRANSMISSION
);
329 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
332 QuicAckFrame ack_frame
;
333 ack_frame
.largest_observed
= 1;
335 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
337 // There should no longer be a pending retransmission.
338 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
340 // No unacked packets remain.
341 VerifyUnackedPackets(NULL
, 0);
342 VerifyRetransmittablePackets(NULL
, 0);
343 EXPECT_EQ(0u, stats_
.packets_spuriously_retransmitted
);
346 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAckPrevious
) {
348 RetransmitPacket(1, 2);
349 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
350 clock_
.AdvanceTime(rtt
);
354 QuicAckFrame ack_frame
;
355 ack_frame
.largest_observed
= 1;
356 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
358 // 2 should be unacked, since it may provide an RTT measurement.
359 QuicPacketSequenceNumber unacked
[] = { 2 };
360 VerifyUnackedPackets(unacked
, arraysize(unacked
));
361 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
362 VerifyRetransmittablePackets(NULL
, 0);
364 // Verify that the retransmission alarm would not fire,
365 // since there is no retransmittable data outstanding.
366 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
367 EXPECT_EQ(1u, stats_
.packets_spuriously_retransmitted
);
370 TEST_F(QuicSentPacketManagerTest
, RetransmitAndSendThenAckPrevious
) {
372 RetransmitAndSendPacket(1, 2);
373 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
374 clock_
.AdvanceTime(rtt
);
378 QuicAckFrame ack_frame
;
379 ack_frame
.largest_observed
= 1;
380 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
382 // 2 remains unacked, but no packets have retransmittable data.
383 QuicPacketSequenceNumber unacked
[] = { 2 };
384 VerifyUnackedPackets(unacked
, arraysize(unacked
));
385 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
386 VerifyRetransmittablePackets(NULL
, 0);
388 EXPECT_EQ(1u, stats_
.packets_spuriously_retransmitted
);
391 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAckPreviousThenNackRetransmit
) {
393 RetransmitPacket(1, 2);
394 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2, _
, _
))
395 .WillOnce(Return(true));
396 manager_
.OnPacketSent(2, clock_
.ApproximateNow(), kDefaultLength
,
397 LOSS_RETRANSMISSION
, HAS_RETRANSMITTABLE_DATA
);
398 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
399 clock_
.AdvanceTime(rtt
);
401 // First, ACK packet 1 which makes packet 2 non-retransmittable.
403 QuicAckFrame ack_frame
;
404 ack_frame
.largest_observed
= 1;
405 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
410 clock_
.AdvanceTime(rtt
);
412 // Next, NACK packet 2 three times.
413 ack_frame
.largest_observed
= 3;
414 ack_frame
.missing_packets
.insert(2);
416 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
418 ack_frame
.largest_observed
= 4;
420 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
422 ack_frame
.largest_observed
= 5;
423 ExpectAckAndLoss(true, 5, 2);
424 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
426 // No packets remain unacked.
427 VerifyUnackedPackets(NULL
, 0);
428 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
429 VerifyRetransmittablePackets(NULL
, 0);
431 // Verify that the retransmission alarm would not fire,
432 // since there is no retransmittable data outstanding.
433 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
436 TEST_F(QuicSentPacketManagerTest
, RetransmitTwiceThenAckPreviousBeforeSend
) {
438 RetransmitAndSendPacket(1, 2);
440 // Fire the RTO, which will mark 2 for retransmission (but will not send it).
441 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
442 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
443 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
444 .WillOnce(Return(2 * kDefaultTCPMSS
));
445 manager_
.OnRetransmissionTimeout();
446 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
448 // Ack 1 but not 2, before 2 is able to be sent.
449 // Since 1 has been retransmitted, it has already been lost, and so the
450 // send algorithm is not informed that it has been ACK'd.
451 QuicAckFrame ack_frame
;
452 ack_frame
.largest_observed
= 1;
454 EXPECT_CALL(*send_algorithm_
, RevertRetransmissionTimeout());
455 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
457 // Since 2 was marked for retransmit, when 1 is acked, 2 is kept for RTT.
458 QuicPacketSequenceNumber unacked
[] = { 2 };
459 VerifyUnackedPackets(unacked
, arraysize(unacked
));
460 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
461 VerifyRetransmittablePackets(NULL
, 0);
463 // Verify that the retransmission alarm would not fire,
464 // since there is no retransmittable data outstanding.
465 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
468 TEST_F(QuicSentPacketManagerTest
, RetransmitTwiceThenAckFirst
) {
469 StrictMock
<MockDebugDelegate
> debug_delegate
;
470 EXPECT_CALL(debug_delegate
, OnSpuriousPacketRetransmition(
471 TLP_RETRANSMISSION
, kDefaultLength
)).Times(2);
472 manager_
.set_debug_delegate(&debug_delegate
);
475 RetransmitAndSendPacket(1, 2);
476 RetransmitAndSendPacket(2, 3);
477 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
478 clock_
.AdvanceTime(rtt
);
480 // Ack 1 but not 2 or 3.
482 QuicAckFrame ack_frame
;
483 ack_frame
.largest_observed
= 1;
484 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
486 // 2 and 3 remain unacked, but no packets have retransmittable data.
487 QuicPacketSequenceNumber unacked
[] = { 2, 3 };
488 VerifyUnackedPackets(unacked
, arraysize(unacked
));
489 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
490 VerifyRetransmittablePackets(NULL
, 0);
492 // Ensure packet 2 is lost when 4 is sent and 3 and 4 are acked.
494 ack_frame
.largest_observed
= 4;
495 ack_frame
.missing_packets
.insert(2);
496 QuicPacketSequenceNumber acked
[] = { 3, 4 };
497 ExpectAcksAndLosses(true, acked
, arraysize(acked
), NULL
, 0);
498 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
500 QuicPacketSequenceNumber unacked2
[] = { 2 };
501 VerifyUnackedPackets(unacked2
, arraysize(unacked2
));
502 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
505 ack_frame
.largest_observed
= 5;
506 ExpectAckAndLoss(true, 5, 2);
507 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
509 VerifyUnackedPackets(NULL
, 0);
510 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
511 EXPECT_EQ(2u, stats_
.packets_spuriously_retransmitted
);
514 TEST_F(QuicSentPacketManagerTest
, LoseButDontRetransmitRevivedPacket
) {
520 // Ack 2 and 3, and mark 1 as revived.
521 QuicAckFrame ack_frame
;
522 ack_frame
.largest_observed
= 3;
523 ack_frame
.missing_packets
.insert(1);
524 ack_frame
.revived_packets
.insert(1);
525 QuicPacketSequenceNumber acked
[] = { 2, 3 };
526 ExpectAcksAndLosses(true, acked
, arraysize(acked
), NULL
, 0);
527 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
529 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
530 QuicPacketSequenceNumber unacked
[] = { 1, 4 };
531 VerifyUnackedPackets(unacked
, arraysize(unacked
));
532 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
533 QuicPacketSequenceNumber retransmittable
[] = { 4 };
534 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
536 // Ack the 4th packet and expect the 1st to be considered lost.
537 ack_frame
.largest_observed
= 4;
538 ExpectAckAndLoss(true, 4, 1);
539 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
541 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
542 VerifyRetransmittablePackets(NULL
, 0);
545 TEST_F(QuicSentPacketManagerTest
, MarkLostThenReviveAndDontRetransmitPacket
) {
552 // Ack 2, 3, and 4, and expect the 1st to be considered lost.
553 QuicAckFrame ack_frame
;
554 ack_frame
.largest_observed
= 4;
555 ack_frame
.missing_packets
.insert(1);
556 QuicPacketSequenceNumber acked
[] = { 2, 3, 4 };
557 QuicPacketSequenceNumber lost
[] = { 1 };
558 ExpectAcksAndLosses(true, acked
, arraysize(acked
), lost
, arraysize(lost
));
559 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
561 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
562 QuicPacketSequenceNumber unacked
[] = { 1, 5 };
563 VerifyUnackedPackets(unacked
, arraysize(unacked
));
564 QuicPacketSequenceNumber retransmittable
[] = { 1 };
565 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
567 // Ack 5th packet (FEC) and revive 1st packet. 1st packet should now be
568 // removed from pending retransmissions map.
569 ack_frame
.largest_observed
= 5;
570 ack_frame
.revived_packets
.insert(1);
572 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
574 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
575 VerifyRetransmittablePackets(NULL
, 0);
578 TEST_F(QuicSentPacketManagerTest
, TruncatedAck
) {
580 RetransmitAndSendPacket(1, 2);
581 RetransmitAndSendPacket(2, 3);
582 RetransmitAndSendPacket(3, 4);
583 RetransmitAndSendPacket(4, 5);
585 // Truncated ack with 4 NACKs, so the first packet is lost.
586 QuicAckFrame ack_frame
;
587 ack_frame
.largest_observed
= 4;
588 ack_frame
.missing_packets
.insert(1);
589 ack_frame
.missing_packets
.insert(2);
590 ack_frame
.missing_packets
.insert(3);
591 ack_frame
.missing_packets
.insert(4);
592 ack_frame
.is_truncated
= true;
594 QuicPacketSequenceNumber lost
[] = { 1 };
595 ExpectAcksAndLosses(true, NULL
, 0, lost
, arraysize(lost
));
596 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
598 // High water mark will be raised.
599 QuicPacketSequenceNumber unacked
[] = { 2, 3, 4, 5 };
600 VerifyUnackedPackets(unacked
, arraysize(unacked
));
601 QuicPacketSequenceNumber retransmittable
[] = { 5 };
602 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
605 TEST_F(QuicSentPacketManagerTest
, AckPreviousTransmissionThenTruncatedAck
) {
607 RetransmitAndSendPacket(1, 2);
608 RetransmitAndSendPacket(2, 3);
609 RetransmitAndSendPacket(3, 4);
610 manager_
.OnSerializedPacket(CreateDataPacket(5));
611 manager_
.OnSerializedPacket(CreateDataPacket(6));
612 manager_
.OnSerializedPacket(CreateDataPacket(7));
613 manager_
.OnSerializedPacket(CreateDataPacket(8));
614 manager_
.OnSerializedPacket(CreateDataPacket(9));
616 // Ack previous transmission
618 QuicAckFrame ack_frame
;
619 ack_frame
.largest_observed
= 2;
620 ack_frame
.missing_packets
.insert(1);
622 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
623 EXPECT_TRUE(manager_
.IsUnacked(4));
626 // Truncated ack with 4 NACKs
628 QuicAckFrame ack_frame
;
629 ack_frame
.largest_observed
= 6;
630 ack_frame
.missing_packets
.insert(3);
631 ack_frame
.missing_packets
.insert(4);
632 ack_frame
.missing_packets
.insert(5);
633 ack_frame
.missing_packets
.insert(6);
634 ack_frame
.is_truncated
= true;
635 ExpectAckAndLoss(false, 1, 3);
636 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
639 // High water mark will be raised.
640 QuicPacketSequenceNumber unacked
[] = { 4, 5, 6, 7, 8, 9 };
641 VerifyUnackedPackets(unacked
, arraysize(unacked
));
642 QuicPacketSequenceNumber retransmittable
[] = { 5, 6, 7, 8, 9 };
643 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
646 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedSentPacket
) {
647 EXPECT_EQ(0u, manager_
.GetLeastUnackedSentPacket());
650 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedSentPacketUnacked
) {
651 SerializedPacket
serialized_packet(CreateDataPacket(1));
653 manager_
.OnSerializedPacket(serialized_packet
);
654 EXPECT_EQ(1u, manager_
.GetLeastUnackedSentPacket());
657 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedSentPacketUnackedFec
) {
658 SerializedPacket
serialized_packet(CreateFecPacket(1));
660 manager_
.OnSerializedPacket(serialized_packet
);
661 EXPECT_EQ(1u, manager_
.GetLeastUnackedSentPacket());
664 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedPacketAndDiscard
) {
665 VerifyUnackedPackets(NULL
, 0);
667 SerializedPacket
serialized_packet(CreateFecPacket(1));
668 manager_
.OnSerializedPacket(serialized_packet
);
669 EXPECT_EQ(1u, manager_
.GetLeastUnackedSentPacket());
671 SerializedPacket
serialized_packet2(CreateFecPacket(2));
672 manager_
.OnSerializedPacket(serialized_packet2
);
673 EXPECT_EQ(1u, manager_
.GetLeastUnackedSentPacket());
675 SerializedPacket
serialized_packet3(CreateFecPacket(3));
676 manager_
.OnSerializedPacket(serialized_packet3
);
677 EXPECT_EQ(1u, manager_
.GetLeastUnackedSentPacket());
679 QuicPacketSequenceNumber unacked
[] = { 1, 2, 3 };
680 VerifyUnackedPackets(unacked
, arraysize(unacked
));
681 VerifyRetransmittablePackets(NULL
, 0);
683 // Ack 2, which has never been sent, so there's no rtt update.
684 QuicAckFrame ack_frame
;
685 ack_frame
.largest_observed
= 2;
686 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
688 EXPECT_EQ(3u, manager_
.GetLeastUnackedSentPacket());
691 TEST_F(QuicSentPacketManagerTest
, GetSentTime
) {
692 VerifyUnackedPackets(NULL
, 0);
694 SerializedPacket
serialized_packet(CreateFecPacket(1));
695 manager_
.OnSerializedPacket(serialized_packet
);
696 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 1, _
, _
))
697 .Times(1).WillOnce(Return(true));
698 manager_
.OnPacketSent(1, QuicTime::Zero(), kDefaultLength
, NOT_RETRANSMISSION
,
699 NO_RETRANSMITTABLE_DATA
);
701 SerializedPacket
serialized_packet2(CreateFecPacket(2));
702 QuicTime sent_time
= QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(1));
703 manager_
.OnSerializedPacket(serialized_packet2
);
704 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2, _
, _
))
705 .Times(1).WillOnce(Return(true));
706 manager_
.OnPacketSent(2, sent_time
, kDefaultLength
, NOT_RETRANSMISSION
,
707 NO_RETRANSMITTABLE_DATA
);
709 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
710 VerifyUnackedPackets(unacked
, arraysize(unacked
));
711 VerifyRetransmittablePackets(NULL
, 0);
713 EXPECT_TRUE(manager_
.HasUnackedPackets());
714 EXPECT_EQ(QuicTime::Zero(),
715 QuicSentPacketManagerPeer::GetSentTime(&manager_
, 1));
716 EXPECT_EQ(sent_time
, QuicSentPacketManagerPeer::GetSentTime(&manager_
, 2));
719 TEST_F(QuicSentPacketManagerTest
, AckAckAndUpdateRtt
) {
723 // Now ack the ack and expect an RTT update.
724 QuicAckFrame ack_frame
;
725 ack_frame
.largest_observed
= 2;
726 ack_frame
.delta_time_largest_observed
=
727 QuicTime::Delta::FromMilliseconds(5);
730 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
734 // Now ack the ack and expect only an RTT update.
735 ack_frame
.largest_observed
= 3;
737 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
740 TEST_F(QuicSentPacketManagerTest
, Rtt
) {
741 QuicPacketSequenceNumber sequence_number
= 1;
742 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(15);
743 SendDataPacket(sequence_number
);
744 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
746 ExpectAck(sequence_number
);
747 QuicAckFrame ack_frame
;
748 ack_frame
.largest_observed
= sequence_number
;
749 ack_frame
.delta_time_largest_observed
=
750 QuicTime::Delta::FromMilliseconds(5);
751 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
752 EXPECT_EQ(expected_rtt
,
753 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
756 TEST_F(QuicSentPacketManagerTest
, RttWithInvalidDelta
) {
757 // Expect that the RTT is equal to the local time elapsed, since the
758 // delta_time_largest_observed is larger than the local time elapsed
759 // and is hence invalid.
760 QuicPacketSequenceNumber sequence_number
= 1;
761 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(10);
762 SendDataPacket(sequence_number
);
763 clock_
.AdvanceTime(expected_rtt
);
765 ExpectAck(sequence_number
);
766 QuicAckFrame ack_frame
;
767 ack_frame
.largest_observed
= sequence_number
;
768 ack_frame
.delta_time_largest_observed
=
769 QuicTime::Delta::FromMilliseconds(11);
770 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
771 EXPECT_EQ(expected_rtt
,
772 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
775 TEST_F(QuicSentPacketManagerTest
, RttWithInfiniteDelta
) {
776 // Expect that the RTT is equal to the local time elapsed, since the
777 // delta_time_largest_observed is infinite, and is hence invalid.
778 QuicPacketSequenceNumber sequence_number
= 1;
779 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(10);
780 SendDataPacket(sequence_number
);
781 clock_
.AdvanceTime(expected_rtt
);
783 ExpectAck(sequence_number
);
784 QuicAckFrame ack_frame
;
785 ack_frame
.largest_observed
= sequence_number
;
786 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Infinite();
787 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
788 EXPECT_EQ(expected_rtt
,
789 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
792 TEST_F(QuicSentPacketManagerTest
, RttZeroDelta
) {
793 // Expect that the RTT is the time between send and receive since the
794 // delta_time_largest_observed is zero.
795 QuicPacketSequenceNumber sequence_number
= 1;
796 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(10);
797 SendDataPacket(sequence_number
);
798 clock_
.AdvanceTime(expected_rtt
);
800 ExpectAck(sequence_number
);
801 QuicAckFrame ack_frame
;
802 ack_frame
.largest_observed
= sequence_number
;
803 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
804 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
805 EXPECT_EQ(expected_rtt
,
806 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
809 TEST_F(QuicSentPacketManagerTest
, TailLossProbeTimeout
) {
810 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
813 QuicPacketSequenceNumber sequence_number
= 1;
814 SendDataPacket(sequence_number
);
816 // The first tail loss probe retransmits 1 packet.
817 manager_
.OnRetransmissionTimeout();
818 EXPECT_EQ(QuicTime::Delta::Zero(),
819 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
820 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
821 manager_
.MaybeRetransmitTailLossProbe();
822 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
823 RetransmitNextPacket(2);
824 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
826 // The second tail loss probe retransmits 1 packet.
827 manager_
.OnRetransmissionTimeout();
828 EXPECT_EQ(QuicTime::Delta::Zero(),
829 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
830 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
831 manager_
.MaybeRetransmitTailLossProbe();
832 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
833 RetransmitNextPacket(3);
834 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
835 QuicTime::Delta::Infinite()));
836 EXPECT_EQ(QuicTime::Delta::Infinite(),
837 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
838 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
840 // Ack the third and ensure the first two are still pending.
842 QuicAckFrame ack_frame
;
843 ack_frame
.largest_observed
= 3;
844 ack_frame
.missing_packets
.insert(1);
845 ack_frame
.missing_packets
.insert(2);
846 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
848 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
850 // Acking two more packets will lose both of them due to nacks.
851 ack_frame
.largest_observed
= 5;
852 QuicPacketSequenceNumber lost
[] = { 1, 2 };
853 ExpectAcksAndLosses(false, NULL
, 0, lost
, arraysize(lost
));
854 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
856 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
857 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
858 EXPECT_EQ(2u, stats_
.tlp_count
);
859 EXPECT_EQ(0u, stats_
.rto_count
);
862 TEST_F(QuicSentPacketManagerTest
, TailLossProbeThenRTO
) {
863 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
866 const size_t kNumSentPackets
= 100;
867 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
871 // The first tail loss probe retransmits 1 packet.
872 manager_
.OnRetransmissionTimeout();
873 EXPECT_EQ(QuicTime::Delta::Zero(),
874 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
875 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
876 manager_
.MaybeRetransmitTailLossProbe();
877 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
878 RetransmitNextPacket(101);
879 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
880 QuicTime::Delta::Infinite()));
881 EXPECT_EQ(QuicTime::Delta::Infinite(),
882 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
883 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
885 // The second tail loss probe retransmits 1 packet.
886 manager_
.OnRetransmissionTimeout();
887 EXPECT_EQ(QuicTime::Delta::Zero(),
888 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
889 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
890 EXPECT_TRUE(manager_
.MaybeRetransmitTailLossProbe());
891 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
892 RetransmitNextPacket(102);
893 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
894 QuicTime::Delta::Infinite()));
895 EXPECT_EQ(QuicTime::Delta::Infinite(),
896 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
898 // Advance the time enough to ensure all packets are RTO'd.
899 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
901 // The final RTO abandons all of them.
902 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
903 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
904 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
905 .WillOnce(Return(2 * kDefaultTCPMSS
));
906 manager_
.OnRetransmissionTimeout();
907 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
908 EXPECT_EQ(2u, stats_
.tlp_count
);
909 EXPECT_EQ(1u, stats_
.rto_count
);
912 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeTimeout
) {
913 // Send 2 crypto packets and 3 data packets.
914 const size_t kNumSentCryptoPackets
= 2;
915 for (size_t i
= 1; i
<= kNumSentCryptoPackets
; ++i
) {
918 const size_t kNumSentDataPackets
= 3;
919 for (size_t i
= 1; i
<= kNumSentDataPackets
; ++i
) {
920 SendDataPacket(kNumSentCryptoPackets
+ i
);
922 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
924 // The first retransmits 2 packets.
925 manager_
.OnRetransmissionTimeout();
926 EXPECT_EQ(QuicTime::Delta::Zero(),
927 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
928 RetransmitNextPacket(6);
929 RetransmitNextPacket(7);
930 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
931 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
933 // The second retransmits 2 packets.
934 manager_
.OnRetransmissionTimeout();
935 EXPECT_EQ(QuicTime::Delta::Zero(),
936 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
937 RetransmitNextPacket(8);
938 RetransmitNextPacket(9);
939 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
940 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
942 // Now ack the two crypto packets and the speculatively encrypted request,
943 // and ensure the first four crypto packets get abandoned, but not lost.
944 QuicPacketSequenceNumber acked
[] = { 3, 4, 5, 8, 9 };
945 ExpectAcksAndLosses(true, acked
, arraysize(acked
), NULL
, 0);
946 QuicAckFrame ack_frame
;
947 ack_frame
.largest_observed
= 9;
948 ack_frame
.missing_packets
.insert(1);
949 ack_frame
.missing_packets
.insert(2);
950 ack_frame
.missing_packets
.insert(6);
951 ack_frame
.missing_packets
.insert(7);
952 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
954 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
957 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeTimeoutVersionNegotiation
) {
958 // Send 2 crypto packets and 3 data packets.
959 const size_t kNumSentCryptoPackets
= 2;
960 for (size_t i
= 1; i
<= kNumSentCryptoPackets
; ++i
) {
963 const size_t kNumSentDataPackets
= 3;
964 for (size_t i
= 1; i
<= kNumSentDataPackets
; ++i
) {
965 SendDataPacket(kNumSentCryptoPackets
+ i
);
967 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
969 // The first retransmission timeout retransmits 2 crypto packets.
970 manager_
.OnRetransmissionTimeout();
971 RetransmitNextPacket(6);
972 RetransmitNextPacket(7);
973 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
974 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
976 // Now act like a version negotiation packet arrived, which would cause all
977 // unacked packets to be retransmitted.
978 manager_
.RetransmitUnackedPackets(ALL_PACKETS
);
980 // Ensure the first two pending packets are the crypto retransmits.
981 ASSERT_TRUE(manager_
.HasPendingRetransmissions());
982 EXPECT_EQ(6u, manager_
.NextPendingRetransmission().sequence_number
);
983 RetransmitNextPacket(8);
984 EXPECT_EQ(7u, manager_
.NextPendingRetransmission().sequence_number
);
985 RetransmitNextPacket(9);
987 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
990 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeSpuriousRetransmission
) {
991 // Send 1 crypto packet.
993 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
995 // Retransmit the crypto packet as 2.
996 manager_
.OnRetransmissionTimeout();
997 RetransmitNextPacket(2);
999 // Retransmit the crypto packet as 3.
1000 manager_
.OnRetransmissionTimeout();
1001 RetransmitNextPacket(3);
1003 // Now ack the second crypto packet, and ensure the first gets removed, but
1004 // the third does not.
1005 ExpectUpdatedRtt(2);
1006 QuicAckFrame ack_frame
;
1007 ack_frame
.largest_observed
= 2;
1008 ack_frame
.missing_packets
.insert(1);
1009 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
1011 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1012 QuicPacketSequenceNumber unacked
[] = { 3 };
1013 VerifyUnackedPackets(unacked
, arraysize(unacked
));
1016 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeTimeoutUnsentDataPacket
) {
1017 // Send 2 crypto packets and serialize 1 data packet.
1018 const size_t kNumSentCryptoPackets
= 2;
1019 for (size_t i
= 1; i
<= kNumSentCryptoPackets
; ++i
) {
1020 SendCryptoPacket(i
);
1022 SerializedPacket
packet(CreateDataPacket(3));
1023 manager_
.OnSerializedPacket(packet
);
1024 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1026 // Retransmit 2 crypto packets, but not the serialized packet.
1027 manager_
.OnRetransmissionTimeout();
1028 RetransmitNextPacket(6);
1029 RetransmitNextPacket(7);
1030 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1031 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1034 TEST_F(QuicSentPacketManagerTest
,
1035 CryptoHandshakeRetransmissionThenRetransmitAll
) {
1036 // Send 1 crypto packet.
1037 SendCryptoPacket(1);
1038 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1040 // Retransmit the crypto packet as 2.
1041 manager_
.OnRetransmissionTimeout();
1042 RetransmitNextPacket(2);
1044 // Now retransmit all the unacked packets, which occurs when there is a
1045 // version negotiation.
1046 manager_
.RetransmitUnackedPackets(ALL_PACKETS
);
1047 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
1048 VerifyUnackedPackets(unacked
, arraysize(unacked
));
1049 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1050 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1051 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
1054 TEST_F(QuicSentPacketManagerTest
,
1055 CryptoHandshakeRetransmissionThenNeuterAndAck
) {
1056 // Send 1 crypto packet.
1057 SendCryptoPacket(1);
1058 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1060 // Retransmit the crypto packet as 2.
1061 manager_
.OnRetransmissionTimeout();
1062 RetransmitNextPacket(2);
1064 // Retransmit the crypto packet as 3.
1065 manager_
.OnRetransmissionTimeout();
1066 RetransmitNextPacket(3);
1068 // Now neuter all unacked unencrypted packets, which occurs when the
1069 // connection goes forward secure.
1070 manager_
.NeuterUnencryptedPackets();
1071 QuicPacketSequenceNumber unacked
[] = { 1, 2, 3};
1072 VerifyUnackedPackets(unacked
, arraysize(unacked
));
1073 VerifyRetransmittablePackets(NULL
, 0);
1074 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1075 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1076 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
1078 // Ensure both packets get discarded when packet 2 is acked.
1079 QuicAckFrame ack_frame
;
1080 ack_frame
.largest_observed
= 3;
1081 ack_frame
.missing_packets
.insert(1);
1082 ack_frame
.missing_packets
.insert(2);
1083 ExpectUpdatedRtt(3);
1084 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
1085 VerifyUnackedPackets(NULL
, 0);
1086 VerifyRetransmittablePackets(NULL
, 0);
1089 TEST_F(QuicSentPacketManagerTest
, TailLossProbeTimeoutUnsentDataPacket
) {
1090 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
1091 // Serialize two data packets and send the latter.
1092 SerializedPacket
packet(CreateDataPacket(1));
1093 manager_
.OnSerializedPacket(packet
);
1095 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1096 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
1098 // Retransmit 1 unacked packets, but not the first serialized packet.
1099 manager_
.OnRetransmissionTimeout();
1100 EXPECT_EQ(QuicTime::Delta::Zero(),
1101 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1102 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1103 manager_
.MaybeRetransmitTailLossProbe();
1104 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1105 RetransmitNextPacket(3);
1106 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1107 QuicTime::Delta::Infinite()));
1108 EXPECT_EQ(QuicTime::Delta::Infinite(),
1109 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1110 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1111 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1112 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
1115 TEST_F(QuicSentPacketManagerTest
, ResetRecentMinRTTWithEmptyWindow
) {
1116 QuicTime::Delta min_rtt
= QuicTime::Delta::FromMilliseconds(50);
1117 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->UpdateRtt(
1118 min_rtt
, QuicTime::Delta::Zero(), QuicTime::Zero());
1120 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->min_rtt());
1122 QuicSentPacketManagerPeer::GetRttStats(
1123 &manager_
)->recent_min_rtt());
1125 // Send two packets with no prior bytes in flight.
1129 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
1130 // Ack two packets with 100ms RTT observations.
1131 QuicAckFrame ack_frame
;
1132 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1133 ack_frame
.largest_observed
= 1;
1135 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1137 // First ack does not change recent min rtt.
1139 QuicSentPacketManagerPeer::GetRttStats(
1140 &manager_
)->recent_min_rtt());
1142 ack_frame
.largest_observed
= 2;
1144 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1147 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->min_rtt());
1148 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(100),
1149 QuicSentPacketManagerPeer::GetRttStats(
1150 &manager_
)->recent_min_rtt());
1153 TEST_F(QuicSentPacketManagerTest
, RetransmissionTimeout
) {
1154 // Send 100 packets and then ensure all are abandoned when the RTO fires.
1155 const size_t kNumSentPackets
= 100;
1156 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
1160 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1161 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
1162 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
1163 .WillOnce(Return(2 * kDefaultTCPMSS
));
1164 EXPECT_FALSE(manager_
.MaybeRetransmitTailLossProbe());
1165 manager_
.OnRetransmissionTimeout();
1168 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTime
) {
1169 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
1172 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeCryptoHandshake
) {
1173 SendCryptoPacket(1);
1176 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->set_initial_rtt_us(
1177 1 * base::Time::kMicrosecondsPerMillisecond
);
1178 EXPECT_EQ(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1179 manager_
.GetRetransmissionTime());
1181 // Test with a standard smoothed RTT.
1182 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->set_initial_rtt_us(
1183 100 * base::Time::kMicrosecondsPerMillisecond
);
1185 QuicTime::Delta srtt
= manager_
.GetRttStats()->SmoothedRtt();
1186 QuicTime expected_time
= clock_
.Now().Add(srtt
.Multiply(1.5));
1187 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1189 // Retransmit the packet by invoking the retransmission timeout.
1190 clock_
.AdvanceTime(srtt
.Multiply(1.5));
1191 manager_
.OnRetransmissionTimeout();
1192 RetransmitNextPacket(2);
1194 // The retransmission time should now be twice as far in the future.
1195 expected_time
= clock_
.Now().Add(srtt
.Multiply(2).Multiply(1.5));
1196 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1199 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeTailLossProbe
) {
1200 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
1205 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->set_initial_rtt_us(
1206 1 * base::Time::kMicrosecondsPerMillisecond
);
1207 EXPECT_EQ(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1208 manager_
.GetRetransmissionTime());
1210 // Test with a standard smoothed RTT.
1211 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->set_initial_rtt_us(
1212 100 * base::Time::kMicrosecondsPerMillisecond
);
1213 QuicTime::Delta srtt
= manager_
.GetRttStats()->SmoothedRtt();
1214 QuicTime::Delta expected_tlp_delay
= srtt
.Multiply(2);
1215 QuicTime expected_time
= clock_
.Now().Add(expected_tlp_delay
);
1216 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1218 // Retransmit the packet by invoking the retransmission timeout.
1219 clock_
.AdvanceTime(expected_tlp_delay
);
1220 manager_
.OnRetransmissionTimeout();
1221 EXPECT_EQ(QuicTime::Delta::Zero(),
1222 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1223 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1224 EXPECT_TRUE(manager_
.MaybeRetransmitTailLossProbe());
1225 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1226 RetransmitNextPacket(3);
1227 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1228 QuicTime::Delta::Infinite()));
1229 EXPECT_EQ(QuicTime::Delta::Infinite(),
1230 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1231 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1233 expected_time
= clock_
.Now().Add(expected_tlp_delay
);
1234 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1237 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeRTO
) {
1238 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->UpdateRtt(
1239 QuicTime::Delta::FromMilliseconds(100),
1240 QuicTime::Delta::Zero(),
1248 QuicTime::Delta expected_rto_delay
= QuicTime::Delta::FromMilliseconds(500);
1249 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1250 .WillRepeatedly(Return(expected_rto_delay
));
1251 QuicTime expected_time
= clock_
.Now().Add(expected_rto_delay
);
1252 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1254 // Retransmit the packet by invoking the retransmission timeout.
1255 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
1256 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
1257 .WillOnce(Return(2 * kDefaultTCPMSS
));
1258 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1259 clock_
.AdvanceTime(expected_rto_delay
);
1260 manager_
.OnRetransmissionTimeout();
1261 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1262 RetransmitNextPacket(5);
1263 RetransmitNextPacket(6);
1264 EXPECT_EQ(2 * kDefaultLength
,
1265 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1266 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1268 // The delay should double the second time.
1269 expected_time
= clock_
.Now().Add(expected_rto_delay
).Add(expected_rto_delay
);
1270 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1272 // Ack a packet and ensure the RTO goes back to the original value.
1273 QuicAckFrame ack_frame
;
1274 ack_frame
.largest_observed
= 2;
1275 ack_frame
.missing_packets
.insert(1);
1276 ExpectUpdatedRtt(2);
1277 EXPECT_CALL(*send_algorithm_
, RevertRetransmissionTimeout());
1278 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
1279 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1280 EXPECT_EQ(4 * kDefaultLength
,
1281 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1283 // Wait 2RTTs from now for the RTO, since it's the max of the RTO time
1284 // and the TLP time. In production, there would always be two TLP's first.
1285 expected_time
= clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(200));
1286 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1289 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelayMin
) {
1291 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1292 .WillRepeatedly(Return(QuicTime::Delta::FromMilliseconds(1)));
1293 QuicTime::Delta delay
= QuicTime::Delta::FromMilliseconds(200);
1295 // If the delay is smaller than the min, ensure it exponentially backs off
1297 for (int i
= 0; i
< 5; ++i
) {
1299 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1300 delay
= delay
.Add(delay
);
1301 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
1302 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
1303 .WillOnce(Return(2 * kDefaultTCPMSS
));
1304 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1305 manager_
.OnRetransmissionTimeout();
1306 RetransmitNextPacket(i
+ 2);
1310 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelayMax
) {
1311 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1312 .WillOnce(Return(QuicTime::Delta::FromSeconds(500)));
1314 EXPECT_EQ(QuicTime::Delta::FromSeconds(60),
1315 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1318 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelay
) {
1320 QuicTime::Delta delay
= QuicTime::Delta::FromMilliseconds(500);
1321 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1322 .WillRepeatedly(Return(delay
));
1324 // Delay should back off exponentially.
1325 for (int i
= 0; i
< 5; ++i
) {
1327 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1328 delay
= delay
.Add(delay
);
1329 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
1330 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
1331 .WillOnce(Return(2 * kDefaultTCPMSS
));
1332 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1333 manager_
.OnRetransmissionTimeout();
1334 RetransmitNextPacket(i
+ 2);
1338 TEST_F(QuicSentPacketManagerTest
, GetLossDelay
) {
1339 MockLossAlgorithm
* loss_algorithm
= new MockLossAlgorithm();
1340 QuicSentPacketManagerPeer::SetLossAlgorithm(&manager_
, loss_algorithm
);
1342 EXPECT_CALL(*loss_algorithm
, GetLossTimeout())
1343 .WillRepeatedly(Return(QuicTime::Zero()));
1347 // Handle an ack which causes the loss algorithm to be evaluated and
1348 // set the loss timeout.
1350 EXPECT_CALL(*loss_algorithm
, DetectLostPackets(_
, _
, _
, _
))
1351 .WillOnce(Return(SequenceNumberSet()));
1352 QuicAckFrame ack_frame
;
1353 ack_frame
.largest_observed
= 2;
1354 ack_frame
.missing_packets
.insert(1);
1355 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1357 QuicTime
timeout(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)));
1358 EXPECT_CALL(*loss_algorithm
, GetLossTimeout())
1359 .WillRepeatedly(Return(timeout
));
1360 EXPECT_EQ(timeout
, manager_
.GetRetransmissionTime());
1362 // Fire the retransmission timeout and ensure the loss detection algorithm
1364 EXPECT_CALL(*loss_algorithm
, DetectLostPackets(_
, _
, _
, _
))
1365 .WillOnce(Return(SequenceNumberSet()));
1366 manager_
.OnRetransmissionTimeout();
1369 TEST_F(QuicSentPacketManagerTest
, NegotiateTimeLossDetection
) {
1371 QuicSentPacketManagerPeer::GetLossAlgorithm(
1372 &manager_
)->GetLossDetectionType());
1375 QuicConfigPeer::SetReceivedLossDetection(&config
, kTIME
);
1376 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1377 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
1378 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
1379 .WillOnce(Return(100 * kDefaultTCPMSS
));
1380 manager_
.SetFromConfig(config
);
1383 QuicSentPacketManagerPeer::GetLossAlgorithm(
1384 &manager_
)->GetLossDetectionType());
1387 TEST_F(QuicSentPacketManagerTest
, NegotiateTimeLossDetectionFromOptions
) {
1389 QuicSentPacketManagerPeer::GetLossAlgorithm(
1390 &manager_
)->GetLossDetectionType());
1393 QuicTagVector options
;
1394 options
.push_back(kTIME
);
1395 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1396 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1397 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
1398 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
1399 .WillOnce(Return(100 * kDefaultTCPMSS
));
1400 manager_
.SetFromConfig(config
);
1403 QuicSentPacketManagerPeer::GetLossAlgorithm(
1404 &manager_
)->GetLossDetectionType());
1407 TEST_F(QuicSentPacketManagerTest
, NegotiateCongestionControlFromOptions
) {
1409 QuicTagVector options
;
1411 options
.push_back(kRENO
);
1412 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1413 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
1414 manager_
.SetFromConfig(config
);
1415 EXPECT_EQ(kReno
, QuicSentPacketManagerPeer::GetCongestionControlAlgorithm(
1416 manager_
)->GetCongestionControlType());
1418 // TODO(rtenneti): Enable the following code after BBR code is checked in.
1421 options
.push_back(kTBBR
);
1422 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1423 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
1424 manager_
.SetFromConfig(config
);
1425 EXPECT_EQ(kBBR
, QuicSentPacketManagerPeer::GetCongestionControlAlgorithm(
1426 manager_
)->GetCongestionControlType());
1430 TEST_F(QuicSentPacketManagerTest
, NegotiatePacingFromOptions
) {
1431 EXPECT_FALSE(manager_
.using_pacing());
1434 QuicTagVector options
;
1435 options
.push_back(kPACE
);
1436 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1437 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange(_
));
1438 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow())
1439 .WillOnce(Return(100 * kDefaultTCPMSS
));
1440 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1441 manager_
.SetFromConfig(config
);
1443 EXPECT_TRUE(manager_
.using_pacing());