Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / quic_sent_packet_manager_test.cc
blob1a99cf1a80cffbba95f407822cc9cadb9cb57ff0
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.insert(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.insert(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.insert(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.insert(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.insert(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.insert(1);
563 ack_frame.missing_packets.insert(2);
564 ack_frame.missing_packets.insert(3);
565 ack_frame.missing_packets.insert(4);
566 ack_frame.is_truncated = true;
568 QuicPacketNumber lost[] = {1};
569 ExpectAcksAndLosses(true, nullptr, 0, lost, arraysize(lost));
570 manager_.OnIncomingAck(ack_frame, clock_.Now());
572 // High water mark will be raised.
573 QuicPacketNumber unacked[] = {2, 3, 4, 5};
574 VerifyUnackedPackets(unacked, arraysize(unacked));
575 QuicPacketNumber retransmittable[] = {5};
576 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
579 TEST_F(QuicSentPacketManagerTest, AckPreviousTransmissionThenTruncatedAck) {
580 SendDataPacket(1);
581 RetransmitAndSendPacket(1, 2);
582 RetransmitAndSendPacket(2, 3);
583 RetransmitAndSendPacket(3, 4);
584 SendDataPacket(5);
585 SendDataPacket(6);
586 SendDataPacket(7);
587 SendDataPacket(8);
588 SendDataPacket(9);
590 // Ack previous transmission
592 QuicAckFrame ack_frame;
593 ack_frame.largest_observed = 2;
594 ack_frame.missing_packets.insert(1);
595 ExpectAck(2);
596 manager_.OnIncomingAck(ack_frame, clock_.Now());
597 EXPECT_TRUE(manager_.IsUnacked(4));
600 // Truncated ack with 4 NACKs
602 QuicAckFrame ack_frame;
603 ack_frame.largest_observed = 6;
604 ack_frame.missing_packets.insert(3);
605 ack_frame.missing_packets.insert(4);
606 ack_frame.missing_packets.insert(5);
607 ack_frame.missing_packets.insert(6);
608 ack_frame.is_truncated = true;
609 ExpectAckAndLoss(true, 1, 3);
610 manager_.OnIncomingAck(ack_frame, clock_.Now());
613 // High water mark will be raised.
614 QuicPacketNumber unacked[] = {4, 5, 6, 7, 8, 9};
615 VerifyUnackedPackets(unacked, arraysize(unacked));
616 QuicPacketNumber retransmittable[] = {5, 6, 7, 8, 9};
617 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
620 TEST_F(QuicSentPacketManagerTest, GetLeastUnacked) {
621 EXPECT_EQ(1u, manager_.GetLeastUnacked());
624 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedUnacked) {
625 SendDataPacket(1);
626 EXPECT_EQ(1u, manager_.GetLeastUnacked());
629 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedUnackedFec) {
630 SendFecPacket(1);
631 EXPECT_EQ(1u, manager_.GetLeastUnacked());
634 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedAndDiscard) {
635 VerifyUnackedPackets(nullptr, 0);
637 SendFecPacket(1);
638 EXPECT_EQ(1u, manager_.GetLeastUnacked());
640 SendFecPacket(2);
641 EXPECT_EQ(1u, manager_.GetLeastUnacked());
643 SendFecPacket(3);
644 EXPECT_EQ(1u, manager_.GetLeastUnacked());
646 QuicPacketNumber unacked[] = {1, 2, 3};
647 VerifyUnackedPackets(unacked, arraysize(unacked));
648 VerifyRetransmittablePackets(nullptr, 0);
650 // Ack 2, so there's an rtt update.
651 ExpectAck(2);
652 QuicAckFrame ack_frame;
653 ack_frame.largest_observed = 2;
654 ack_frame.missing_packets.insert(1);
655 manager_.OnIncomingAck(ack_frame, clock_.Now());
657 EXPECT_EQ(1u, manager_.GetLeastUnacked());
660 TEST_F(QuicSentPacketManagerTest, GetSentTime) {
661 VerifyUnackedPackets(nullptr, 0);
663 QuicTime sent_time = clock_.Now();
664 SendFecPacket(1);
665 QuicTime sent_time2 = clock_.Now();
666 SendFecPacket(2);
667 QuicPacketNumber unacked[] = {1, 2};
668 VerifyUnackedPackets(unacked, arraysize(unacked));
669 VerifyRetransmittablePackets(nullptr, 0);
671 EXPECT_TRUE(manager_.HasUnackedPackets());
672 EXPECT_EQ(sent_time, QuicSentPacketManagerPeer::GetSentTime(&manager_, 1));
673 EXPECT_EQ(sent_time2, QuicSentPacketManagerPeer::GetSentTime(&manager_, 2));
676 TEST_F(QuicSentPacketManagerTest, AckAckAndUpdateRtt) {
677 SendDataPacket(1);
678 SendAckPacket(2);
680 // Now ack the ack and expect an RTT update.
681 QuicAckFrame ack_frame;
682 ack_frame.largest_observed = 2;
683 ack_frame.delta_time_largest_observed =
684 QuicTime::Delta::FromMilliseconds(5);
686 ExpectAck(1);
687 manager_.OnIncomingAck(ack_frame, clock_.Now());
689 SendAckPacket(3);
691 // Now ack the ack and expect only an RTT update.
692 ack_frame.largest_observed = 3;
693 ExpectUpdatedRtt(3);
694 manager_.OnIncomingAck(ack_frame, clock_.Now());
697 TEST_F(QuicSentPacketManagerTest, Rtt) {
698 QuicPacketNumber packet_number = 1;
699 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(15);
700 SendDataPacket(packet_number);
701 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
703 ExpectAck(packet_number);
704 QuicAckFrame ack_frame;
705 ack_frame.largest_observed = packet_number;
706 ack_frame.delta_time_largest_observed =
707 QuicTime::Delta::FromMilliseconds(5);
708 manager_.OnIncomingAck(ack_frame, clock_.Now());
709 EXPECT_EQ(expected_rtt,
710 QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
713 TEST_F(QuicSentPacketManagerTest, RttWithInvalidDelta) {
714 // Expect that the RTT is equal to the local time elapsed, since the
715 // delta_time_largest_observed is larger than the local time elapsed
716 // and is hence invalid.
717 QuicPacketNumber packet_number = 1;
718 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
719 SendDataPacket(packet_number);
720 clock_.AdvanceTime(expected_rtt);
722 ExpectAck(packet_number);
723 QuicAckFrame ack_frame;
724 ack_frame.largest_observed = packet_number;
725 ack_frame.delta_time_largest_observed =
726 QuicTime::Delta::FromMilliseconds(11);
727 manager_.OnIncomingAck(ack_frame, clock_.Now());
728 EXPECT_EQ(expected_rtt,
729 QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
732 TEST_F(QuicSentPacketManagerTest, RttWithInfiniteDelta) {
733 // Expect that the RTT is equal to the local time elapsed, since the
734 // delta_time_largest_observed is infinite, and is hence invalid.
735 QuicPacketNumber packet_number = 1;
736 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
737 SendDataPacket(packet_number);
738 clock_.AdvanceTime(expected_rtt);
740 ExpectAck(packet_number);
741 QuicAckFrame ack_frame;
742 ack_frame.largest_observed = packet_number;
743 ack_frame.delta_time_largest_observed = QuicTime::Delta::Infinite();
744 manager_.OnIncomingAck(ack_frame, clock_.Now());
745 EXPECT_EQ(expected_rtt,
746 QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
749 TEST_F(QuicSentPacketManagerTest, RttZeroDelta) {
750 // Expect that the RTT is the time between send and receive since the
751 // delta_time_largest_observed is zero.
752 QuicPacketNumber packet_number = 1;
753 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
754 SendDataPacket(packet_number);
755 clock_.AdvanceTime(expected_rtt);
757 ExpectAck(packet_number);
758 QuicAckFrame ack_frame;
759 ack_frame.largest_observed = packet_number;
760 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
761 manager_.OnIncomingAck(ack_frame, clock_.Now());
762 EXPECT_EQ(expected_rtt,
763 QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
766 TEST_F(QuicSentPacketManagerTest, TailLossProbeTimeout) {
767 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
769 // Send 1 packet.
770 QuicPacketNumber packet_number = 1;
771 SendDataPacket(packet_number);
773 // The first tail loss probe retransmits 1 packet.
774 manager_.OnRetransmissionTimeout();
775 EXPECT_EQ(QuicTime::Delta::Zero(),
776 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
777 EXPECT_FALSE(manager_.HasPendingRetransmissions());
778 manager_.MaybeRetransmitTailLossProbe();
779 EXPECT_TRUE(manager_.HasPendingRetransmissions());
780 RetransmitNextPacket(2);
781 EXPECT_FALSE(manager_.HasPendingRetransmissions());
783 // The second tail loss probe retransmits 1 packet.
784 manager_.OnRetransmissionTimeout();
785 EXPECT_EQ(QuicTime::Delta::Zero(),
786 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
787 EXPECT_FALSE(manager_.HasPendingRetransmissions());
788 manager_.MaybeRetransmitTailLossProbe();
789 EXPECT_TRUE(manager_.HasPendingRetransmissions());
790 RetransmitNextPacket(3);
791 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
792 QuicTime::Delta::Infinite()));
793 EXPECT_EQ(QuicTime::Delta::Infinite(),
794 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
795 EXPECT_FALSE(manager_.HasPendingRetransmissions());
797 // Ack the third and ensure the first two are still pending.
798 ExpectAck(3);
799 QuicAckFrame ack_frame;
800 ack_frame.largest_observed = 3;
801 ack_frame.missing_packets.insert(1);
802 ack_frame.missing_packets.insert(2);
803 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
805 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
807 // Acking two more packets will lose both of them due to nacks.
808 ack_frame.largest_observed = 5;
809 QuicPacketNumber lost[] = {1, 2};
810 ExpectAcksAndLosses(false, nullptr, 0, lost, arraysize(lost));
811 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
813 EXPECT_FALSE(manager_.HasPendingRetransmissions());
814 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
815 EXPECT_EQ(2u, stats_.tlp_count);
816 EXPECT_EQ(0u, stats_.rto_count);
819 TEST_F(QuicSentPacketManagerTest, TailLossProbeThenRTO) {
820 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
822 // Send 100 packets.
823 const size_t kNumSentPackets = 100;
824 for (size_t i = 1; i <= kNumSentPackets; ++i) {
825 SendDataPacket(i);
827 QuicTime rto_packet_time = clock_.Now();
828 // Advance the time.
829 clock_.AdvanceTime(manager_.GetRetransmissionTime().Subtract(clock_.Now()));
831 // The first tail loss probe retransmits 1 packet.
832 manager_.OnRetransmissionTimeout();
833 EXPECT_EQ(QuicTime::Delta::Zero(),
834 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
835 EXPECT_FALSE(manager_.HasPendingRetransmissions());
836 manager_.MaybeRetransmitTailLossProbe();
837 EXPECT_TRUE(manager_.HasPendingRetransmissions());
838 RetransmitNextPacket(101);
839 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
840 QuicTime::Delta::Infinite()));
841 EXPECT_EQ(QuicTime::Delta::Infinite(),
842 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
843 EXPECT_FALSE(manager_.HasPendingRetransmissions());
844 clock_.AdvanceTime(manager_.GetRetransmissionTime().Subtract(clock_.Now()));
846 // The second tail loss probe retransmits 1 packet.
847 manager_.OnRetransmissionTimeout();
848 EXPECT_EQ(QuicTime::Delta::Zero(),
849 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
850 EXPECT_FALSE(manager_.HasPendingRetransmissions());
851 EXPECT_TRUE(manager_.MaybeRetransmitTailLossProbe());
852 EXPECT_TRUE(manager_.HasPendingRetransmissions());
853 RetransmitNextPacket(102);
854 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
855 QuicTime::Delta::Infinite()));
856 EXPECT_EQ(QuicTime::Delta::Infinite(),
857 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
859 // Ensure the RTO is set based on the correct packet.
860 rto_packet_time = clock_.Now();
861 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
862 .WillOnce(Return(QuicTime::Delta::FromSeconds(1)));
863 EXPECT_EQ(rto_packet_time.Add(QuicTime::Delta::FromSeconds(1)),
864 manager_.GetRetransmissionTime());
866 // Advance the time enough to ensure all packets are RTO'd.
867 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
869 manager_.OnRetransmissionTimeout();
870 EXPECT_TRUE(manager_.HasPendingRetransmissions());
871 EXPECT_EQ(2u, stats_.tlp_count);
872 EXPECT_EQ(1u, stats_.rto_count);
874 // Send and Ack the RTO and ensure OnRetransmissionTimeout is called.
875 EXPECT_EQ(102 * kDefaultLength,
876 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
878 RetransmitNextPacket(103);
879 QuicAckFrame ack_frame;
880 ack_frame.largest_observed = 103;
881 for (int i = 0; i < 103; ++i) {
882 ack_frame.missing_packets.insert(i);
884 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
885 EXPECT_CALL(*send_algorithm_,
886 OnCongestionEvent(true, _, ElementsAre(Pair(103, _)), _));
887 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
888 EXPECT_CALL(*network_change_visitor_, OnRttChange());
889 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
890 // All packets before 103 should be lost.
891 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
894 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeout) {
895 // Send 2 crypto packets and 3 data packets.
896 const size_t kNumSentCryptoPackets = 2;
897 for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
898 SendCryptoPacket(i);
900 const size_t kNumSentDataPackets = 3;
901 for (size_t i = 1; i <= kNumSentDataPackets; ++i) {
902 SendDataPacket(kNumSentCryptoPackets + i);
904 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
906 // The first retransmits 2 packets.
907 manager_.OnRetransmissionTimeout();
908 EXPECT_EQ(QuicTime::Delta::Zero(),
909 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
910 RetransmitNextPacket(6);
911 RetransmitNextPacket(7);
912 EXPECT_FALSE(manager_.HasPendingRetransmissions());
913 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
915 // The second retransmits 2 packets.
916 manager_.OnRetransmissionTimeout();
917 EXPECT_EQ(QuicTime::Delta::Zero(),
918 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
919 RetransmitNextPacket(8);
920 RetransmitNextPacket(9);
921 EXPECT_FALSE(manager_.HasPendingRetransmissions());
922 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
924 // Now ack the two crypto packets and the speculatively encrypted request,
925 // and ensure the first four crypto packets get abandoned, but not lost.
926 QuicPacketNumber acked[] = {3, 4, 5, 8, 9};
927 ExpectAcksAndLosses(true, acked, arraysize(acked), nullptr, 0);
928 QuicAckFrame ack_frame;
929 ack_frame.largest_observed = 9;
930 ack_frame.missing_packets.insert(1);
931 ack_frame.missing_packets.insert(2);
932 ack_frame.missing_packets.insert(6);
933 ack_frame.missing_packets.insert(7);
934 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
936 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
939 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeoutVersionNegotiation) {
940 // Send 2 crypto packets and 3 data packets.
941 const size_t kNumSentCryptoPackets = 2;
942 for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
943 SendCryptoPacket(i);
945 const size_t kNumSentDataPackets = 3;
946 for (size_t i = 1; i <= kNumSentDataPackets; ++i) {
947 SendDataPacket(kNumSentCryptoPackets + i);
949 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
951 // The first retransmission timeout retransmits 2 crypto packets.
952 manager_.OnRetransmissionTimeout();
953 RetransmitNextPacket(6);
954 RetransmitNextPacket(7);
955 EXPECT_FALSE(manager_.HasPendingRetransmissions());
956 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
958 // Now act like a version negotiation packet arrived, which would cause all
959 // unacked packets to be retransmitted.
960 manager_.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION);
962 // Ensure the first two pending packets are the crypto retransmits.
963 ASSERT_TRUE(manager_.HasPendingRetransmissions());
964 EXPECT_EQ(6u, manager_.NextPendingRetransmission().packet_number);
965 RetransmitNextPacket(8);
966 EXPECT_EQ(7u, manager_.NextPendingRetransmission().packet_number);
967 RetransmitNextPacket(9);
969 EXPECT_TRUE(manager_.HasPendingRetransmissions());
970 // Send 3 more data packets and ensure the least unacked is raised.
971 RetransmitNextPacket(10);
972 RetransmitNextPacket(11);
973 RetransmitNextPacket(12);
974 EXPECT_FALSE(manager_.HasPendingRetransmissions());
976 EXPECT_EQ(8u, manager_.GetLeastUnacked());
979 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeSpuriousRetransmission) {
980 // Send 1 crypto packet.
981 SendCryptoPacket(1);
982 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
984 // Retransmit the crypto packet as 2.
985 manager_.OnRetransmissionTimeout();
986 RetransmitNextPacket(2);
988 // Retransmit the crypto packet as 3.
989 manager_.OnRetransmissionTimeout();
990 RetransmitNextPacket(3);
992 // Now ack the second crypto packet, and ensure the first gets removed, but
993 // the third does not.
994 ExpectUpdatedRtt(2);
995 QuicAckFrame ack_frame;
996 ack_frame.largest_observed = 2;
997 ack_frame.missing_packets.insert(1);
998 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
1000 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1001 QuicPacketNumber unacked[] = {3};
1002 VerifyUnackedPackets(unacked, arraysize(unacked));
1005 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeoutUnsentDataPacket) {
1006 // Send 2 crypto packets and 1 data packet.
1007 const size_t kNumSentCryptoPackets = 2;
1008 for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
1009 SendCryptoPacket(i);
1011 SendDataPacket(3);
1012 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1014 // Retransmit 2 crypto packets, but not the serialized packet.
1015 manager_.OnRetransmissionTimeout();
1016 RetransmitNextPacket(4);
1017 RetransmitNextPacket(5);
1018 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1019 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1022 TEST_F(QuicSentPacketManagerTest,
1023 CryptoHandshakeRetransmissionThenRetransmitAll) {
1024 // Send 1 crypto packet.
1025 SendCryptoPacket(1);
1026 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1028 // Retransmit the crypto packet as 2.
1029 manager_.OnRetransmissionTimeout();
1030 RetransmitNextPacket(2);
1032 // Now retransmit all the unacked packets, which occurs when there is a
1033 // version negotiation.
1034 manager_.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION);
1035 QuicPacketNumber unacked[] = {1, 2};
1036 VerifyUnackedPackets(unacked, arraysize(unacked));
1037 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1038 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1039 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
1042 TEST_F(QuicSentPacketManagerTest,
1043 CryptoHandshakeRetransmissionThenNeuterAndAck) {
1044 // Send 1 crypto packet.
1045 SendCryptoPacket(1);
1046 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1048 // Retransmit the crypto packet as 2.
1049 manager_.OnRetransmissionTimeout();
1050 RetransmitNextPacket(2);
1052 // Retransmit the crypto packet as 3.
1053 manager_.OnRetransmissionTimeout();
1054 RetransmitNextPacket(3);
1056 // Now neuter all unacked unencrypted packets, which occurs when the
1057 // connection goes forward secure.
1058 manager_.NeuterUnencryptedPackets();
1059 QuicPacketNumber unacked[] = {1, 2, 3};
1060 VerifyUnackedPackets(unacked, arraysize(unacked));
1061 VerifyRetransmittablePackets(nullptr, 0);
1062 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1063 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1064 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
1066 // Ensure both packets get discarded when packet 2 is acked.
1067 QuicAckFrame ack_frame;
1068 ack_frame.largest_observed = 3;
1069 ack_frame.missing_packets.insert(1);
1070 ack_frame.missing_packets.insert(2);
1071 ExpectUpdatedRtt(3);
1072 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
1073 VerifyUnackedPackets(nullptr, 0);
1074 VerifyRetransmittablePackets(nullptr, 0);
1077 TEST_F(QuicSentPacketManagerTest, RetransmissionTimeout) {
1078 // Send 100 packets.
1079 const size_t kNumSentPackets = 100;
1080 for (size_t i = 1; i <= kNumSentPackets; ++i) {
1081 SendDataPacket(i);
1084 EXPECT_FALSE(manager_.MaybeRetransmitTailLossProbe());
1085 manager_.OnRetransmissionTimeout();
1086 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1087 EXPECT_EQ(100 * kDefaultLength,
1088 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1089 RetransmitNextPacket(101);
1090 RetransmitNextPacket(102);
1091 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1093 // Ack a retransmission.
1094 QuicAckFrame ack_frame;
1095 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
1096 ack_frame.largest_observed = 102;
1097 for (int i = 0; i < 102; ++i) {
1098 ack_frame.missing_packets.insert(i);
1100 // Ensure no packets are lost.
1101 EXPECT_CALL(*send_algorithm_,
1102 OnCongestionEvent(true, _, ElementsAre(Pair(102, _)),
1103 /*lost_packets=*/IsEmpty()));
1104 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1105 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1106 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1107 manager_.OnIncomingAck(ack_frame, clock_.Now());
1110 TEST_F(QuicSentPacketManagerTest, NewRetransmissionTimeout) {
1111 QuicConfig client_config;
1112 QuicTagVector options;
1113 options.push_back(kNRTO);
1114 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1115 client_config.SetConnectionOptionsToSend(options);
1116 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1117 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1118 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1119 EXPECT_CALL(*send_algorithm_, PacingRate())
1120 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1121 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1122 .WillOnce(Return(10 * kDefaultTCPMSS));
1123 manager_.SetFromConfig(client_config);
1124 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
1126 // Send 100 packets.
1127 const size_t kNumSentPackets = 100;
1128 for (size_t i = 1; i <= kNumSentPackets; ++i) {
1129 SendDataPacket(i);
1132 EXPECT_FALSE(manager_.MaybeRetransmitTailLossProbe());
1133 manager_.OnRetransmissionTimeout();
1134 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1135 EXPECT_EQ(100 * kDefaultLength,
1136 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1137 RetransmitNextPacket(101);
1138 RetransmitNextPacket(102);
1139 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1141 // Ack a retransmission and expect no call to OnRetransmissionTimeout.
1142 QuicAckFrame ack_frame;
1143 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
1144 ack_frame.largest_observed = 102;
1145 for (int i = 0; i < 102; ++i) {
1146 ack_frame.missing_packets.insert(i);
1148 // This will include packets in the lost packet map.
1149 EXPECT_CALL(*send_algorithm_,
1150 OnCongestionEvent(true, _, ElementsAre(Pair(102, _)),
1151 /*lost_packets=*/Not(IsEmpty())));
1152 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1153 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1154 manager_.OnIncomingAck(ack_frame, clock_.Now());
1157 TEST_F(QuicSentPacketManagerTest, TwoRetransmissionTimeoutsAckSecond) {
1158 // Send 1 packet.
1159 SendDataPacket(1);
1161 manager_.OnRetransmissionTimeout();
1162 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1163 EXPECT_EQ(kDefaultLength,
1164 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1165 RetransmitNextPacket(2);
1166 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1168 // Rto a second time.
1169 manager_.OnRetransmissionTimeout();
1170 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1171 EXPECT_EQ(2 * kDefaultLength,
1172 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1173 RetransmitNextPacket(3);
1174 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1176 // Ack a retransmission and ensure OnRetransmissionTimeout is called.
1177 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1178 QuicAckFrame ack_frame;
1179 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
1180 ack_frame.largest_observed = 2;
1181 ack_frame.missing_packets.insert(1);
1182 ExpectAck(2);
1183 manager_.OnIncomingAck(ack_frame, clock_.Now());
1185 // The original packet and newest should be outstanding.
1186 EXPECT_EQ(2 * kDefaultLength,
1187 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1190 TEST_F(QuicSentPacketManagerTest, TwoRetransmissionTimeoutsAckFirst) {
1191 // Send 1 packet.
1192 SendDataPacket(1);
1194 manager_.OnRetransmissionTimeout();
1195 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1196 EXPECT_EQ(kDefaultLength,
1197 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1198 RetransmitNextPacket(2);
1199 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1201 // Rto a second time.
1202 manager_.OnRetransmissionTimeout();
1203 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1204 EXPECT_EQ(2 * kDefaultLength,
1205 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1206 RetransmitNextPacket(3);
1207 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1209 // Ack a retransmission and ensure OnRetransmissionTimeout is called.
1210 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1211 QuicAckFrame ack_frame;
1212 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
1213 ack_frame.largest_observed = 3;
1214 ack_frame.missing_packets.insert(1);
1215 ack_frame.missing_packets.insert(2);
1216 ExpectAck(3);
1217 manager_.OnIncomingAck(ack_frame, clock_.Now());
1219 // The first two packets should still be outstanding.
1220 EXPECT_EQ(2 * kDefaultLength,
1221 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1224 TEST_F(QuicSentPacketManagerTest, GetTransmissionTime) {
1225 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
1228 TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeCryptoHandshake) {
1229 SendCryptoPacket(1);
1231 // Check the min.
1232 RttStats* rtt_stats = QuicSentPacketManagerPeer::GetRttStats(&manager_);
1233 rtt_stats->set_initial_rtt_us(1 * kNumMicrosPerMilli);
1234 EXPECT_EQ(clock_.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1235 manager_.GetRetransmissionTime());
1237 // Test with a standard smoothed RTT.
1238 rtt_stats->set_initial_rtt_us(100 * kNumMicrosPerMilli);
1240 QuicTime::Delta srtt =
1241 QuicTime::Delta::FromMicroseconds(rtt_stats->initial_rtt_us());
1242 QuicTime expected_time = clock_.Now().Add(srtt.Multiply(1.5));
1243 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1245 // Retransmit the packet by invoking the retransmission timeout.
1246 clock_.AdvanceTime(srtt.Multiply(1.5));
1247 manager_.OnRetransmissionTimeout();
1248 RetransmitNextPacket(2);
1250 // The retransmission time should now be twice as far in the future.
1251 expected_time = clock_.Now().Add(srtt.Multiply(2).Multiply(1.5));
1252 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1255 TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeTailLossProbe) {
1256 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
1257 SendDataPacket(1);
1258 SendDataPacket(2);
1260 // Check the min.
1261 RttStats* rtt_stats = QuicSentPacketManagerPeer::GetRttStats(&manager_);
1262 rtt_stats->set_initial_rtt_us(1 * kNumMicrosPerMilli);
1263 EXPECT_EQ(clock_.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1264 manager_.GetRetransmissionTime());
1266 // Test with a standard smoothed RTT.
1267 rtt_stats->set_initial_rtt_us(100 * kNumMicrosPerMilli);
1268 QuicTime::Delta srtt =
1269 QuicTime::Delta::FromMicroseconds(rtt_stats->initial_rtt_us());
1270 QuicTime::Delta expected_tlp_delay = srtt.Multiply(2);
1271 QuicTime expected_time = clock_.Now().Add(expected_tlp_delay);
1272 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1274 // Retransmit the packet by invoking the retransmission timeout.
1275 clock_.AdvanceTime(expected_tlp_delay);
1276 manager_.OnRetransmissionTimeout();
1277 EXPECT_EQ(QuicTime::Delta::Zero(),
1278 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1279 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1280 EXPECT_TRUE(manager_.MaybeRetransmitTailLossProbe());
1281 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1282 RetransmitNextPacket(3);
1283 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
1284 QuicTime::Delta::Infinite()));
1285 EXPECT_EQ(QuicTime::Delta::Infinite(),
1286 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1287 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1289 expected_time = clock_.Now().Add(expected_tlp_delay);
1290 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1293 TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeSpuriousRTO) {
1294 QuicSentPacketManagerPeer::GetRttStats(&manager_)->UpdateRtt(
1295 QuicTime::Delta::FromMilliseconds(100),
1296 QuicTime::Delta::Zero(),
1297 QuicTime::Zero());
1299 SendDataPacket(1);
1300 SendDataPacket(2);
1301 SendDataPacket(3);
1302 SendDataPacket(4);
1304 QuicTime::Delta expected_rto_delay = QuicTime::Delta::FromMilliseconds(500);
1305 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
1306 .WillRepeatedly(Return(expected_rto_delay));
1307 QuicTime expected_time = clock_.Now().Add(expected_rto_delay);
1308 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1310 // Retransmit the packet by invoking the retransmission timeout.
1311 clock_.AdvanceTime(expected_rto_delay);
1312 manager_.OnRetransmissionTimeout();
1313 // All packets are still considered inflight.
1314 EXPECT_EQ(4 * kDefaultLength,
1315 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1316 RetransmitNextPacket(5);
1317 RetransmitNextPacket(6);
1318 // All previous packets are inflight, plus two rto retransmissions.
1319 EXPECT_EQ(6 * kDefaultLength,
1320 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1321 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1323 // The delay should double the second time.
1324 expected_time = clock_.Now().Add(expected_rto_delay).Add(expected_rto_delay);
1325 // Once we always base the timer on the right edge, leaving the older packets
1326 // in flight doesn't change the timeout.
1327 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1329 // Ack a packet before the first RTO and ensure the RTO timeout returns to the
1330 // original value and OnRetransmissionTimeout is not called or reverted.
1331 QuicAckFrame ack_frame;
1332 ack_frame.largest_observed = 2;
1333 ack_frame.missing_packets.insert(1);
1334 ExpectAck(2);
1335 manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
1336 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1337 EXPECT_EQ(5 * kDefaultLength,
1338 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1340 // Wait 2RTTs from now for the RTO, since it's the max of the RTO time
1341 // and the TLP time. In production, there would always be two TLP's first.
1342 // Since retransmission was spurious, smoothed_rtt_ is expired, and replaced
1343 // by the latest RTT sample of 500ms.
1344 expected_time = clock_.Now().Add(QuicTime::Delta::FromMilliseconds(1000));
1345 // Once we always base the timer on the right edge, leaving the older packets
1346 // in flight doesn't change the timeout.
1347 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1350 TEST_F(QuicSentPacketManagerTest, GetTransmissionDelayMin) {
1351 SendDataPacket(1);
1352 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
1353 .WillRepeatedly(Return(QuicTime::Delta::FromMilliseconds(1)));
1354 QuicTime::Delta delay = QuicTime::Delta::FromMilliseconds(200);
1356 // If the delay is smaller than the min, ensure it exponentially backs off
1357 // from the min.
1358 for (int i = 0; i < 5; ++i) {
1359 EXPECT_EQ(delay,
1360 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
1361 delay = delay.Add(delay);
1362 manager_.OnRetransmissionTimeout();
1363 RetransmitNextPacket(i + 2);
1367 TEST_F(QuicSentPacketManagerTest, GetTransmissionDelayMax) {
1368 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
1369 .WillOnce(Return(QuicTime::Delta::FromSeconds(500)));
1371 EXPECT_EQ(QuicTime::Delta::FromSeconds(60),
1372 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
1375 TEST_F(QuicSentPacketManagerTest, GetTransmissionDelay) {
1376 SendDataPacket(1);
1377 QuicTime::Delta delay = QuicTime::Delta::FromMilliseconds(500);
1378 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
1379 .WillRepeatedly(Return(delay));
1381 // Delay should back off exponentially.
1382 for (int i = 0; i < 5; ++i) {
1383 EXPECT_EQ(delay,
1384 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
1385 delay = delay.Add(delay);
1386 manager_.OnRetransmissionTimeout();
1387 RetransmitNextPacket(i + 2);
1391 TEST_F(QuicSentPacketManagerTest, GetLossDelay) {
1392 MockLossAlgorithm* loss_algorithm = new MockLossAlgorithm();
1393 QuicSentPacketManagerPeer::SetLossAlgorithm(&manager_, loss_algorithm);
1395 EXPECT_CALL(*loss_algorithm, GetLossTimeout())
1396 .WillRepeatedly(Return(QuicTime::Zero()));
1397 SendDataPacket(1);
1398 SendDataPacket(2);
1400 // Handle an ack which causes the loss algorithm to be evaluated and
1401 // set the loss timeout.
1402 ExpectAck(2);
1403 EXPECT_CALL(*loss_algorithm, DetectLostPackets(_, _, _, _))
1404 .WillOnce(Return(PacketNumberSet()));
1405 QuicAckFrame ack_frame;
1406 ack_frame.largest_observed = 2;
1407 ack_frame.missing_packets.insert(1);
1408 manager_.OnIncomingAck(ack_frame, clock_.Now());
1410 QuicTime timeout(clock_.Now().Add(QuicTime::Delta::FromMilliseconds(10)));
1411 EXPECT_CALL(*loss_algorithm, GetLossTimeout())
1412 .WillRepeatedly(Return(timeout));
1413 EXPECT_EQ(timeout, manager_.GetRetransmissionTime());
1415 // Fire the retransmission timeout and ensure the loss detection algorithm
1416 // is invoked.
1417 EXPECT_CALL(*loss_algorithm, DetectLostPackets(_, _, _, _))
1418 .WillOnce(Return(PacketNumberSet()));
1419 manager_.OnRetransmissionTimeout();
1422 TEST_F(QuicSentPacketManagerTest, NegotiateTimeLossDetectionFromOptions) {
1423 EXPECT_EQ(kNack,
1424 QuicSentPacketManagerPeer::GetLossAlgorithm(
1425 &manager_)->GetLossDetectionType());
1427 QuicConfig config;
1428 QuicTagVector options;
1429 options.push_back(kTIME);
1430 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1431 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1432 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1433 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1434 manager_.SetFromConfig(config);
1436 EXPECT_EQ(kTime,
1437 QuicSentPacketManagerPeer::GetLossAlgorithm(
1438 &manager_)->GetLossDetectionType());
1441 TEST_F(QuicSentPacketManagerTest, NegotiateCongestionControlFromOptions) {
1442 ValueRestore<bool> old_flag(&FLAGS_quic_allow_bbr, true);
1443 QuicConfig config;
1444 QuicTagVector options;
1446 options.push_back(kRENO);
1447 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1448 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1449 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1450 manager_.SetFromConfig(config);
1451 EXPECT_EQ(kReno, QuicSentPacketManagerPeer::GetSendAlgorithm(
1452 manager_)->GetCongestionControlType());
1454 // TODO(rtenneti): Enable the following code after BBR code is checked in.
1455 #if 0
1456 options.clear();
1457 options.push_back(kTBBR);
1458 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1459 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1460 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1461 manager_.SetFromConfig(config);
1462 EXPECT_EQ(kBBR, QuicSentPacketManagerPeer::GetSendAlgorithm(
1463 manager_)->GetCongestionControlType());
1464 #endif
1466 options.clear();
1467 options.push_back(kBYTE);
1468 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1469 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1470 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1471 manager_.SetFromConfig(config);
1472 EXPECT_EQ(kCubicBytes, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_)
1473 ->GetCongestionControlType());
1475 options.clear();
1476 options.push_back(kRENO);
1477 options.push_back(kBYTE);
1478 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1479 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1480 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1481 manager_.SetFromConfig(config);
1482 EXPECT_EQ(kRenoBytes, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_)
1483 ->GetCongestionControlType());
1486 TEST_F(QuicSentPacketManagerTest, NegotiateNumConnectionsFromOptions) {
1487 QuicConfig config;
1488 QuicTagVector options;
1490 options.push_back(k1CON);
1491 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1492 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1493 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1494 EXPECT_CALL(*send_algorithm_, SetNumEmulatedConnections(1));
1495 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1496 manager_.SetFromConfig(config);
1498 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1499 QuicConfig client_config;
1500 client_config.SetConnectionOptionsToSend(options);
1501 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1502 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1503 EXPECT_CALL(*send_algorithm_, SetNumEmulatedConnections(1));
1504 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1505 manager_.SetFromConfig(client_config);
1508 TEST_F(QuicSentPacketManagerTest, NegotiateNConnectionFromOptions) {
1509 // By default, changing the number of open streams does nothing.
1510 manager_.SetNumOpenStreams(5);
1512 QuicConfig config;
1513 QuicTagVector options;
1515 options.push_back(kNCON);
1516 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1517 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1518 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1519 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1520 manager_.SetFromConfig(config);
1522 EXPECT_CALL(*send_algorithm_, SetNumEmulatedConnections(5));
1523 manager_.SetNumOpenStreams(5);
1526 TEST_F(QuicSentPacketManagerTest, NegotiateNoTLPFromOptionsAtServer) {
1527 QuicConfig config;
1528 QuicTagVector options;
1530 options.push_back(kNTLP);
1531 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1532 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1533 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1534 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1535 manager_.SetFromConfig(config);
1536 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_));
1539 TEST_F(QuicSentPacketManagerTest, NegotiateNoTLPFromOptionsAtClient) {
1540 QuicConfig client_config;
1541 QuicTagVector options;
1543 options.push_back(kNTLP);
1544 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1545 client_config.SetConnectionOptionsToSend(options);
1546 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1547 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1548 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1549 manager_.SetFromConfig(client_config);
1550 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_));
1553 TEST_F(QuicSentPacketManagerTest, NegotiateTLPRttFromOptionsAtServer) {
1554 QuicConfig config;
1555 QuicTagVector options;
1557 options.push_back(kTLPR);
1558 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1559 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1560 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1561 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1562 manager_.SetFromConfig(config);
1563 EXPECT_TRUE(
1564 QuicSentPacketManagerPeer::GetEnableHalfRttTailLossProbe(&manager_));
1567 TEST_F(QuicSentPacketManagerTest, NegotiateTLPRttFromOptionsAtClient) {
1568 QuicConfig client_config;
1569 QuicTagVector options;
1571 options.push_back(kTLPR);
1572 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1573 client_config.SetConnectionOptionsToSend(options);
1574 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1575 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1576 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1577 manager_.SetFromConfig(client_config);
1578 EXPECT_TRUE(
1579 QuicSentPacketManagerPeer::GetEnableHalfRttTailLossProbe(&manager_));
1582 TEST_F(QuicSentPacketManagerTest, NegotiateNewRTOFromOptionsAtServer) {
1583 EXPECT_FALSE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
1584 QuicConfig config;
1585 QuicTagVector options;
1587 options.push_back(kNRTO);
1588 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1589 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1590 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1591 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1592 manager_.SetFromConfig(config);
1593 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
1596 TEST_F(QuicSentPacketManagerTest, NegotiateNewRTOFromOptionsAtClient) {
1597 EXPECT_FALSE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
1598 QuicConfig client_config;
1599 QuicTagVector options;
1601 options.push_back(kNRTO);
1602 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1603 client_config.SetConnectionOptionsToSend(options);
1604 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1605 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1606 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1607 manager_.SetFromConfig(client_config);
1608 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
1611 TEST_F(QuicSentPacketManagerTest, NegotiateReceiveWindowFromOptions) {
1612 ValueRestore<bool> old_flag(&FLAGS_quic_use_conservative_receive_buffer,
1613 false);
1614 EXPECT_EQ(kDefaultSocketReceiveBuffer,
1615 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_));
1617 // Try to set a size below the minimum and ensure it gets set to the min.
1618 QuicConfig client_config;
1619 QuicConfigPeer::SetReceivedSocketReceiveBuffer(&client_config, 1024);
1620 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1621 EXPECT_CALL(*send_algorithm_,
1622 SetMaxCongestionWindow(kMinSocketReceiveBuffer * 0.95));
1623 EXPECT_CALL(*send_algorithm_, PacingRate())
1624 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1625 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1626 .WillOnce(Return(10 * kDefaultTCPMSS));
1627 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1628 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1629 manager_.SetFromConfig(client_config);
1631 EXPECT_EQ(kMinSocketReceiveBuffer,
1632 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_));
1634 // Ensure the smaller send window only allows 16 packets to be sent.
1635 for (QuicPacketNumber i = 1; i <= 16; ++i) {
1636 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
1637 QuicTime::Delta::Zero()));
1638 EXPECT_EQ(QuicTime::Delta::Zero(),
1639 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1640 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, BytesInFlight(), i, 1024,
1641 HAS_RETRANSMITTABLE_DATA))
1642 .WillOnce(Return(true));
1643 SerializedPacket packet(CreatePacket(i, true));
1644 manager_.OnPacketSent(&packet, 0, clock_.Now(), 1024, NOT_RETRANSMISSION,
1645 HAS_RETRANSMITTABLE_DATA);
1647 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _))
1648 .WillOnce(Return(QuicTime::Delta::Infinite()));
1649 EXPECT_EQ(QuicTime::Delta::Infinite(),
1650 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1653 TEST_F(QuicSentPacketManagerTest,
1654 NegotiateConservativeReceiveWindowFromOptions) {
1655 ValueRestore<bool> old_flag(&FLAGS_quic_use_conservative_receive_buffer,
1656 true);
1657 EXPECT_EQ(kDefaultSocketReceiveBuffer,
1658 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_));
1660 // Try to set a size below the minimum and ensure it gets set to the min.
1661 QuicConfig client_config;
1662 QuicConfigPeer::SetReceivedSocketReceiveBuffer(&client_config, 1024);
1663 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1664 EXPECT_CALL(*send_algorithm_,
1665 SetMaxCongestionWindow(kMinSocketReceiveBuffer * 0.6));
1666 EXPECT_CALL(*send_algorithm_, PacingRate())
1667 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1668 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1669 .WillOnce(Return(10 * kDefaultTCPMSS));
1670 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1671 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1672 manager_.SetFromConfig(client_config);
1674 EXPECT_EQ(kMinSocketReceiveBuffer,
1675 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_));
1677 // Ensure the smaller send window only allows 16 packets to be sent.
1678 for (QuicPacketNumber i = 1; i <= 16; ++i) {
1679 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _))
1680 .WillOnce(Return(QuicTime::Delta::Zero()));
1681 EXPECT_EQ(QuicTime::Delta::Zero(),
1682 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1683 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, BytesInFlight(), i, 1024,
1684 HAS_RETRANSMITTABLE_DATA))
1685 .WillOnce(Return(true));
1686 SerializedPacket packet(CreatePacket(i, true));
1687 manager_.OnPacketSent(&packet, 0, clock_.Now(), 1024, NOT_RETRANSMISSION,
1688 HAS_RETRANSMITTABLE_DATA);
1690 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _))
1691 .WillOnce(Return(QuicTime::Delta::Infinite()));
1692 EXPECT_EQ(QuicTime::Delta::Infinite(),
1693 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1696 TEST_F(QuicSentPacketManagerTest, ReceiveWindowLimited) {
1697 EXPECT_EQ(kDefaultSocketReceiveBuffer,
1698 QuicSentPacketManagerPeer::GetReceiveWindow(&manager_));
1700 // Ensure the smaller send window only allows 256 * 0.95 packets to be sent.
1701 for (QuicPacketNumber i = 1; i <= 244; ++i) {
1702 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
1703 QuicTime::Delta::Zero()));
1704 EXPECT_EQ(QuicTime::Delta::Zero(),
1705 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1706 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, BytesInFlight(), i, 1024,
1707 HAS_RETRANSMITTABLE_DATA))
1708 .WillOnce(Return(true));
1709 SerializedPacket packet(CreatePacket(i, true));
1710 manager_.OnPacketSent(&packet, 0, clock_.Now(), 1024, NOT_RETRANSMISSION,
1711 HAS_RETRANSMITTABLE_DATA);
1713 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _))
1714 .WillOnce(Return(QuicTime::Delta::Infinite()));
1715 EXPECT_EQ(QuicTime::Delta::Infinite(),
1716 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1719 TEST_F(QuicSentPacketManagerTest, UseInitialRoundTripTimeToSend) {
1720 uint32 initial_rtt_us = 325000;
1721 EXPECT_NE(initial_rtt_us,
1722 manager_.GetRttStats()->smoothed_rtt().ToMicroseconds());
1724 QuicConfig config;
1725 config.SetInitialRoundTripTimeUsToSend(initial_rtt_us);
1726 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1727 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange());
1728 EXPECT_CALL(*network_change_visitor_, OnRttChange());
1729 manager_.SetFromConfig(config);
1731 EXPECT_EQ(0, manager_.GetRttStats()->smoothed_rtt().ToMicroseconds());
1732 EXPECT_EQ(initial_rtt_us, manager_.GetRttStats()->initial_rtt_us());
1735 TEST_F(QuicSentPacketManagerTest, ResumeConnectionState) {
1736 // The sent packet manager should use the RTT from CachedNetworkParameters if
1737 // it is provided.
1738 const int kRttMs = 1234;
1739 CachedNetworkParameters cached_network_params;
1740 cached_network_params.set_min_rtt_ms(kRttMs);
1742 EXPECT_CALL(*send_algorithm_, ResumeConnectionState(_, false));
1743 manager_.ResumeConnectionState(cached_network_params, false);
1744 EXPECT_EQ(kRttMs * kNumMicrosPerMilli,
1745 static_cast<uint64>(manager_.GetRttStats()->initial_rtt_us()));
1748 } // namespace
1749 } // namespace test
1750 } // namespace net