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 // Stream ID for data sent in CreatePacket().
34 const QuicStreamId kStreamId
= 7;
36 // Matcher to check the key of the key-value pair it receives as first argument
37 // equals its second argument.
39 return std::tr1::get
<0>(arg
).first
== std::tr1::get
<1>(arg
);
42 class MockDebugDelegate
: public QuicSentPacketManager::DebugDelegate
{
44 MOCK_METHOD2(OnSpuriousPacketRetransmission
,
45 void(TransmissionType transmission_type
,
46 QuicByteCount byte_size
));
49 class QuicSentPacketManagerTest
: public ::testing::TestWithParam
<bool> {
51 QuicSentPacketManagerTest()
52 : manager_(Perspective::IS_SERVER
, &clock_
, &stats_
, kCubic
, kNack
, true),
53 send_algorithm_(new StrictMock
<MockSendAlgorithm
>),
54 network_change_visitor_(new StrictMock
<MockNetworkChangeVisitor
>) {
55 QuicSentPacketManagerPeer::SetSendAlgorithm(&manager_
, send_algorithm_
);
56 // Disable tail loss probes for most tests.
57 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 0);
58 // Advance the time 1s so the send times are never QuicTime::Zero.
59 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
60 manager_
.set_network_change_visitor(network_change_visitor_
.get());
62 EXPECT_CALL(*send_algorithm_
, HasReliableBandwidthEstimate())
64 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate())
66 .WillRepeatedly(Return(QuicBandwidth::Zero()));
67 EXPECT_CALL(*send_algorithm_
, InSlowStart()).Times(AnyNumber());
68 EXPECT_CALL(*send_algorithm_
, InRecovery()).Times(AnyNumber());
71 ~QuicSentPacketManagerTest() override
{ STLDeleteElements(&packets_
); }
73 QuicByteCount
BytesInFlight() {
74 return QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
);
76 void VerifyUnackedPackets(QuicPacketSequenceNumber
* packets
,
78 if (num_packets
== 0) {
79 EXPECT_FALSE(manager_
.HasUnackedPackets());
80 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
85 EXPECT_TRUE(manager_
.HasUnackedPackets());
86 EXPECT_EQ(packets
[0], manager_
.GetLeastUnacked());
87 for (size_t i
= 0; i
< num_packets
; ++i
) {
88 EXPECT_TRUE(manager_
.IsUnacked(packets
[i
])) << packets
[i
];
92 void VerifyRetransmittablePackets(QuicPacketSequenceNumber
* packets
,
94 EXPECT_EQ(num_packets
,
95 QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
97 for (size_t i
= 0; i
< num_packets
; ++i
) {
98 EXPECT_TRUE(manager_
.HasRetransmittableFrames(packets
[i
]))
99 << " packets[" << i
<< "]:" << packets
[i
];
103 void ExpectAck(QuicPacketSequenceNumber largest_observed
) {
104 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(
105 true, _
, ElementsAre(Pair(largest_observed
, _
)), IsEmpty()));
106 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
107 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
110 void ExpectUpdatedRtt(QuicPacketSequenceNumber largest_observed
) {
111 EXPECT_CALL(*send_algorithm_
,
112 OnCongestionEvent(true, _
, IsEmpty(), IsEmpty()));
113 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
114 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
117 void ExpectAckAndLoss(bool rtt_updated
,
118 QuicPacketSequenceNumber largest_observed
,
119 QuicPacketSequenceNumber lost_packet
) {
120 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(
121 rtt_updated
, _
, ElementsAre(Pair(largest_observed
, _
)),
122 ElementsAre(Pair(lost_packet
, _
))));
123 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
124 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
127 // |packets_acked| and |packets_lost| should be in sequence number order.
128 void ExpectAcksAndLosses(bool rtt_updated
,
129 QuicPacketSequenceNumber
* packets_acked
,
130 size_t num_packets_acked
,
131 QuicPacketSequenceNumber
* packets_lost
,
132 size_t num_packets_lost
) {
133 vector
<QuicPacketSequenceNumber
> ack_vector
;
134 for (size_t i
= 0; i
< num_packets_acked
; ++i
) {
135 ack_vector
.push_back(packets_acked
[i
]);
137 vector
<QuicPacketSequenceNumber
> lost_vector
;
138 for (size_t i
= 0; i
< num_packets_lost
; ++i
) {
139 lost_vector
.push_back(packets_lost
[i
]);
141 EXPECT_CALL(*send_algorithm_
,
142 OnCongestionEvent(rtt_updated
, _
,
143 Pointwise(KeyEq(), ack_vector
),
144 Pointwise(KeyEq(), lost_vector
)));
145 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange()).
147 EXPECT_CALL(*network_change_visitor_
, OnRttChange()).Times(AnyNumber());
150 void RetransmitAndSendPacket(QuicPacketSequenceNumber old_sequence_number
,
151 QuicPacketSequenceNumber new_sequence_number
) {
152 QuicSentPacketManagerPeer::MarkForRetransmission(
153 &manager_
, old_sequence_number
, TLP_RETRANSMISSION
);
154 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
155 QuicSentPacketManager::PendingRetransmission next_retransmission
=
156 manager_
.NextPendingRetransmission();
157 EXPECT_EQ(old_sequence_number
, next_retransmission
.sequence_number
);
158 EXPECT_EQ(TLP_RETRANSMISSION
,
159 next_retransmission
.transmission_type
);
161 EXPECT_CALL(*send_algorithm_
,
162 OnPacketSent(_
, BytesInFlight(), new_sequence_number
,
163 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
164 .WillOnce(Return(true));
165 SerializedPacket
packet(CreatePacket(new_sequence_number
, false));
166 manager_
.OnPacketSent(&packet
,
171 HAS_RETRANSMITTABLE_DATA
);
172 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(
173 &manager_
, new_sequence_number
));
176 SerializedPacket
CreateDataPacket(QuicPacketSequenceNumber sequence_number
) {
177 return CreatePacket(sequence_number
, true);
180 SerializedPacket
CreatePacket(QuicPacketSequenceNumber sequence_number
,
181 bool retransmittable
) {
182 packets_
.push_back(new QuicEncryptedPacket(nullptr, kDefaultLength
));
183 RetransmittableFrames
* frames
= nullptr;
184 if (retransmittable
) {
185 frames
= new RetransmittableFrames(ENCRYPTION_NONE
);
186 frames
->AddStreamFrame(
187 new QuicStreamFrame(kStreamId
, false, 0, IOVector()));
189 return SerializedPacket(sequence_number
, PACKET_6BYTE_SEQUENCE_NUMBER
,
190 packets_
.back(), 0u, frames
);
193 SerializedPacket
CreateFecPacket(QuicPacketSequenceNumber sequence_number
) {
194 packets_
.push_back(new QuicEncryptedPacket(nullptr, kDefaultLength
));
195 SerializedPacket
serialized(sequence_number
, PACKET_6BYTE_SEQUENCE_NUMBER
,
196 packets_
.back(), 0u, nullptr);
197 serialized
.is_fec_packet
= true;
201 void SendDataPacket(QuicPacketSequenceNumber sequence_number
) {
202 EXPECT_CALL(*send_algorithm_
,
203 OnPacketSent(_
, BytesInFlight(), sequence_number
, _
, _
))
204 .Times(1).WillOnce(Return(true));
205 SerializedPacket
packet(CreateDataPacket(sequence_number
));
206 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(),
207 packet
.packet
->length(), NOT_RETRANSMISSION
,
208 HAS_RETRANSMITTABLE_DATA
);
211 void SendCryptoPacket(QuicPacketSequenceNumber sequence_number
) {
212 EXPECT_CALL(*send_algorithm_
,
213 OnPacketSent(_
, BytesInFlight(), sequence_number
,
214 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
215 .Times(1).WillOnce(Return(true));
216 SerializedPacket
packet(CreateDataPacket(sequence_number
));
217 packet
.retransmittable_frames
->AddStreamFrame(
218 new QuicStreamFrame(1, false, 0, IOVector()));
219 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(),
220 packet
.packet
->length(), NOT_RETRANSMISSION
,
221 HAS_RETRANSMITTABLE_DATA
);
224 void SendFecPacket(QuicPacketSequenceNumber sequence_number
) {
225 EXPECT_CALL(*send_algorithm_
,
226 OnPacketSent(_
, BytesInFlight(), sequence_number
,
227 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
228 .Times(1).WillOnce(Return(true));
229 SerializedPacket
packet(CreateFecPacket(sequence_number
));
230 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(),
231 packet
.packet
->length(), NOT_RETRANSMISSION
,
232 NO_RETRANSMITTABLE_DATA
);
235 void SendAckPacket(QuicPacketSequenceNumber sequence_number
) {
236 EXPECT_CALL(*send_algorithm_
,
237 OnPacketSent(_
, BytesInFlight(), sequence_number
,
238 kDefaultLength
, NO_RETRANSMITTABLE_DATA
))
239 .Times(1).WillOnce(Return(false));
240 SerializedPacket
packet(CreatePacket(sequence_number
, false));
241 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(),
242 packet
.packet
->length(), NOT_RETRANSMISSION
,
243 NO_RETRANSMITTABLE_DATA
);
246 // Based on QuicConnection's WritePendingRetransmissions.
247 void RetransmitNextPacket(
248 QuicPacketSequenceNumber retransmission_sequence_number
) {
249 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
250 EXPECT_CALL(*send_algorithm_
,
251 OnPacketSent(_
, _
, retransmission_sequence_number
,
252 kDefaultLength
, HAS_RETRANSMITTABLE_DATA
))
253 .Times(1).WillOnce(Return(true));
254 const QuicSentPacketManager::PendingRetransmission pending
=
255 manager_
.NextPendingRetransmission();
256 SerializedPacket
packet(
257 CreatePacket(retransmission_sequence_number
, false));
258 manager_
.OnPacketSent(&packet
, pending
.sequence_number
, clock_
.Now(),
259 kDefaultLength
, pending
.transmission_type
,
260 HAS_RETRANSMITTABLE_DATA
);
263 QuicSentPacketManager manager_
;
264 vector
<QuicEncryptedPacket
*> packets_
;
266 QuicConnectionStats stats_
;
267 MockSendAlgorithm
* send_algorithm_
;
268 scoped_ptr
<MockNetworkChangeVisitor
> network_change_visitor_
;
271 TEST_F(QuicSentPacketManagerTest
, IsUnacked
) {
272 VerifyUnackedPackets(nullptr, 0);
275 QuicPacketSequenceNumber unacked
[] = { 1 };
276 VerifyUnackedPackets(unacked
, arraysize(unacked
));
277 QuicPacketSequenceNumber retransmittable
[] = { 1 };
278 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
281 TEST_F(QuicSentPacketManagerTest
, IsUnAckedRetransmit
) {
283 RetransmitAndSendPacket(1, 2);
285 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(&manager_
, 2));
286 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
287 VerifyUnackedPackets(unacked
, arraysize(unacked
));
288 QuicPacketSequenceNumber retransmittable
[] = { 2 };
289 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
292 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAck
) {
294 RetransmitAndSendPacket(1, 2);
297 QuicAckFrame ack_frame
;
298 ack_frame
.largest_observed
= 2;
299 ack_frame
.missing_packets
.insert(1);
301 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
303 // Packet 1 is unacked, pending, but not retransmittable.
304 QuicPacketSequenceNumber unacked
[] = { 1 };
305 VerifyUnackedPackets(unacked
, arraysize(unacked
));
306 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
307 VerifyRetransmittablePackets(nullptr, 0);
310 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAckBeforeSend
) {
312 QuicSentPacketManagerPeer::MarkForRetransmission(
313 &manager_
, 1, TLP_RETRANSMISSION
);
314 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
317 QuicAckFrame ack_frame
;
318 ack_frame
.largest_observed
= 1;
320 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
322 // There should no longer be a pending retransmission.
323 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
325 // No unacked packets remain.
326 VerifyUnackedPackets(nullptr, 0);
327 VerifyRetransmittablePackets(nullptr, 0);
328 EXPECT_EQ(0u, stats_
.packets_spuriously_retransmitted
);
331 TEST_F(QuicSentPacketManagerTest
, RetransmitThenStopRetransmittingBeforeSend
) {
333 QuicSentPacketManagerPeer::MarkForRetransmission(&manager_
, 1,
335 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
337 manager_
.CancelRetransmissionsForStream(kStreamId
);
339 // There should no longer be a pending retransmission.
340 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
342 QuicPacketSequenceNumber unacked
[] = {1};
343 VerifyUnackedPackets(unacked
, arraysize(unacked
));
344 VerifyRetransmittablePackets(nullptr, 0);
345 EXPECT_EQ(0u, stats_
.packets_spuriously_retransmitted
);
348 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAckPrevious
) {
350 RetransmitAndSendPacket(1, 2);
351 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
352 clock_
.AdvanceTime(rtt
);
356 QuicAckFrame ack_frame
;
357 ack_frame
.largest_observed
= 1;
358 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
360 // 2 remains unacked, but no packets have retransmittable data.
361 QuicPacketSequenceNumber unacked
[] = { 2 };
362 VerifyUnackedPackets(unacked
, arraysize(unacked
));
363 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
364 VerifyRetransmittablePackets(nullptr, 0);
366 EXPECT_EQ(1u, stats_
.packets_spuriously_retransmitted
);
369 TEST_F(QuicSentPacketManagerTest
, RetransmitThenAckPreviousThenNackRetransmit
) {
371 RetransmitAndSendPacket(1, 2);
372 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
373 clock_
.AdvanceTime(rtt
);
375 // First, ACK packet 1 which makes packet 2 non-retransmittable.
377 QuicAckFrame ack_frame
;
378 ack_frame
.largest_observed
= 1;
379 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
384 clock_
.AdvanceTime(rtt
);
386 // Next, NACK packet 2 three times.
387 ack_frame
.largest_observed
= 3;
388 ack_frame
.missing_packets
.insert(2);
390 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
392 ack_frame
.largest_observed
= 4;
394 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
396 ack_frame
.largest_observed
= 5;
397 ExpectAckAndLoss(true, 5, 2);
398 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
400 // No packets remain unacked.
401 VerifyUnackedPackets(nullptr, 0);
402 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
403 VerifyRetransmittablePackets(nullptr, 0);
405 // Verify that the retransmission alarm would not fire,
406 // since there is no retransmittable data outstanding.
407 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
410 TEST_F(QuicSentPacketManagerTest
,
411 DISABLED_RetransmitTwiceThenAckPreviousBeforeSend
) {
413 RetransmitAndSendPacket(1, 2);
415 // Fire the RTO, which will mark 2 for retransmission (but will not send it).
416 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
417 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
418 manager_
.OnRetransmissionTimeout();
419 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
421 // Ack 1 but not 2, before 2 is able to be sent.
422 // Since 1 has been retransmitted, it has already been lost, and so the
423 // send algorithm is not informed that it has been ACK'd.
424 QuicAckFrame ack_frame
;
425 ack_frame
.largest_observed
= 1;
427 EXPECT_CALL(*send_algorithm_
, RevertRetransmissionTimeout());
428 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
430 // Since 2 was marked for retransmit, when 1 is acked, 2 is kept for RTT.
431 QuicPacketSequenceNumber unacked
[] = { 2 };
432 VerifyUnackedPackets(unacked
, arraysize(unacked
));
433 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
434 VerifyRetransmittablePackets(nullptr, 0);
436 // Verify that the retransmission alarm would not fire,
437 // since there is no retransmittable data outstanding.
438 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
441 TEST_F(QuicSentPacketManagerTest
, RetransmitTwiceThenAckFirst
) {
442 StrictMock
<MockDebugDelegate
> debug_delegate
;
443 EXPECT_CALL(debug_delegate
, OnSpuriousPacketRetransmission(
444 TLP_RETRANSMISSION
, kDefaultLength
)).Times(2);
445 manager_
.set_debug_delegate(&debug_delegate
);
448 RetransmitAndSendPacket(1, 2);
449 RetransmitAndSendPacket(2, 3);
450 QuicTime::Delta rtt
= QuicTime::Delta::FromMilliseconds(15);
451 clock_
.AdvanceTime(rtt
);
453 // Ack 1 but not 2 or 3.
455 QuicAckFrame ack_frame
;
456 ack_frame
.largest_observed
= 1;
457 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
459 // 2 and 3 remain unacked, but no packets have retransmittable data.
460 QuicPacketSequenceNumber unacked
[] = { 2, 3 };
461 VerifyUnackedPackets(unacked
, arraysize(unacked
));
462 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
463 VerifyRetransmittablePackets(nullptr, 0);
465 // Ensure packet 2 is lost when 4 is sent and 3 and 4 are acked.
467 ack_frame
.largest_observed
= 4;
468 ack_frame
.missing_packets
.insert(2);
469 QuicPacketSequenceNumber acked
[] = { 3, 4 };
470 ExpectAcksAndLosses(true, acked
, arraysize(acked
), nullptr, 0);
471 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
473 QuicPacketSequenceNumber unacked2
[] = { 2 };
474 VerifyUnackedPackets(unacked2
, arraysize(unacked2
));
475 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
478 ack_frame
.largest_observed
= 5;
479 ExpectAckAndLoss(true, 5, 2);
480 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
482 VerifyUnackedPackets(nullptr, 0);
483 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
484 EXPECT_EQ(2u, stats_
.packets_spuriously_retransmitted
);
487 TEST_F(QuicSentPacketManagerTest
, LoseButDontRetransmitRevivedPacket
) {
493 // Ack 2 and 3, and mark 1 as revived.
494 QuicAckFrame ack_frame
;
495 ack_frame
.largest_observed
= 3;
496 ack_frame
.missing_packets
.insert(1);
497 ack_frame
.revived_packets
.insert(1);
498 QuicPacketSequenceNumber acked
[] = { 2, 3 };
499 ExpectAcksAndLosses(true, acked
, arraysize(acked
), nullptr, 0);
500 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
502 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
503 QuicPacketSequenceNumber unacked
[] = { 1, 4 };
504 VerifyUnackedPackets(unacked
, arraysize(unacked
));
505 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
506 QuicPacketSequenceNumber retransmittable
[] = { 4 };
507 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
509 // Ack the 4th packet and expect the 1st to be considered lost.
510 ack_frame
.largest_observed
= 4;
511 ExpectAckAndLoss(true, 4, 1);
512 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
514 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
515 VerifyRetransmittablePackets(nullptr, 0);
518 TEST_F(QuicSentPacketManagerTest
, MarkLostThenReviveAndDontRetransmitPacket
) {
525 // Ack 2, 3, and 4, and expect the 1st to be considered lost.
526 QuicAckFrame ack_frame
;
527 ack_frame
.largest_observed
= 4;
528 ack_frame
.missing_packets
.insert(1);
529 QuicPacketSequenceNumber acked
[] = { 2, 3, 4 };
530 QuicPacketSequenceNumber lost
[] = { 1 };
531 ExpectAcksAndLosses(true, acked
, arraysize(acked
), lost
, arraysize(lost
));
532 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
534 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
535 QuicPacketSequenceNumber unacked
[] = { 1, 5 };
536 VerifyUnackedPackets(unacked
, arraysize(unacked
));
537 QuicPacketSequenceNumber retransmittable
[] = { 1 };
538 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
540 // Ack 5th packet (FEC) and revive 1st packet. 1st packet should now be
541 // removed from pending retransmissions map.
542 ack_frame
.largest_observed
= 5;
543 ack_frame
.revived_packets
.insert(1);
545 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
547 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
548 VerifyRetransmittablePackets(nullptr, 0);
551 TEST_F(QuicSentPacketManagerTest
, TruncatedAck
) {
553 RetransmitAndSendPacket(1, 2);
554 RetransmitAndSendPacket(2, 3);
555 RetransmitAndSendPacket(3, 4);
556 RetransmitAndSendPacket(4, 5);
558 // Truncated ack with 4 NACKs, so the first packet is lost.
559 QuicAckFrame ack_frame
;
560 ack_frame
.largest_observed
= 4;
561 ack_frame
.missing_packets
.insert(1);
562 ack_frame
.missing_packets
.insert(2);
563 ack_frame
.missing_packets
.insert(3);
564 ack_frame
.missing_packets
.insert(4);
565 ack_frame
.is_truncated
= true;
567 QuicPacketSequenceNumber lost
[] = { 1 };
568 ExpectAcksAndLosses(true, nullptr, 0, lost
, arraysize(lost
));
569 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
571 // High water mark will be raised.
572 QuicPacketSequenceNumber unacked
[] = { 2, 3, 4, 5 };
573 VerifyUnackedPackets(unacked
, arraysize(unacked
));
574 QuicPacketSequenceNumber retransmittable
[] = { 5 };
575 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
578 TEST_F(QuicSentPacketManagerTest
, AckPreviousTransmissionThenTruncatedAck
) {
580 RetransmitAndSendPacket(1, 2);
581 RetransmitAndSendPacket(2, 3);
582 RetransmitAndSendPacket(3, 4);
589 // Ack previous transmission
591 QuicAckFrame ack_frame
;
592 ack_frame
.largest_observed
= 2;
593 ack_frame
.missing_packets
.insert(1);
595 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
596 EXPECT_TRUE(manager_
.IsUnacked(4));
599 // Truncated ack with 4 NACKs
601 QuicAckFrame ack_frame
;
602 ack_frame
.largest_observed
= 6;
603 ack_frame
.missing_packets
.insert(3);
604 ack_frame
.missing_packets
.insert(4);
605 ack_frame
.missing_packets
.insert(5);
606 ack_frame
.missing_packets
.insert(6);
607 ack_frame
.is_truncated
= true;
608 ExpectAckAndLoss(true, 1, 3);
609 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
612 // High water mark will be raised.
613 QuicPacketSequenceNumber unacked
[] = { 4, 5, 6, 7, 8, 9 };
614 VerifyUnackedPackets(unacked
, arraysize(unacked
));
615 QuicPacketSequenceNumber retransmittable
[] = { 5, 6, 7, 8, 9 };
616 VerifyRetransmittablePackets(retransmittable
, arraysize(retransmittable
));
619 TEST_F(QuicSentPacketManagerTest
, GetLeastUnacked
) {
620 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
623 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedUnacked
) {
625 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
628 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedUnackedFec
) {
630 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
633 TEST_F(QuicSentPacketManagerTest
, GetLeastUnackedAndDiscard
) {
634 VerifyUnackedPackets(nullptr, 0);
637 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
640 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
643 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
645 QuicPacketSequenceNumber unacked
[] = { 1, 2, 3 };
646 VerifyUnackedPackets(unacked
, arraysize(unacked
));
647 VerifyRetransmittablePackets(nullptr, 0);
649 // Ack 2, so there's an rtt update.
651 QuicAckFrame ack_frame
;
652 ack_frame
.largest_observed
= 2;
653 ack_frame
.missing_packets
.insert(1);
654 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
656 EXPECT_EQ(1u, manager_
.GetLeastUnacked());
659 TEST_F(QuicSentPacketManagerTest
, GetSentTime
) {
660 VerifyUnackedPackets(nullptr, 0);
662 QuicTime sent_time
= clock_
.Now();
664 QuicTime sent_time2
= clock_
.Now();
666 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
667 VerifyUnackedPackets(unacked
, arraysize(unacked
));
668 VerifyRetransmittablePackets(nullptr, 0);
670 EXPECT_TRUE(manager_
.HasUnackedPackets());
671 EXPECT_EQ(sent_time
, QuicSentPacketManagerPeer::GetSentTime(&manager_
, 1));
672 EXPECT_EQ(sent_time2
, QuicSentPacketManagerPeer::GetSentTime(&manager_
, 2));
675 TEST_F(QuicSentPacketManagerTest
, AckAckAndUpdateRtt
) {
679 // Now ack the ack and expect an RTT update.
680 QuicAckFrame ack_frame
;
681 ack_frame
.largest_observed
= 2;
682 ack_frame
.delta_time_largest_observed
=
683 QuicTime::Delta::FromMilliseconds(5);
686 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
690 // Now ack the ack and expect only an RTT update.
691 ack_frame
.largest_observed
= 3;
693 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
696 TEST_F(QuicSentPacketManagerTest
, Rtt
) {
697 QuicPacketSequenceNumber sequence_number
= 1;
698 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(15);
699 SendDataPacket(sequence_number
);
700 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
702 ExpectAck(sequence_number
);
703 QuicAckFrame ack_frame
;
704 ack_frame
.largest_observed
= sequence_number
;
705 ack_frame
.delta_time_largest_observed
=
706 QuicTime::Delta::FromMilliseconds(5);
707 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
708 EXPECT_EQ(expected_rtt
,
709 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
712 TEST_F(QuicSentPacketManagerTest
, RttWithInvalidDelta
) {
713 // Expect that the RTT is equal to the local time elapsed, since the
714 // delta_time_largest_observed is larger than the local time elapsed
715 // and is hence invalid.
716 QuicPacketSequenceNumber sequence_number
= 1;
717 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(10);
718 SendDataPacket(sequence_number
);
719 clock_
.AdvanceTime(expected_rtt
);
721 ExpectAck(sequence_number
);
722 QuicAckFrame ack_frame
;
723 ack_frame
.largest_observed
= sequence_number
;
724 ack_frame
.delta_time_largest_observed
=
725 QuicTime::Delta::FromMilliseconds(11);
726 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
727 EXPECT_EQ(expected_rtt
,
728 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
731 TEST_F(QuicSentPacketManagerTest
, RttWithInfiniteDelta
) {
732 // Expect that the RTT is equal to the local time elapsed, since the
733 // delta_time_largest_observed is infinite, and is hence invalid.
734 QuicPacketSequenceNumber sequence_number
= 1;
735 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(10);
736 SendDataPacket(sequence_number
);
737 clock_
.AdvanceTime(expected_rtt
);
739 ExpectAck(sequence_number
);
740 QuicAckFrame ack_frame
;
741 ack_frame
.largest_observed
= sequence_number
;
742 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Infinite();
743 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
744 EXPECT_EQ(expected_rtt
,
745 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
748 TEST_F(QuicSentPacketManagerTest
, RttZeroDelta
) {
749 // Expect that the RTT is the time between send and receive since the
750 // delta_time_largest_observed is zero.
751 QuicPacketSequenceNumber sequence_number
= 1;
752 QuicTime::Delta expected_rtt
= QuicTime::Delta::FromMilliseconds(10);
753 SendDataPacket(sequence_number
);
754 clock_
.AdvanceTime(expected_rtt
);
756 ExpectAck(sequence_number
);
757 QuicAckFrame ack_frame
;
758 ack_frame
.largest_observed
= sequence_number
;
759 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
760 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
761 EXPECT_EQ(expected_rtt
,
762 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->latest_rtt());
765 TEST_F(QuicSentPacketManagerTest
, TailLossProbeTimeout
) {
766 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
769 QuicPacketSequenceNumber sequence_number
= 1;
770 SendDataPacket(sequence_number
);
772 // The first tail loss probe retransmits 1 packet.
773 manager_
.OnRetransmissionTimeout();
774 EXPECT_EQ(QuicTime::Delta::Zero(),
775 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
776 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
777 manager_
.MaybeRetransmitTailLossProbe();
778 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
779 RetransmitNextPacket(2);
780 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
782 // The second tail loss probe retransmits 1 packet.
783 manager_
.OnRetransmissionTimeout();
784 EXPECT_EQ(QuicTime::Delta::Zero(),
785 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
786 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
787 manager_
.MaybeRetransmitTailLossProbe();
788 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
789 RetransmitNextPacket(3);
790 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
791 QuicTime::Delta::Infinite()));
792 EXPECT_EQ(QuicTime::Delta::Infinite(),
793 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
794 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
796 // Ack the third and ensure the first two are still pending.
798 QuicAckFrame ack_frame
;
799 ack_frame
.largest_observed
= 3;
800 ack_frame
.missing_packets
.insert(1);
801 ack_frame
.missing_packets
.insert(2);
802 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
804 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
806 // Acking two more packets will lose both of them due to nacks.
807 ack_frame
.largest_observed
= 5;
808 QuicPacketSequenceNumber lost
[] = { 1, 2 };
809 ExpectAcksAndLosses(false, nullptr, 0, lost
, arraysize(lost
));
810 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
812 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
813 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
814 EXPECT_EQ(2u, stats_
.tlp_count
);
815 EXPECT_EQ(0u, stats_
.rto_count
);
818 TEST_F(QuicSentPacketManagerTest
, TailLossProbeThenRTO
) {
819 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
822 const size_t kNumSentPackets
= 100;
823 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
826 QuicTime rto_packet_time
= clock_
.Now();
828 clock_
.AdvanceTime(manager_
.GetRetransmissionTime().Subtract(clock_
.Now()));
830 // The first tail loss probe retransmits 1 packet.
831 manager_
.OnRetransmissionTimeout();
832 EXPECT_EQ(QuicTime::Delta::Zero(),
833 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
834 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
835 manager_
.MaybeRetransmitTailLossProbe();
836 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
837 RetransmitNextPacket(101);
838 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
839 QuicTime::Delta::Infinite()));
840 EXPECT_EQ(QuicTime::Delta::Infinite(),
841 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
842 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
843 clock_
.AdvanceTime(manager_
.GetRetransmissionTime().Subtract(clock_
.Now()));
845 // The second tail loss probe retransmits 1 packet.
846 manager_
.OnRetransmissionTimeout();
847 EXPECT_EQ(QuicTime::Delta::Zero(),
848 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
849 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
850 EXPECT_TRUE(manager_
.MaybeRetransmitTailLossProbe());
851 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
852 RetransmitNextPacket(102);
853 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
854 QuicTime::Delta::Infinite()));
855 EXPECT_EQ(QuicTime::Delta::Infinite(),
856 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
858 // Ensure the RTO is set based on the correct packet.
859 rto_packet_time
= clock_
.Now();
860 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
861 .WillOnce(Return(QuicTime::Delta::FromSeconds(1)));
862 EXPECT_EQ(rto_packet_time
.Add(QuicTime::Delta::FromSeconds(1)),
863 manager_
.GetRetransmissionTime());
865 // Advance the time enough to ensure all packets are RTO'd.
866 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
868 manager_
.OnRetransmissionTimeout();
869 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
870 EXPECT_EQ(2u, stats_
.tlp_count
);
871 EXPECT_EQ(1u, stats_
.rto_count
);
873 // Send and Ack the RTO and ensure OnRetransmissionTimeout is called.
874 EXPECT_EQ(102 * kDefaultLength
,
875 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
877 RetransmitNextPacket(103);
878 QuicAckFrame ack_frame
;
879 ack_frame
.largest_observed
= 103;
880 for (int i
= 0; i
< 103; ++i
) {
881 ack_frame
.missing_packets
.insert(i
);
883 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
884 EXPECT_CALL(*send_algorithm_
,
885 OnCongestionEvent(true, _
, ElementsAre(Pair(103, _
)), _
));
886 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
887 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
888 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
889 // All packets before 103 should be lost.
890 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
893 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeTimeout
) {
894 // Send 2 crypto packets and 3 data packets.
895 const size_t kNumSentCryptoPackets
= 2;
896 for (size_t i
= 1; i
<= kNumSentCryptoPackets
; ++i
) {
899 const size_t kNumSentDataPackets
= 3;
900 for (size_t i
= 1; i
<= kNumSentDataPackets
; ++i
) {
901 SendDataPacket(kNumSentCryptoPackets
+ i
);
903 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
905 // The first retransmits 2 packets.
906 manager_
.OnRetransmissionTimeout();
907 EXPECT_EQ(QuicTime::Delta::Zero(),
908 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
909 RetransmitNextPacket(6);
910 RetransmitNextPacket(7);
911 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
912 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
914 // The second retransmits 2 packets.
915 manager_
.OnRetransmissionTimeout();
916 EXPECT_EQ(QuicTime::Delta::Zero(),
917 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
918 RetransmitNextPacket(8);
919 RetransmitNextPacket(9);
920 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
921 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
923 // Now ack the two crypto packets and the speculatively encrypted request,
924 // and ensure the first four crypto packets get abandoned, but not lost.
925 QuicPacketSequenceNumber acked
[] = { 3, 4, 5, 8, 9 };
926 ExpectAcksAndLosses(true, acked
, arraysize(acked
), nullptr, 0);
927 QuicAckFrame ack_frame
;
928 ack_frame
.largest_observed
= 9;
929 ack_frame
.missing_packets
.insert(1);
930 ack_frame
.missing_packets
.insert(2);
931 ack_frame
.missing_packets
.insert(6);
932 ack_frame
.missing_packets
.insert(7);
933 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
935 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
938 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeTimeoutVersionNegotiation
) {
939 // Send 2 crypto packets and 3 data packets.
940 const size_t kNumSentCryptoPackets
= 2;
941 for (size_t i
= 1; i
<= kNumSentCryptoPackets
; ++i
) {
944 const size_t kNumSentDataPackets
= 3;
945 for (size_t i
= 1; i
<= kNumSentDataPackets
; ++i
) {
946 SendDataPacket(kNumSentCryptoPackets
+ i
);
948 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
950 // The first retransmission timeout retransmits 2 crypto packets.
951 manager_
.OnRetransmissionTimeout();
952 RetransmitNextPacket(6);
953 RetransmitNextPacket(7);
954 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
955 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
957 // Now act like a version negotiation packet arrived, which would cause all
958 // unacked packets to be retransmitted.
959 manager_
.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION
);
961 // Ensure the first two pending packets are the crypto retransmits.
962 ASSERT_TRUE(manager_
.HasPendingRetransmissions());
963 EXPECT_EQ(6u, manager_
.NextPendingRetransmission().sequence_number
);
964 RetransmitNextPacket(8);
965 EXPECT_EQ(7u, manager_
.NextPendingRetransmission().sequence_number
);
966 RetransmitNextPacket(9);
968 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
969 // Send 3 more data packets and ensure the least unacked is raised.
970 RetransmitNextPacket(10);
971 RetransmitNextPacket(11);
972 RetransmitNextPacket(12);
973 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
975 EXPECT_EQ(8u, manager_
.GetLeastUnacked());
978 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeSpuriousRetransmission
) {
979 // Send 1 crypto packet.
981 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
983 // Retransmit the crypto packet as 2.
984 manager_
.OnRetransmissionTimeout();
985 RetransmitNextPacket(2);
987 // Retransmit the crypto packet as 3.
988 manager_
.OnRetransmissionTimeout();
989 RetransmitNextPacket(3);
991 // Now ack the second crypto packet, and ensure the first gets removed, but
992 // the third does not.
994 QuicAckFrame ack_frame
;
995 ack_frame
.largest_observed
= 2;
996 ack_frame
.missing_packets
.insert(1);
997 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
999 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1000 QuicPacketSequenceNumber unacked
[] = { 3 };
1001 VerifyUnackedPackets(unacked
, arraysize(unacked
));
1004 TEST_F(QuicSentPacketManagerTest
, CryptoHandshakeTimeoutUnsentDataPacket
) {
1005 // Send 2 crypto packets and 1 data packet.
1006 const size_t kNumSentCryptoPackets
= 2;
1007 for (size_t i
= 1; i
<= kNumSentCryptoPackets
; ++i
) {
1008 SendCryptoPacket(i
);
1011 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1013 // Retransmit 2 crypto packets, but not the serialized packet.
1014 manager_
.OnRetransmissionTimeout();
1015 RetransmitNextPacket(4);
1016 RetransmitNextPacket(5);
1017 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1018 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1021 TEST_F(QuicSentPacketManagerTest
,
1022 CryptoHandshakeRetransmissionThenRetransmitAll
) {
1023 // Send 1 crypto packet.
1024 SendCryptoPacket(1);
1025 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1027 // Retransmit the crypto packet as 2.
1028 manager_
.OnRetransmissionTimeout();
1029 RetransmitNextPacket(2);
1031 // Now retransmit all the unacked packets, which occurs when there is a
1032 // version negotiation.
1033 manager_
.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION
);
1034 QuicPacketSequenceNumber unacked
[] = { 1, 2 };
1035 VerifyUnackedPackets(unacked
, arraysize(unacked
));
1036 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1037 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1038 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
1041 TEST_F(QuicSentPacketManagerTest
,
1042 CryptoHandshakeRetransmissionThenNeuterAndAck
) {
1043 // Send 1 crypto packet.
1044 SendCryptoPacket(1);
1045 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1047 // Retransmit the crypto packet as 2.
1048 manager_
.OnRetransmissionTimeout();
1049 RetransmitNextPacket(2);
1051 // Retransmit the crypto packet as 3.
1052 manager_
.OnRetransmissionTimeout();
1053 RetransmitNextPacket(3);
1055 // Now neuter all unacked unencrypted packets, which occurs when the
1056 // connection goes forward secure.
1057 manager_
.NeuterUnencryptedPackets();
1058 QuicPacketSequenceNumber unacked
[] = { 1, 2, 3};
1059 VerifyUnackedPackets(unacked
, arraysize(unacked
));
1060 VerifyRetransmittablePackets(nullptr, 0);
1061 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1062 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_
));
1063 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_
));
1065 // Ensure both packets get discarded when packet 2 is acked.
1066 QuicAckFrame ack_frame
;
1067 ack_frame
.largest_observed
= 3;
1068 ack_frame
.missing_packets
.insert(1);
1069 ack_frame
.missing_packets
.insert(2);
1070 ExpectUpdatedRtt(3);
1071 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
1072 VerifyUnackedPackets(nullptr, 0);
1073 VerifyRetransmittablePackets(nullptr, 0);
1076 TEST_F(QuicSentPacketManagerTest
, RetransmissionTimeout
) {
1077 // Send 100 packets.
1078 const size_t kNumSentPackets
= 100;
1079 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
1083 EXPECT_FALSE(manager_
.MaybeRetransmitTailLossProbe());
1084 manager_
.OnRetransmissionTimeout();
1085 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1086 EXPECT_EQ(100 * kDefaultLength
,
1087 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1088 RetransmitNextPacket(101);
1089 RetransmitNextPacket(102);
1090 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1092 // Ack a retransmission.
1093 QuicAckFrame ack_frame
;
1094 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1095 ack_frame
.largest_observed
= 102;
1096 for (int i
= 0; i
< 102; ++i
) {
1097 ack_frame
.missing_packets
.insert(i
);
1099 // Ensure no packets are lost.
1100 EXPECT_CALL(*send_algorithm_
,
1101 OnCongestionEvent(true, _
, ElementsAre(Pair(102, _
)),
1102 /*lost_packets=*/IsEmpty()));
1103 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1104 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1105 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1106 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1109 TEST_F(QuicSentPacketManagerTest
, NewRetransmissionTimeout
) {
1110 QuicConfig client_config
;
1111 QuicTagVector options
;
1112 options
.push_back(kNRTO
);
1113 QuicSentPacketManagerPeer::SetPerspective(&manager_
, Perspective::IS_CLIENT
);
1114 client_config
.SetConnectionOptionsToSend(options
);
1115 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1116 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1117 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1118 EXPECT_CALL(*send_algorithm_
, PacingRate())
1119 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1120 manager_
.SetFromConfig(client_config
);
1121 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1123 // Send 100 packets.
1124 const size_t kNumSentPackets
= 100;
1125 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
1129 EXPECT_FALSE(manager_
.MaybeRetransmitTailLossProbe());
1130 manager_
.OnRetransmissionTimeout();
1131 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1132 EXPECT_EQ(100 * kDefaultLength
,
1133 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1134 RetransmitNextPacket(101);
1135 RetransmitNextPacket(102);
1136 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1138 // Ack a retransmission and expect no call to OnRetransmissionTimeout.
1139 QuicAckFrame ack_frame
;
1140 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1141 ack_frame
.largest_observed
= 102;
1142 for (int i
= 0; i
< 102; ++i
) {
1143 ack_frame
.missing_packets
.insert(i
);
1145 // This will include packets in the lost packet map.
1146 EXPECT_CALL(*send_algorithm_
,
1147 OnCongestionEvent(true, _
, ElementsAre(Pair(102, _
)),
1148 /*lost_packets=*/Not(IsEmpty())));
1149 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1150 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1151 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1154 TEST_F(QuicSentPacketManagerTest
, TwoRetransmissionTimeoutsAckSecond
) {
1158 manager_
.OnRetransmissionTimeout();
1159 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1160 EXPECT_EQ(kDefaultLength
,
1161 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1162 RetransmitNextPacket(2);
1163 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1165 // Rto a second time.
1166 manager_
.OnRetransmissionTimeout();
1167 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1168 EXPECT_EQ(2 * kDefaultLength
,
1169 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1170 RetransmitNextPacket(3);
1171 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1173 // Ack a retransmission and ensure OnRetransmissionTimeout is called.
1174 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1175 QuicAckFrame ack_frame
;
1176 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1177 ack_frame
.largest_observed
= 2;
1178 ack_frame
.missing_packets
.insert(1);
1180 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1182 // The original packet and newest should be outstanding.
1183 EXPECT_EQ(2 * kDefaultLength
,
1184 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1187 TEST_F(QuicSentPacketManagerTest
, TwoRetransmissionTimeoutsAckFirst
) {
1191 manager_
.OnRetransmissionTimeout();
1192 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1193 EXPECT_EQ(kDefaultLength
,
1194 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1195 RetransmitNextPacket(2);
1196 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1198 // Rto a second time.
1199 manager_
.OnRetransmissionTimeout();
1200 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1201 EXPECT_EQ(2 * kDefaultLength
,
1202 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1203 RetransmitNextPacket(3);
1204 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1206 // Ack a retransmission and ensure OnRetransmissionTimeout is called.
1207 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1208 QuicAckFrame ack_frame
;
1209 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1210 ack_frame
.largest_observed
= 3;
1211 ack_frame
.missing_packets
.insert(1);
1212 ack_frame
.missing_packets
.insert(2);
1214 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1216 // The first two packets should still be outstanding.
1217 EXPECT_EQ(2 * kDefaultLength
,
1218 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1221 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTime
) {
1222 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
1225 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeCryptoHandshake
) {
1226 SendCryptoPacket(1);
1229 RttStats
* rtt_stats
= QuicSentPacketManagerPeer::GetRttStats(&manager_
);
1230 rtt_stats
->set_initial_rtt_us(1 * kNumMicrosPerMilli
);
1231 EXPECT_EQ(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1232 manager_
.GetRetransmissionTime());
1234 // Test with a standard smoothed RTT.
1235 rtt_stats
->set_initial_rtt_us(100 * kNumMicrosPerMilli
);
1237 QuicTime::Delta srtt
=
1238 QuicTime::Delta::FromMicroseconds(rtt_stats
->initial_rtt_us());
1239 QuicTime expected_time
= clock_
.Now().Add(srtt
.Multiply(1.5));
1240 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1242 // Retransmit the packet by invoking the retransmission timeout.
1243 clock_
.AdvanceTime(srtt
.Multiply(1.5));
1244 manager_
.OnRetransmissionTimeout();
1245 RetransmitNextPacket(2);
1247 // The retransmission time should now be twice as far in the future.
1248 expected_time
= clock_
.Now().Add(srtt
.Multiply(2).Multiply(1.5));
1249 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1252 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeTailLossProbe
) {
1253 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
1258 RttStats
* rtt_stats
= QuicSentPacketManagerPeer::GetRttStats(&manager_
);
1259 rtt_stats
->set_initial_rtt_us(1 * kNumMicrosPerMilli
);
1260 EXPECT_EQ(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1261 manager_
.GetRetransmissionTime());
1263 // Test with a standard smoothed RTT.
1264 rtt_stats
->set_initial_rtt_us(100 * kNumMicrosPerMilli
);
1265 QuicTime::Delta srtt
=
1266 QuicTime::Delta::FromMicroseconds(rtt_stats
->initial_rtt_us());
1267 QuicTime::Delta expected_tlp_delay
= srtt
.Multiply(2);
1268 QuicTime expected_time
= clock_
.Now().Add(expected_tlp_delay
);
1269 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1271 // Retransmit the packet by invoking the retransmission timeout.
1272 clock_
.AdvanceTime(expected_tlp_delay
);
1273 manager_
.OnRetransmissionTimeout();
1274 EXPECT_EQ(QuicTime::Delta::Zero(),
1275 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1276 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1277 EXPECT_TRUE(manager_
.MaybeRetransmitTailLossProbe());
1278 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1279 RetransmitNextPacket(3);
1280 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1281 QuicTime::Delta::Infinite()));
1282 EXPECT_EQ(QuicTime::Delta::Infinite(),
1283 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1284 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1286 expected_time
= clock_
.Now().Add(expected_tlp_delay
);
1287 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1290 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeSpuriousRTO
) {
1291 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->UpdateRtt(
1292 QuicTime::Delta::FromMilliseconds(100),
1293 QuicTime::Delta::Zero(),
1301 QuicTime::Delta expected_rto_delay
= QuicTime::Delta::FromMilliseconds(500);
1302 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1303 .WillRepeatedly(Return(expected_rto_delay
));
1304 QuicTime expected_time
= clock_
.Now().Add(expected_rto_delay
);
1305 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1307 // Retransmit the packet by invoking the retransmission timeout.
1308 clock_
.AdvanceTime(expected_rto_delay
);
1309 manager_
.OnRetransmissionTimeout();
1310 // All packets are still considered inflight.
1311 EXPECT_EQ(4 * kDefaultLength
,
1312 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1313 RetransmitNextPacket(5);
1314 RetransmitNextPacket(6);
1315 // All previous packets are inflight, plus two rto retransmissions.
1316 EXPECT_EQ(6 * kDefaultLength
,
1317 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1318 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1320 // The delay should double the second time.
1321 expected_time
= clock_
.Now().Add(expected_rto_delay
).Add(expected_rto_delay
);
1322 // Once we always base the timer on the right edge, leaving the older packets
1323 // in flight doesn't change the timeout.
1324 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1326 // Ack a packet before the first RTO and ensure the RTO timeout returns to the
1327 // original value and OnRetransmissionTimeout is not called or reverted.
1328 QuicAckFrame ack_frame
;
1329 ack_frame
.largest_observed
= 2;
1330 ack_frame
.missing_packets
.insert(1);
1332 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
1333 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1334 EXPECT_EQ(5 * kDefaultLength
,
1335 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1337 // Wait 2RTTs from now for the RTO, since it's the max of the RTO time
1338 // and the TLP time. In production, there would always be two TLP's first.
1339 // Since retransmission was spurious, smoothed_rtt_ is expired, and replaced
1340 // by the latest RTT sample of 500ms.
1341 expected_time
= clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(1000));
1342 // Once we always base the timer on the right edge, leaving the older packets
1343 // in flight doesn't change the timeout.
1344 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1347 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelayMin
) {
1349 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1350 .WillRepeatedly(Return(QuicTime::Delta::FromMilliseconds(1)));
1351 QuicTime::Delta delay
= QuicTime::Delta::FromMilliseconds(200);
1353 // If the delay is smaller than the min, ensure it exponentially backs off
1355 for (int i
= 0; i
< 5; ++i
) {
1357 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1358 delay
= delay
.Add(delay
);
1359 manager_
.OnRetransmissionTimeout();
1360 RetransmitNextPacket(i
+ 2);
1364 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelayMax
) {
1365 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1366 .WillOnce(Return(QuicTime::Delta::FromSeconds(500)));
1368 EXPECT_EQ(QuicTime::Delta::FromSeconds(60),
1369 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1372 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelay
) {
1374 QuicTime::Delta delay
= QuicTime::Delta::FromMilliseconds(500);
1375 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1376 .WillRepeatedly(Return(delay
));
1378 // Delay should back off exponentially.
1379 for (int i
= 0; i
< 5; ++i
) {
1381 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1382 delay
= delay
.Add(delay
);
1383 manager_
.OnRetransmissionTimeout();
1384 RetransmitNextPacket(i
+ 2);
1388 TEST_F(QuicSentPacketManagerTest
, GetLossDelay
) {
1389 MockLossAlgorithm
* loss_algorithm
= new MockLossAlgorithm();
1390 QuicSentPacketManagerPeer::SetLossAlgorithm(&manager_
, loss_algorithm
);
1392 EXPECT_CALL(*loss_algorithm
, GetLossTimeout())
1393 .WillRepeatedly(Return(QuicTime::Zero()));
1397 // Handle an ack which causes the loss algorithm to be evaluated and
1398 // set the loss timeout.
1400 EXPECT_CALL(*loss_algorithm
, DetectLostPackets(_
, _
, _
, _
))
1401 .WillOnce(Return(SequenceNumberSet()));
1402 QuicAckFrame ack_frame
;
1403 ack_frame
.largest_observed
= 2;
1404 ack_frame
.missing_packets
.insert(1);
1405 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1407 QuicTime
timeout(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)));
1408 EXPECT_CALL(*loss_algorithm
, GetLossTimeout())
1409 .WillRepeatedly(Return(timeout
));
1410 EXPECT_EQ(timeout
, manager_
.GetRetransmissionTime());
1412 // Fire the retransmission timeout and ensure the loss detection algorithm
1414 EXPECT_CALL(*loss_algorithm
, DetectLostPackets(_
, _
, _
, _
))
1415 .WillOnce(Return(SequenceNumberSet()));
1416 manager_
.OnRetransmissionTimeout();
1419 TEST_F(QuicSentPacketManagerTest
, NegotiateTimeLossDetectionFromOptions
) {
1421 QuicSentPacketManagerPeer::GetLossAlgorithm(
1422 &manager_
)->GetLossDetectionType());
1425 QuicTagVector options
;
1426 options
.push_back(kTIME
);
1427 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1428 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1429 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1430 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1431 manager_
.SetFromConfig(config
);
1434 QuicSentPacketManagerPeer::GetLossAlgorithm(
1435 &manager_
)->GetLossDetectionType());
1438 TEST_F(QuicSentPacketManagerTest
, NegotiateCongestionControlFromOptions
) {
1439 ValueRestore
<bool> old_flag(&FLAGS_quic_allow_bbr
, true);
1441 QuicTagVector options
;
1443 options
.push_back(kRENO
);
1444 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1445 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1446 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1447 manager_
.SetFromConfig(config
);
1448 EXPECT_EQ(kReno
, QuicSentPacketManagerPeer::GetSendAlgorithm(
1449 manager_
)->GetCongestionControlType());
1451 // TODO(rtenneti): Enable the following code after BBR code is checked in.
1454 options
.push_back(kTBBR
);
1455 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1456 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1457 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1458 manager_
.SetFromConfig(config
);
1459 EXPECT_EQ(kBBR
, QuicSentPacketManagerPeer::GetSendAlgorithm(
1460 manager_
)->GetCongestionControlType());
1464 options
.push_back(kBYTE
);
1465 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1466 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1467 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1468 manager_
.SetFromConfig(config
);
1469 EXPECT_EQ(kCubicBytes
, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_
)
1470 ->GetCongestionControlType());
1473 options
.push_back(kRENO
);
1474 options
.push_back(kBYTE
);
1475 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1476 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1477 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1478 manager_
.SetFromConfig(config
);
1479 EXPECT_EQ(kRenoBytes
, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_
)
1480 ->GetCongestionControlType());
1483 TEST_F(QuicSentPacketManagerTest
, NegotiateNumConnectionsFromOptions
) {
1485 QuicTagVector options
;
1487 options
.push_back(k1CON
);
1488 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1489 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1490 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1491 EXPECT_CALL(*send_algorithm_
, SetNumEmulatedConnections(1));
1492 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1493 manager_
.SetFromConfig(config
);
1495 QuicSentPacketManagerPeer::SetPerspective(&manager_
, Perspective::IS_CLIENT
);
1496 QuicConfig client_config
;
1497 client_config
.SetConnectionOptionsToSend(options
);
1498 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1499 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1500 EXPECT_CALL(*send_algorithm_
, SetNumEmulatedConnections(1));
1501 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1502 manager_
.SetFromConfig(client_config
);
1505 TEST_F(QuicSentPacketManagerTest
, NegotiateNConnectionFromOptions
) {
1506 // By default, changing the number of open streams does nothing.
1507 manager_
.SetNumOpenStreams(5);
1510 QuicTagVector options
;
1512 options
.push_back(kNCON
);
1513 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1514 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1515 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1516 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1517 manager_
.SetFromConfig(config
);
1519 EXPECT_CALL(*send_algorithm_
, SetNumEmulatedConnections(5));
1520 manager_
.SetNumOpenStreams(5);
1523 TEST_F(QuicSentPacketManagerTest
, NegotiateNoTLPFromOptionsAtServer
) {
1525 QuicTagVector options
;
1527 options
.push_back(kNTLP
);
1528 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1529 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1530 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1531 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1532 manager_
.SetFromConfig(config
);
1533 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_
));
1536 TEST_F(QuicSentPacketManagerTest
, NegotiateNoTLPFromOptionsAtClient
) {
1537 QuicConfig client_config
;
1538 QuicTagVector options
;
1540 options
.push_back(kNTLP
);
1541 QuicSentPacketManagerPeer::SetPerspective(&manager_
, Perspective::IS_CLIENT
);
1542 client_config
.SetConnectionOptionsToSend(options
);
1543 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1544 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1545 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1546 manager_
.SetFromConfig(client_config
);
1547 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_
));
1550 TEST_F(QuicSentPacketManagerTest
, NegotiateNewRTOFromOptionsAtServer
) {
1551 EXPECT_FALSE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1553 QuicTagVector options
;
1555 options
.push_back(kNRTO
);
1556 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1557 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1558 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1559 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1560 manager_
.SetFromConfig(config
);
1561 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1564 TEST_F(QuicSentPacketManagerTest
, NegotiateNewRTOFromOptionsAtClient
) {
1565 EXPECT_FALSE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1566 QuicConfig client_config
;
1567 QuicTagVector options
;
1569 options
.push_back(kNRTO
);
1570 QuicSentPacketManagerPeer::SetPerspective(&manager_
, Perspective::IS_CLIENT
);
1571 client_config
.SetConnectionOptionsToSend(options
);
1572 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1573 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1574 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1575 manager_
.SetFromConfig(client_config
);
1576 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1579 TEST_F(QuicSentPacketManagerTest
, NegotiateReceiveWindowFromOptions
) {
1580 EXPECT_EQ(kDefaultSocketReceiveBuffer
,
1581 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_
));
1583 // Try to set a size below the minimum and ensure it gets set to the min.
1584 QuicConfig client_config
;
1585 QuicConfigPeer::SetReceivedSocketReceiveBuffer(&client_config
, 1024);
1586 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1587 if (FLAGS_quic_limit_max_cwnd_to_receive_buffer
) {
1588 EXPECT_CALL(*send_algorithm_
,
1589 SetMaxCongestionWindow(kMinSocketReceiveBuffer
* 0.95));
1591 EXPECT_CALL(*send_algorithm_
, PacingRate())
1592 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1593 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1594 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1595 manager_
.SetFromConfig(client_config
);
1597 EXPECT_EQ(kMinSocketReceiveBuffer
,
1598 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_
));
1600 // Ensure the smaller send window only allows 16 packets to be sent.
1601 for (QuicPacketSequenceNumber i
= 1; i
<= 16; ++i
) {
1602 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1603 QuicTime::Delta::Zero()));
1604 EXPECT_EQ(QuicTime::Delta::Zero(),
1605 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1606 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, BytesInFlight(), i
,
1607 1024, HAS_RETRANSMITTABLE_DATA
))
1608 .WillOnce(Return(true));
1609 SerializedPacket
packet(CreatePacket(i
, true));
1610 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(), 1024,
1611 NOT_RETRANSMISSION
, HAS_RETRANSMITTABLE_DATA
);
1613 if (FLAGS_quic_limit_max_cwnd_to_receive_buffer
) {
1614 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
))
1615 .WillOnce(Return(QuicTime::Delta::Infinite()));
1617 EXPECT_EQ(QuicTime::Delta::Infinite(),
1618 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1621 TEST_F(QuicSentPacketManagerTest
, ReceiveWindowLimited
) {
1622 EXPECT_EQ(kDefaultSocketReceiveBuffer
,
1623 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_
));
1625 // Ensure the smaller send window only allows 256 * 0.95 packets to be sent.
1626 for (QuicPacketSequenceNumber i
= 1; i
<= 244; ++i
) {
1627 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1628 QuicTime::Delta::Zero()));
1629 EXPECT_EQ(QuicTime::Delta::Zero(),
1630 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1631 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, BytesInFlight(), i
,
1632 1024, HAS_RETRANSMITTABLE_DATA
))
1633 .WillOnce(Return(true));
1634 SerializedPacket
packet(CreatePacket(i
, true));
1635 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(), 1024,
1636 NOT_RETRANSMISSION
, HAS_RETRANSMITTABLE_DATA
);
1638 if (FLAGS_quic_limit_max_cwnd_to_receive_buffer
) {
1639 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
))
1640 .WillOnce(Return(QuicTime::Delta::Infinite()));
1642 EXPECT_EQ(QuicTime::Delta::Infinite(),
1643 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1646 TEST_F(QuicSentPacketManagerTest
, UseInitialRoundTripTimeToSend
) {
1647 uint32 initial_rtt_us
= 325000;
1648 EXPECT_NE(initial_rtt_us
,
1649 manager_
.GetRttStats()->smoothed_rtt().ToMicroseconds());
1652 config
.SetInitialRoundTripTimeUsToSend(initial_rtt_us
);
1653 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1654 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1655 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1656 manager_
.SetFromConfig(config
);
1658 EXPECT_EQ(0, manager_
.GetRttStats()->smoothed_rtt().ToMicroseconds());
1659 EXPECT_EQ(initial_rtt_us
, manager_
.GetRttStats()->initial_rtt_us());
1662 TEST_F(QuicSentPacketManagerTest
, ResumeConnectionState
) {
1663 // The sent packet manager should use the RTT from CachedNetworkParameters if
1665 const int kRttMs
= 1234;
1666 CachedNetworkParameters cached_network_params
;
1667 cached_network_params
.set_min_rtt_ms(kRttMs
);
1669 EXPECT_CALL(*send_algorithm_
, ResumeConnectionState(_
, false));
1670 manager_
.ResumeConnectionState(cached_network_params
, false);
1671 EXPECT_EQ(kRttMs
* kNumMicrosPerMilli
,
1672 static_cast<uint64
>(manager_
.GetRttStats()->initial_rtt_us()));