Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / net / quic / quic_sent_packet_manager_test.cc
blobb057a1dc07982558c402da87069cc9fce0e31279
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"
15 using std::vector;
16 using testing::AnyNumber;
17 using testing::ElementsAre;
18 using testing::IsEmpty;
19 using testing::Not;
20 using testing::Pair;
21 using testing::Pointwise;
22 using testing::Return;
23 using testing::StrictMock;
24 using testing::_;
26 namespace net {
27 namespace test {
28 namespace {
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.
38 MATCHER(KeyEq, "") {
39 return std::tr1::get<0>(arg).first == std::tr1::get<1>(arg);
42 class MockDebugDelegate : public QuicSentPacketManager::DebugDelegate {
43 public:
44 MOCK_METHOD2(OnSpuriousPacketRetransmission,
45 void(TransmissionType transmission_type,
46 QuicByteCount byte_size));
49 class QuicSentPacketManagerTest : public ::testing::TestWithParam<bool> {
50 protected:
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())
63 .Times(AnyNumber());
64 EXPECT_CALL(*send_algorithm_, BandwidthEstimate())
65 .Times(AnyNumber())
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(QuicPacketNumber* packets, size_t num_packets) {
77 if (num_packets == 0) {
78 EXPECT_FALSE(manager_.HasUnackedPackets());
79 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
80 &manager_));
81 return;
84 EXPECT_TRUE(manager_.HasUnackedPackets());
85 EXPECT_EQ(packets[0], manager_.GetLeastUnacked());
86 for (size_t i = 0; i < num_packets; ++i) {
87 EXPECT_TRUE(manager_.IsUnacked(packets[i])) << packets[i];
91 void VerifyRetransmittablePackets(QuicPacketNumber* packets,
92 size_t num_packets) {
93 EXPECT_EQ(num_packets,
94 QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
95 &manager_));
96 for (size_t i = 0; i < num_packets; ++i) {
97 EXPECT_TRUE(manager_.HasRetransmittableFrames(packets[i]))
98 << " packets[" << i << "]:" << packets[i];
102 void ExpectAck(QuicPacketNumber largest_observed) {
103 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(
104 true, _, ElementsAre(Pair(largest_observed, _)), IsEmpty()));
105 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
106 EXPECT_CALL(*network_change_visitor_, OnRttChange());
109 void ExpectUpdatedRtt(QuicPacketNumber largest_observed) {
110 EXPECT_CALL(*send_algorithm_,
111 OnCongestionEvent(true, _, IsEmpty(), IsEmpty()));
112 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
113 EXPECT_CALL(*network_change_visitor_, OnRttChange());
116 void ExpectAckAndLoss(bool rtt_updated,
117 QuicPacketNumber largest_observed,
118 QuicPacketNumber lost_packet) {
119 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(
120 rtt_updated, _, ElementsAre(Pair(largest_observed, _)),
121 ElementsAre(Pair(lost_packet, _))));
122 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
123 EXPECT_CALL(*network_change_visitor_, OnRttChange());
126 // |packets_acked| and |packets_lost| should be in packet number order.
127 void ExpectAcksAndLosses(bool rtt_updated,
128 QuicPacketNumber* packets_acked,
129 size_t num_packets_acked,
130 QuicPacketNumber* packets_lost,
131 size_t num_packets_lost) {
132 vector<QuicPacketNumber> ack_vector;
133 for (size_t i = 0; i < num_packets_acked; ++i) {
134 ack_vector.push_back(packets_acked[i]);
136 vector<QuicPacketNumber> lost_vector;
137 for (size_t i = 0; i < num_packets_lost; ++i) {
138 lost_vector.push_back(packets_lost[i]);
140 EXPECT_CALL(*send_algorithm_,
141 OnCongestionEvent(rtt_updated, _,
142 Pointwise(KeyEq(), ack_vector),
143 Pointwise(KeyEq(), lost_vector)));
144 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange()).
145 Times(AnyNumber());
146 EXPECT_CALL(*network_change_visitor_, OnRttChange()).Times(AnyNumber());
149 void RetransmitAndSendPacket(QuicPacketNumber old_packet_number,
150 QuicPacketNumber new_packet_number) {
151 QuicSentPacketManagerPeer::MarkForRetransmission(
152 &manager_, old_packet_number, TLP_RETRANSMISSION);
153 EXPECT_TRUE(manager_.HasPendingRetransmissions());
154 QuicSentPacketManager::PendingRetransmission next_retransmission =
155 manager_.NextPendingRetransmission();
156 EXPECT_EQ(old_packet_number, next_retransmission.packet_number);
157 EXPECT_EQ(TLP_RETRANSMISSION,
158 next_retransmission.transmission_type);
160 EXPECT_CALL(*send_algorithm_,
161 OnPacketSent(_, BytesInFlight(), new_packet_number,
162 kDefaultLength, HAS_RETRANSMITTABLE_DATA))
163 .WillOnce(Return(true));
164 SerializedPacket packet(CreatePacket(new_packet_number, false));
165 manager_.OnPacketSent(&packet, old_packet_number, clock_.Now(),
166 kDefaultLength, TLP_RETRANSMISSION,
167 HAS_RETRANSMITTABLE_DATA);
168 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(&manager_,
169 new_packet_number));
172 SerializedPacket CreateDataPacket(QuicPacketNumber packet_number) {
173 return CreatePacket(packet_number, true);
176 SerializedPacket CreatePacket(QuicPacketNumber packet_number,
177 bool retransmittable) {
178 packets_.push_back(new QuicEncryptedPacket(nullptr, kDefaultLength));
179 RetransmittableFrames* frames = nullptr;
180 if (retransmittable) {
181 frames = new RetransmittableFrames(ENCRYPTION_NONE);
182 frames->AddFrame(
183 QuicFrame(new QuicStreamFrame(kStreamId, false, 0, StringPiece())));
185 return SerializedPacket(packet_number, PACKET_6BYTE_PACKET_NUMBER,
186 packets_.back(), 0u, frames, false, false);
189 SerializedPacket CreateFecPacket(QuicPacketNumber packet_number) {
190 packets_.push_back(new QuicEncryptedPacket(nullptr, kDefaultLength));
191 SerializedPacket serialized(packet_number, PACKET_6BYTE_PACKET_NUMBER,
192 packets_.back(), 0u, nullptr, false, false);
193 serialized.is_fec_packet = true;
194 return serialized;
197 void SendDataPacket(QuicPacketNumber packet_number) {
198 EXPECT_CALL(*send_algorithm_,
199 OnPacketSent(_, BytesInFlight(), packet_number, _, _))
200 .Times(1)
201 .WillOnce(Return(true));
202 SerializedPacket packet(CreateDataPacket(packet_number));
203 manager_.OnPacketSent(&packet, 0, clock_.Now(),
204 packet.packet->length(), NOT_RETRANSMISSION,
205 HAS_RETRANSMITTABLE_DATA);
208 void SendCryptoPacket(QuicPacketNumber packet_number) {
209 EXPECT_CALL(*send_algorithm_,
210 OnPacketSent(_, BytesInFlight(), packet_number, kDefaultLength,
211 HAS_RETRANSMITTABLE_DATA))
212 .Times(1)
213 .WillOnce(Return(true));
214 SerializedPacket packet(CreateDataPacket(packet_number));
215 packet.retransmittable_frames->AddFrame(
216 QuicFrame(new QuicStreamFrame(1, false, 0, StringPiece())));
217 manager_.OnPacketSent(&packet, 0, clock_.Now(),
218 packet.packet->length(), NOT_RETRANSMISSION,
219 HAS_RETRANSMITTABLE_DATA);
222 void SendFecPacket(QuicPacketNumber packet_number) {
223 EXPECT_CALL(*send_algorithm_,
224 OnPacketSent(_, BytesInFlight(), packet_number, kDefaultLength,
225 HAS_RETRANSMITTABLE_DATA))
226 .Times(1)
227 .WillOnce(Return(true));
228 SerializedPacket packet(CreateFecPacket(packet_number));
229 manager_.OnPacketSent(&packet, 0, clock_.Now(),
230 packet.packet->length(), NOT_RETRANSMISSION,
231 NO_RETRANSMITTABLE_DATA);
234 void SendAckPacket(QuicPacketNumber packet_number) {
235 EXPECT_CALL(*send_algorithm_,
236 OnPacketSent(_, BytesInFlight(), packet_number, kDefaultLength,
237 NO_RETRANSMITTABLE_DATA))
238 .Times(1)
239 .WillOnce(Return(false));
240 SerializedPacket packet(CreatePacket(packet_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(QuicPacketNumber retransmission_packet_number) {
248 EXPECT_TRUE(manager_.HasPendingRetransmissions());
249 EXPECT_CALL(*send_algorithm_,
250 OnPacketSent(_, _, retransmission_packet_number, kDefaultLength,
251 HAS_RETRANSMITTABLE_DATA))
252 .Times(1)
253 .WillOnce(Return(true));
254 const QuicSentPacketManager::PendingRetransmission pending =
255 manager_.NextPendingRetransmission();
256 SerializedPacket packet(CreatePacket(retransmission_packet_number, false));
257 manager_.OnPacketSent(&packet, pending.packet_number, clock_.Now(),
258 kDefaultLength, pending.transmission_type,
259 HAS_RETRANSMITTABLE_DATA);
262 QuicSentPacketManager manager_;
263 vector<QuicEncryptedPacket*> packets_;
264 MockClock clock_;
265 QuicConnectionStats stats_;
266 MockSendAlgorithm* send_algorithm_;
267 scoped_ptr<MockNetworkChangeVisitor> network_change_visitor_;
270 TEST_F(QuicSentPacketManagerTest, IsUnacked) {
271 VerifyUnackedPackets(nullptr, 0);
272 SendDataPacket(1);
274 QuicPacketNumber unacked[] = {1};
275 VerifyUnackedPackets(unacked, arraysize(unacked));
276 QuicPacketNumber retransmittable[] = {1};
277 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
280 TEST_F(QuicSentPacketManagerTest, IsUnAckedRetransmit) {
281 SendDataPacket(1);
282 RetransmitAndSendPacket(1, 2);
284 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(&manager_, 2));
285 QuicPacketNumber unacked[] = {1, 2};
286 VerifyUnackedPackets(unacked, arraysize(unacked));
287 QuicPacketNumber retransmittable[] = {2};
288 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
291 TEST_F(QuicSentPacketManagerTest, RetransmitThenAck) {
292 SendDataPacket(1);
293 RetransmitAndSendPacket(1, 2);
295 // Ack 2 but not 1.
296 QuicAckFrame ack_frame;
297 ack_frame.largest_observed = 2;
298 ack_frame.missing_packets.Add(1);
299 ExpectAck(2);
300 manager_.OnIncomingAck(ack_frame, clock_.Now());
302 // Packet 1 is unacked, pending, but not retransmittable.
303 QuicPacketNumber unacked[] = {1};
304 VerifyUnackedPackets(unacked, arraysize(unacked));
305 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
306 VerifyRetransmittablePackets(nullptr, 0);
309 TEST_F(QuicSentPacketManagerTest, RetransmitThenAckBeforeSend) {
310 SendDataPacket(1);
311 QuicSentPacketManagerPeer::MarkForRetransmission(
312 &manager_, 1, TLP_RETRANSMISSION);
313 EXPECT_TRUE(manager_.HasPendingRetransmissions());
315 // Ack 1.
316 QuicAckFrame ack_frame;
317 ack_frame.largest_observed = 1;
318 ExpectAck(1);
319 manager_.OnIncomingAck(ack_frame, clock_.Now());
321 // There should no longer be a pending retransmission.
322 EXPECT_FALSE(manager_.HasPendingRetransmissions());
324 // No unacked packets remain.
325 VerifyUnackedPackets(nullptr, 0);
326 VerifyRetransmittablePackets(nullptr, 0);
327 EXPECT_EQ(0u, stats_.packets_spuriously_retransmitted);
330 TEST_F(QuicSentPacketManagerTest, RetransmitThenStopRetransmittingBeforeSend) {
331 SendDataPacket(1);
332 QuicSentPacketManagerPeer::MarkForRetransmission(&manager_, 1,
333 TLP_RETRANSMISSION);
334 EXPECT_TRUE(manager_.HasPendingRetransmissions());
336 manager_.CancelRetransmissionsForStream(kStreamId);
338 // There should no longer be a pending retransmission.
339 EXPECT_FALSE(manager_.HasPendingRetransmissions());
341 QuicPacketNumber unacked[] = {1};
342 VerifyUnackedPackets(unacked, arraysize(unacked));
343 VerifyRetransmittablePackets(nullptr, 0);
344 EXPECT_EQ(0u, stats_.packets_spuriously_retransmitted);
347 TEST_F(QuicSentPacketManagerTest, RetransmitThenAckPrevious) {
348 SendDataPacket(1);
349 RetransmitAndSendPacket(1, 2);
350 QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
351 clock_.AdvanceTime(rtt);
353 // Ack 1 but not 2.
354 ExpectAck(1);
355 QuicAckFrame ack_frame;
356 ack_frame.largest_observed = 1;
357 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
359 // 2 remains unacked, but no packets have retransmittable data.
360 QuicPacketNumber unacked[] = {2};
361 VerifyUnackedPackets(unacked, arraysize(unacked));
362 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
363 VerifyRetransmittablePackets(nullptr, 0);
365 EXPECT_EQ(1u, stats_.packets_spuriously_retransmitted);
368 TEST_F(QuicSentPacketManagerTest, RetransmitThenAckPreviousThenNackRetransmit) {
369 SendDataPacket(1);
370 RetransmitAndSendPacket(1, 2);
371 QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
372 clock_.AdvanceTime(rtt);
374 // First, ACK packet 1 which makes packet 2 non-retransmittable.
375 ExpectAck(1);
376 QuicAckFrame ack_frame;
377 ack_frame.largest_observed = 1;
378 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
380 SendDataPacket(3);
381 SendDataPacket(4);
382 SendDataPacket(5);
383 clock_.AdvanceTime(rtt);
385 // Next, NACK packet 2 three times.
386 ack_frame.largest_observed = 3;
387 ack_frame.missing_packets.Add(2);
388 ExpectAck(3);
389 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
391 ack_frame.largest_observed = 4;
392 ExpectAck(4);
393 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
395 ack_frame.largest_observed = 5;
396 ExpectAckAndLoss(true, 5, 2);
397 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
399 // No packets remain unacked.
400 VerifyUnackedPackets(nullptr, 0);
401 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
402 VerifyRetransmittablePackets(nullptr, 0);
404 // Verify that the retransmission alarm would not fire,
405 // since there is no retransmittable data outstanding.
406 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
409 TEST_F(QuicSentPacketManagerTest,
410 DISABLED_RetransmitTwiceThenAckPreviousBeforeSend) {
411 SendDataPacket(1);
412 RetransmitAndSendPacket(1, 2);
414 // Fire the RTO, which will mark 2 for retransmission (but will not send it).
415 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
416 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
417 manager_.OnRetransmissionTimeout();
418 EXPECT_TRUE(manager_.HasPendingRetransmissions());
420 // Ack 1 but not 2, before 2 is able to be sent.
421 // Since 1 has been retransmitted, it has already been lost, and so the
422 // send algorithm is not informed that it has been ACK'd.
423 QuicAckFrame ack_frame;
424 ack_frame.largest_observed = 1;
425 ExpectUpdatedRtt(1);
426 EXPECT_CALL(*send_algorithm_, RevertRetransmissionTimeout());
427 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
429 // Since 2 was marked for retransmit, when 1 is acked, 2 is kept for RTT.
430 QuicPacketNumber unacked[] = {2};
431 VerifyUnackedPackets(unacked, arraysize(unacked));
432 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
433 VerifyRetransmittablePackets(nullptr, 0);
435 // Verify that the retransmission alarm would not fire,
436 // since there is no retransmittable data outstanding.
437 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
440 TEST_F(QuicSentPacketManagerTest, RetransmitTwiceThenAckFirst) {
441 StrictMock<MockDebugDelegate> debug_delegate;
442 EXPECT_CALL(debug_delegate, OnSpuriousPacketRetransmission(
443 TLP_RETRANSMISSION, kDefaultLength)).Times(2);
444 manager_.set_debug_delegate(&debug_delegate);
446 SendDataPacket(1);
447 RetransmitAndSendPacket(1, 2);
448 RetransmitAndSendPacket(2, 3);
449 QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
450 clock_.AdvanceTime(rtt);
452 // Ack 1 but not 2 or 3.
453 ExpectAck(1);
454 QuicAckFrame ack_frame;
455 ack_frame.largest_observed = 1;
456 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
458 // 2 and 3 remain unacked, but no packets have retransmittable data.
459 QuicPacketNumber unacked[] = {2, 3};
460 VerifyUnackedPackets(unacked, arraysize(unacked));
461 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
462 VerifyRetransmittablePackets(nullptr, 0);
464 // Ensure packet 2 is lost when 4 is sent and 3 and 4 are acked.
465 SendDataPacket(4);
466 ack_frame.largest_observed = 4;
467 ack_frame.missing_packets.Add(2);
468 QuicPacketNumber acked[] = {3, 4};
469 ExpectAcksAndLosses(true, acked, arraysize(acked), nullptr, 0);
470 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
472 QuicPacketNumber unacked2[] = {2};
473 VerifyUnackedPackets(unacked2, arraysize(unacked2));
474 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
476 SendDataPacket(5);
477 ack_frame.largest_observed = 5;
478 ExpectAckAndLoss(true, 5, 2);
479 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
481 VerifyUnackedPackets(nullptr, 0);
482 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
483 EXPECT_EQ(2u, stats_.packets_spuriously_retransmitted);
486 TEST_F(QuicSentPacketManagerTest, LoseButDontRetransmitRevivedPacket) {
487 SendDataPacket(1);
488 SendDataPacket(2);
489 SendFecPacket(3);
490 SendDataPacket(4);
492 // Ack 2 and 3, and mark 1 as revived.
493 QuicAckFrame ack_frame;
494 ack_frame.largest_observed = 3;
495 ack_frame.missing_packets.Add(1);
496 ack_frame.revived_packets.insert(1);
497 QuicPacketNumber acked[] = {2, 3};
498 ExpectAcksAndLosses(true, acked, arraysize(acked), nullptr, 0);
499 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
501 EXPECT_FALSE(manager_.HasPendingRetransmissions());
502 QuicPacketNumber unacked[] = {1, 4};
503 VerifyUnackedPackets(unacked, arraysize(unacked));
504 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
505 QuicPacketNumber retransmittable[] = {4};
506 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
508 // Ack the 4th packet and expect the 1st to be considered lost.
509 ack_frame.largest_observed = 4;
510 ExpectAckAndLoss(true, 4, 1);
511 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
513 EXPECT_FALSE(manager_.HasPendingRetransmissions());
514 VerifyRetransmittablePackets(nullptr, 0);
517 TEST_F(QuicSentPacketManagerTest, MarkLostThenReviveAndDontRetransmitPacket) {
518 SendDataPacket(1);
519 SendDataPacket(2);
520 SendDataPacket(3);
521 SendDataPacket(4);
522 SendFecPacket(5);
524 // Ack 2, 3, and 4, and expect the 1st to be considered lost.
525 QuicAckFrame ack_frame;
526 ack_frame.largest_observed = 4;
527 ack_frame.missing_packets.Add(1);
528 QuicPacketNumber acked[] = {2, 3, 4};
529 QuicPacketNumber lost[] = {1};
530 ExpectAcksAndLosses(true, acked, arraysize(acked), lost, arraysize(lost));
531 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
533 EXPECT_TRUE(manager_.HasPendingRetransmissions());
534 QuicPacketNumber unacked[] = {1, 5};
535 VerifyUnackedPackets(unacked, arraysize(unacked));
536 QuicPacketNumber retransmittable[] = {1};
537 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
539 // Ack 5th packet (FEC) and revive 1st packet. 1st packet should now be
540 // removed from pending retransmissions map.
541 ack_frame.largest_observed = 5;
542 ack_frame.revived_packets.insert(1);
543 ExpectAck(5);
544 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
546 EXPECT_FALSE(manager_.HasPendingRetransmissions());
547 VerifyRetransmittablePackets(nullptr, 0);
550 TEST_F(QuicSentPacketManagerTest, TruncatedAck) {
551 ValueRestore<bool> old_flag(&FLAGS_quic_disable_truncated_ack_handling,
552 false);
553 SendDataPacket(1);
554 RetransmitAndSendPacket(1, 2);
555 RetransmitAndSendPacket(2, 3);
556 RetransmitAndSendPacket(3, 4);
557 RetransmitAndSendPacket(4, 5);
559 // Truncated ack with 4 NACKs, so the first packet is lost.
560 QuicAckFrame ack_frame;
561 ack_frame.largest_observed = 4;
562 ack_frame.missing_packets.Add(1, 5);
563 ack_frame.is_truncated = true;
565 QuicPacketNumber lost[] = {1};
566 ExpectAcksAndLosses(true, nullptr, 0, lost, arraysize(lost));
567 manager_.OnIncomingAck(ack_frame, clock_.Now());
569 // High water mark will be raised.
570 QuicPacketNumber unacked[] = {2, 3, 4, 5};
571 VerifyUnackedPackets(unacked, arraysize(unacked));
572 QuicPacketNumber retransmittable[] = {5};
573 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
576 TEST_F(QuicSentPacketManagerTest, AckPreviousTransmissionThenTruncatedAck) {
577 SendDataPacket(1);
578 RetransmitAndSendPacket(1, 2);
579 RetransmitAndSendPacket(2, 3);
580 RetransmitAndSendPacket(3, 4);
581 SendDataPacket(5);
582 SendDataPacket(6);
583 SendDataPacket(7);
584 SendDataPacket(8);
585 SendDataPacket(9);
587 // Ack previous transmission
589 QuicAckFrame ack_frame;
590 ack_frame.largest_observed = 2;
591 ack_frame.missing_packets.Add(1);
592 ExpectAck(2);
593 manager_.OnIncomingAck(ack_frame, clock_.Now());
594 EXPECT_TRUE(manager_.IsUnacked(4));
597 // Truncated ack with 4 NACKs
599 QuicAckFrame ack_frame;
600 ack_frame.largest_observed = 6;
601 ack_frame.missing_packets.Add(3, 7);
602 ack_frame.is_truncated = true;
603 ExpectAckAndLoss(true, 1, 3);
604 manager_.OnIncomingAck(ack_frame, clock_.Now());
607 // High water mark will be raised.
608 QuicPacketNumber unacked[] = {4, 5, 6, 7, 8, 9};
609 VerifyUnackedPackets(unacked, arraysize(unacked));
610 QuicPacketNumber retransmittable[] = {5, 6, 7, 8, 9};
611 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
614 TEST_F(QuicSentPacketManagerTest, GetLeastUnacked) {
615 EXPECT_EQ(1u, manager_.GetLeastUnacked());
618 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedUnacked) {
619 SendDataPacket(1);
620 EXPECT_EQ(1u, manager_.GetLeastUnacked());
623 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedUnackedFec) {
624 SendFecPacket(1);
625 EXPECT_EQ(1u, manager_.GetLeastUnacked());
628 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedAndDiscard) {
629 VerifyUnackedPackets(nullptr, 0);
631 SendFecPacket(1);
632 EXPECT_EQ(1u, manager_.GetLeastUnacked());
634 SendFecPacket(2);
635 EXPECT_EQ(1u, manager_.GetLeastUnacked());
637 SendFecPacket(3);
638 EXPECT_EQ(1u, manager_.GetLeastUnacked());
640 QuicPacketNumber unacked[] = {1, 2, 3};
641 VerifyUnackedPackets(unacked, arraysize(unacked));
642 VerifyRetransmittablePackets(nullptr, 0);
644 // Ack 2, so there's an rtt update.
645 ExpectAck(2);
646 QuicAckFrame ack_frame;
647 ack_frame.largest_observed = 2;
648 ack_frame.missing_packets.Add(1);
649 manager_.OnIncomingAck(ack_frame, clock_.Now());
651 EXPECT_EQ(1u, manager_.GetLeastUnacked());
654 TEST_F(QuicSentPacketManagerTest, GetSentTime) {
655 VerifyUnackedPackets(nullptr, 0);
657 QuicTime sent_time = clock_.Now();
658 SendFecPacket(1);
659 QuicTime sent_time2 = clock_.Now();
660 SendFecPacket(2);
661 QuicPacketNumber unacked[] = {1, 2};
662 VerifyUnackedPackets(unacked, arraysize(unacked));
663 VerifyRetransmittablePackets(nullptr, 0);
665 EXPECT_TRUE(manager_.HasUnackedPackets());
666 EXPECT_EQ(sent_time, QuicSentPacketManagerPeer::GetSentTime(&manager_, 1));
667 EXPECT_EQ(sent_time2, QuicSentPacketManagerPeer::GetSentTime(&manager_, 2));
670 TEST_F(QuicSentPacketManagerTest, AckAckAndUpdateRtt) {
671 SendDataPacket(1);
672 SendAckPacket(2);
674 // Now ack the ack and expect an RTT update.
675 QuicAckFrame ack_frame;
676 ack_frame.largest_observed = 2;
677 ack_frame.delta_time_largest_observed =
678 QuicTime::Delta::FromMilliseconds(5);
680 ExpectAck(1);
681 manager_.OnIncomingAck(ack_frame, clock_.Now());
683 SendAckPacket(3);
685 // Now ack the ack and expect only an RTT update.
686 ack_frame.largest_observed = 3;
687 ExpectUpdatedRtt(3);
688 manager_.OnIncomingAck(ack_frame, clock_.Now());
691 TEST_F(QuicSentPacketManagerTest, Rtt) {
692 QuicPacketNumber packet_number = 1;
693 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(15);
694 SendDataPacket(packet_number);
695 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
697 ExpectAck(packet_number);
698 QuicAckFrame ack_frame;
699 ack_frame.largest_observed = packet_number;
700 ack_frame.delta_time_largest_observed =
701 QuicTime::Delta::FromMilliseconds(5);
702 manager_.OnIncomingAck(ack_frame, clock_.Now());
703 EXPECT_EQ(expected_rtt,
704 QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
707 TEST_F(QuicSentPacketManagerTest, RttWithInvalidDelta) {
708 // Expect that the RTT is equal to the local time elapsed, since the
709 // delta_time_largest_observed is larger than the local time elapsed
710 // and is hence invalid.
711 QuicPacketNumber packet_number = 1;
712 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
713 SendDataPacket(packet_number);
714 clock_.AdvanceTime(expected_rtt);
716 ExpectAck(packet_number);
717 QuicAckFrame ack_frame;
718 ack_frame.largest_observed = packet_number;
719 ack_frame.delta_time_largest_observed =
720 QuicTime::Delta::FromMilliseconds(11);
721 manager_.OnIncomingAck(ack_frame, clock_.Now());
722 EXPECT_EQ(expected_rtt,
723 QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
726 TEST_F(QuicSentPacketManagerTest, RttWithInfiniteDelta) {
727 // Expect that the RTT is equal to the local time elapsed, since the
728 // delta_time_largest_observed is infinite, and is hence invalid.
729 QuicPacketNumber packet_number = 1;
730 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
731 SendDataPacket(packet_number);
732 clock_.AdvanceTime(expected_rtt);
734 ExpectAck(packet_number);
735 QuicAckFrame ack_frame;
736 ack_frame.largest_observed = packet_number;
737 ack_frame.delta_time_largest_observed = QuicTime::Delta::Infinite();
738 manager_.OnIncomingAck(ack_frame, clock_.Now());
739 EXPECT_EQ(expected_rtt,
740 QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
743 TEST_F(QuicSentPacketManagerTest, RttZeroDelta) {
744 // Expect that the RTT is the time between send and receive since the
745 // delta_time_largest_observed is zero.
746 QuicPacketNumber packet_number = 1;
747 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
748 SendDataPacket(packet_number);
749 clock_.AdvanceTime(expected_rtt);
751 ExpectAck(packet_number);
752 QuicAckFrame ack_frame;
753 ack_frame.largest_observed = packet_number;
754 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
755 manager_.OnIncomingAck(ack_frame, clock_.Now());
756 EXPECT_EQ(expected_rtt,
757 QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
760 TEST_F(QuicSentPacketManagerTest, TailLossProbeTimeout) {
761 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
763 // Send 1 packet.
764 QuicPacketNumber packet_number = 1;
765 SendDataPacket(packet_number);
767 // The first tail loss probe retransmits 1 packet.
768 manager_.OnRetransmissionTimeout();
769 EXPECT_EQ(QuicTime::Delta::Zero(),
770 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
771 EXPECT_FALSE(manager_.HasPendingRetransmissions());
772 manager_.MaybeRetransmitTailLossProbe();
773 EXPECT_TRUE(manager_.HasPendingRetransmissions());
774 RetransmitNextPacket(2);
775 EXPECT_FALSE(manager_.HasPendingRetransmissions());
777 // The second tail loss probe retransmits 1 packet.
778 manager_.OnRetransmissionTimeout();
779 EXPECT_EQ(QuicTime::Delta::Zero(),
780 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
781 EXPECT_FALSE(manager_.HasPendingRetransmissions());
782 manager_.MaybeRetransmitTailLossProbe();
783 EXPECT_TRUE(manager_.HasPendingRetransmissions());
784 RetransmitNextPacket(3);
785 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
786 QuicTime::Delta::Infinite()));
787 EXPECT_EQ(QuicTime::Delta::Infinite(),
788 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
789 EXPECT_FALSE(manager_.HasPendingRetransmissions());
791 // Ack the third and ensure the first two are still pending.
792 ExpectAck(3);
793 QuicAckFrame ack_frame;
794 ack_frame.largest_observed = 3;
795 ack_frame.missing_packets.Add(1, 3);
796 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
798 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
800 // Acking two more packets will lose both of them due to nacks.
801 ack_frame.largest_observed = 5;
802 QuicPacketNumber lost[] = {1, 2};
803 ExpectAcksAndLosses(false, nullptr, 0, lost, arraysize(lost));
804 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
806 EXPECT_FALSE(manager_.HasPendingRetransmissions());
807 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
808 EXPECT_EQ(2u, stats_.tlp_count);
809 EXPECT_EQ(0u, stats_.rto_count);
812 TEST_F(QuicSentPacketManagerTest, TailLossProbeThenRTO) {
813 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
815 // Send 100 packets.
816 const size_t kNumSentPackets = 100;
817 for (size_t i = 1; i <= kNumSentPackets; ++i) {
818 SendDataPacket(i);
820 QuicTime rto_packet_time = clock_.Now();
821 // Advance the time.
822 clock_.AdvanceTime(manager_.GetRetransmissionTime().Subtract(clock_.Now()));
824 // The first tail loss probe retransmits 1 packet.
825 manager_.OnRetransmissionTimeout();
826 EXPECT_EQ(QuicTime::Delta::Zero(),
827 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
828 EXPECT_FALSE(manager_.HasPendingRetransmissions());
829 manager_.MaybeRetransmitTailLossProbe();
830 EXPECT_TRUE(manager_.HasPendingRetransmissions());
831 RetransmitNextPacket(101);
832 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
833 QuicTime::Delta::Infinite()));
834 EXPECT_EQ(QuicTime::Delta::Infinite(),
835 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
836 EXPECT_FALSE(manager_.HasPendingRetransmissions());
837 clock_.AdvanceTime(manager_.GetRetransmissionTime().Subtract(clock_.Now()));
839 // The second tail loss probe retransmits 1 packet.
840 manager_.OnRetransmissionTimeout();
841 EXPECT_EQ(QuicTime::Delta::Zero(),
842 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
843 EXPECT_FALSE(manager_.HasPendingRetransmissions());
844 EXPECT_TRUE(manager_.MaybeRetransmitTailLossProbe());
845 EXPECT_TRUE(manager_.HasPendingRetransmissions());
846 RetransmitNextPacket(102);
847 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
848 QuicTime::Delta::Infinite()));
849 EXPECT_EQ(QuicTime::Delta::Infinite(),
850 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
852 // Ensure the RTO is set based on the correct packet.
853 rto_packet_time = clock_.Now();
854 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
855 .WillOnce(Return(QuicTime::Delta::FromSeconds(1)));
856 EXPECT_EQ(rto_packet_time.Add(QuicTime::Delta::FromSeconds(1)),
857 manager_.GetRetransmissionTime());
859 // Advance the time enough to ensure all packets are RTO'd.
860 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
862 manager_.OnRetransmissionTimeout();
863 EXPECT_TRUE(manager_.HasPendingRetransmissions());
864 EXPECT_EQ(2u, stats_.tlp_count);
865 EXPECT_EQ(1u, stats_.rto_count);
867 // Send and Ack the RTO and ensure OnRetransmissionTimeout is called.
868 EXPECT_EQ(102 * kDefaultLength,
869 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
871 RetransmitNextPacket(103);
872 QuicAckFrame ack_frame;
873 ack_frame.largest_observed = 103;
874 ack_frame.missing_packets.Add(0, 103);
875 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
876 EXPECT_CALL(*send_algorithm_,
877 OnCongestionEvent(true, _, ElementsAre(Pair(103, _)), _));
878 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
879 EXPECT_CALL(*network_change_visitor_, OnRttChange());
880 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
881 // All packets before 103 should be lost.
882 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
885 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeout) {
886 // Send 2 crypto packets and 3 data packets.
887 const size_t kNumSentCryptoPackets = 2;
888 for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
889 SendCryptoPacket(i);
891 const size_t kNumSentDataPackets = 3;
892 for (size_t i = 1; i <= kNumSentDataPackets; ++i) {
893 SendDataPacket(kNumSentCryptoPackets + i);
895 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
897 // The first retransmits 2 packets.
898 manager_.OnRetransmissionTimeout();
899 EXPECT_EQ(QuicTime::Delta::Zero(),
900 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
901 RetransmitNextPacket(6);
902 RetransmitNextPacket(7);
903 EXPECT_FALSE(manager_.HasPendingRetransmissions());
904 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
906 // The second retransmits 2 packets.
907 manager_.OnRetransmissionTimeout();
908 EXPECT_EQ(QuicTime::Delta::Zero(),
909 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
910 RetransmitNextPacket(8);
911 RetransmitNextPacket(9);
912 EXPECT_FALSE(manager_.HasPendingRetransmissions());
913 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
915 // Now ack the two crypto packets and the speculatively encrypted request,
916 // and ensure the first four crypto packets get abandoned, but not lost.
917 QuicPacketNumber acked[] = {3, 4, 5, 8, 9};
918 ExpectAcksAndLosses(true, acked, arraysize(acked), nullptr, 0);
919 QuicAckFrame ack_frame;
920 ack_frame.largest_observed = 9;
921 ack_frame.missing_packets.Add(1, 3);
922 ack_frame.missing_packets.Add(6, 8);
923 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
925 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
928 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeoutVersionNegotiation) {
929 // Send 2 crypto packets and 3 data packets.
930 const size_t kNumSentCryptoPackets = 2;
931 for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
932 SendCryptoPacket(i);
934 const size_t kNumSentDataPackets = 3;
935 for (size_t i = 1; i <= kNumSentDataPackets; ++i) {
936 SendDataPacket(kNumSentCryptoPackets + i);
938 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
940 // The first retransmission timeout retransmits 2 crypto packets.
941 manager_.OnRetransmissionTimeout();
942 RetransmitNextPacket(6);
943 RetransmitNextPacket(7);
944 EXPECT_FALSE(manager_.HasPendingRetransmissions());
945 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
947 // Now act like a version negotiation packet arrived, which would cause all
948 // unacked packets to be retransmitted.
949 manager_.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION);
951 // Ensure the first two pending packets are the crypto retransmits.
952 ASSERT_TRUE(manager_.HasPendingRetransmissions());
953 EXPECT_EQ(6u, manager_.NextPendingRetransmission().packet_number);
954 RetransmitNextPacket(8);
955 EXPECT_EQ(7u, manager_.NextPendingRetransmission().packet_number);
956 RetransmitNextPacket(9);
958 EXPECT_TRUE(manager_.HasPendingRetransmissions());
959 // Send 3 more data packets and ensure the least unacked is raised.
960 RetransmitNextPacket(10);
961 RetransmitNextPacket(11);
962 RetransmitNextPacket(12);
963 EXPECT_FALSE(manager_.HasPendingRetransmissions());
965 EXPECT_EQ(8u, manager_.GetLeastUnacked());
968 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeSpuriousRetransmission) {
969 // Send 1 crypto packet.
970 SendCryptoPacket(1);
971 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
973 // Retransmit the crypto packet as 2.
974 manager_.OnRetransmissionTimeout();
975 RetransmitNextPacket(2);
977 // Retransmit the crypto packet as 3.
978 manager_.OnRetransmissionTimeout();
979 RetransmitNextPacket(3);
981 // Now ack the second crypto packet, and ensure the first gets removed, but
982 // the third does not.
983 ExpectUpdatedRtt(2);
984 QuicAckFrame ack_frame;
985 ack_frame.largest_observed = 2;
986 ack_frame.missing_packets.Add(1);
987 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
989 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
990 QuicPacketNumber unacked[] = {3};
991 VerifyUnackedPackets(unacked, arraysize(unacked));
994 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeoutUnsentDataPacket) {
995 // Send 2 crypto packets and 1 data packet.
996 const size_t kNumSentCryptoPackets = 2;
997 for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
998 SendCryptoPacket(i);
1000 SendDataPacket(3);
1001 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1003 // Retransmit 2 crypto packets, but not the serialized packet.
1004 manager_.OnRetransmissionTimeout();
1005 RetransmitNextPacket(4);
1006 RetransmitNextPacket(5);
1007 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1008 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1011 TEST_F(QuicSentPacketManagerTest,
1012 CryptoHandshakeRetransmissionThenRetransmitAll) {
1013 // Send 1 crypto packet.
1014 SendCryptoPacket(1);
1015 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1017 // Retransmit the crypto packet as 2.
1018 manager_.OnRetransmissionTimeout();
1019 RetransmitNextPacket(2);
1021 // Now retransmit all the unacked packets, which occurs when there is a
1022 // version negotiation.
1023 manager_.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION);
1024 QuicPacketNumber unacked[] = {1, 2};
1025 VerifyUnackedPackets(unacked, arraysize(unacked));
1026 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1027 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1028 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
1031 TEST_F(QuicSentPacketManagerTest,
1032 CryptoHandshakeRetransmissionThenNeuterAndAck) {
1033 // Send 1 crypto packet.
1034 SendCryptoPacket(1);
1035 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1037 // Retransmit the crypto packet as 2.
1038 manager_.OnRetransmissionTimeout();
1039 RetransmitNextPacket(2);
1041 // Retransmit the crypto packet as 3.
1042 manager_.OnRetransmissionTimeout();
1043 RetransmitNextPacket(3);
1045 // Now neuter all unacked unencrypted packets, which occurs when the
1046 // connection goes forward secure.
1047 manager_.NeuterUnencryptedPackets();
1048 QuicPacketNumber unacked[] = {1, 2, 3};
1049 VerifyUnackedPackets(unacked, arraysize(unacked));
1050 VerifyRetransmittablePackets(nullptr, 0);
1051 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1052 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1053 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
1055 // Ensure both packets get discarded when packet 2 is acked.
1056 QuicAckFrame ack_frame;
1057 ack_frame.largest_observed = 3;
1058 ack_frame.missing_packets.Add(1, 3);
1059 ExpectUpdatedRtt(3);
1060 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
1061 VerifyUnackedPackets(nullptr, 0);
1062 VerifyRetransmittablePackets(nullptr, 0);
1065 TEST_F(QuicSentPacketManagerTest, RetransmissionTimeout) {
1066 // Send 100 packets.
1067 const size_t kNumSentPackets = 100;
1068 for (size_t i = 1; i <= kNumSentPackets; ++i) {
1069 SendDataPacket(i);
1072 EXPECT_FALSE(manager_.MaybeRetransmitTailLossProbe());
1073 manager_.OnRetransmissionTimeout();
1074 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1075 EXPECT_EQ(100 * kDefaultLength,
1076 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1077 RetransmitNextPacket(101);
1078 RetransmitNextPacket(102);
1079 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1081 // Ack a retransmission.
1082 QuicAckFrame ack_frame;
1083 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
1084 ack_frame.largest_observed = 102;
1085 ack_frame.missing_packets.Add(0, 102);
1086 // Ensure no packets are lost.
1087 EXPECT_CALL(*send_algorithm_,
1088 OnCongestionEvent(true, _, ElementsAre(Pair(102, _)),
1089 /*lost_packets=*/IsEmpty()));
1090 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1091 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1092 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1093 manager_.OnIncomingAck(ack_frame, clock_.Now());
1096 TEST_F(QuicSentPacketManagerTest, NewRetransmissionTimeout) {
1097 QuicConfig client_config;
1098 QuicTagVector options;
1099 options.push_back(kNRTO);
1100 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1101 client_config.SetConnectionOptionsToSend(options);
1102 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1103 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1104 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1105 EXPECT_CALL(*send_algorithm_, PacingRate())
1106 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1107 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1108 .WillOnce(Return(10 * kDefaultTCPMSS));
1109 manager_.SetFromConfig(client_config);
1110 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
1112 // Send 100 packets.
1113 const size_t kNumSentPackets = 100;
1114 for (size_t i = 1; i <= kNumSentPackets; ++i) {
1115 SendDataPacket(i);
1118 EXPECT_FALSE(manager_.MaybeRetransmitTailLossProbe());
1119 manager_.OnRetransmissionTimeout();
1120 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1121 EXPECT_EQ(100 * kDefaultLength,
1122 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1123 RetransmitNextPacket(101);
1124 RetransmitNextPacket(102);
1125 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1127 // Ack a retransmission and expect no call to OnRetransmissionTimeout.
1128 QuicAckFrame ack_frame;
1129 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
1130 ack_frame.largest_observed = 102;
1131 ack_frame.missing_packets.Add(0, 102);
1132 // This will include packets in the lost packet map.
1133 EXPECT_CALL(*send_algorithm_,
1134 OnCongestionEvent(true, _, ElementsAre(Pair(102, _)),
1135 /*lost_packets=*/Not(IsEmpty())));
1136 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1137 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1138 manager_.OnIncomingAck(ack_frame, clock_.Now());
1141 TEST_F(QuicSentPacketManagerTest, TwoRetransmissionTimeoutsAckSecond) {
1142 // Send 1 packet.
1143 SendDataPacket(1);
1145 manager_.OnRetransmissionTimeout();
1146 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1147 EXPECT_EQ(kDefaultLength,
1148 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1149 RetransmitNextPacket(2);
1150 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1152 // Rto a second time.
1153 manager_.OnRetransmissionTimeout();
1154 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1155 EXPECT_EQ(2 * kDefaultLength,
1156 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1157 RetransmitNextPacket(3);
1158 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1160 // Ack a retransmission and ensure OnRetransmissionTimeout is called.
1161 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1162 QuicAckFrame ack_frame;
1163 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
1164 ack_frame.largest_observed = 2;
1165 ack_frame.missing_packets.Add(1);
1166 ExpectAck(2);
1167 manager_.OnIncomingAck(ack_frame, clock_.Now());
1169 // The original packet and newest should be outstanding.
1170 EXPECT_EQ(2 * kDefaultLength,
1171 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1174 TEST_F(QuicSentPacketManagerTest, TwoRetransmissionTimeoutsAckFirst) {
1175 // Send 1 packet.
1176 SendDataPacket(1);
1178 manager_.OnRetransmissionTimeout();
1179 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1180 EXPECT_EQ(kDefaultLength,
1181 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1182 RetransmitNextPacket(2);
1183 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1185 // Rto a second time.
1186 manager_.OnRetransmissionTimeout();
1187 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1188 EXPECT_EQ(2 * kDefaultLength,
1189 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1190 RetransmitNextPacket(3);
1191 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1193 // Ack a retransmission and ensure OnRetransmissionTimeout is called.
1194 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1195 QuicAckFrame ack_frame;
1196 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
1197 ack_frame.largest_observed = 3;
1198 ack_frame.missing_packets.Add(1, 3);
1199 ExpectAck(3);
1200 manager_.OnIncomingAck(ack_frame, clock_.Now());
1202 // The first two packets should still be outstanding.
1203 EXPECT_EQ(2 * kDefaultLength,
1204 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1207 TEST_F(QuicSentPacketManagerTest, GetTransmissionTime) {
1208 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
1211 TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeCryptoHandshake) {
1212 SendCryptoPacket(1);
1214 // Check the min.
1215 RttStats* rtt_stats = QuicSentPacketManagerPeer::GetRttStats(&manager_);
1216 rtt_stats->set_initial_rtt_us(1 * kNumMicrosPerMilli);
1217 EXPECT_EQ(clock_.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1218 manager_.GetRetransmissionTime());
1220 // Test with a standard smoothed RTT.
1221 rtt_stats->set_initial_rtt_us(100 * kNumMicrosPerMilli);
1223 QuicTime::Delta srtt =
1224 QuicTime::Delta::FromMicroseconds(rtt_stats->initial_rtt_us());
1225 QuicTime expected_time = clock_.Now().Add(srtt.Multiply(1.5));
1226 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1228 // Retransmit the packet by invoking the retransmission timeout.
1229 clock_.AdvanceTime(srtt.Multiply(1.5));
1230 manager_.OnRetransmissionTimeout();
1231 RetransmitNextPacket(2);
1233 // The retransmission time should now be twice as far in the future.
1234 expected_time = clock_.Now().Add(srtt.Multiply(2).Multiply(1.5));
1235 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1238 TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeTailLossProbe) {
1239 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
1240 SendDataPacket(1);
1241 SendDataPacket(2);
1243 // Check the min.
1244 RttStats* rtt_stats = QuicSentPacketManagerPeer::GetRttStats(&manager_);
1245 rtt_stats->set_initial_rtt_us(1 * kNumMicrosPerMilli);
1246 EXPECT_EQ(clock_.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1247 manager_.GetRetransmissionTime());
1249 // Test with a standard smoothed RTT.
1250 rtt_stats->set_initial_rtt_us(100 * kNumMicrosPerMilli);
1251 QuicTime::Delta srtt =
1252 QuicTime::Delta::FromMicroseconds(rtt_stats->initial_rtt_us());
1253 QuicTime::Delta expected_tlp_delay = srtt.Multiply(2);
1254 QuicTime expected_time = clock_.Now().Add(expected_tlp_delay);
1255 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1257 // Retransmit the packet by invoking the retransmission timeout.
1258 clock_.AdvanceTime(expected_tlp_delay);
1259 manager_.OnRetransmissionTimeout();
1260 EXPECT_EQ(QuicTime::Delta::Zero(),
1261 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1262 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1263 EXPECT_TRUE(manager_.MaybeRetransmitTailLossProbe());
1264 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1265 RetransmitNextPacket(3);
1266 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
1267 QuicTime::Delta::Infinite()));
1268 EXPECT_EQ(QuicTime::Delta::Infinite(),
1269 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1270 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1272 expected_time = clock_.Now().Add(expected_tlp_delay);
1273 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1276 TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeSpuriousRTO) {
1277 QuicSentPacketManagerPeer::GetRttStats(&manager_)->UpdateRtt(
1278 QuicTime::Delta::FromMilliseconds(100),
1279 QuicTime::Delta::Zero(),
1280 QuicTime::Zero());
1282 SendDataPacket(1);
1283 SendDataPacket(2);
1284 SendDataPacket(3);
1285 SendDataPacket(4);
1287 QuicTime::Delta expected_rto_delay = QuicTime::Delta::FromMilliseconds(500);
1288 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
1289 .WillRepeatedly(Return(expected_rto_delay));
1290 QuicTime expected_time = clock_.Now().Add(expected_rto_delay);
1291 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1293 // Retransmit the packet by invoking the retransmission timeout.
1294 clock_.AdvanceTime(expected_rto_delay);
1295 manager_.OnRetransmissionTimeout();
1296 // All packets are still considered inflight.
1297 EXPECT_EQ(4 * kDefaultLength,
1298 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1299 RetransmitNextPacket(5);
1300 RetransmitNextPacket(6);
1301 // All previous packets are inflight, plus two rto retransmissions.
1302 EXPECT_EQ(6 * kDefaultLength,
1303 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1304 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1306 // The delay should double the second time.
1307 expected_time = clock_.Now().Add(expected_rto_delay).Add(expected_rto_delay);
1308 // Once we always base the timer on the right edge, leaving the older packets
1309 // in flight doesn't change the timeout.
1310 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1312 // Ack a packet before the first RTO and ensure the RTO timeout returns to the
1313 // original value and OnRetransmissionTimeout is not called or reverted.
1314 QuicAckFrame ack_frame;
1315 ack_frame.largest_observed = 2;
1316 ack_frame.missing_packets.Add(1);
1317 ExpectAck(2);
1318 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
1319 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1320 EXPECT_EQ(5 * kDefaultLength,
1321 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1323 // Wait 2RTTs from now for the RTO, since it's the max of the RTO time
1324 // and the TLP time. In production, there would always be two TLP's first.
1325 // Since retransmission was spurious, smoothed_rtt_ is expired, and replaced
1326 // by the latest RTT sample of 500ms.
1327 expected_time = clock_.Now().Add(QuicTime::Delta::FromMilliseconds(1000));
1328 // Once we always base the timer on the right edge, leaving the older packets
1329 // in flight doesn't change the timeout.
1330 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1333 TEST_F(QuicSentPacketManagerTest, GetTransmissionDelayMin) {
1334 SendDataPacket(1);
1335 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
1336 .WillRepeatedly(Return(QuicTime::Delta::FromMilliseconds(1)));
1337 QuicTime::Delta delay = QuicTime::Delta::FromMilliseconds(200);
1339 // If the delay is smaller than the min, ensure it exponentially backs off
1340 // from the min.
1341 for (int i = 0; i < 5; ++i) {
1342 EXPECT_EQ(delay,
1343 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
1344 delay = delay.Add(delay);
1345 manager_.OnRetransmissionTimeout();
1346 RetransmitNextPacket(i + 2);
1350 TEST_F(QuicSentPacketManagerTest, GetTransmissionDelayMax) {
1351 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
1352 .WillOnce(Return(QuicTime::Delta::FromSeconds(500)));
1354 EXPECT_EQ(QuicTime::Delta::FromSeconds(60),
1355 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
1358 TEST_F(QuicSentPacketManagerTest, GetTransmissionDelay) {
1359 SendDataPacket(1);
1360 QuicTime::Delta delay = QuicTime::Delta::FromMilliseconds(500);
1361 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
1362 .WillRepeatedly(Return(delay));
1364 // Delay should back off exponentially.
1365 for (int i = 0; i < 5; ++i) {
1366 EXPECT_EQ(delay,
1367 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
1368 delay = delay.Add(delay);
1369 manager_.OnRetransmissionTimeout();
1370 RetransmitNextPacket(i + 2);
1374 TEST_F(QuicSentPacketManagerTest, GetLossDelay) {
1375 MockLossAlgorithm* loss_algorithm = new MockLossAlgorithm();
1376 QuicSentPacketManagerPeer::SetLossAlgorithm(&manager_, loss_algorithm);
1378 EXPECT_CALL(*loss_algorithm, GetLossTimeout())
1379 .WillRepeatedly(Return(QuicTime::Zero()));
1380 SendDataPacket(1);
1381 SendDataPacket(2);
1383 // Handle an ack which causes the loss algorithm to be evaluated and
1384 // set the loss timeout.
1385 ExpectAck(2);
1386 EXPECT_CALL(*loss_algorithm, DetectLostPackets(_, _, _, _))
1387 .WillOnce(Return(PacketNumberSet()));
1388 QuicAckFrame ack_frame;
1389 ack_frame.largest_observed = 2;
1390 ack_frame.missing_packets.Add(1);
1391 manager_.OnIncomingAck(ack_frame, clock_.Now());
1393 QuicTime timeout(clock_.Now().Add(QuicTime::Delta::FromMilliseconds(10)));
1394 EXPECT_CALL(*loss_algorithm, GetLossTimeout())
1395 .WillRepeatedly(Return(timeout));
1396 EXPECT_EQ(timeout, manager_.GetRetransmissionTime());
1398 // Fire the retransmission timeout and ensure the loss detection algorithm
1399 // is invoked.
1400 EXPECT_CALL(*loss_algorithm, DetectLostPackets(_, _, _, _))
1401 .WillOnce(Return(PacketNumberSet()));
1402 manager_.OnRetransmissionTimeout();
1405 TEST_F(QuicSentPacketManagerTest, NegotiateTimeLossDetectionFromOptions) {
1406 EXPECT_EQ(kNack,
1407 QuicSentPacketManagerPeer::GetLossAlgorithm(
1408 &manager_)->GetLossDetectionType());
1410 QuicConfig config;
1411 QuicTagVector options;
1412 options.push_back(kTIME);
1413 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1414 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1415 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1416 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1417 manager_.SetFromConfig(config);
1419 EXPECT_EQ(kTime,
1420 QuicSentPacketManagerPeer::GetLossAlgorithm(
1421 &manager_)->GetLossDetectionType());
1424 TEST_F(QuicSentPacketManagerTest, NegotiateCongestionControlFromOptions) {
1425 ValueRestore<bool> old_flag(&FLAGS_quic_allow_bbr, true);
1426 QuicConfig config;
1427 QuicTagVector options;
1429 options.push_back(kRENO);
1430 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1431 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1432 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1433 manager_.SetFromConfig(config);
1434 EXPECT_EQ(kReno, QuicSentPacketManagerPeer::GetSendAlgorithm(
1435 manager_)->GetCongestionControlType());
1437 // TODO(rtenneti): Enable the following code after BBR code is checked in.
1438 #if 0
1439 options.clear();
1440 options.push_back(kTBBR);
1441 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1442 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1443 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1444 manager_.SetFromConfig(config);
1445 EXPECT_EQ(kBBR, QuicSentPacketManagerPeer::GetSendAlgorithm(
1446 manager_)->GetCongestionControlType());
1447 #endif
1449 options.clear();
1450 options.push_back(kBYTE);
1451 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1452 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1453 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1454 manager_.SetFromConfig(config);
1455 EXPECT_EQ(kCubicBytes, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_)
1456 ->GetCongestionControlType());
1458 options.clear();
1459 options.push_back(kRENO);
1460 options.push_back(kBYTE);
1461 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1462 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1463 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1464 manager_.SetFromConfig(config);
1465 EXPECT_EQ(kRenoBytes, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_)
1466 ->GetCongestionControlType());
1469 TEST_F(QuicSentPacketManagerTest, NegotiateNumConnectionsFromOptions) {
1470 QuicConfig config;
1471 QuicTagVector options;
1473 options.push_back(k1CON);
1474 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1475 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1476 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1477 EXPECT_CALL(*send_algorithm_, SetNumEmulatedConnections(1));
1478 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1479 manager_.SetFromConfig(config);
1481 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1482 QuicConfig client_config;
1483 client_config.SetConnectionOptionsToSend(options);
1484 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1485 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1486 EXPECT_CALL(*send_algorithm_, SetNumEmulatedConnections(1));
1487 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1488 manager_.SetFromConfig(client_config);
1491 TEST_F(QuicSentPacketManagerTest, NegotiateNConnectionFromOptions) {
1492 // By default, changing the number of open streams does nothing.
1493 manager_.SetNumOpenStreams(5);
1495 QuicConfig config;
1496 QuicTagVector options;
1498 options.push_back(kNCON);
1499 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1500 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1501 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1502 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1503 manager_.SetFromConfig(config);
1505 EXPECT_CALL(*send_algorithm_, SetNumEmulatedConnections(5));
1506 manager_.SetNumOpenStreams(5);
1509 TEST_F(QuicSentPacketManagerTest, NegotiateNoTLPFromOptionsAtServer) {
1510 QuicConfig config;
1511 QuicTagVector options;
1513 options.push_back(kNTLP);
1514 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1515 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1516 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1517 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1518 manager_.SetFromConfig(config);
1519 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_));
1522 TEST_F(QuicSentPacketManagerTest, NegotiateNoTLPFromOptionsAtClient) {
1523 QuicConfig client_config;
1524 QuicTagVector options;
1526 options.push_back(kNTLP);
1527 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1528 client_config.SetConnectionOptionsToSend(options);
1529 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1530 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1531 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1532 manager_.SetFromConfig(client_config);
1533 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_));
1536 TEST_F(QuicSentPacketManagerTest, NegotiateTLPRttFromOptionsAtServer) {
1537 QuicConfig config;
1538 QuicTagVector options;
1540 options.push_back(kTLPR);
1541 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1542 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1543 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1544 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1545 manager_.SetFromConfig(config);
1546 EXPECT_TRUE(
1547 QuicSentPacketManagerPeer::GetEnableHalfRttTailLossProbe(&manager_));
1550 TEST_F(QuicSentPacketManagerTest, NegotiateTLPRttFromOptionsAtClient) {
1551 QuicConfig client_config;
1552 QuicTagVector options;
1554 options.push_back(kTLPR);
1555 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1556 client_config.SetConnectionOptionsToSend(options);
1557 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1558 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1559 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1560 manager_.SetFromConfig(client_config);
1561 EXPECT_TRUE(
1562 QuicSentPacketManagerPeer::GetEnableHalfRttTailLossProbe(&manager_));
1565 TEST_F(QuicSentPacketManagerTest, NegotiateNewRTOFromOptionsAtServer) {
1566 EXPECT_FALSE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
1567 QuicConfig config;
1568 QuicTagVector options;
1570 options.push_back(kNRTO);
1571 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1572 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1573 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1574 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1575 manager_.SetFromConfig(config);
1576 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
1579 TEST_F(QuicSentPacketManagerTest, NegotiateNewRTOFromOptionsAtClient) {
1580 EXPECT_FALSE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
1581 QuicConfig client_config;
1582 QuicTagVector options;
1584 options.push_back(kNRTO);
1585 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1586 client_config.SetConnectionOptionsToSend(options);
1587 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1588 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1589 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1590 manager_.SetFromConfig(client_config);
1591 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
1594 TEST_F(QuicSentPacketManagerTest,
1595 NegotiateConservativeReceiveWindowFromOptions) {
1596 EXPECT_EQ(kDefaultSocketReceiveBuffer,
1597 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_));
1599 // Try to set a size below the minimum and ensure it gets set to the min.
1600 QuicConfig client_config;
1601 QuicConfigPeer::SetReceivedSocketReceiveBuffer(&client_config, 1024);
1602 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1603 EXPECT_CALL(*send_algorithm_,
1604 SetMaxCongestionWindow(kMinSocketReceiveBuffer * 0.6));
1605 EXPECT_CALL(*send_algorithm_, PacingRate())
1606 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1607 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1608 .WillOnce(Return(10 * kDefaultTCPMSS));
1609 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1610 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1611 manager_.SetFromConfig(client_config);
1613 EXPECT_EQ(kMinSocketReceiveBuffer,
1614 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_));
1616 // Ensure the smaller send window only allows 16 packets to be sent.
1617 for (QuicPacketNumber i = 1; i <= 16; ++i) {
1618 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _))
1619 .WillOnce(Return(QuicTime::Delta::Zero()));
1620 EXPECT_EQ(QuicTime::Delta::Zero(),
1621 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1622 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, BytesInFlight(), i, 1024,
1623 HAS_RETRANSMITTABLE_DATA))
1624 .WillOnce(Return(true));
1625 SerializedPacket packet(CreatePacket(i, true));
1626 manager_.OnPacketSent(&packet, 0, clock_.Now(), 1024, NOT_RETRANSMISSION,
1627 HAS_RETRANSMITTABLE_DATA);
1629 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _))
1630 .WillOnce(Return(QuicTime::Delta::Infinite()));
1631 EXPECT_EQ(QuicTime::Delta::Infinite(),
1632 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1635 TEST_F(QuicSentPacketManagerTest, ReceiveWindowLimited) {
1636 EXPECT_EQ(kDefaultSocketReceiveBuffer,
1637 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_));
1639 // Ensure the smaller send window only allows 256 * 0.95 packets to be sent.
1640 for (QuicPacketNumber i = 1; i <= 244; ++i) {
1641 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
1642 QuicTime::Delta::Zero()));
1643 EXPECT_EQ(QuicTime::Delta::Zero(),
1644 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1645 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, BytesInFlight(), i, 1024,
1646 HAS_RETRANSMITTABLE_DATA))
1647 .WillOnce(Return(true));
1648 SerializedPacket packet(CreatePacket(i, true));
1649 manager_.OnPacketSent(&packet, 0, clock_.Now(), 1024, NOT_RETRANSMISSION,
1650 HAS_RETRANSMITTABLE_DATA);
1652 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _))
1653 .WillOnce(Return(QuicTime::Delta::Infinite()));
1654 EXPECT_EQ(QuicTime::Delta::Infinite(),
1655 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1658 TEST_F(QuicSentPacketManagerTest, UseInitialRoundTripTimeToSend) {
1659 uint32 initial_rtt_us = 325000;
1660 EXPECT_NE(initial_rtt_us,
1661 manager_.GetRttStats()->smoothed_rtt().ToMicroseconds());
1663 QuicConfig config;
1664 config.SetInitialRoundTripTimeUsToSend(initial_rtt_us);
1665 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1666 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1667 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1668 manager_.SetFromConfig(config);
1670 EXPECT_EQ(0, manager_.GetRttStats()->smoothed_rtt().ToMicroseconds());
1671 EXPECT_EQ(initial_rtt_us, manager_.GetRttStats()->initial_rtt_us());
1674 TEST_F(QuicSentPacketManagerTest, ResumeConnectionState) {
1675 // The sent packet manager should use the RTT from CachedNetworkParameters if
1676 // it is provided.
1677 const int kRttMs = 1234;
1678 CachedNetworkParameters cached_network_params;
1679 cached_network_params.set_min_rtt_ms(kRttMs);
1681 EXPECT_CALL(*send_algorithm_, ResumeConnectionState(_, false));
1682 manager_.ResumeConnectionState(cached_network_params, false);
1683 EXPECT_EQ(kRttMs * kNumMicrosPerMilli,
1684 static_cast<uint64>(manager_.GetRttStats()->initial_rtt_us()));
1687 } // namespace
1688 } // namespace test
1689 } // namespace net