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
;
21 using testing::Pointwise
;
22 using testing::Return
;
23 using testing::StrictMock
;
30 // Default packet length.
31 const uint32 kDefaultLength
= 1000;
33 // Matcher to check the key of the key-value pair it receives as first argument
34 // equals its second argument.
36 return std::tr1::get
<0>(arg
).first
== std::tr1::get
<1>(arg
);
39 class MockDebugDelegate
: public QuicSentPacketManager::DebugDelegate
{
41 MOCK_METHOD2(OnSpuriousPacketRetransmission
,
42 void(TransmissionType transmission_type
,
43 QuicByteCount byte_size
));
46 class QuicSentPacketManagerTest
: public ::testing::TestWithParam
<bool> {
48 QuicSentPacketManagerTest()
49 : manager_(true, &clock_
, &stats_
, kCubic
, kNack
, false),
50 send_algorithm_(new StrictMock
<MockSendAlgorithm
>),
51 network_change_visitor_(new StrictMock
<MockNetworkChangeVisitor
>) {
52 QuicSentPacketManagerPeer::SetSendAlgorithm(&manager_
, send_algorithm_
);
53 // Disable tail loss probes for most tests.
54 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 0);
55 // Advance the time 1s so the send times are never QuicTime::Zero.
56 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
57 manager_
.set_network_change_visitor(network_change_visitor_
.get());
59 EXPECT_CALL(*send_algorithm_
, HasReliableBandwidthEstimate())
61 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate())
63 .WillRepeatedly(Return(QuicBandwidth::Zero()));
64 EXPECT_CALL(*send_algorithm_
, InSlowStart()).Times(AnyNumber());
65 EXPECT_CALL(*send_algorithm_
, InRecovery()).Times(AnyNumber());
68 ~QuicSentPacketManagerTest() override
{ 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_
.GetLeastUnacked());
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
, _
)), IsEmpty()));
103 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
104 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
107 void ExpectUpdatedRtt(QuicPacketSequenceNumber largest_observed
) {
108 EXPECT_CALL(*send_algorithm_
,
109 OnCongestionEvent(true, _
, IsEmpty(), IsEmpty()));
110 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
111 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
114 void ExpectAckAndLoss(bool rtt_updated
,
115 QuicPacketSequenceNumber largest_observed
,
116 QuicPacketSequenceNumber lost_packet
) {
117 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(
118 rtt_updated
, _
, ElementsAre(Pair(largest_observed
, _
)),
119 ElementsAre(Pair(lost_packet
, _
))));
120 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
121 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
124 // |packets_acked| and |packets_lost| should be in sequence number order.
125 void ExpectAcksAndLosses(bool rtt_updated
,
126 QuicPacketSequenceNumber
* packets_acked
,
127 size_t num_packets_acked
,
128 QuicPacketSequenceNumber
* packets_lost
,
129 size_t num_packets_lost
) {
130 vector
<QuicPacketSequenceNumber
> ack_vector
;
131 for (size_t i
= 0; i
< num_packets_acked
; ++i
) {
132 ack_vector
.push_back(packets_acked
[i
]);
134 vector
<QuicPacketSequenceNumber
> lost_vector
;
135 for (size_t i
= 0; i
< num_packets_lost
; ++i
) {
136 lost_vector
.push_back(packets_lost
[i
]);
138 EXPECT_CALL(*send_algorithm_
,
139 OnCongestionEvent(rtt_updated
, _
,
140 Pointwise(KeyEq(), ack_vector
),
141 Pointwise(KeyEq(), lost_vector
)));
142 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange()).
144 EXPECT_CALL(*network_change_visitor_
, OnRttChange()).Times(AnyNumber());
147 void RetransmitAndSendPacket(QuicPacketSequenceNumber old_sequence_number
,
148 QuicPacketSequenceNumber new_sequence_number
) {
149 QuicSentPacketManagerPeer::MarkForRetransmission(
150 &manager_
, old_sequence_number
, TLP_RETRANSMISSION
);
151 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
152 QuicSentPacketManager::PendingRetransmission next_retransmission
=
153 manager_
.NextPendingRetransmission();
154 EXPECT_EQ(old_sequence_number
, next_retransmission
.sequence_number
);
155 EXPECT_EQ(TLP_RETRANSMISSION
,
156 next_retransmission
.transmission_type
);
158 EXPECT_CALL(*send_algorithm_
,
159 OnPacketSent(_
, BytesInFlight(), new_sequence_number
,
160 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
161 .WillOnce(Return(true));
162 SerializedPacket
packet(CreatePacket(new_sequence_number
, false));
163 manager_
.OnPacketSent(&packet
,
168 HAS_RETRANSMITTABLE_DATA
);
169 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(
170 &manager_
, new_sequence_number
));
173 SerializedPacket
CreateDataPacket(QuicPacketSequenceNumber sequence_number
) {
174 return CreatePacket(sequence_number
, true);
177 SerializedPacket
CreatePacket(QuicPacketSequenceNumber sequence_number
,
178 bool retransmittable
) {
179 packets_
.push_back(new QuicEncryptedPacket(nullptr, kDefaultLength
));
180 return SerializedPacket(
181 sequence_number
, PACKET_6BYTE_SEQUENCE_NUMBER
, packets_
.back(), 0u,
182 retransmittable
? new RetransmittableFrames(ENCRYPTION_NONE
) : nullptr);
185 SerializedPacket
CreateFecPacket(QuicPacketSequenceNumber sequence_number
) {
186 packets_
.push_back(new QuicEncryptedPacket(nullptr, kDefaultLength
));
187 SerializedPacket
serialized(sequence_number
, PACKET_6BYTE_SEQUENCE_NUMBER
,
188 packets_
.back(), 0u, nullptr);
189 serialized
.is_fec_packet
= true;
193 void SendDataPacket(QuicPacketSequenceNumber sequence_number
) {
194 EXPECT_CALL(*send_algorithm_
,
195 OnPacketSent(_
, BytesInFlight(), sequence_number
, _
, _
))
196 .Times(1).WillOnce(Return(true));
197 SerializedPacket
packet(CreateDataPacket(sequence_number
));
198 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(),
199 packet
.packet
->length(), NOT_RETRANSMISSION
,
200 HAS_RETRANSMITTABLE_DATA
);
203 void SendCryptoPacket(QuicPacketSequenceNumber sequence_number
) {
204 EXPECT_CALL(*send_algorithm_
,
205 OnPacketSent(_
, BytesInFlight(), sequence_number
,
206 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
207 .Times(1).WillOnce(Return(true));
208 SerializedPacket
packet(CreateDataPacket(sequence_number
));
209 packet
.retransmittable_frames
->AddStreamFrame(
210 new QuicStreamFrame(1, false, 0, IOVector()));
211 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(),
212 packet
.packet
->length(), NOT_RETRANSMISSION
,
213 HAS_RETRANSMITTABLE_DATA
);
216 void SendFecPacket(QuicPacketSequenceNumber sequence_number
) {
217 EXPECT_CALL(*send_algorithm_
,
218 OnPacketSent(_
, BytesInFlight(), sequence_number
,
219 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
220 .Times(1).WillOnce(Return(true));
221 SerializedPacket
packet(CreateFecPacket(sequence_number
));
222 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(),
223 packet
.packet
->length(), NOT_RETRANSMISSION
,
224 NO_RETRANSMITTABLE_DATA
);
227 void SendAckPacket(QuicPacketSequenceNumber sequence_number
) {
228 EXPECT_CALL(*send_algorithm_
,
229 OnPacketSent(_
, BytesInFlight(), sequence_number
,
230 kDefaultLength
, NO_RETRANSMITTABLE_DATA
))
231 .Times(1).WillOnce(Return(false));
232 SerializedPacket
packet(CreatePacket(sequence_number
, false));
233 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(),
234 packet
.packet
->length(), NOT_RETRANSMISSION
,
235 NO_RETRANSMITTABLE_DATA
);
238 // Based on QuicConnection's WritePendingRetransmissions.
239 void RetransmitNextPacket(
240 QuicPacketSequenceNumber retransmission_sequence_number
) {
241 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
242 EXPECT_CALL(*send_algorithm_
,
243 OnPacketSent(_
, _
, retransmission_sequence_number
,
244 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
245 .Times(1).WillOnce(Return(true));
246 const QuicSentPacketManager::PendingRetransmission pending
=
247 manager_
.NextPendingRetransmission();
248 SerializedPacket
packet(
249 CreatePacket(retransmission_sequence_number
, false));
250 manager_
.OnPacketSent(&packet
, pending
.sequence_number
, clock_
.Now(),
251 kDefaultLength
, pending
.transmission_type
,
252 HAS_RETRANSMITTABLE_DATA
);
255 QuicSentPacketManager manager_
;
256 vector
<QuicEncryptedPacket
*> packets_
;
258 QuicConnectionStats stats_
;
259 MockSendAlgorithm
* send_algorithm_
;
260 scoped_ptr
<MockNetworkChangeVisitor
> network_change_visitor_
;
263 TEST_F(QuicSentPacketManagerTest
, IsUnacked
) {
264 VerifyUnackedPackets(nullptr, 0);
267 QuicPacketSequenceNumber unacked
[] = { 1 };
268 VerifyUnackedPackets(unacked
, arraysize(unacked
));
269 QuicPacketSequenceNumber retransmittable
[] = { 1 };
270 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
273 TEST_F(QuicSentPacketManagerTest
, IsUnAckedRetransmit
) {
275 RetransmitAndSendPacket(1, 2);
277 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(&manager_
, 2));
278 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
279 VerifyUnackedPackets(unacked
, arraysize(unacked
));
280 QuicPacketSequenceNumber retransmittable
[] = { 2 };
281 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
284 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAck
) {
286 RetransmitAndSendPacket(1, 2);
289 QuicAckFrame ack_frame
;
290 ack_frame
.largest_observed
= 2;
291 ack_frame
.missing_packets
.insert(1);
293 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
295 // Packet 1 is unacked, pending, but not retransmittable.
296 QuicPacketSequenceNumber unacked
[] = { 1 };
297 VerifyUnackedPackets(unacked
, arraysize(unacked
));
298 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
299 VerifyRetransmittablePackets(nullptr, 0);
302 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAckBeforeSend
) {
304 QuicSentPacketManagerPeer::MarkForRetransmission(
305 &manager_
, 1, TLP_RETRANSMISSION
);
306 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
309 QuicAckFrame ack_frame
;
310 ack_frame
.largest_observed
= 1;
312 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
314 // There should no longer be a pending retransmission.
315 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
317 // No unacked packets remain.
318 VerifyUnackedPackets(nullptr, 0);
319 VerifyRetransmittablePackets(nullptr, 0);
320 EXPECT_EQ(0u, stats_
.packets_spuriously_retransmitted
);
323 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAckPrevious
) {
325 RetransmitAndSendPacket(1, 2);
326 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
327 clock_
.AdvanceTime(rtt
);
331 QuicAckFrame ack_frame
;
332 ack_frame
.largest_observed
= 1;
333 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
335 // 2 remains unacked, but no packets have retransmittable data.
336 QuicPacketSequenceNumber unacked
[] = { 2 };
337 VerifyUnackedPackets(unacked
, arraysize(unacked
));
338 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
339 VerifyRetransmittablePackets(nullptr, 0);
341 EXPECT_EQ(1u, stats_
.packets_spuriously_retransmitted
);
344 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAckPreviousThenNackRetransmit
) {
346 RetransmitAndSendPacket(1, 2);
347 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
348 clock_
.AdvanceTime(rtt
);
350 // First, ACK packet 1 which makes packet 2 non-retransmittable.
352 QuicAckFrame ack_frame
;
353 ack_frame
.largest_observed
= 1;
354 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
359 clock_
.AdvanceTime(rtt
);
361 // Next, NACK packet 2 three times.
362 ack_frame
.largest_observed
= 3;
363 ack_frame
.missing_packets
.insert(2);
365 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
367 ack_frame
.largest_observed
= 4;
369 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
371 ack_frame
.largest_observed
= 5;
372 ExpectAckAndLoss(true, 5, 2);
373 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
375 // No packets remain unacked.
376 VerifyUnackedPackets(nullptr, 0);
377 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
378 VerifyRetransmittablePackets(nullptr, 0);
380 // Verify that the retransmission alarm would not fire,
381 // since there is no retransmittable data outstanding.
382 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
385 TEST_F(QuicSentPacketManagerTest
,
386 DISABLED_RetransmitTwiceThenAckPreviousBeforeSend
) {
388 RetransmitAndSendPacket(1, 2);
390 // Fire the RTO, which will mark 2 for retransmission (but will not send it).
391 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
392 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
393 manager_
.OnRetransmissionTimeout();
394 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
396 // Ack 1 but not 2, before 2 is able to be sent.
397 // Since 1 has been retransmitted, it has already been lost, and so the
398 // send algorithm is not informed that it has been ACK'd.
399 QuicAckFrame ack_frame
;
400 ack_frame
.largest_observed
= 1;
402 EXPECT_CALL(*send_algorithm_
, RevertRetransmissionTimeout());
403 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
405 // Since 2 was marked for retransmit, when 1 is acked, 2 is kept for RTT.
406 QuicPacketSequenceNumber unacked
[] = { 2 };
407 VerifyUnackedPackets(unacked
, arraysize(unacked
));
408 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
409 VerifyRetransmittablePackets(nullptr, 0);
411 // Verify that the retransmission alarm would not fire,
412 // since there is no retransmittable data outstanding.
413 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
416 TEST_F(QuicSentPacketManagerTest
, RetransmitTwiceThenAckFirst
) {
417 StrictMock
<MockDebugDelegate
> debug_delegate
;
418 EXPECT_CALL(debug_delegate
, OnSpuriousPacketRetransmission(
419 TLP_RETRANSMISSION
, kDefaultLength
)).Times(2);
420 manager_
.set_debug_delegate(&debug_delegate
);
423 RetransmitAndSendPacket(1, 2);
424 RetransmitAndSendPacket(2, 3);
425 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
426 clock_
.AdvanceTime(rtt
);
428 // Ack 1 but not 2 or 3.
430 QuicAckFrame ack_frame
;
431 ack_frame
.largest_observed
= 1;
432 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
434 // 2 and 3 remain unacked, but no packets have retransmittable data.
435 QuicPacketSequenceNumber unacked
[] = { 2, 3 };
436 VerifyUnackedPackets(unacked
, arraysize(unacked
));
437 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
438 VerifyRetransmittablePackets(nullptr, 0);
440 // Ensure packet 2 is lost when 4 is sent and 3 and 4 are acked.
442 ack_frame
.largest_observed
= 4;
443 ack_frame
.missing_packets
.insert(2);
444 QuicPacketSequenceNumber acked
[] = { 3, 4 };
445 ExpectAcksAndLosses(true, acked
, arraysize(acked
), nullptr, 0);
446 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
448 QuicPacketSequenceNumber unacked2
[] = { 2 };
449 VerifyUnackedPackets(unacked2
, arraysize(unacked2
));
450 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
453 ack_frame
.largest_observed
= 5;
454 ExpectAckAndLoss(true, 5, 2);
455 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
457 VerifyUnackedPackets(nullptr, 0);
458 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
459 EXPECT_EQ(2u, stats_
.packets_spuriously_retransmitted
);
462 TEST_F(QuicSentPacketManagerTest
, LoseButDontRetransmitRevivedPacket
) {
468 // Ack 2 and 3, and mark 1 as revived.
469 QuicAckFrame ack_frame
;
470 ack_frame
.largest_observed
= 3;
471 ack_frame
.missing_packets
.insert(1);
472 ack_frame
.revived_packets
.insert(1);
473 QuicPacketSequenceNumber acked
[] = { 2, 3 };
474 ExpectAcksAndLosses(true, acked
, arraysize(acked
), nullptr, 0);
475 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
477 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
478 QuicPacketSequenceNumber unacked
[] = { 1, 4 };
479 VerifyUnackedPackets(unacked
, arraysize(unacked
));
480 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
481 QuicPacketSequenceNumber retransmittable
[] = { 4 };
482 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
484 // Ack the 4th packet and expect the 1st to be considered lost.
485 ack_frame
.largest_observed
= 4;
486 ExpectAckAndLoss(true, 4, 1);
487 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
489 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
490 VerifyRetransmittablePackets(nullptr, 0);
493 TEST_F(QuicSentPacketManagerTest
, MarkLostThenReviveAndDontRetransmitPacket
) {
500 // Ack 2, 3, and 4, and expect the 1st to be considered lost.
501 QuicAckFrame ack_frame
;
502 ack_frame
.largest_observed
= 4;
503 ack_frame
.missing_packets
.insert(1);
504 QuicPacketSequenceNumber acked
[] = { 2, 3, 4 };
505 QuicPacketSequenceNumber lost
[] = { 1 };
506 ExpectAcksAndLosses(true, acked
, arraysize(acked
), lost
, arraysize(lost
));
507 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
509 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
510 QuicPacketSequenceNumber unacked
[] = { 1, 5 };
511 VerifyUnackedPackets(unacked
, arraysize(unacked
));
512 QuicPacketSequenceNumber retransmittable
[] = { 1 };
513 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
515 // Ack 5th packet (FEC) and revive 1st packet. 1st packet should now be
516 // removed from pending retransmissions map.
517 ack_frame
.largest_observed
= 5;
518 ack_frame
.revived_packets
.insert(1);
520 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
522 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
523 VerifyRetransmittablePackets(nullptr, 0);
526 TEST_F(QuicSentPacketManagerTest
, TruncatedAck
) {
528 RetransmitAndSendPacket(1, 2);
529 RetransmitAndSendPacket(2, 3);
530 RetransmitAndSendPacket(3, 4);
531 RetransmitAndSendPacket(4, 5);
533 // Truncated ack with 4 NACKs, so the first packet is lost.
534 QuicAckFrame ack_frame
;
535 ack_frame
.largest_observed
= 4;
536 ack_frame
.missing_packets
.insert(1);
537 ack_frame
.missing_packets
.insert(2);
538 ack_frame
.missing_packets
.insert(3);
539 ack_frame
.missing_packets
.insert(4);
540 ack_frame
.is_truncated
= true;
542 QuicPacketSequenceNumber lost
[] = { 1 };
543 ExpectAcksAndLosses(true, nullptr, 0, lost
, arraysize(lost
));
544 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
546 // High water mark will be raised.
547 QuicPacketSequenceNumber unacked
[] = { 2, 3, 4, 5 };
548 VerifyUnackedPackets(unacked
, arraysize(unacked
));
549 QuicPacketSequenceNumber retransmittable
[] = { 5 };
550 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
553 TEST_F(QuicSentPacketManagerTest
, AckPreviousTransmissionThenTruncatedAck
) {
555 RetransmitAndSendPacket(1, 2);
556 RetransmitAndSendPacket(2, 3);
557 RetransmitAndSendPacket(3, 4);
564 // Ack previous transmission
566 QuicAckFrame ack_frame
;
567 ack_frame
.largest_observed
= 2;
568 ack_frame
.missing_packets
.insert(1);
570 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
571 EXPECT_TRUE(manager_
.IsUnacked(4));
574 // Truncated ack with 4 NACKs
576 QuicAckFrame ack_frame
;
577 ack_frame
.largest_observed
= 6;
578 ack_frame
.missing_packets
.insert(3);
579 ack_frame
.missing_packets
.insert(4);
580 ack_frame
.missing_packets
.insert(5);
581 ack_frame
.missing_packets
.insert(6);
582 ack_frame
.is_truncated
= true;
583 ExpectAckAndLoss(true, 1, 3);
584 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
587 // High water mark will be raised.
588 QuicPacketSequenceNumber unacked
[] = { 4, 5, 6, 7, 8, 9 };
589 VerifyUnackedPackets(unacked
, arraysize(unacked
));
590 QuicPacketSequenceNumber retransmittable
[] = { 5, 6, 7, 8, 9 };
591 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
594 TEST_F(QuicSentPacketManagerTest
, GetLeastUnacked
) {
595 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
598 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedUnacked
) {
600 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
603 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedUnackedFec
) {
605 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
608 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedAndDiscard
) {
609 VerifyUnackedPackets(nullptr, 0);
612 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
615 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
618 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
620 QuicPacketSequenceNumber unacked
[] = { 1, 2, 3 };
621 VerifyUnackedPackets(unacked
, arraysize(unacked
));
622 VerifyRetransmittablePackets(nullptr, 0);
624 // Ack 2, so there's an rtt update.
626 QuicAckFrame ack_frame
;
627 ack_frame
.largest_observed
= 2;
628 ack_frame
.missing_packets
.insert(1);
629 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
631 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
634 TEST_F(QuicSentPacketManagerTest
, GetSentTime
) {
635 VerifyUnackedPackets(nullptr, 0);
637 QuicTime sent_time
= clock_
.Now();
639 QuicTime sent_time2
= clock_
.Now();
641 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
642 VerifyUnackedPackets(unacked
, arraysize(unacked
));
643 VerifyRetransmittablePackets(nullptr, 0);
645 EXPECT_TRUE(manager_
.HasUnackedPackets());
646 EXPECT_EQ(sent_time
, QuicSentPacketManagerPeer::GetSentTime(&manager_
, 1));
647 EXPECT_EQ(sent_time2
, QuicSentPacketManagerPeer::GetSentTime(&manager_
, 2));
650 TEST_F(QuicSentPacketManagerTest
, AckAckAndUpdateRtt
) {
654 // Now ack the ack and expect an RTT update.
655 QuicAckFrame ack_frame
;
656 ack_frame
.largest_observed
= 2;
657 ack_frame
.delta_time_largest_observed
=
658 QuicTime::Delta::FromMilliseconds(5);
661 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
665 // Now ack the ack and expect only an RTT update.
666 ack_frame
.largest_observed
= 3;
668 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
671 TEST_F(QuicSentPacketManagerTest
, Rtt
) {
672 QuicPacketSequenceNumber sequence_number
= 1;
673 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(15);
674 SendDataPacket(sequence_number
);
675 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
677 ExpectAck(sequence_number
);
678 QuicAckFrame ack_frame
;
679 ack_frame
.largest_observed
= sequence_number
;
680 ack_frame
.delta_time_largest_observed
=
681 QuicTime::Delta::FromMilliseconds(5);
682 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
683 EXPECT_EQ(expected_rtt
,
684 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
687 TEST_F(QuicSentPacketManagerTest
, RttWithInvalidDelta
) {
688 // Expect that the RTT is equal to the local time elapsed, since the
689 // delta_time_largest_observed is larger than the local time elapsed
690 // and is hence invalid.
691 QuicPacketSequenceNumber sequence_number
= 1;
692 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(10);
693 SendDataPacket(sequence_number
);
694 clock_
.AdvanceTime(expected_rtt
);
696 ExpectAck(sequence_number
);
697 QuicAckFrame ack_frame
;
698 ack_frame
.largest_observed
= sequence_number
;
699 ack_frame
.delta_time_largest_observed
=
700 QuicTime::Delta::FromMilliseconds(11);
701 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
702 EXPECT_EQ(expected_rtt
,
703 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
706 TEST_F(QuicSentPacketManagerTest
, RttWithInfiniteDelta
) {
707 // Expect that the RTT is equal to the local time elapsed, since the
708 // delta_time_largest_observed is infinite, and is hence invalid.
709 QuicPacketSequenceNumber sequence_number
= 1;
710 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(10);
711 SendDataPacket(sequence_number
);
712 clock_
.AdvanceTime(expected_rtt
);
714 ExpectAck(sequence_number
);
715 QuicAckFrame ack_frame
;
716 ack_frame
.largest_observed
= sequence_number
;
717 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Infinite();
718 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
719 EXPECT_EQ(expected_rtt
,
720 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
723 TEST_F(QuicSentPacketManagerTest
, RttZeroDelta
) {
724 // Expect that the RTT is the time between send and receive since the
725 // delta_time_largest_observed is zero.
726 QuicPacketSequenceNumber sequence_number
= 1;
727 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(10);
728 SendDataPacket(sequence_number
);
729 clock_
.AdvanceTime(expected_rtt
);
731 ExpectAck(sequence_number
);
732 QuicAckFrame ack_frame
;
733 ack_frame
.largest_observed
= sequence_number
;
734 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
735 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
736 EXPECT_EQ(expected_rtt
,
737 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
740 TEST_F(QuicSentPacketManagerTest
, TailLossProbeTimeout
) {
741 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
744 QuicPacketSequenceNumber sequence_number
= 1;
745 SendDataPacket(sequence_number
);
747 // The first tail loss probe retransmits 1 packet.
748 manager_
.OnRetransmissionTimeout();
749 EXPECT_EQ(QuicTime::Delta::Zero(),
750 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
751 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
752 manager_
.MaybeRetransmitTailLossProbe();
753 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
754 RetransmitNextPacket(2);
755 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
757 // The second tail loss probe retransmits 1 packet.
758 manager_
.OnRetransmissionTimeout();
759 EXPECT_EQ(QuicTime::Delta::Zero(),
760 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
761 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
762 manager_
.MaybeRetransmitTailLossProbe();
763 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
764 RetransmitNextPacket(3);
765 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
766 QuicTime::Delta::Infinite()));
767 EXPECT_EQ(QuicTime::Delta::Infinite(),
768 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
769 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
771 // Ack the third and ensure the first two are still pending.
773 QuicAckFrame ack_frame
;
774 ack_frame
.largest_observed
= 3;
775 ack_frame
.missing_packets
.insert(1);
776 ack_frame
.missing_packets
.insert(2);
777 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
779 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
781 // Acking two more packets will lose both of them due to nacks.
782 ack_frame
.largest_observed
= 5;
783 QuicPacketSequenceNumber lost
[] = { 1, 2 };
784 ExpectAcksAndLosses(false, nullptr, 0, lost
, arraysize(lost
));
785 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
787 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
788 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
789 EXPECT_EQ(2u, stats_
.tlp_count
);
790 EXPECT_EQ(0u, stats_
.rto_count
);
793 TEST_F(QuicSentPacketManagerTest
, TailLossProbeThenRTO
) {
794 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
797 const size_t kNumSentPackets
= 100;
798 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
801 QuicTime rto_packet_time
= clock_
.Now();
803 clock_
.AdvanceTime(manager_
.GetRetransmissionTime().Subtract(clock_
.Now()));
805 // The first tail loss probe retransmits 1 packet.
806 manager_
.OnRetransmissionTimeout();
807 EXPECT_EQ(QuicTime::Delta::Zero(),
808 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
809 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
810 manager_
.MaybeRetransmitTailLossProbe();
811 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
812 RetransmitNextPacket(101);
813 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
814 QuicTime::Delta::Infinite()));
815 EXPECT_EQ(QuicTime::Delta::Infinite(),
816 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
817 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
818 clock_
.AdvanceTime(manager_
.GetRetransmissionTime().Subtract(clock_
.Now()));
820 // The second tail loss probe retransmits 1 packet.
821 manager_
.OnRetransmissionTimeout();
822 EXPECT_EQ(QuicTime::Delta::Zero(),
823 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
824 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
825 EXPECT_TRUE(manager_
.MaybeRetransmitTailLossProbe());
826 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
827 RetransmitNextPacket(102);
828 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
829 QuicTime::Delta::Infinite()));
830 EXPECT_EQ(QuicTime::Delta::Infinite(),
831 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
833 // Ensure the RTO is set based on the correct packet.
834 rto_packet_time
= clock_
.Now();
835 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
836 .WillOnce(Return(QuicTime::Delta::FromSeconds(1)));
837 EXPECT_EQ(rto_packet_time
.Add(QuicTime::Delta::FromSeconds(1)),
838 manager_
.GetRetransmissionTime());
840 // Advance the time enough to ensure all packets are RTO'd.
841 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
843 manager_
.OnRetransmissionTimeout();
844 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
845 EXPECT_EQ(2u, stats_
.tlp_count
);
846 EXPECT_EQ(1u, stats_
.rto_count
);
848 // Send and Ack the RTO and ensure OnRetransmissionTimeout is called.
849 EXPECT_EQ(102 * kDefaultLength
,
850 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
852 RetransmitNextPacket(103);
853 QuicAckFrame ack_frame
;
854 ack_frame
.largest_observed
= 103;
855 for (int i
= 0; i
< 103; ++i
) {
856 ack_frame
.missing_packets
.insert(i
);
858 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
859 EXPECT_CALL(*send_algorithm_
,
860 OnCongestionEvent(true, _
, ElementsAre(Pair(103, _
)), _
));
861 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
862 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
863 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
864 // All packets before 103 should be lost.
865 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
868 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeTimeout
) {
869 // Send 2 crypto packets and 3 data packets.
870 const size_t kNumSentCryptoPackets
= 2;
871 for (size_t i
= 1; i
<= kNumSentCryptoPackets
; ++i
) {
874 const size_t kNumSentDataPackets
= 3;
875 for (size_t i
= 1; i
<= kNumSentDataPackets
; ++i
) {
876 SendDataPacket(kNumSentCryptoPackets
+ i
);
878 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
880 // The first retransmits 2 packets.
881 manager_
.OnRetransmissionTimeout();
882 EXPECT_EQ(QuicTime::Delta::Zero(),
883 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
884 RetransmitNextPacket(6);
885 RetransmitNextPacket(7);
886 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
887 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
889 // The second retransmits 2 packets.
890 manager_
.OnRetransmissionTimeout();
891 EXPECT_EQ(QuicTime::Delta::Zero(),
892 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
893 RetransmitNextPacket(8);
894 RetransmitNextPacket(9);
895 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
896 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
898 // Now ack the two crypto packets and the speculatively encrypted request,
899 // and ensure the first four crypto packets get abandoned, but not lost.
900 QuicPacketSequenceNumber acked
[] = { 3, 4, 5, 8, 9 };
901 ExpectAcksAndLosses(true, acked
, arraysize(acked
), nullptr, 0);
902 QuicAckFrame ack_frame
;
903 ack_frame
.largest_observed
= 9;
904 ack_frame
.missing_packets
.insert(1);
905 ack_frame
.missing_packets
.insert(2);
906 ack_frame
.missing_packets
.insert(6);
907 ack_frame
.missing_packets
.insert(7);
908 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
910 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
913 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeTimeoutVersionNegotiation
) {
914 // Send 2 crypto packets and 3 data packets.
915 const size_t kNumSentCryptoPackets
= 2;
916 for (size_t i
= 1; i
<= kNumSentCryptoPackets
; ++i
) {
919 const size_t kNumSentDataPackets
= 3;
920 for (size_t i
= 1; i
<= kNumSentDataPackets
; ++i
) {
921 SendDataPacket(kNumSentCryptoPackets
+ i
);
923 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
925 // The first retransmission timeout retransmits 2 crypto packets.
926 manager_
.OnRetransmissionTimeout();
927 RetransmitNextPacket(6);
928 RetransmitNextPacket(7);
929 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
930 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
932 // Now act like a version negotiation packet arrived, which would cause all
933 // unacked packets to be retransmitted.
934 manager_
.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION
);
936 // Ensure the first two pending packets are the crypto retransmits.
937 ASSERT_TRUE(manager_
.HasPendingRetransmissions());
938 EXPECT_EQ(6u, manager_
.NextPendingRetransmission().sequence_number
);
939 RetransmitNextPacket(8);
940 EXPECT_EQ(7u, manager_
.NextPendingRetransmission().sequence_number
);
941 RetransmitNextPacket(9);
943 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
944 // Send 3 more data packets and ensure the least unacked is raised.
945 RetransmitNextPacket(10);
946 RetransmitNextPacket(11);
947 RetransmitNextPacket(12);
948 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
950 EXPECT_EQ(8u, manager_
.GetLeastUnacked());
953 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeSpuriousRetransmission
) {
954 // Send 1 crypto packet.
956 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
958 // Retransmit the crypto packet as 2.
959 manager_
.OnRetransmissionTimeout();
960 RetransmitNextPacket(2);
962 // Retransmit the crypto packet as 3.
963 manager_
.OnRetransmissionTimeout();
964 RetransmitNextPacket(3);
966 // Now ack the second crypto packet, and ensure the first gets removed, but
967 // the third does not.
969 QuicAckFrame ack_frame
;
970 ack_frame
.largest_observed
= 2;
971 ack_frame
.missing_packets
.insert(1);
972 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
974 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
975 QuicPacketSequenceNumber unacked
[] = { 3 };
976 VerifyUnackedPackets(unacked
, arraysize(unacked
));
979 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeTimeoutUnsentDataPacket
) {
980 // Send 2 crypto packets and 1 data packet.
981 const size_t kNumSentCryptoPackets
= 2;
982 for (size_t i
= 1; i
<= kNumSentCryptoPackets
; ++i
) {
986 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
988 // Retransmit 2 crypto packets, but not the serialized packet.
989 manager_
.OnRetransmissionTimeout();
990 RetransmitNextPacket(4);
991 RetransmitNextPacket(5);
992 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
993 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
996 TEST_F(QuicSentPacketManagerTest
,
997 CryptoHandshakeRetransmissionThenRetransmitAll
) {
998 // Send 1 crypto packet.
1000 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1002 // Retransmit the crypto packet as 2.
1003 manager_
.OnRetransmissionTimeout();
1004 RetransmitNextPacket(2);
1006 // Now retransmit all the unacked packets, which occurs when there is a
1007 // version negotiation.
1008 manager_
.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION
);
1009 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
1010 VerifyUnackedPackets(unacked
, arraysize(unacked
));
1011 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1012 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1013 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
1016 TEST_F(QuicSentPacketManagerTest
,
1017 CryptoHandshakeRetransmissionThenNeuterAndAck
) {
1018 // Send 1 crypto packet.
1019 SendCryptoPacket(1);
1020 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1022 // Retransmit the crypto packet as 2.
1023 manager_
.OnRetransmissionTimeout();
1024 RetransmitNextPacket(2);
1026 // Retransmit the crypto packet as 3.
1027 manager_
.OnRetransmissionTimeout();
1028 RetransmitNextPacket(3);
1030 // Now neuter all unacked unencrypted packets, which occurs when the
1031 // connection goes forward secure.
1032 manager_
.NeuterUnencryptedPackets();
1033 QuicPacketSequenceNumber unacked
[] = { 1, 2, 3};
1034 VerifyUnackedPackets(unacked
, arraysize(unacked
));
1035 VerifyRetransmittablePackets(nullptr, 0);
1036 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1037 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1038 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
1040 // Ensure both packets get discarded when packet 2 is acked.
1041 QuicAckFrame ack_frame
;
1042 ack_frame
.largest_observed
= 3;
1043 ack_frame
.missing_packets
.insert(1);
1044 ack_frame
.missing_packets
.insert(2);
1045 ExpectUpdatedRtt(3);
1046 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
1047 VerifyUnackedPackets(nullptr, 0);
1048 VerifyRetransmittablePackets(nullptr, 0);
1051 TEST_F(QuicSentPacketManagerTest
, ResetRecentMinRTTWithEmptyWindow
) {
1052 QuicTime::Delta min_rtt
= QuicTime::Delta::FromMilliseconds(50);
1053 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->UpdateRtt(
1054 min_rtt
, QuicTime::Delta::Zero(), QuicTime::Zero());
1056 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->min_rtt());
1058 QuicSentPacketManagerPeer::GetRttStats(
1059 &manager_
)->recent_min_rtt());
1061 // Send two packets with no prior bytes in flight.
1065 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
1066 // Ack two packets with 100ms RTT observations.
1067 QuicAckFrame ack_frame
;
1068 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1069 ack_frame
.largest_observed
= 1;
1071 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1073 // First ack does not change recent min rtt.
1075 QuicSentPacketManagerPeer::GetRttStats(
1076 &manager_
)->recent_min_rtt());
1078 ack_frame
.largest_observed
= 2;
1080 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1083 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->min_rtt());
1084 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(100),
1085 QuicSentPacketManagerPeer::GetRttStats(
1086 &manager_
)->recent_min_rtt());
1089 TEST_F(QuicSentPacketManagerTest
, RetransmissionTimeout
) {
1090 // Send 100 packets.
1091 const size_t kNumSentPackets
= 100;
1092 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
1096 EXPECT_FALSE(manager_
.MaybeRetransmitTailLossProbe());
1097 manager_
.OnRetransmissionTimeout();
1098 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1099 EXPECT_EQ(100 * kDefaultLength
,
1100 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1101 RetransmitNextPacket(101);
1102 RetransmitNextPacket(102);
1103 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1105 // Ack a retransmission.
1106 QuicAckFrame ack_frame
;
1107 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1108 ack_frame
.largest_observed
= 102;
1109 for (int i
= 0; i
< 102; ++i
) {
1110 ack_frame
.missing_packets
.insert(i
);
1112 // Ensure no packets are lost.
1113 EXPECT_CALL(*send_algorithm_
,
1114 OnCongestionEvent(true, _
, ElementsAre(Pair(102, _
)),
1115 /*lost_packets=*/IsEmpty()));
1116 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1117 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1118 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1119 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1122 TEST_F(QuicSentPacketManagerTest
, NewRetransmissionTimeout
) {
1123 QuicConfig client_config
;
1124 QuicTagVector options
;
1125 options
.push_back(kNRTO
);
1126 QuicSentPacketManagerPeer::SetIsServer(&manager_
, false);
1127 client_config
.SetConnectionOptionsToSend(options
);
1128 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1129 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1130 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1131 EXPECT_CALL(*send_algorithm_
, PacingRate())
1132 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1133 manager_
.SetFromConfig(client_config
);
1134 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1136 // Send 100 packets.
1137 const size_t kNumSentPackets
= 100;
1138 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
1142 EXPECT_FALSE(manager_
.MaybeRetransmitTailLossProbe());
1143 manager_
.OnRetransmissionTimeout();
1144 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1145 EXPECT_EQ(100 * kDefaultLength
,
1146 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1147 RetransmitNextPacket(101);
1148 RetransmitNextPacket(102);
1149 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1151 // Ack a retransmission and expect no call to OnRetransmissionTimeout.
1152 QuicAckFrame ack_frame
;
1153 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1154 ack_frame
.largest_observed
= 102;
1155 for (int i
= 0; i
< 102; ++i
) {
1156 ack_frame
.missing_packets
.insert(i
);
1158 // This will include packets in the lost packet map.
1159 EXPECT_CALL(*send_algorithm_
,
1160 OnCongestionEvent(true, _
, ElementsAre(Pair(102, _
)),
1161 /*lost_packets=*/Not(IsEmpty())));
1162 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1163 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1164 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1167 TEST_F(QuicSentPacketManagerTest
, TwoRetransmissionTimeoutsAckSecond
) {
1171 manager_
.OnRetransmissionTimeout();
1172 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1173 EXPECT_EQ(kDefaultLength
,
1174 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1175 RetransmitNextPacket(2);
1176 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1178 // Rto a second time.
1179 manager_
.OnRetransmissionTimeout();
1180 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1181 EXPECT_EQ(2 * kDefaultLength
,
1182 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1183 RetransmitNextPacket(3);
1184 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1186 // Ack a retransmission and ensure OnRetransmissionTimeout is called.
1187 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1188 QuicAckFrame ack_frame
;
1189 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1190 ack_frame
.largest_observed
= 2;
1191 ack_frame
.missing_packets
.insert(1);
1193 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1195 // The original packet and newest should be outstanding.
1196 EXPECT_EQ(2 * kDefaultLength
,
1197 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1200 TEST_F(QuicSentPacketManagerTest
, TwoRetransmissionTimeoutsAckFirst
) {
1204 manager_
.OnRetransmissionTimeout();
1205 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1206 EXPECT_EQ(kDefaultLength
,
1207 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1208 RetransmitNextPacket(2);
1209 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1211 // Rto a second time.
1212 manager_
.OnRetransmissionTimeout();
1213 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1214 EXPECT_EQ(2 * kDefaultLength
,
1215 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1216 RetransmitNextPacket(3);
1217 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1219 // Ack a retransmission and ensure OnRetransmissionTimeout is called.
1220 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1221 QuicAckFrame ack_frame
;
1222 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1223 ack_frame
.largest_observed
= 3;
1224 ack_frame
.missing_packets
.insert(1);
1225 ack_frame
.missing_packets
.insert(2);
1227 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1229 // The first two packets should still be outstanding.
1230 EXPECT_EQ(2 * kDefaultLength
,
1231 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1234 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTime
) {
1235 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
1238 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeCryptoHandshake
) {
1239 SendCryptoPacket(1);
1242 RttStats
* rtt_stats
= QuicSentPacketManagerPeer::GetRttStats(&manager_
);
1243 rtt_stats
->set_initial_rtt_us(1 * kNumMicrosPerMilli
);
1244 EXPECT_EQ(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1245 manager_
.GetRetransmissionTime());
1247 // Test with a standard smoothed RTT.
1248 rtt_stats
->set_initial_rtt_us(100 * kNumMicrosPerMilli
);
1250 QuicTime::Delta srtt
=
1251 QuicTime::Delta::FromMicroseconds(rtt_stats
->initial_rtt_us());
1252 QuicTime expected_time
= clock_
.Now().Add(srtt
.Multiply(1.5));
1253 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1255 // Retransmit the packet by invoking the retransmission timeout.
1256 clock_
.AdvanceTime(srtt
.Multiply(1.5));
1257 manager_
.OnRetransmissionTimeout();
1258 RetransmitNextPacket(2);
1260 // The retransmission time should now be twice as far in the future.
1261 expected_time
= clock_
.Now().Add(srtt
.Multiply(2).Multiply(1.5));
1262 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1265 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeTailLossProbe
) {
1266 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
1271 RttStats
* rtt_stats
= QuicSentPacketManagerPeer::GetRttStats(&manager_
);
1272 rtt_stats
->set_initial_rtt_us(1 * kNumMicrosPerMilli
);
1273 EXPECT_EQ(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1274 manager_
.GetRetransmissionTime());
1276 // Test with a standard smoothed RTT.
1277 rtt_stats
->set_initial_rtt_us(100 * kNumMicrosPerMilli
);
1278 QuicTime::Delta srtt
=
1279 QuicTime::Delta::FromMicroseconds(rtt_stats
->initial_rtt_us());
1280 QuicTime::Delta expected_tlp_delay
= srtt
.Multiply(2);
1281 QuicTime expected_time
= clock_
.Now().Add(expected_tlp_delay
);
1282 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1284 // Retransmit the packet by invoking the retransmission timeout.
1285 clock_
.AdvanceTime(expected_tlp_delay
);
1286 manager_
.OnRetransmissionTimeout();
1287 EXPECT_EQ(QuicTime::Delta::Zero(),
1288 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1289 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1290 EXPECT_TRUE(manager_
.MaybeRetransmitTailLossProbe());
1291 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1292 RetransmitNextPacket(3);
1293 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1294 QuicTime::Delta::Infinite()));
1295 EXPECT_EQ(QuicTime::Delta::Infinite(),
1296 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1297 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1299 expected_time
= clock_
.Now().Add(expected_tlp_delay
);
1300 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1303 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeSpuriousRTO
) {
1304 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->UpdateRtt(
1305 QuicTime::Delta::FromMilliseconds(100),
1306 QuicTime::Delta::Zero(),
1314 QuicTime::Delta expected_rto_delay
= QuicTime::Delta::FromMilliseconds(500);
1315 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1316 .WillRepeatedly(Return(expected_rto_delay
));
1317 QuicTime expected_time
= clock_
.Now().Add(expected_rto_delay
);
1318 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1320 // Retransmit the packet by invoking the retransmission timeout.
1321 clock_
.AdvanceTime(expected_rto_delay
);
1322 manager_
.OnRetransmissionTimeout();
1323 // All packets are still considered inflight.
1324 EXPECT_EQ(4 * kDefaultLength
,
1325 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1326 RetransmitNextPacket(5);
1327 RetransmitNextPacket(6);
1328 // All previous packets are inflight, plus two rto retransmissions.
1329 EXPECT_EQ(6 * kDefaultLength
,
1330 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1331 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1333 // The delay should double the second time.
1334 expected_time
= clock_
.Now().Add(expected_rto_delay
).Add(expected_rto_delay
);
1335 // Once we always base the timer on the right edge, leaving the older packets
1336 // in flight doesn't change the timeout.
1337 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1339 // Ack a packet before the first RTO and ensure the RTO timeout returns to the
1340 // original value and OnRetransmissionTimeout is not called or reverted.
1341 QuicAckFrame ack_frame
;
1342 ack_frame
.largest_observed
= 2;
1343 ack_frame
.missing_packets
.insert(1);
1345 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
1346 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1347 EXPECT_EQ(5 * kDefaultLength
,
1348 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1350 // Wait 2RTTs from now for the RTO, since it's the max of the RTO time
1351 // and the TLP time. In production, there would always be two TLP's first.
1352 // Since retransmission was spurious, smoothed_rtt_ is expired, and replaced
1353 // by the latest RTT sample of 500ms.
1354 expected_time
= clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(1000));
1355 // Once we always base the timer on the right edge, leaving the older packets
1356 // in flight doesn't change the timeout.
1357 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1360 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelayMin
) {
1362 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1363 .WillRepeatedly(Return(QuicTime::Delta::FromMilliseconds(1)));
1364 QuicTime::Delta delay
= QuicTime::Delta::FromMilliseconds(200);
1366 // If the delay is smaller than the min, ensure it exponentially backs off
1368 for (int i
= 0; i
< 5; ++i
) {
1370 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1371 delay
= delay
.Add(delay
);
1372 manager_
.OnRetransmissionTimeout();
1373 RetransmitNextPacket(i
+ 2);
1377 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelayMax
) {
1378 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1379 .WillOnce(Return(QuicTime::Delta::FromSeconds(500)));
1381 EXPECT_EQ(QuicTime::Delta::FromSeconds(60),
1382 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1385 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelay
) {
1387 QuicTime::Delta delay
= QuicTime::Delta::FromMilliseconds(500);
1388 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1389 .WillRepeatedly(Return(delay
));
1391 // Delay should back off exponentially.
1392 for (int i
= 0; i
< 5; ++i
) {
1394 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1395 delay
= delay
.Add(delay
);
1396 manager_
.OnRetransmissionTimeout();
1397 RetransmitNextPacket(i
+ 2);
1401 TEST_F(QuicSentPacketManagerTest
, GetLossDelay
) {
1402 MockLossAlgorithm
* loss_algorithm
= new MockLossAlgorithm();
1403 QuicSentPacketManagerPeer::SetLossAlgorithm(&manager_
, loss_algorithm
);
1405 EXPECT_CALL(*loss_algorithm
, GetLossTimeout())
1406 .WillRepeatedly(Return(QuicTime::Zero()));
1410 // Handle an ack which causes the loss algorithm to be evaluated and
1411 // set the loss timeout.
1413 EXPECT_CALL(*loss_algorithm
, DetectLostPackets(_
, _
, _
, _
))
1414 .WillOnce(Return(SequenceNumberSet()));
1415 QuicAckFrame ack_frame
;
1416 ack_frame
.largest_observed
= 2;
1417 ack_frame
.missing_packets
.insert(1);
1418 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1420 QuicTime
timeout(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)));
1421 EXPECT_CALL(*loss_algorithm
, GetLossTimeout())
1422 .WillRepeatedly(Return(timeout
));
1423 EXPECT_EQ(timeout
, manager_
.GetRetransmissionTime());
1425 // Fire the retransmission timeout and ensure the loss detection algorithm
1427 EXPECT_CALL(*loss_algorithm
, DetectLostPackets(_
, _
, _
, _
))
1428 .WillOnce(Return(SequenceNumberSet()));
1429 manager_
.OnRetransmissionTimeout();
1432 TEST_F(QuicSentPacketManagerTest
, NegotiateTimeLossDetectionFromOptions
) {
1434 QuicSentPacketManagerPeer::GetLossAlgorithm(
1435 &manager_
)->GetLossDetectionType());
1438 QuicTagVector options
;
1439 options
.push_back(kTIME
);
1440 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1441 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1442 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1443 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1444 manager_
.SetFromConfig(config
);
1447 QuicSentPacketManagerPeer::GetLossAlgorithm(
1448 &manager_
)->GetLossDetectionType());
1451 TEST_F(QuicSentPacketManagerTest
, NegotiateCongestionControlFromOptions
) {
1452 ValueRestore
<bool> old_flag(&FLAGS_quic_allow_bbr
, true);
1454 QuicTagVector options
;
1456 options
.push_back(kRENO
);
1457 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1458 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1459 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1460 manager_
.SetFromConfig(config
);
1461 EXPECT_EQ(kReno
, QuicSentPacketManagerPeer::GetSendAlgorithm(
1462 manager_
)->GetCongestionControlType());
1464 // TODO(rtenneti): Enable the following code after BBR code is checked in.
1467 options
.push_back(kTBBR
);
1468 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1469 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1470 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1471 manager_
.SetFromConfig(config
);
1472 EXPECT_EQ(kBBR
, QuicSentPacketManagerPeer::GetSendAlgorithm(
1473 manager_
)->GetCongestionControlType());
1477 TEST_F(QuicSentPacketManagerTest
, NegotiateNumConnectionsFromOptions
) {
1479 QuicTagVector options
;
1481 options
.push_back(k1CON
);
1482 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1483 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1484 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1485 EXPECT_CALL(*send_algorithm_
, SetNumEmulatedConnections(1));
1486 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1487 manager_
.SetFromConfig(config
);
1489 QuicSentPacketManagerPeer::SetIsServer(&manager_
, false);
1490 QuicConfig client_config
;
1491 client_config
.SetConnectionOptionsToSend(options
);
1492 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1493 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1494 EXPECT_CALL(*send_algorithm_
, SetNumEmulatedConnections(1));
1495 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1496 manager_
.SetFromConfig(client_config
);
1499 TEST_F(QuicSentPacketManagerTest
, NegotiateNConnectionFromOptions
) {
1500 // By default, changing the number of open streams does nothing.
1501 manager_
.SetNumOpenStreams(5);
1504 QuicTagVector options
;
1506 options
.push_back(kNCON
);
1507 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1508 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1509 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1510 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1511 manager_
.SetFromConfig(config
);
1513 EXPECT_CALL(*send_algorithm_
, SetNumEmulatedConnections(5));
1514 manager_
.SetNumOpenStreams(5);
1517 TEST_F(QuicSentPacketManagerTest
, NegotiateNoTLPFromOptionsAtServer
) {
1519 QuicTagVector options
;
1521 options
.push_back(kNTLP
);
1522 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1523 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1524 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1525 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1526 manager_
.SetFromConfig(config
);
1527 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_
));
1530 TEST_F(QuicSentPacketManagerTest
, NegotiateNoTLPFromOptionsAtClient
) {
1531 QuicConfig client_config
;
1532 QuicTagVector options
;
1534 options
.push_back(kNTLP
);
1535 QuicSentPacketManagerPeer::SetIsServer(&manager_
, false);
1536 client_config
.SetConnectionOptionsToSend(options
);
1537 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1538 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1539 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1540 manager_
.SetFromConfig(client_config
);
1541 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_
));
1544 TEST_F(QuicSentPacketManagerTest
, NegotiateNewRTOFromOptionsAtServer
) {
1545 EXPECT_FALSE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1547 QuicTagVector options
;
1549 options
.push_back(kNRTO
);
1550 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1551 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1552 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1553 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1554 manager_
.SetFromConfig(config
);
1555 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1558 TEST_F(QuicSentPacketManagerTest
, NegotiateNewRTOFromOptionsAtClient
) {
1559 EXPECT_FALSE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1560 QuicConfig client_config
;
1561 QuicTagVector options
;
1563 options
.push_back(kNRTO
);
1564 QuicSentPacketManagerPeer::SetIsServer(&manager_
, false);
1565 client_config
.SetConnectionOptionsToSend(options
);
1566 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1567 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1568 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1569 manager_
.SetFromConfig(client_config
);
1570 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1573 TEST_F(QuicSentPacketManagerTest
, NegotiatePacingFromOptions
) {
1574 EXPECT_FALSE(manager_
.using_pacing());
1577 QuicTagVector options
;
1578 options
.push_back(kPACE
);
1579 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1580 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1581 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1582 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, /*using_pacing=*/true));
1583 manager_
.SetFromConfig(config
);
1585 EXPECT_TRUE(manager_
.using_pacing());
1588 TEST_F(QuicSentPacketManagerTest
, EnablePacingViaFlag
) {
1589 EXPECT_FALSE(manager_
.using_pacing());
1591 // If pacing is enabled via command-line flag, it will be turned on,
1592 // regardless of the contents of the config.
1593 ValueRestore
<bool> old_flag(&FLAGS_quic_enable_pacing
, true);
1595 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1596 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1597 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, /*using_pacing=*/true));
1598 manager_
.SetFromConfig(config
);
1600 EXPECT_TRUE(manager_
.using_pacing());
1603 TEST_F(QuicSentPacketManagerTest
, NegotiateReceiveWindowFromOptions
) {
1604 EXPECT_EQ(kDefaultSocketReceiveBuffer
,
1605 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_
));
1607 // Try to set a size below the minimum and ensure it gets set to the min.
1608 QuicConfig client_config
;
1609 QuicConfigPeer::SetReceivedSocketReceiveBuffer(&client_config
, 1024);
1610 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1611 EXPECT_CALL(*send_algorithm_
, PacingRate())
1612 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1613 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1614 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1615 manager_
.SetFromConfig(client_config
);
1617 EXPECT_EQ(kMinSocketReceiveBuffer
,
1618 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_
));
1620 // Ensure the smaller send window only allows 16 packets to be sent.
1621 for (QuicPacketSequenceNumber i
= 1; i
<= 16; ++i
) {
1622 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1623 QuicTime::Delta::Zero()));
1624 EXPECT_EQ(QuicTime::Delta::Zero(),
1625 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1626 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, BytesInFlight(), i
,
1627 1024, HAS_RETRANSMITTABLE_DATA
))
1628 .WillOnce(Return(true));
1629 SerializedPacket
packet(CreatePacket(i
, true));
1630 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(), 1024,
1631 NOT_RETRANSMISSION
, HAS_RETRANSMITTABLE_DATA
);
1633 EXPECT_EQ(QuicTime::Delta::Infinite(),
1634 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1637 TEST_F(QuicSentPacketManagerTest
, ReceiveWindowLimited
) {
1638 EXPECT_EQ(kDefaultSocketReceiveBuffer
,
1639 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_
));
1641 // Ensure the smaller send window only allows 256 * 0.95 packets to be sent.
1642 for (QuicPacketSequenceNumber i
= 1; i
<= 244; ++i
) {
1643 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1644 QuicTime::Delta::Zero()));
1645 EXPECT_EQ(QuicTime::Delta::Zero(),
1646 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1647 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, BytesInFlight(), i
,
1648 1024, HAS_RETRANSMITTABLE_DATA
))
1649 .WillOnce(Return(true));
1650 SerializedPacket
packet(CreatePacket(i
, true));
1651 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(), 1024,
1652 NOT_RETRANSMISSION
, HAS_RETRANSMITTABLE_DATA
);
1654 EXPECT_EQ(QuicTime::Delta::Infinite(),
1655 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1658 TEST_F(QuicSentPacketManagerTest
, UseInitialRoundTripTimeToSend
) {
1659 uint32 initial_rtt_us
= 325000;
1660 EXPECT_NE(initial_rtt_us
,
1661 manager_
.GetRttStats()->smoothed_rtt().ToMicroseconds());
1664 config
.SetInitialRoundTripTimeUsToSend(initial_rtt_us
);
1665 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
, _
));
1666 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1667 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1668 manager_
.SetFromConfig(config
);
1670 EXPECT_EQ(0, manager_
.GetRttStats()->smoothed_rtt().ToMicroseconds());
1671 EXPECT_EQ(initial_rtt_us
, manager_
.GetRttStats()->initial_rtt_us());
1674 TEST_F(QuicSentPacketManagerTest
, ResumeConnectionState
) {
1675 // The sent packet manager should use the RTT from CachedNetworkParameters if
1677 const int kRttMs
= 1234;
1678 CachedNetworkParameters cached_network_params
;
1679 cached_network_params
.set_min_rtt_ms(kRttMs
);
1681 EXPECT_CALL(*send_algorithm_
, ResumeConnectionState(_
));
1682 manager_
.ResumeConnectionState(cached_network_params
);
1683 EXPECT_EQ(kRttMs
* kNumMicrosPerMilli
,
1684 static_cast<uint64
>(manager_
.GetRttStats()->initial_rtt_us()));