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
, ResetRecentMinRTTWithEmptyWindow
) {
1077 QuicTime::Delta min_rtt
= QuicTime::Delta::FromMilliseconds(50);
1078 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->UpdateRtt(
1079 min_rtt
, QuicTime::Delta::Zero(), QuicTime::Zero());
1081 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->min_rtt());
1083 QuicSentPacketManagerPeer::GetRttStats(
1084 &manager_
)->recent_min_rtt());
1086 // Send two packets with no prior bytes in flight.
1090 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
1091 // Ack two packets with 100ms RTT observations.
1092 QuicAckFrame ack_frame
;
1093 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1094 ack_frame
.largest_observed
= 1;
1096 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1098 // First ack does not change recent min rtt.
1100 QuicSentPacketManagerPeer::GetRttStats(
1101 &manager_
)->recent_min_rtt());
1103 ack_frame
.largest_observed
= 2;
1105 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1108 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->min_rtt());
1109 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(100),
1110 QuicSentPacketManagerPeer::GetRttStats(
1111 &manager_
)->recent_min_rtt());
1114 TEST_F(QuicSentPacketManagerTest
, RetransmissionTimeout
) {
1115 // Send 100 packets.
1116 const size_t kNumSentPackets
= 100;
1117 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
1121 EXPECT_FALSE(manager_
.MaybeRetransmitTailLossProbe());
1122 manager_
.OnRetransmissionTimeout();
1123 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1124 EXPECT_EQ(100 * kDefaultLength
,
1125 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1126 RetransmitNextPacket(101);
1127 RetransmitNextPacket(102);
1128 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1130 // Ack a retransmission.
1131 QuicAckFrame ack_frame
;
1132 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1133 ack_frame
.largest_observed
= 102;
1134 for (int i
= 0; i
< 102; ++i
) {
1135 ack_frame
.missing_packets
.insert(i
);
1137 // Ensure no packets are lost.
1138 EXPECT_CALL(*send_algorithm_
,
1139 OnCongestionEvent(true, _
, ElementsAre(Pair(102, _
)),
1140 /*lost_packets=*/IsEmpty()));
1141 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1142 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1143 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1144 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1147 TEST_F(QuicSentPacketManagerTest
, NewRetransmissionTimeout
) {
1148 QuicConfig client_config
;
1149 QuicTagVector options
;
1150 options
.push_back(kNRTO
);
1151 QuicSentPacketManagerPeer::SetPerspective(&manager_
, Perspective::IS_CLIENT
);
1152 client_config
.SetConnectionOptionsToSend(options
);
1153 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1154 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1155 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1156 EXPECT_CALL(*send_algorithm_
, PacingRate())
1157 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1158 manager_
.SetFromConfig(client_config
);
1159 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1161 // Send 100 packets.
1162 const size_t kNumSentPackets
= 100;
1163 for (size_t i
= 1; i
<= kNumSentPackets
; ++i
) {
1167 EXPECT_FALSE(manager_
.MaybeRetransmitTailLossProbe());
1168 manager_
.OnRetransmissionTimeout();
1169 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1170 EXPECT_EQ(100 * kDefaultLength
,
1171 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1172 RetransmitNextPacket(101);
1173 RetransmitNextPacket(102);
1174 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1176 // Ack a retransmission and expect no call to OnRetransmissionTimeout.
1177 QuicAckFrame ack_frame
;
1178 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1179 ack_frame
.largest_observed
= 102;
1180 for (int i
= 0; i
< 102; ++i
) {
1181 ack_frame
.missing_packets
.insert(i
);
1183 // This will include packets in the lost packet map.
1184 EXPECT_CALL(*send_algorithm_
,
1185 OnCongestionEvent(true, _
, ElementsAre(Pair(102, _
)),
1186 /*lost_packets=*/Not(IsEmpty())));
1187 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1188 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1189 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1192 TEST_F(QuicSentPacketManagerTest
, TwoRetransmissionTimeoutsAckSecond
) {
1196 manager_
.OnRetransmissionTimeout();
1197 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1198 EXPECT_EQ(kDefaultLength
,
1199 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1200 RetransmitNextPacket(2);
1201 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1203 // Rto a second time.
1204 manager_
.OnRetransmissionTimeout();
1205 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1206 EXPECT_EQ(2 * kDefaultLength
,
1207 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1208 RetransmitNextPacket(3);
1209 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1211 // Ack a retransmission and ensure OnRetransmissionTimeout is called.
1212 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1213 QuicAckFrame ack_frame
;
1214 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1215 ack_frame
.largest_observed
= 2;
1216 ack_frame
.missing_packets
.insert(1);
1218 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1220 // The original packet and newest should be outstanding.
1221 EXPECT_EQ(2 * kDefaultLength
,
1222 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1225 TEST_F(QuicSentPacketManagerTest
, TwoRetransmissionTimeoutsAckFirst
) {
1229 manager_
.OnRetransmissionTimeout();
1230 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1231 EXPECT_EQ(kDefaultLength
,
1232 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1233 RetransmitNextPacket(2);
1234 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1236 // Rto a second time.
1237 manager_
.OnRetransmissionTimeout();
1238 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1239 EXPECT_EQ(2 * kDefaultLength
,
1240 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1241 RetransmitNextPacket(3);
1242 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1244 // Ack a retransmission and ensure OnRetransmissionTimeout is called.
1245 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1246 QuicAckFrame ack_frame
;
1247 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
1248 ack_frame
.largest_observed
= 3;
1249 ack_frame
.missing_packets
.insert(1);
1250 ack_frame
.missing_packets
.insert(2);
1252 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1254 // The first two packets should still be outstanding.
1255 EXPECT_EQ(2 * kDefaultLength
,
1256 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1259 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTime
) {
1260 EXPECT_EQ(QuicTime::Zero(), manager_
.GetRetransmissionTime());
1263 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeCryptoHandshake
) {
1264 SendCryptoPacket(1);
1267 RttStats
* rtt_stats
= QuicSentPacketManagerPeer::GetRttStats(&manager_
);
1268 rtt_stats
->set_initial_rtt_us(1 * kNumMicrosPerMilli
);
1269 EXPECT_EQ(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1270 manager_
.GetRetransmissionTime());
1272 // Test with a standard smoothed RTT.
1273 rtt_stats
->set_initial_rtt_us(100 * kNumMicrosPerMilli
);
1275 QuicTime::Delta srtt
=
1276 QuicTime::Delta::FromMicroseconds(rtt_stats
->initial_rtt_us());
1277 QuicTime expected_time
= clock_
.Now().Add(srtt
.Multiply(1.5));
1278 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1280 // Retransmit the packet by invoking the retransmission timeout.
1281 clock_
.AdvanceTime(srtt
.Multiply(1.5));
1282 manager_
.OnRetransmissionTimeout();
1283 RetransmitNextPacket(2);
1285 // The retransmission time should now be twice as far in the future.
1286 expected_time
= clock_
.Now().Add(srtt
.Multiply(2).Multiply(1.5));
1287 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1290 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeTailLossProbe
) {
1291 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_
, 2);
1296 RttStats
* rtt_stats
= QuicSentPacketManagerPeer::GetRttStats(&manager_
);
1297 rtt_stats
->set_initial_rtt_us(1 * kNumMicrosPerMilli
);
1298 EXPECT_EQ(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1299 manager_
.GetRetransmissionTime());
1301 // Test with a standard smoothed RTT.
1302 rtt_stats
->set_initial_rtt_us(100 * kNumMicrosPerMilli
);
1303 QuicTime::Delta srtt
=
1304 QuicTime::Delta::FromMicroseconds(rtt_stats
->initial_rtt_us());
1305 QuicTime::Delta expected_tlp_delay
= srtt
.Multiply(2);
1306 QuicTime expected_time
= clock_
.Now().Add(expected_tlp_delay
);
1307 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1309 // Retransmit the packet by invoking the retransmission timeout.
1310 clock_
.AdvanceTime(expected_tlp_delay
);
1311 manager_
.OnRetransmissionTimeout();
1312 EXPECT_EQ(QuicTime::Delta::Zero(),
1313 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1314 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1315 EXPECT_TRUE(manager_
.MaybeRetransmitTailLossProbe());
1316 EXPECT_TRUE(manager_
.HasPendingRetransmissions());
1317 RetransmitNextPacket(3);
1318 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1319 QuicTime::Delta::Infinite()));
1320 EXPECT_EQ(QuicTime::Delta::Infinite(),
1321 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1322 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1324 expected_time
= clock_
.Now().Add(expected_tlp_delay
);
1325 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1328 TEST_F(QuicSentPacketManagerTest
, GetTransmissionTimeSpuriousRTO
) {
1329 QuicSentPacketManagerPeer::GetRttStats(&manager_
)->UpdateRtt(
1330 QuicTime::Delta::FromMilliseconds(100),
1331 QuicTime::Delta::Zero(),
1339 QuicTime::Delta expected_rto_delay
= QuicTime::Delta::FromMilliseconds(500);
1340 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1341 .WillRepeatedly(Return(expected_rto_delay
));
1342 QuicTime expected_time
= clock_
.Now().Add(expected_rto_delay
);
1343 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1345 // Retransmit the packet by invoking the retransmission timeout.
1346 clock_
.AdvanceTime(expected_rto_delay
);
1347 manager_
.OnRetransmissionTimeout();
1348 // All packets are still considered inflight.
1349 EXPECT_EQ(4 * kDefaultLength
,
1350 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1351 RetransmitNextPacket(5);
1352 RetransmitNextPacket(6);
1353 // All previous packets are inflight, plus two rto retransmissions.
1354 EXPECT_EQ(6 * kDefaultLength
,
1355 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1356 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1358 // The delay should double the second time.
1359 expected_time
= clock_
.Now().Add(expected_rto_delay
).Add(expected_rto_delay
);
1360 // Once we always base the timer on the right edge, leaving the older packets
1361 // in flight doesn't change the timeout.
1362 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1364 // Ack a packet before the first RTO and ensure the RTO timeout returns to the
1365 // original value and OnRetransmissionTimeout is not called or reverted.
1366 QuicAckFrame ack_frame
;
1367 ack_frame
.largest_observed
= 2;
1368 ack_frame
.missing_packets
.insert(1);
1370 manager_
.OnIncomingAck(ack_frame
, clock_
.ApproximateNow());
1371 EXPECT_FALSE(manager_
.HasPendingRetransmissions());
1372 EXPECT_EQ(5 * kDefaultLength
,
1373 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_
));
1375 // Wait 2RTTs from now for the RTO, since it's the max of the RTO time
1376 // and the TLP time. In production, there would always be two TLP's first.
1377 // Since retransmission was spurious, smoothed_rtt_ is expired, and replaced
1378 // by the latest RTT sample of 500ms.
1379 expected_time
= clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(1000));
1380 // Once we always base the timer on the right edge, leaving the older packets
1381 // in flight doesn't change the timeout.
1382 EXPECT_EQ(expected_time
, manager_
.GetRetransmissionTime());
1385 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelayMin
) {
1387 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1388 .WillRepeatedly(Return(QuicTime::Delta::FromMilliseconds(1)));
1389 QuicTime::Delta delay
= QuicTime::Delta::FromMilliseconds(200);
1391 // If the delay is smaller than the min, ensure it exponentially backs off
1393 for (int i
= 0; i
< 5; ++i
) {
1395 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1396 delay
= delay
.Add(delay
);
1397 manager_
.OnRetransmissionTimeout();
1398 RetransmitNextPacket(i
+ 2);
1402 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelayMax
) {
1403 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1404 .WillOnce(Return(QuicTime::Delta::FromSeconds(500)));
1406 EXPECT_EQ(QuicTime::Delta::FromSeconds(60),
1407 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1410 TEST_F(QuicSentPacketManagerTest
, GetTransmissionDelay
) {
1412 QuicTime::Delta delay
= QuicTime::Delta::FromMilliseconds(500);
1413 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay())
1414 .WillRepeatedly(Return(delay
));
1416 // Delay should back off exponentially.
1417 for (int i
= 0; i
< 5; ++i
) {
1419 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_
));
1420 delay
= delay
.Add(delay
);
1421 manager_
.OnRetransmissionTimeout();
1422 RetransmitNextPacket(i
+ 2);
1426 TEST_F(QuicSentPacketManagerTest
, GetLossDelay
) {
1427 MockLossAlgorithm
* loss_algorithm
= new MockLossAlgorithm();
1428 QuicSentPacketManagerPeer::SetLossAlgorithm(&manager_
, loss_algorithm
);
1430 EXPECT_CALL(*loss_algorithm
, GetLossTimeout())
1431 .WillRepeatedly(Return(QuicTime::Zero()));
1435 // Handle an ack which causes the loss algorithm to be evaluated and
1436 // set the loss timeout.
1438 EXPECT_CALL(*loss_algorithm
, DetectLostPackets(_
, _
, _
, _
))
1439 .WillOnce(Return(SequenceNumberSet()));
1440 QuicAckFrame ack_frame
;
1441 ack_frame
.largest_observed
= 2;
1442 ack_frame
.missing_packets
.insert(1);
1443 manager_
.OnIncomingAck(ack_frame
, clock_
.Now());
1445 QuicTime
timeout(clock_
.Now().Add(QuicTime::Delta::FromMilliseconds(10)));
1446 EXPECT_CALL(*loss_algorithm
, GetLossTimeout())
1447 .WillRepeatedly(Return(timeout
));
1448 EXPECT_EQ(timeout
, manager_
.GetRetransmissionTime());
1450 // Fire the retransmission timeout and ensure the loss detection algorithm
1452 EXPECT_CALL(*loss_algorithm
, DetectLostPackets(_
, _
, _
, _
))
1453 .WillOnce(Return(SequenceNumberSet()));
1454 manager_
.OnRetransmissionTimeout();
1457 TEST_F(QuicSentPacketManagerTest
, NegotiateTimeLossDetectionFromOptions
) {
1459 QuicSentPacketManagerPeer::GetLossAlgorithm(
1460 &manager_
)->GetLossDetectionType());
1463 QuicTagVector options
;
1464 options
.push_back(kTIME
);
1465 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1466 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1467 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1468 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1469 manager_
.SetFromConfig(config
);
1472 QuicSentPacketManagerPeer::GetLossAlgorithm(
1473 &manager_
)->GetLossDetectionType());
1476 TEST_F(QuicSentPacketManagerTest
, NegotiateCongestionControlFromOptions
) {
1477 ValueRestore
<bool> old_flag(&FLAGS_quic_allow_bbr
, true);
1479 QuicTagVector options
;
1481 options
.push_back(kRENO
);
1482 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1483 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1484 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1485 manager_
.SetFromConfig(config
);
1486 EXPECT_EQ(kReno
, QuicSentPacketManagerPeer::GetSendAlgorithm(
1487 manager_
)->GetCongestionControlType());
1489 // TODO(rtenneti): Enable the following code after BBR code is checked in.
1492 options
.push_back(kTBBR
);
1493 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1494 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1495 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1496 manager_
.SetFromConfig(config
);
1497 EXPECT_EQ(kBBR
, QuicSentPacketManagerPeer::GetSendAlgorithm(
1498 manager_
)->GetCongestionControlType());
1502 options
.push_back(kBYTE
);
1503 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1504 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1505 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1506 manager_
.SetFromConfig(config
);
1507 EXPECT_EQ(kCubicBytes
, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_
)
1508 ->GetCongestionControlType());
1511 options
.push_back(kRENO
);
1512 options
.push_back(kBYTE
);
1513 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1514 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1515 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1516 manager_
.SetFromConfig(config
);
1517 EXPECT_EQ(kRenoBytes
, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_
)
1518 ->GetCongestionControlType());
1521 TEST_F(QuicSentPacketManagerTest
, NegotiateNumConnectionsFromOptions
) {
1523 QuicTagVector options
;
1525 options
.push_back(k1CON
);
1526 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1527 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1528 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1529 EXPECT_CALL(*send_algorithm_
, SetNumEmulatedConnections(1));
1530 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1531 manager_
.SetFromConfig(config
);
1533 QuicSentPacketManagerPeer::SetPerspective(&manager_
, Perspective::IS_CLIENT
);
1534 QuicConfig client_config
;
1535 client_config
.SetConnectionOptionsToSend(options
);
1536 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1537 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1538 EXPECT_CALL(*send_algorithm_
, SetNumEmulatedConnections(1));
1539 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1540 manager_
.SetFromConfig(client_config
);
1543 TEST_F(QuicSentPacketManagerTest
, NegotiateNConnectionFromOptions
) {
1544 // By default, changing the number of open streams does nothing.
1545 manager_
.SetNumOpenStreams(5);
1548 QuicTagVector options
;
1550 options
.push_back(kNCON
);
1551 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1552 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1553 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1554 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1555 manager_
.SetFromConfig(config
);
1557 EXPECT_CALL(*send_algorithm_
, SetNumEmulatedConnections(5));
1558 manager_
.SetNumOpenStreams(5);
1561 TEST_F(QuicSentPacketManagerTest
, NegotiateNoTLPFromOptionsAtServer
) {
1563 QuicTagVector options
;
1565 options
.push_back(kNTLP
);
1566 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1567 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1568 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1569 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1570 manager_
.SetFromConfig(config
);
1571 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_
));
1574 TEST_F(QuicSentPacketManagerTest
, NegotiateNoTLPFromOptionsAtClient
) {
1575 QuicConfig client_config
;
1576 QuicTagVector options
;
1578 options
.push_back(kNTLP
);
1579 QuicSentPacketManagerPeer::SetPerspective(&manager_
, Perspective::IS_CLIENT
);
1580 client_config
.SetConnectionOptionsToSend(options
);
1581 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1582 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1583 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1584 manager_
.SetFromConfig(client_config
);
1585 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_
));
1588 TEST_F(QuicSentPacketManagerTest
, NegotiateNewRTOFromOptionsAtServer
) {
1589 EXPECT_FALSE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1591 QuicTagVector options
;
1593 options
.push_back(kNRTO
);
1594 QuicConfigPeer::SetReceivedConnectionOptions(&config
, options
);
1595 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1596 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1597 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1598 manager_
.SetFromConfig(config
);
1599 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1602 TEST_F(QuicSentPacketManagerTest
, NegotiateNewRTOFromOptionsAtClient
) {
1603 EXPECT_FALSE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1604 QuicConfig client_config
;
1605 QuicTagVector options
;
1607 options
.push_back(kNRTO
);
1608 QuicSentPacketManagerPeer::SetPerspective(&manager_
, Perspective::IS_CLIENT
);
1609 client_config
.SetConnectionOptionsToSend(options
);
1610 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1611 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1612 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1613 manager_
.SetFromConfig(client_config
);
1614 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_
));
1617 TEST_F(QuicSentPacketManagerTest
, NegotiateReceiveWindowFromOptions
) {
1618 EXPECT_EQ(kDefaultSocketReceiveBuffer
,
1619 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_
));
1621 // Try to set a size below the minimum and ensure it gets set to the min.
1622 QuicConfig client_config
;
1623 QuicConfigPeer::SetReceivedSocketReceiveBuffer(&client_config
, 1024);
1624 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1625 if (FLAGS_quic_limit_max_cwnd_to_receive_buffer
) {
1626 EXPECT_CALL(*send_algorithm_
,
1627 SetMaxCongestionWindow(kMinSocketReceiveBuffer
* 0.95));
1629 EXPECT_CALL(*send_algorithm_
, PacingRate())
1630 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1631 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1632 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1633 manager_
.SetFromConfig(client_config
);
1635 EXPECT_EQ(kMinSocketReceiveBuffer
,
1636 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_
));
1638 // Ensure the smaller send window only allows 16 packets to be sent.
1639 for (QuicPacketSequenceNumber i
= 1; i
<= 16; ++i
) {
1640 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1641 QuicTime::Delta::Zero()));
1642 EXPECT_EQ(QuicTime::Delta::Zero(),
1643 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1644 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, BytesInFlight(), i
,
1645 1024, HAS_RETRANSMITTABLE_DATA
))
1646 .WillOnce(Return(true));
1647 SerializedPacket
packet(CreatePacket(i
, true));
1648 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(), 1024,
1649 NOT_RETRANSMISSION
, HAS_RETRANSMITTABLE_DATA
);
1651 if (FLAGS_quic_limit_max_cwnd_to_receive_buffer
) {
1652 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
))
1653 .WillOnce(Return(QuicTime::Delta::Infinite()));
1655 EXPECT_EQ(QuicTime::Delta::Infinite(),
1656 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1659 TEST_F(QuicSentPacketManagerTest
, ReceiveWindowLimited
) {
1660 EXPECT_EQ(kDefaultSocketReceiveBuffer
,
1661 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_
));
1663 // Ensure the smaller send window only allows 256 * 0.95 packets to be sent.
1664 for (QuicPacketSequenceNumber i
= 1; i
<= 244; ++i
) {
1665 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillOnce(Return(
1666 QuicTime::Delta::Zero()));
1667 EXPECT_EQ(QuicTime::Delta::Zero(),
1668 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1669 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, BytesInFlight(), i
,
1670 1024, HAS_RETRANSMITTABLE_DATA
))
1671 .WillOnce(Return(true));
1672 SerializedPacket
packet(CreatePacket(i
, true));
1673 manager_
.OnPacketSent(&packet
, 0, clock_
.Now(), 1024,
1674 NOT_RETRANSMISSION
, HAS_RETRANSMITTABLE_DATA
);
1676 if (FLAGS_quic_limit_max_cwnd_to_receive_buffer
) {
1677 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
))
1678 .WillOnce(Return(QuicTime::Delta::Infinite()));
1680 EXPECT_EQ(QuicTime::Delta::Infinite(),
1681 manager_
.TimeUntilSend(clock_
.Now(), HAS_RETRANSMITTABLE_DATA
));
1684 TEST_F(QuicSentPacketManagerTest
, UseInitialRoundTripTimeToSend
) {
1685 uint32 initial_rtt_us
= 325000;
1686 EXPECT_NE(initial_rtt_us
,
1687 manager_
.GetRttStats()->smoothed_rtt().ToMicroseconds());
1690 config
.SetInitialRoundTripTimeUsToSend(initial_rtt_us
);
1691 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
1692 EXPECT_CALL(*network_change_visitor_
, OnCongestionWindowChange());
1693 EXPECT_CALL(*network_change_visitor_
, OnRttChange());
1694 manager_
.SetFromConfig(config
);
1696 EXPECT_EQ(0, manager_
.GetRttStats()->smoothed_rtt().ToMicroseconds());
1697 EXPECT_EQ(initial_rtt_us
, manager_
.GetRttStats()->initial_rtt_us());
1700 TEST_F(QuicSentPacketManagerTest
, ResumeConnectionState
) {
1701 // The sent packet manager should use the RTT from CachedNetworkParameters if
1703 const int kRttMs
= 1234;
1704 CachedNetworkParameters cached_network_params
;
1705 cached_network_params
.set_min_rtt_ms(kRttMs
);
1707 EXPECT_CALL(*send_algorithm_
, ResumeConnectionState(_
, false));
1708 manager_
.ResumeConnectionState(cached_network_params
, false);
1709 EXPECT_EQ(kRttMs
* kNumMicrosPerMilli
,
1710 static_cast<uint64
>(manager_
.GetRttStats()->initial_rtt_us()));