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
;
18 using testing::IsEmpty
;
20 using testing::Pointwise
;
21 using testing::Return
;
22 using testing::StrictMock
;
29 // Default packet length.
30 const uint32 kDefaultLength
= 1000;
32 // Matcher to check the key of the key-value pair it receives as first argument
33 // equals its second argument.
35 return std::tr1::get
<0>(arg
).first
== std::tr1::get
<1>(arg
);
38 class MockDebugDelegate
: public QuicSentPacketManager::DebugDelegate
{
40 MOCK_METHOD2(OnSpuriousPacketRetransmition
,
41 void(TransmissionType transmission_type
,
42 QuicByteCount byte_size
));
45 class QuicSentPacketManagerTest
: public ::testing::TestWithParam
<bool> {
47 QuicSentPacketManagerTest()
48 : manager_(true, &clock_
, &stats_
, kCubic
, kNack
, false),
49 send_algorithm_(new StrictMock
<MockSendAlgorithm
>),
50 network_change_visitor_(new StrictMock
<MockNetworkChangeVisitor
>) {
51 QuicSentPacketManagerPeer::SetSendAlgorithm(&manager_
, send_algorithm_
);
52 // Disable tail loss probes for most tests.
53 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 0);
54 // Advance the time 1s so the send times are never QuicTime::Zero.
55 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
56 manager_
.set_network_change_visitor(network_change_visitor_
.get());
58 EXPECT_CALL(*send_algorithm_
, HasReliableBandwidthEstimate())
60 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate())
62 .WillRepeatedly(Return(QuicBandwidth::Zero()));
63 EXPECT_CALL(*send_algorithm_
, InSlowStart()).Times(AnyNumber());
64 EXPECT_CALL(*send_algorithm_
, InRecovery()).Times(AnyNumber());
67 ~QuicSentPacketManagerTest() override
{ STLDeleteElements(&packets_
); }
69 QuicByteCount
BytesInFlight() {
70 return QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
);
72 void VerifyUnackedPackets(QuicPacketSequenceNumber
* packets
,
74 if (num_packets
== 0) {
75 EXPECT_FALSE(manager_
.HasUnackedPackets());
76 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
81 EXPECT_TRUE(manager_
.HasUnackedPackets());
82 EXPECT_EQ(packets
[0], manager_
.GetLeastUnacked());
83 for (size_t i
= 0; i
< num_packets
; ++i
) {
84 EXPECT_TRUE(manager_
.IsUnacked(packets
[i
])) << packets
[i
];
88 void VerifyRetransmittablePackets(QuicPacketSequenceNumber
* packets
,
90 EXPECT_EQ(num_packets
,
91 QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
93 for (size_t i
= 0; i
< num_packets
; ++i
) {
94 EXPECT_TRUE(manager_
.HasRetransmittableFrames(packets
[i
]))
95 << " packets[" << i
<< "]:" << packets
[i
];
99 void ExpectAck(QuicPacketSequenceNumber largest_observed
) {
100 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(
101 true, _
, ElementsAre(Pair(largest_observed
, _
)), IsEmpty()));
102 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
105 void ExpectUpdatedRtt(QuicPacketSequenceNumber largest_observed
) {
106 EXPECT_CALL(*send_algorithm_
,
107 OnCongestionEvent(true, _
, IsEmpty(), IsEmpty()));
108 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
111 void ExpectAckAndLoss(bool rtt_updated
,
112 QuicPacketSequenceNumber largest_observed
,
113 QuicPacketSequenceNumber lost_packet
) {
114 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(
115 rtt_updated
, _
, ElementsAre(Pair(largest_observed
, _
)),
116 ElementsAre(Pair(lost_packet
, _
))));
117 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
120 // |packets_acked| and |packets_lost| should be in sequence number order.
121 void ExpectAcksAndLosses(bool rtt_updated
,
122 QuicPacketSequenceNumber
* packets_acked
,
123 size_t num_packets_acked
,
124 QuicPacketSequenceNumber
* packets_lost
,
125 size_t num_packets_lost
) {
126 vector
<QuicPacketSequenceNumber
> ack_vector
;
127 for (size_t i
= 0; i
< num_packets_acked
; ++i
) {
128 ack_vector
.push_back(packets_acked
[i
]);
130 vector
<QuicPacketSequenceNumber
> lost_vector
;
131 for (size_t i
= 0; i
< num_packets_lost
; ++i
) {
132 lost_vector
.push_back(packets_lost
[i
]);
134 EXPECT_CALL(*send_algorithm_
,
135 OnCongestionEvent(rtt_updated
, _
,
136 Pointwise(KeyEq(), ack_vector
),
137 Pointwise(KeyEq(), lost_vector
)));
138 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange()).
142 void RetransmitAndSendPacket(QuicPacketSequenceNumber old_sequence_number
,
143 QuicPacketSequenceNumber new_sequence_number
) {
144 QuicSentPacketManagerPeer::MarkForRetransmission(
145 &manager_
, old_sequence_number
, TLP_RETRANSMISSION
);
146 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
147 QuicSentPacketManager::PendingRetransmission next_retransmission
=
148 manager_
.NextPendingRetransmission();
149 EXPECT_EQ(old_sequence_number
, next_retransmission
.sequence_number
);
150 EXPECT_EQ(TLP_RETRANSMISSION
,
151 next_retransmission
.transmission_type
);
153 EXPECT_CALL(*send_algorithm_
,
154 OnPacketSent(_
, BytesInFlight(), new_sequence_number
,
155 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
156 .WillOnce(Return(true));
157 SerializedPacket
packet(CreatePacket(new_sequence_number
, false));
158 manager_
.OnPacketSent(&packet
,
163 HAS_RETRANSMITTABLE_DATA
);
164 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(
165 &manager_
, new_sequence_number
));
168 SerializedPacket
CreateDataPacket(QuicPacketSequenceNumber sequence_number
) {
169 return CreatePacket(sequence_number
, true);
172 SerializedPacket
CreatePacket(QuicPacketSequenceNumber sequence_number
,
173 bool retransmittable
) {
174 packets_
.push_back(QuicPacket::NewDataPacket(
175 nullptr, kDefaultLength
, false, PACKET_8BYTE_CONNECTION_ID
, false,
176 PACKET_6BYTE_SEQUENCE_NUMBER
));
177 return SerializedPacket(
178 sequence_number
, PACKET_6BYTE_SEQUENCE_NUMBER
, packets_
.back(), 0u,
179 retransmittable
? new RetransmittableFrames() : nullptr);
182 SerializedPacket
CreateFecPacket(QuicPacketSequenceNumber sequence_number
) {
183 packets_
.push_back(QuicPacket::NewFecPacket(
184 nullptr, kDefaultLength
, false, PACKET_8BYTE_CONNECTION_ID
, false,
185 PACKET_6BYTE_SEQUENCE_NUMBER
));
186 return SerializedPacket(sequence_number
, PACKET_6BYTE_SEQUENCE_NUMBER
,
187 packets_
.back(), 0u, nullptr);
190 void SendDataPacket(QuicPacketSequenceNumber sequence_number
) {
191 EXPECT_CALL(*send_algorithm_
,
192 OnPacketSent(_
, BytesInFlight(), sequence_number
, _
, _
))
193 .Times(1).WillOnce(Return(true));
194 SerializedPacket
packet(CreateDataPacket(sequence_number
));
195 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(),
196 packet
.packet
->length(), NOT_RETRANSMISSION
,
197 HAS_RETRANSMITTABLE_DATA
);
200 void SendCryptoPacket(QuicPacketSequenceNumber sequence_number
) {
201 EXPECT_CALL(*send_algorithm_
,
202 OnPacketSent(_
, BytesInFlight(), sequence_number
,
203 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
204 .Times(1).WillOnce(Return(true));
205 SerializedPacket
packet(CreateDataPacket(sequence_number
));
206 packet
.retransmittable_frames
->AddStreamFrame(
207 new QuicStreamFrame(1, false, 0, IOVector()));
208 packet
.retransmittable_frames
->set_encryption_level(ENCRYPTION_NONE
);
209 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(),
210 packet
.packet
->length(), NOT_RETRANSMISSION
,
211 HAS_RETRANSMITTABLE_DATA
);
214 void SendFecPacket(QuicPacketSequenceNumber sequence_number
) {
215 EXPECT_CALL(*send_algorithm_
,
216 OnPacketSent(_
, BytesInFlight(), sequence_number
,
217 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
218 .Times(1).WillOnce(Return(true));
219 SerializedPacket
packet(CreateFecPacket(sequence_number
));
220 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(),
221 packet
.packet
->length(), NOT_RETRANSMISSION
,
222 NO_RETRANSMITTABLE_DATA
);
225 void SendAckPacket(QuicPacketSequenceNumber sequence_number
) {
226 EXPECT_CALL(*send_algorithm_
,
227 OnPacketSent(_
, BytesInFlight(), sequence_number
,
228 kDefaultLength
, NO_RETRANSMITTABLE_DATA
))
229 .Times(1).WillOnce(Return(false));
230 SerializedPacket
packet(CreatePacket(sequence_number
, false));
231 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(),
232 packet
.packet
->length(), NOT_RETRANSMISSION
,
233 NO_RETRANSMITTABLE_DATA
);
236 // Based on QuicConnection's WritePendingRetransmissions.
237 void RetransmitNextPacket(
238 QuicPacketSequenceNumber retransmission_sequence_number
) {
239 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
240 EXPECT_CALL(*send_algorithm_
,
241 OnPacketSent(_
, _
, retransmission_sequence_number
,
242 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
243 .Times(1).WillOnce(Return(true));
244 const QuicSentPacketManager::PendingRetransmission pending
=
245 manager_
.NextPendingRetransmission();
246 SerializedPacket
packet(
247 CreatePacket(retransmission_sequence_number
, false));
248 manager_
.OnPacketSent(&packet
, pending
.sequence_number
, clock_
.Now(),
249 kDefaultLength
, pending
.transmission_type
,
250 HAS_RETRANSMITTABLE_DATA
);
253 QuicSentPacketManager manager_
;
254 vector
<QuicPacket
*> packets_
;
256 QuicConnectionStats stats_
;
257 MockSendAlgorithm
* send_algorithm_
;
258 scoped_ptr
<MockNetworkChangeVisitor
> network_change_visitor_
;
261 TEST_F(QuicSentPacketManagerTest
, IsUnacked
) {
262 VerifyUnackedPackets(nullptr, 0);
265 QuicPacketSequenceNumber unacked
[] = { 1 };
266 VerifyUnackedPackets(unacked
, arraysize(unacked
));
267 QuicPacketSequenceNumber retransmittable
[] = { 1 };
268 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
271 TEST_F(QuicSentPacketManagerTest
, IsUnAckedRetransmit
) {
273 RetransmitAndSendPacket(1, 2);
275 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(&manager_
, 2));
276 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
277 VerifyUnackedPackets(unacked
, arraysize(unacked
));
278 QuicPacketSequenceNumber retransmittable
[] = { 2 };
279 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
282 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAck
) {
284 RetransmitAndSendPacket(1, 2);
287 QuicAckFrame ack_frame
;
288 ack_frame
.largest_observed
= 2;
289 ack_frame
.missing_packets
.insert(1);
291 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
293 // Packet 1 is unacked, pending, but not retransmittable.
294 QuicPacketSequenceNumber unacked
[] = { 1 };
295 VerifyUnackedPackets(unacked
, arraysize(unacked
));
296 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
297 VerifyRetransmittablePackets(nullptr, 0);
300 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAckBeforeSend
) {
302 QuicSentPacketManagerPeer::MarkForRetransmission(
303 &manager_
, 1, TLP_RETRANSMISSION
);
304 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
307 QuicAckFrame ack_frame
;
308 ack_frame
.largest_observed
= 1;
310 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
312 // There should no longer be a pending retransmission.
313 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
315 // No unacked packets remain.
316 VerifyUnackedPackets(nullptr, 0);
317 VerifyRetransmittablePackets(nullptr, 0);
318 EXPECT_EQ(0u, stats_
.packets_spuriously_retransmitted
);
321 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAckPrevious
) {
323 RetransmitAndSendPacket(1, 2);
324 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
325 clock_
.AdvanceTime(rtt
);
329 QuicAckFrame ack_frame
;
330 ack_frame
.largest_observed
= 1;
331 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
333 // 2 remains unacked, but no packets have retransmittable data.
334 QuicPacketSequenceNumber unacked
[] = { 2 };
335 VerifyUnackedPackets(unacked
, arraysize(unacked
));
336 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
337 VerifyRetransmittablePackets(nullptr, 0);
339 EXPECT_EQ(1u, stats_
.packets_spuriously_retransmitted
);
342 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAckPreviousThenNackRetransmit
) {
344 RetransmitAndSendPacket(1, 2);
345 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
346 clock_
.AdvanceTime(rtt
);
348 // First, ACK packet 1 which makes packet 2 non-retransmittable.
350 QuicAckFrame ack_frame
;
351 ack_frame
.largest_observed
= 1;
352 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
357 clock_
.AdvanceTime(rtt
);
359 // Next, NACK packet 2 three times.
360 ack_frame
.largest_observed
= 3;
361 ack_frame
.missing_packets
.insert(2);
363 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
365 ack_frame
.largest_observed
= 4;
367 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
369 ack_frame
.largest_observed
= 5;
370 ExpectAckAndLoss(true, 5, 2);
371 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
373 // No packets remain unacked.
374 VerifyUnackedPackets(nullptr, 0);
375 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
376 VerifyRetransmittablePackets(nullptr, 0);
378 // Verify that the retransmission alarm would not fire,
379 // since there is no retransmittable data outstanding.
380 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
383 TEST_F(QuicSentPacketManagerTest
, RetransmitTwiceThenAckPreviousBeforeSend
) {
385 RetransmitAndSendPacket(1, 2);
387 // Fire the RTO, which will mark 2 for retransmission (but will not send it).
388 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
389 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
390 manager_
.OnRetransmissionTimeout();
391 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
393 // Ack 1 but not 2, before 2 is able to be sent.
394 // Since 1 has been retransmitted, it has already been lost, and so the
395 // send algorithm is not informed that it has been ACK'd.
396 QuicAckFrame ack_frame
;
397 ack_frame
.largest_observed
= 1;
399 EXPECT_CALL(*send_algorithm_
, RevertRetransmissionTimeout());
400 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
402 // Since 2 was marked for retransmit, when 1 is acked, 2 is kept for RTT.
403 QuicPacketSequenceNumber unacked
[] = { 2 };
404 VerifyUnackedPackets(unacked
, arraysize(unacked
));
405 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
406 VerifyRetransmittablePackets(nullptr, 0);
408 // Verify that the retransmission alarm would not fire,
409 // since there is no retransmittable data outstanding.
410 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
413 TEST_F(QuicSentPacketManagerTest
, RetransmitTwiceThenAckFirst
) {
414 StrictMock
<MockDebugDelegate
> debug_delegate
;
415 EXPECT_CALL(debug_delegate
, OnSpuriousPacketRetransmition(
416 TLP_RETRANSMISSION
, kDefaultLength
)).Times(2);
417 manager_
.set_debug_delegate(&debug_delegate
);
420 RetransmitAndSendPacket(1, 2);
421 RetransmitAndSendPacket(2, 3);
422 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
423 clock_
.AdvanceTime(rtt
);
425 // Ack 1 but not 2 or 3.
427 QuicAckFrame ack_frame
;
428 ack_frame
.largest_observed
= 1;
429 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
431 // 2 and 3 remain unacked, but no packets have retransmittable data.
432 QuicPacketSequenceNumber unacked
[] = { 2, 3 };
433 VerifyUnackedPackets(unacked
, arraysize(unacked
));
434 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
435 VerifyRetransmittablePackets(nullptr, 0);
437 // Ensure packet 2 is lost when 4 is sent and 3 and 4 are acked.
439 ack_frame
.largest_observed
= 4;
440 ack_frame
.missing_packets
.insert(2);
441 QuicPacketSequenceNumber acked
[] = { 3, 4 };
442 ExpectAcksAndLosses(true, acked
, arraysize(acked
), nullptr, 0);
443 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
445 QuicPacketSequenceNumber unacked2
[] = { 2 };
446 VerifyUnackedPackets(unacked2
, arraysize(unacked2
));
447 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
450 ack_frame
.largest_observed
= 5;
451 ExpectAckAndLoss(true, 5, 2);
452 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
454 VerifyUnackedPackets(nullptr, 0);
455 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
456 EXPECT_EQ(2u, stats_
.packets_spuriously_retransmitted
);
459 TEST_F(QuicSentPacketManagerTest
, LoseButDontRetransmitRevivedPacket
) {
465 // Ack 2 and 3, and mark 1 as revived.
466 QuicAckFrame ack_frame
;
467 ack_frame
.largest_observed
= 3;
468 ack_frame
.missing_packets
.insert(1);
469 ack_frame
.revived_packets
.insert(1);
470 QuicPacketSequenceNumber acked
[] = { 2, 3 };
471 ExpectAcksAndLosses(true, acked
, arraysize(acked
), nullptr, 0);
472 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
474 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
475 QuicPacketSequenceNumber unacked
[] = { 1, 4 };
476 VerifyUnackedPackets(unacked
, arraysize(unacked
));
477 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
478 QuicPacketSequenceNumber retransmittable
[] = { 4 };
479 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
481 // Ack the 4th packet and expect the 1st to be considered lost.
482 ack_frame
.largest_observed
= 4;
483 ExpectAckAndLoss(true, 4, 1);
484 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
486 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
487 VerifyRetransmittablePackets(nullptr, 0);
490 TEST_F(QuicSentPacketManagerTest
, MarkLostThenReviveAndDontRetransmitPacket
) {
497 // Ack 2, 3, and 4, and expect the 1st to be considered lost.
498 QuicAckFrame ack_frame
;
499 ack_frame
.largest_observed
= 4;
500 ack_frame
.missing_packets
.insert(1);
501 QuicPacketSequenceNumber acked
[] = { 2, 3, 4 };
502 QuicPacketSequenceNumber lost
[] = { 1 };
503 ExpectAcksAndLosses(true, acked
, arraysize(acked
), lost
, arraysize(lost
));
504 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
506 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
507 QuicPacketSequenceNumber unacked
[] = { 1, 5 };
508 VerifyUnackedPackets(unacked
, arraysize(unacked
));
509 QuicPacketSequenceNumber retransmittable
[] = { 1 };
510 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
512 // Ack 5th packet (FEC) and revive 1st packet. 1st packet should now be
513 // removed from pending retransmissions map.
514 ack_frame
.largest_observed
= 5;
515 ack_frame
.revived_packets
.insert(1);
517 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
519 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
520 VerifyRetransmittablePackets(nullptr, 0);
523 TEST_F(QuicSentPacketManagerTest
, TruncatedAck
) {
525 RetransmitAndSendPacket(1, 2);
526 RetransmitAndSendPacket(2, 3);
527 RetransmitAndSendPacket(3, 4);
528 RetransmitAndSendPacket(4, 5);
530 // Truncated ack with 4 NACKs, so the first packet is lost.
531 QuicAckFrame ack_frame
;
532 ack_frame
.largest_observed
= 4;
533 ack_frame
.missing_packets
.insert(1);
534 ack_frame
.missing_packets
.insert(2);
535 ack_frame
.missing_packets
.insert(3);
536 ack_frame
.missing_packets
.insert(4);
537 ack_frame
.is_truncated
= true;
539 QuicPacketSequenceNumber lost
[] = { 1 };
540 ExpectAcksAndLosses(true, nullptr, 0, lost
, arraysize(lost
));
541 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
543 // High water mark will be raised.
544 QuicPacketSequenceNumber unacked
[] = { 2, 3, 4, 5 };
545 VerifyUnackedPackets(unacked
, arraysize(unacked
));
546 QuicPacketSequenceNumber retransmittable
[] = { 5 };
547 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
550 TEST_F(QuicSentPacketManagerTest
, AckPreviousTransmissionThenTruncatedAck
) {
552 RetransmitAndSendPacket(1, 2);
553 RetransmitAndSendPacket(2, 3);
554 RetransmitAndSendPacket(3, 4);
561 // Ack previous transmission
563 QuicAckFrame ack_frame
;
564 ack_frame
.largest_observed
= 2;
565 ack_frame
.missing_packets
.insert(1);
567 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
568 EXPECT_TRUE(manager_
.IsUnacked(4));
571 // Truncated ack with 4 NACKs
573 QuicAckFrame ack_frame
;
574 ack_frame
.largest_observed
= 6;
575 ack_frame
.missing_packets
.insert(3);
576 ack_frame
.missing_packets
.insert(4);
577 ack_frame
.missing_packets
.insert(5);
578 ack_frame
.missing_packets
.insert(6);
579 ack_frame
.is_truncated
= true;
580 ExpectAckAndLoss(true, 1, 3);
581 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
584 // High water mark will be raised.
585 QuicPacketSequenceNumber unacked
[] = { 4, 5, 6, 7, 8, 9 };
586 VerifyUnackedPackets(unacked
, arraysize(unacked
));
587 QuicPacketSequenceNumber retransmittable
[] = { 5, 6, 7, 8, 9 };
588 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
591 TEST_F(QuicSentPacketManagerTest
, GetLeastUnacked
) {
592 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
595 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedUnacked
) {
597 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
600 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedUnackedFec
) {
602 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
605 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedAndDiscard
) {
606 VerifyUnackedPackets(nullptr, 0);
609 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
612 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
615 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
617 QuicPacketSequenceNumber unacked
[] = { 1, 2, 3 };
618 VerifyUnackedPackets(unacked
, arraysize(unacked
));
619 VerifyRetransmittablePackets(nullptr, 0);
621 // Ack 2, so there's an rtt update.
623 QuicAckFrame ack_frame
;
624 ack_frame
.largest_observed
= 2;
625 ack_frame
.missing_packets
.insert(1);
626 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
628 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
631 TEST_F(QuicSentPacketManagerTest
, GetSentTime
) {
632 VerifyUnackedPackets(nullptr, 0);
634 QuicTime sent_time
= clock_
.Now();
636 QuicTime sent_time2
= clock_
.Now();
638 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
639 VerifyUnackedPackets(unacked
, arraysize(unacked
));
640 VerifyRetransmittablePackets(nullptr, 0);
642 EXPECT_TRUE(manager_
.HasUnackedPackets());
643 EXPECT_EQ(sent_time
, QuicSentPacketManagerPeer::GetSentTime(&manager_
, 1));
644 EXPECT_EQ(sent_time2
, QuicSentPacketManagerPeer::GetSentTime(&manager_
, 2));
647 TEST_F(QuicSentPacketManagerTest
, AckAckAndUpdateRtt
) {
651 // Now ack the ack and expect an RTT update.
652 QuicAckFrame ack_frame
;
653 ack_frame
.largest_observed
= 2;
654 ack_frame
.delta_time_largest_observed
=
655 QuicTime::Delta::FromMilliseconds(5);
658 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
662 // Now ack the ack and expect only an RTT update.
663 ack_frame
.largest_observed
= 3;
665 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
668 TEST_F(QuicSentPacketManagerTest
, Rtt
) {
669 QuicPacketSequenceNumber sequence_number
= 1;
670 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(15);
671 SendDataPacket(sequence_number
);
672 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
674 ExpectAck(sequence_number
);
675 QuicAckFrame ack_frame
;
676 ack_frame
.largest_observed
= sequence_number
;
677 ack_frame
.delta_time_largest_observed
=
678 QuicTime::Delta::FromMilliseconds(5);
679 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
680 EXPECT_EQ(expected_rtt
,
681 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
684 TEST_F(QuicSentPacketManagerTest
, RttWithInvalidDelta
) {
685 // Expect that the RTT is equal to the local time elapsed, since the
686 // delta_time_largest_observed is larger than the local time elapsed
687 // and is hence invalid.
688 QuicPacketSequenceNumber sequence_number
= 1;
689 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(10);
690 SendDataPacket(sequence_number
);
691 clock_
.AdvanceTime(expected_rtt
);
693 ExpectAck(sequence_number
);
694 QuicAckFrame ack_frame
;
695 ack_frame
.largest_observed
= sequence_number
;
696 ack_frame
.delta_time_largest_observed
=
697 QuicTime::Delta::FromMilliseconds(11);
698 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
699 EXPECT_EQ(expected_rtt
,
700 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
703 TEST_F(QuicSentPacketManagerTest
, RttWithInfiniteDelta
) {
704 // Expect that the RTT is equal to the local time elapsed, since the
705 // delta_time_largest_observed is infinite, and is hence invalid.
706 QuicPacketSequenceNumber sequence_number
= 1;
707 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(10);
708 SendDataPacket(sequence_number
);
709 clock_
.AdvanceTime(expected_rtt
);
711 ExpectAck(sequence_number
);
712 QuicAckFrame ack_frame
;
713 ack_frame
.largest_observed
= sequence_number
;
714 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Infinite();
715 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
716 EXPECT_EQ(expected_rtt
,
717 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
720 TEST_F(QuicSentPacketManagerTest
, RttZeroDelta
) {
721 // Expect that the RTT is the time between send and receive since the
722 // delta_time_largest_observed is zero.
723 QuicPacketSequenceNumber sequence_number
= 1;
724 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(10);
725 SendDataPacket(sequence_number
);
726 clock_
.AdvanceTime(expected_rtt
);
728 ExpectAck(sequence_number
);
729 QuicAckFrame ack_frame
;
730 ack_frame
.largest_observed
= sequence_number
;
731 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
732 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
733 EXPECT_EQ(expected_rtt
,
734 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
737 TEST_F(QuicSentPacketManagerTest
, TailLossProbeTimeout
) {
738 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
741 QuicPacketSequenceNumber sequence_number
= 1;
742 SendDataPacket(sequence_number
);
744 // The first tail loss probe retransmits 1 packet.
745 manager_
.OnRetransmissionTimeout();
746 EXPECT_EQ(QuicTime::Delta::Zero(),
747 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
748 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
749 manager_
.MaybeRetransmitTailLossProbe();
750 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
751 RetransmitNextPacket(2);
752 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
754 // The second tail loss probe retransmits 1 packet.
755 manager_
.OnRetransmissionTimeout();
756 EXPECT_EQ(QuicTime::Delta::Zero(),
757 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
758 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
759 manager_
.MaybeRetransmitTailLossProbe();
760 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
761 RetransmitNextPacket(3);
762 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
763 QuicTime::Delta::Infinite()));
764 EXPECT_EQ(QuicTime::Delta::Infinite(),
765 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
766 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
768 // Ack the third and ensure the first two are still pending.
770 QuicAckFrame ack_frame
;
771 ack_frame
.largest_observed
= 3;
772 ack_frame
.missing_packets
.insert(1);
773 ack_frame
.missing_packets
.insert(2);
774 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
776 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
778 // Acking two more packets will lose both of them due to nacks.
779 ack_frame
.largest_observed
= 5;
780 QuicPacketSequenceNumber lost
[] = { 1, 2 };
781 ExpectAcksAndLosses(false, nullptr, 0, lost
, arraysize(lost
));
782 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
784 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
785 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
786 EXPECT_EQ(2u, stats_
.tlp_count
);
787 EXPECT_EQ(0u, stats_
.rto_count
);
790 TEST_F(QuicSentPacketManagerTest
, TailLossProbeThenRTO
) {
791 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
794 const size_t kNumSentPackets
= 100;
795 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
799 // The first tail loss probe retransmits 1 packet.
800 manager_
.OnRetransmissionTimeout();
801 EXPECT_EQ(QuicTime::Delta::Zero(),
802 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
803 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
804 manager_
.MaybeRetransmitTailLossProbe();
805 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
806 RetransmitNextPacket(101);
807 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
808 QuicTime::Delta::Infinite()));
809 EXPECT_EQ(QuicTime::Delta::Infinite(),
810 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
811 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
813 // The second tail loss probe retransmits 1 packet.
814 manager_
.OnRetransmissionTimeout();
815 EXPECT_EQ(QuicTime::Delta::Zero(),
816 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
817 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
818 EXPECT_TRUE(manager_
.MaybeRetransmitTailLossProbe());
819 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
820 RetransmitNextPacket(102);
821 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
822 QuicTime::Delta::Infinite()));
823 EXPECT_EQ(QuicTime::Delta::Infinite(),
824 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
826 // Advance the time enough to ensure all packets are RTO'd.
827 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
829 // The final RTO abandons all of them.
830 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
831 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
832 manager_
.OnRetransmissionTimeout();
833 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
834 EXPECT_EQ(2u, stats_
.tlp_count
);
835 EXPECT_EQ(1u, stats_
.rto_count
);
838 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeTimeout
) {
839 // Send 2 crypto packets and 3 data packets.
840 const size_t kNumSentCryptoPackets
= 2;
841 for (size_t i
= 1; i
<= kNumSentCryptoPackets
; ++i
) {
844 const size_t kNumSentDataPackets
= 3;
845 for (size_t i
= 1; i
<= kNumSentDataPackets
; ++i
) {
846 SendDataPacket(kNumSentCryptoPackets
+ i
);
848 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
850 // The first retransmits 2 packets.
851 manager_
.OnRetransmissionTimeout();
852 EXPECT_EQ(QuicTime::Delta::Zero(),
853 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
854 RetransmitNextPacket(6);
855 RetransmitNextPacket(7);
856 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
857 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
859 // The second retransmits 2 packets.
860 manager_
.OnRetransmissionTimeout();
861 EXPECT_EQ(QuicTime::Delta::Zero(),
862 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
863 RetransmitNextPacket(8);
864 RetransmitNextPacket(9);
865 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
866 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
868 // Now ack the two crypto packets and the speculatively encrypted request,
869 // and ensure the first four crypto packets get abandoned, but not lost.
870 QuicPacketSequenceNumber acked
[] = { 3, 4, 5, 8, 9 };
871 ExpectAcksAndLosses(true, acked
, arraysize(acked
), nullptr, 0);
872 QuicAckFrame ack_frame
;
873 ack_frame
.largest_observed
= 9;
874 ack_frame
.missing_packets
.insert(1);
875 ack_frame
.missing_packets
.insert(2);
876 ack_frame
.missing_packets
.insert(6);
877 ack_frame
.missing_packets
.insert(7);
878 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
880 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
883 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeTimeoutVersionNegotiation
) {
884 // Send 2 crypto packets and 3 data packets.
885 const size_t kNumSentCryptoPackets
= 2;
886 for (size_t i
= 1; i
<= kNumSentCryptoPackets
; ++i
) {
889 const size_t kNumSentDataPackets
= 3;
890 for (size_t i
= 1; i
<= kNumSentDataPackets
; ++i
) {
891 SendDataPacket(kNumSentCryptoPackets
+ i
);
893 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
895 // The first retransmission timeout retransmits 2 crypto packets.
896 manager_
.OnRetransmissionTimeout();
897 RetransmitNextPacket(6);
898 RetransmitNextPacket(7);
899 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
900 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
902 // Now act like a version negotiation packet arrived, which would cause all
903 // unacked packets to be retransmitted.
904 manager_
.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION
);
906 // Ensure the first two pending packets are the crypto retransmits.
907 ASSERT_TRUE(manager_
.HasPendingRetransmissions());
908 EXPECT_EQ(6u, manager_
.NextPendingRetransmission().sequence_number
);
909 RetransmitNextPacket(8);
910 EXPECT_EQ(7u, manager_
.NextPendingRetransmission().sequence_number
);
911 RetransmitNextPacket(9);
913 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
914 // Send 3 more data packets and ensure the least unacked is raised.
915 RetransmitNextPacket(10);
916 RetransmitNextPacket(11);
917 RetransmitNextPacket(12);
918 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
920 EXPECT_EQ(8u, manager_
.GetLeastUnacked());
923 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeSpuriousRetransmission
) {
924 // Send 1 crypto packet.
926 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
928 // Retransmit the crypto packet as 2.
929 manager_
.OnRetransmissionTimeout();
930 RetransmitNextPacket(2);
932 // Retransmit the crypto packet as 3.
933 manager_
.OnRetransmissionTimeout();
934 RetransmitNextPacket(3);
936 // Now ack the second crypto packet, and ensure the first gets removed, but
937 // the third does not.
939 QuicAckFrame ack_frame
;
940 ack_frame
.largest_observed
= 2;
941 ack_frame
.missing_packets
.insert(1);
942 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
944 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
945 QuicPacketSequenceNumber unacked
[] = { 3 };
946 VerifyUnackedPackets(unacked
, arraysize(unacked
));
949 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeTimeoutUnsentDataPacket
) {
950 // Send 2 crypto packets and 1 data packet.
951 const size_t kNumSentCryptoPackets
= 2;
952 for (size_t i
= 1; i
<= kNumSentCryptoPackets
; ++i
) {
956 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
958 // Retransmit 2 crypto packets, but not the serialized packet.
959 manager_
.OnRetransmissionTimeout();
960 RetransmitNextPacket(4);
961 RetransmitNextPacket(5);
962 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
963 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
966 TEST_F(QuicSentPacketManagerTest
,
967 CryptoHandshakeRetransmissionThenRetransmitAll
) {
968 // Send 1 crypto packet.
970 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
972 // Retransmit the crypto packet as 2.
973 manager_
.OnRetransmissionTimeout();
974 RetransmitNextPacket(2);
976 // Now retransmit all the unacked packets, which occurs when there is a
977 // version negotiation.
978 manager_
.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION
);
979 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
980 VerifyUnackedPackets(unacked
, arraysize(unacked
));
981 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
982 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
983 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
986 TEST_F(QuicSentPacketManagerTest
,
987 CryptoHandshakeRetransmissionThenNeuterAndAck
) {
988 // Send 1 crypto packet.
990 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
992 // Retransmit the crypto packet as 2.
993 manager_
.OnRetransmissionTimeout();
994 RetransmitNextPacket(2);
996 // Retransmit the crypto packet as 3.
997 manager_
.OnRetransmissionTimeout();
998 RetransmitNextPacket(3);
1000 // Now neuter all unacked unencrypted packets, which occurs when the
1001 // connection goes forward secure.
1002 manager_
.NeuterUnencryptedPackets();
1003 QuicPacketSequenceNumber unacked
[] = { 1, 2, 3};
1004 VerifyUnackedPackets(unacked
, arraysize(unacked
));
1005 VerifyRetransmittablePackets(nullptr, 0);
1006 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1007 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1008 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
1010 // Ensure both packets get discarded when packet 2 is acked.
1011 QuicAckFrame ack_frame
;
1012 ack_frame
.largest_observed
= 3;
1013 ack_frame
.missing_packets
.insert(1);
1014 ack_frame
.missing_packets
.insert(2);
1015 ExpectUpdatedRtt(3);
1016 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
1017 VerifyUnackedPackets(nullptr, 0);
1018 VerifyRetransmittablePackets(nullptr, 0);
1021 TEST_F(QuicSentPacketManagerTest
, ResetRecentMinRTTWithEmptyWindow
) {
1022 QuicTime::Delta min_rtt
= QuicTime::Delta::FromMilliseconds(50);
1023 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->UpdateRtt(
1024 min_rtt
, QuicTime::Delta::Zero(), QuicTime::Zero());
1026 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->min_rtt());
1028 QuicSentPacketManagerPeer::GetRttStats(
1029 &manager_
)->recent_min_rtt());
1031 // Send two packets with no prior bytes in flight.
1035 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
1036 // Ack two packets with 100ms RTT observations.
1037 QuicAckFrame ack_frame
;
1038 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1039 ack_frame
.largest_observed
= 1;
1041 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1043 // First ack does not change recent min rtt.
1045 QuicSentPacketManagerPeer::GetRttStats(
1046 &manager_
)->recent_min_rtt());
1048 ack_frame
.largest_observed
= 2;
1050 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1053 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->min_rtt());
1054 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(100),
1055 QuicSentPacketManagerPeer::GetRttStats(
1056 &manager_
)->recent_min_rtt());
1059 TEST_F(QuicSentPacketManagerTest
, RetransmissionTimeout
) {
1060 // Send 100 packets and then ensure all are abandoned when the RTO fires.
1061 const size_t kNumSentPackets
= 100;
1062 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
1066 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1067 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1068 EXPECT_FALSE(manager_
.MaybeRetransmitTailLossProbe());
1069 manager_
.OnRetransmissionTimeout();
1072 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTime
) {
1073 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
1076 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeCryptoHandshake
) {
1077 SendCryptoPacket(1);
1080 RttStats
* rtt_stats
= QuicSentPacketManagerPeer::GetRttStats(&manager_
);
1081 rtt_stats
->set_initial_rtt_us(1 * kNumMicrosPerMilli
);
1082 EXPECT_EQ(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1083 manager_
.GetRetransmissionTime());
1085 // Test with a standard smoothed RTT.
1086 rtt_stats
->set_initial_rtt_us(100 * kNumMicrosPerMilli
);
1088 QuicTime::Delta srtt
=
1089 QuicTime::Delta::FromMicroseconds(rtt_stats
->initial_rtt_us());
1090 QuicTime expected_time
= clock_
.Now().Add(srtt
.Multiply(1.5));
1091 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1093 // Retransmit the packet by invoking the retransmission timeout.
1094 clock_
.AdvanceTime(srtt
.Multiply(1.5));
1095 manager_
.OnRetransmissionTimeout();
1096 RetransmitNextPacket(2);
1098 // The retransmission time should now be twice as far in the future.
1099 expected_time
= clock_
.Now().Add(srtt
.Multiply(2).Multiply(1.5));
1100 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1103 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeTailLossProbe
) {
1104 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
1109 RttStats
* rtt_stats
= QuicSentPacketManagerPeer::GetRttStats(&manager_
);
1110 rtt_stats
->set_initial_rtt_us(1 * kNumMicrosPerMilli
);
1111 EXPECT_EQ(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1112 manager_
.GetRetransmissionTime());
1114 // Test with a standard smoothed RTT.
1115 rtt_stats
->set_initial_rtt_us(100 * kNumMicrosPerMilli
);
1116 QuicTime::Delta srtt
=
1117 QuicTime::Delta::FromMicroseconds(rtt_stats
->initial_rtt_us());
1118 QuicTime::Delta expected_tlp_delay
= srtt
.Multiply(2);
1119 QuicTime expected_time
= clock_
.Now().Add(expected_tlp_delay
);
1120 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1122 // Retransmit the packet by invoking the retransmission timeout.
1123 clock_
.AdvanceTime(expected_tlp_delay
);
1124 manager_
.OnRetransmissionTimeout();
1125 EXPECT_EQ(QuicTime::Delta::Zero(),
1126 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1127 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1128 EXPECT_TRUE(manager_
.MaybeRetransmitTailLossProbe());
1129 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1130 RetransmitNextPacket(3);
1131 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1132 QuicTime::Delta::Infinite()));
1133 EXPECT_EQ(QuicTime::Delta::Infinite(),
1134 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1135 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1137 expected_time
= clock_
.Now().Add(expected_tlp_delay
);
1138 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1141 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeRTO
) {
1142 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->UpdateRtt(
1143 QuicTime::Delta::FromMilliseconds(100),
1144 QuicTime::Delta::Zero(),
1152 QuicTime::Delta expected_rto_delay
= QuicTime::Delta::FromMilliseconds(500);
1153 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1154 .WillRepeatedly(Return(expected_rto_delay
));
1155 QuicTime expected_time
= clock_
.Now().Add(expected_rto_delay
);
1156 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1158 // Retransmit the packet by invoking the retransmission timeout.
1159 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1160 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1161 clock_
.AdvanceTime(expected_rto_delay
);
1162 manager_
.OnRetransmissionTimeout();
1163 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1164 RetransmitNextPacket(5);
1165 RetransmitNextPacket(6);
1166 EXPECT_EQ(2 * kDefaultLength
,
1167 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1168 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1170 // The delay should double the second time.
1171 expected_time
= clock_
.Now().Add(expected_rto_delay
).Add(expected_rto_delay
);
1172 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1174 // Ack a packet and ensure the RTO goes back to the original value.
1175 QuicAckFrame ack_frame
;
1176 ack_frame
.largest_observed
= 2;
1177 ack_frame
.missing_packets
.insert(1);
1178 ExpectUpdatedRtt(2);
1179 EXPECT_CALL(*send_algorithm_
, RevertRetransmissionTimeout());
1180 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
1181 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1182 EXPECT_EQ(4 * kDefaultLength
,
1183 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1185 // Wait 2RTTs from now for the RTO, since it's the max of the RTO time
1186 // and the TLP time. In production, there would always be two TLP's first.
1187 // Since retransmission was spurious, smoothed_rtt_ is expired, and replaced
1188 // by the latest RTT sample of 500ms.
1189 expected_time
= clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(1000));
1190 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1193 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelayMin
) {
1195 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1196 .WillRepeatedly(Return(QuicTime::Delta::FromMilliseconds(1)));
1197 QuicTime::Delta delay
= QuicTime::Delta::FromMilliseconds(200);
1199 // If the delay is smaller than the min, ensure it exponentially backs off
1201 for (int i
= 0; i
< 5; ++i
) {
1203 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1204 delay
= delay
.Add(delay
);
1205 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1206 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1207 manager_
.OnRetransmissionTimeout();
1208 RetransmitNextPacket(i
+ 2);
1212 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelayMax
) {
1213 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1214 .WillOnce(Return(QuicTime::Delta::FromSeconds(500)));
1216 EXPECT_EQ(QuicTime::Delta::FromSeconds(60),
1217 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1220 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelay
) {
1222 QuicTime::Delta delay
= QuicTime::Delta::FromMilliseconds(500);
1223 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1224 .WillRepeatedly(Return(delay
));
1226 // Delay should back off exponentially.
1227 for (int i
= 0; i
< 5; ++i
) {
1229 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1230 delay
= delay
.Add(delay
);
1231 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1232 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1233 manager_
.OnRetransmissionTimeout();
1234 RetransmitNextPacket(i
+ 2);
1238 TEST_F(QuicSentPacketManagerTest
, GetLossDelay
) {
1239 MockLossAlgorithm
* loss_algorithm
= new MockLossAlgorithm();
1240 QuicSentPacketManagerPeer::SetLossAlgorithm(&manager_
, loss_algorithm
);
1242 EXPECT_CALL(*loss_algorithm
, GetLossTimeout())
1243 .WillRepeatedly(Return(QuicTime::Zero()));
1247 // Handle an ack which causes the loss algorithm to be evaluated and
1248 // set the loss timeout.
1250 EXPECT_CALL(*loss_algorithm
, DetectLostPackets(_
, _
, _
, _
))
1251 .WillOnce(Return(SequenceNumberSet()));
1252 QuicAckFrame ack_frame
;
1253 ack_frame
.largest_observed
= 2;
1254 ack_frame
.missing_packets
.insert(1);
1255 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1257 QuicTime
timeout(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)));
1258 EXPECT_CALL(*loss_algorithm
, GetLossTimeout())
1259 .WillRepeatedly(Return(timeout
));
1260 EXPECT_EQ(timeout
, manager_
.GetRetransmissionTime());
1262 // Fire the retransmission timeout and ensure the loss detection algorithm
1264 EXPECT_CALL(*loss_algorithm
, DetectLostPackets(_
, _
, _
, _
))
1265 .WillOnce(Return(SequenceNumberSet()));
1266 manager_
.OnRetransmissionTimeout();
1269 TEST_F(QuicSentPacketManagerTest
, NegotiateTimeLossDetectionFromOptions
) {
1271 QuicSentPacketManagerPeer::GetLossAlgorithm(
1272 &manager_
)->GetLossDetectionType());
1275 QuicTagVector options
;
1276 options
.push_back(kTIME
);
1277 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1278 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1279 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1280 manager_
.SetFromConfig(config
);
1283 QuicSentPacketManagerPeer::GetLossAlgorithm(
1284 &manager_
)->GetLossDetectionType());
1287 TEST_F(QuicSentPacketManagerTest
, NegotiateCongestionControlFromOptions
) {
1288 ValueRestore
<bool> old_flag(&FLAGS_quic_allow_bbr
, true);
1290 QuicTagVector options
;
1292 options
.push_back(kRENO
);
1293 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1294 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1295 manager_
.SetFromConfig(config
);
1296 EXPECT_EQ(kReno
, QuicSentPacketManagerPeer::GetSendAlgorithm(
1297 manager_
)->GetCongestionControlType());
1299 // TODO(rtenneti): Enable the following code after BBR code is checked in.
1302 options
.push_back(kTBBR
);
1303 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1304 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1305 manager_
.SetFromConfig(config
);
1306 EXPECT_EQ(kBBR
, QuicSentPacketManagerPeer::GetSendAlgorithm(
1307 manager_
)->GetCongestionControlType());
1311 TEST_F(QuicSentPacketManagerTest
, NegotiateNumConnectionsFromOptions
) {
1313 QuicTagVector options
;
1315 options
.push_back(k1CON
);
1316 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1317 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1318 EXPECT_CALL(*send_algorithm_
, SetNumEmulatedConnections(1));
1319 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1320 manager_
.SetFromConfig(config
);
1322 QuicSentPacketManagerPeer::SetIsServer(&manager_
, false);
1323 QuicConfig client_config
;
1324 client_config
.SetConnectionOptionsToSend(options
);
1325 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1326 EXPECT_CALL(*send_algorithm_
, SetNumEmulatedConnections(1));
1327 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1328 manager_
.SetFromConfig(client_config
);
1331 TEST_F(QuicSentPacketManagerTest
, NegotiateNConnectionFromOptions
) {
1332 // By default, changing the number of open streams does nothing.
1333 manager_
.SetNumOpenStreams(5);
1336 QuicTagVector options
;
1338 options
.push_back(kNCON
);
1339 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1340 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1341 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1342 manager_
.SetFromConfig(config
);
1344 EXPECT_CALL(*send_algorithm_
, SetNumEmulatedConnections(5));
1345 manager_
.SetNumOpenStreams(5);
1348 TEST_F(QuicSentPacketManagerTest
, NegotiateNoTLPFromOptionsAtServer
) {
1350 QuicTagVector options
;
1352 options
.push_back(kNTLP
);
1353 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1354 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1355 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1356 manager_
.SetFromConfig(config
);
1357 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_
));
1360 TEST_F(QuicSentPacketManagerTest
, NegotiateNoTLPFromOptionsAtClient
) {
1361 QuicConfig client_config
;
1362 QuicTagVector options
;
1364 options
.push_back(kNTLP
);
1365 QuicSentPacketManagerPeer::SetIsServer(&manager_
, false);
1366 client_config
.SetConnectionOptionsToSend(options
);
1367 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1368 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1369 manager_
.SetFromConfig(client_config
);
1370 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_
));
1373 TEST_F(QuicSentPacketManagerTest
, NegotiatePacingFromOptions
) {
1374 EXPECT_FALSE(manager_
.using_pacing());
1377 QuicTagVector options
;
1378 options
.push_back(kPACE
);
1379 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1380 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1381 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, /* using_pacing= */ true));
1382 manager_
.SetFromConfig(config
);
1384 EXPECT_TRUE(manager_
.using_pacing());
1387 TEST_F(QuicSentPacketManagerTest
, EnablePacingViaFlag
) {
1388 EXPECT_FALSE(manager_
.using_pacing());
1390 // If pacing is enabled via command-line flag, it will be turned on,
1391 // regardless of the contents of the config.
1392 ValueRestore
<bool> old_flag(&FLAGS_quic_enable_pacing
, true);
1394 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1395 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, /* using_pacing= */ true));
1396 manager_
.SetFromConfig(config
);
1398 EXPECT_TRUE(manager_
.using_pacing());
1401 TEST_F(QuicSentPacketManagerTest
, NegotiateReceiveWindowFromOptions
) {
1402 EXPECT_EQ(kDefaultSocketReceiveBuffer
,
1403 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_
));
1405 // Try to set a size below the minimum and ensure it gets set to the min.
1406 QuicConfig client_config
;
1407 QuicConfigPeer::SetReceivedSocketReceiveBuffer(&client_config
, 1024);
1408 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1409 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1410 manager_
.SetFromConfig(client_config
);
1412 EXPECT_EQ(kMinSocketReceiveBuffer
,
1413 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_
));
1415 // Ensure the smaller send window only allows 16 packets to be sent.
1416 for (QuicPacketSequenceNumber i
= 1; i
<= 16; ++i
) {
1417 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1418 QuicTime::Delta::Zero()));
1419 EXPECT_EQ(QuicTime::Delta::Zero(),
1420 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1421 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, BytesInFlight(), i
,
1422 1024, HAS_RETRANSMITTABLE_DATA
))
1423 .WillOnce(Return(true));
1424 SerializedPacket
packet(CreatePacket(i
, true));
1425 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(), 1024,
1426 NOT_RETRANSMISSION
, HAS_RETRANSMITTABLE_DATA
);
1428 EXPECT_EQ(QuicTime::Delta::Infinite(),
1429 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1432 TEST_F(QuicSentPacketManagerTest
, ReceiveWindowLimited
) {
1433 EXPECT_EQ(kDefaultSocketReceiveBuffer
,
1434 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_
));
1436 // Ensure the smaller send window only allows 256 * 0.95 packets to be sent.
1437 for (QuicPacketSequenceNumber i
= 1; i
<= 244; ++i
) {
1438 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1439 QuicTime::Delta::Zero()));
1440 EXPECT_EQ(QuicTime::Delta::Zero(),
1441 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1442 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, BytesInFlight(), i
,
1443 1024, HAS_RETRANSMITTABLE_DATA
))
1444 .WillOnce(Return(true));
1445 SerializedPacket
packet(CreatePacket(i
, true));
1446 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(), 1024,
1447 NOT_RETRANSMISSION
, HAS_RETRANSMITTABLE_DATA
);
1449 EXPECT_EQ(QuicTime::Delta::Infinite(),
1450 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1453 TEST_F(QuicSentPacketManagerTest
, UseInitialRoundTripTimeToSend
) {
1454 uint32 initial_rtt_us
= 325000;
1455 EXPECT_NE(initial_rtt_us
,
1456 manager_
.GetRttStats()->smoothed_rtt().ToMicroseconds());
1459 config
.SetInitialRoundTripTimeUsToSend(initial_rtt_us
);
1460 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1461 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1462 manager_
.SetFromConfig(config
);
1464 EXPECT_EQ(0, manager_
.GetRttStats()->smoothed_rtt().ToMicroseconds());
1465 EXPECT_EQ(initial_rtt_us
, manager_
.GetRttStats()->initial_rtt_us());