In about:extensions, don't show the "allow in incognito" checkbox if incognito mode...
[chromium-blink-merge.git] / net / quic / quic_sent_packet_manager_test.cc
blob90f94f7270f5517947aa7b2c122049c08be728f2
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::Pair;
19 using testing::Pointwise;
20 using testing::Return;
21 using testing::StrictMock;
22 using testing::_;
24 namespace net {
25 namespace test {
26 namespace {
28 // Default packet length.
29 const uint32 kDefaultLength = 1000;
31 // Matcher to check the key of the key-value pair it receives as first argument
32 // equals its second argument.
33 MATCHER(KeyEq, "") {
34 return std::tr1::get<0>(arg).first == std::tr1::get<1>(arg);
37 class MockDebugDelegate : public QuicSentPacketManager::DebugDelegate {
38 public:
39 MOCK_METHOD2(OnSpuriousPacketRetransmition,
40 void(TransmissionType transmission_type,
41 QuicByteCount byte_size));
44 class QuicSentPacketManagerTest : public ::testing::TestWithParam<bool> {
45 protected:
46 QuicSentPacketManagerTest()
47 : manager_(true, &clock_, &stats_, kFixRateCongestionControl, kNack),
48 send_algorithm_(new StrictMock<MockSendAlgorithm>),
49 network_change_visitor_(new StrictMock<MockNetworkChangeVisitor>) {
50 QuicSentPacketManagerPeer::SetSendAlgorithm(&manager_, send_algorithm_);
51 // Disable tail loss probes for most tests.
52 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 0);
53 // Advance the time 1s so the send times are never QuicTime::Zero.
54 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
55 manager_.set_network_change_visitor(network_change_visitor_.get());
58 virtual ~QuicSentPacketManagerTest() OVERRIDE {
59 STLDeleteElements(&packets_);
62 QuicByteCount BytesInFlight() {
63 return QuicSentPacketManagerPeer::GetBytesInFlight(&manager_);
65 void VerifyUnackedPackets(QuicPacketSequenceNumber* packets,
66 size_t num_packets) {
67 if (num_packets == 0) {
68 EXPECT_FALSE(manager_.HasUnackedPackets());
69 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
70 &manager_));
71 return;
74 EXPECT_TRUE(manager_.HasUnackedPackets());
75 EXPECT_EQ(packets[0], manager_.GetLeastUnackedSentPacket());
76 for (size_t i = 0; i < num_packets; ++i) {
77 EXPECT_TRUE(manager_.IsUnacked(packets[i])) << packets[i];
81 void VerifyRetransmittablePackets(QuicPacketSequenceNumber* packets,
82 size_t num_packets) {
83 EXPECT_EQ(num_packets,
84 QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
85 &manager_));
86 for (size_t i = 0; i < num_packets; ++i) {
87 EXPECT_TRUE(manager_.HasRetransmittableFrames(packets[i]))
88 << " packets[" << i << "]:" << packets[i];
92 void ExpectAck(QuicPacketSequenceNumber largest_observed) {
93 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(
94 true, _, ElementsAre(Pair(largest_observed, _)), _));
95 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
96 .WillOnce(Return(100 * kDefaultTCPMSS));
97 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
100 void ExpectUpdatedRtt(QuicPacketSequenceNumber largest_observed) {
101 EXPECT_CALL(*send_algorithm_,
102 OnCongestionEvent(true, _, _, _));
103 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
104 .WillOnce(Return(100 * kDefaultTCPMSS));
105 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
108 void ExpectAckAndLoss(bool rtt_updated,
109 QuicPacketSequenceNumber largest_observed,
110 QuicPacketSequenceNumber lost_packet) {
111 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(
112 rtt_updated, _, ElementsAre(Pair(largest_observed, _)),
113 ElementsAre(Pair(lost_packet, _))));
114 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
115 .WillOnce(Return(100 * kDefaultTCPMSS));
116 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
119 // |packets_acked| and |packets_lost| should be in sequence number order.
120 void ExpectAcksAndLosses(bool rtt_updated,
121 QuicPacketSequenceNumber* packets_acked,
122 size_t num_packets_acked,
123 QuicPacketSequenceNumber* packets_lost,
124 size_t num_packets_lost) {
125 vector<QuicPacketSequenceNumber> ack_vector;
126 for (size_t i = 0; i < num_packets_acked; ++i) {
127 ack_vector.push_back(packets_acked[i]);
129 vector<QuicPacketSequenceNumber> lost_vector;
130 for (size_t i = 0; i < num_packets_lost; ++i) {
131 lost_vector.push_back(packets_lost[i]);
133 EXPECT_CALL(*send_algorithm_,
134 OnCongestionEvent(rtt_updated, _,
135 Pointwise(KeyEq(), ack_vector),
136 Pointwise(KeyEq(), lost_vector)));
137 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
138 .WillRepeatedly(Return(100 * kDefaultTCPMSS));
139 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_)).
140 Times(AnyNumber());
143 // Retransmits a packet as though it was a TLP retransmission, because TLP
144 // leaves the |old_sequence_number| pending.
145 // TODO(ianswett): Test with transmission types besides TLP.
146 void RetransmitPacket(QuicPacketSequenceNumber old_sequence_number,
147 QuicPacketSequenceNumber new_sequence_number) {
148 QuicSentPacketManagerPeer::MarkForRetransmission(
149 &manager_, old_sequence_number, TLP_RETRANSMISSION);
150 EXPECT_TRUE(manager_.HasPendingRetransmissions());
151 QuicSentPacketManager::PendingRetransmission next_retransmission =
152 manager_.NextPendingRetransmission();
153 EXPECT_EQ(old_sequence_number, next_retransmission.sequence_number);
154 EXPECT_EQ(TLP_RETRANSMISSION,
155 next_retransmission.transmission_type);
156 manager_.OnRetransmittedPacket(old_sequence_number,
157 new_sequence_number);
158 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(
159 &manager_, new_sequence_number));
162 void RetransmitAndSendPacket(QuicPacketSequenceNumber old_sequence_number,
163 QuicPacketSequenceNumber new_sequence_number) {
164 RetransmitPacket(old_sequence_number, new_sequence_number);
166 EXPECT_CALL(*send_algorithm_,
167 OnPacketSent(_, BytesInFlight(), new_sequence_number,
168 kDefaultLength, HAS_RETRANSMITTABLE_DATA))
169 .WillOnce(Return(true));
170 manager_.OnPacketSent(new_sequence_number,
171 clock_.Now(),
172 kDefaultLength,
173 LOSS_RETRANSMISSION,
174 HAS_RETRANSMITTABLE_DATA);
177 SerializedPacket CreateDataPacket(QuicPacketSequenceNumber sequence_number) {
178 return CreatePacket(sequence_number, true);
181 SerializedPacket CreatePacket(QuicPacketSequenceNumber sequence_number,
182 bool retransmittable) {
183 packets_.push_back(QuicPacket::NewDataPacket(
184 NULL, kDefaultLength, false, PACKET_8BYTE_CONNECTION_ID, false,
185 PACKET_6BYTE_SEQUENCE_NUMBER));
186 return SerializedPacket(
187 sequence_number, PACKET_6BYTE_SEQUENCE_NUMBER,
188 packets_.back(), 0u,
189 retransmittable ? new RetransmittableFrames() : NULL);
192 SerializedPacket CreateFecPacket(QuicPacketSequenceNumber sequence_number) {
193 packets_.push_back(QuicPacket::NewFecPacket(
194 NULL, kDefaultLength, false, PACKET_8BYTE_CONNECTION_ID, false,
195 PACKET_6BYTE_SEQUENCE_NUMBER));
196 return SerializedPacket(sequence_number, PACKET_6BYTE_SEQUENCE_NUMBER,
197 packets_.back(), 0u, NULL);
200 void SendDataPacket(QuicPacketSequenceNumber sequence_number) {
201 EXPECT_CALL(*send_algorithm_,
202 OnPacketSent(_, BytesInFlight(), sequence_number, _, _))
203 .Times(1).WillOnce(Return(true));
204 SerializedPacket packet(CreateDataPacket(sequence_number));
205 manager_.OnSerializedPacket(packet);
206 manager_.OnPacketSent(sequence_number, clock_.Now(),
207 packet.packet->length(), NOT_RETRANSMISSION,
208 HAS_RETRANSMITTABLE_DATA);
211 void SendCryptoPacket(QuicPacketSequenceNumber sequence_number) {
212 EXPECT_CALL(*send_algorithm_,
213 OnPacketSent(_, BytesInFlight(), sequence_number,
214 kDefaultLength, HAS_RETRANSMITTABLE_DATA))
215 .Times(1).WillOnce(Return(true));
216 SerializedPacket packet(CreateDataPacket(sequence_number));
217 packet.retransmittable_frames->AddStreamFrame(
218 new QuicStreamFrame(1, false, 0, IOVector()));
219 packet.retransmittable_frames->set_encryption_level(ENCRYPTION_NONE);
220 manager_.OnSerializedPacket(packet);
221 manager_.OnPacketSent(sequence_number, clock_.ApproximateNow(),
222 packet.packet->length(), NOT_RETRANSMISSION,
223 HAS_RETRANSMITTABLE_DATA);
226 void SendFecPacket(QuicPacketSequenceNumber sequence_number) {
227 EXPECT_CALL(*send_algorithm_,
228 OnPacketSent(_, BytesInFlight(), sequence_number,
229 kDefaultLength, NO_RETRANSMITTABLE_DATA))
230 .Times(1).WillOnce(Return(true));
231 SerializedPacket packet(CreateFecPacket(sequence_number));
232 manager_.OnSerializedPacket(packet);
233 manager_.OnPacketSent(sequence_number, clock_.ApproximateNow(),
234 packet.packet->length(), NOT_RETRANSMISSION,
235 NO_RETRANSMITTABLE_DATA);
238 void SendAckPacket(QuicPacketSequenceNumber sequence_number) {
239 EXPECT_CALL(*send_algorithm_,
240 OnPacketSent(_, BytesInFlight(), sequence_number,
241 kDefaultLength, NO_RETRANSMITTABLE_DATA))
242 .Times(1).WillOnce(Return(false));
243 SerializedPacket packet(CreatePacket(sequence_number, false));
244 manager_.OnSerializedPacket(packet);
245 manager_.OnPacketSent(sequence_number, clock_.Now(),
246 packet.packet->length(), NOT_RETRANSMISSION,
247 NO_RETRANSMITTABLE_DATA);
250 // Based on QuicConnection's WritePendingRetransmissions.
251 void RetransmitNextPacket(
252 QuicPacketSequenceNumber retransmission_sequence_number) {
253 EXPECT_TRUE(manager_.HasPendingRetransmissions());
254 EXPECT_CALL(*send_algorithm_,
255 OnPacketSent(_, _, retransmission_sequence_number,
256 kDefaultLength, HAS_RETRANSMITTABLE_DATA))
257 .Times(1).WillOnce(Return(true));
258 const QuicSentPacketManager::PendingRetransmission pending =
259 manager_.NextPendingRetransmission();
260 manager_.OnRetransmittedPacket(pending.sequence_number,
261 retransmission_sequence_number);
262 manager_.OnPacketSent(retransmission_sequence_number, clock_.Now(),
263 kDefaultLength, pending.transmission_type,
264 HAS_RETRANSMITTABLE_DATA);
267 QuicSentPacketManager manager_;
268 vector<QuicPacket*> packets_;
269 MockClock clock_;
270 QuicConnectionStats stats_;
271 MockSendAlgorithm* send_algorithm_;
272 scoped_ptr<MockNetworkChangeVisitor> network_change_visitor_;
275 TEST_F(QuicSentPacketManagerTest, IsUnacked) {
276 VerifyUnackedPackets(NULL, 0);
278 SerializedPacket serialized_packet(CreateDataPacket(1));
280 manager_.OnSerializedPacket(serialized_packet);
282 QuicPacketSequenceNumber unacked[] = { 1 };
283 VerifyUnackedPackets(unacked, arraysize(unacked));
284 QuicPacketSequenceNumber retransmittable[] = { 1 };
285 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
288 TEST_F(QuicSentPacketManagerTest, IsUnAckedRetransmit) {
289 SendDataPacket(1);
290 RetransmitPacket(1, 2);
292 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(&manager_, 2));
293 QuicPacketSequenceNumber unacked[] = { 1, 2 };
294 VerifyUnackedPackets(unacked, arraysize(unacked));
295 QuicPacketSequenceNumber retransmittable[] = { 2 };
296 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
299 TEST_F(QuicSentPacketManagerTest, RetransmitThenAck) {
300 SendDataPacket(1);
301 RetransmitAndSendPacket(1, 2);
303 // Ack 2 but not 1.
304 ReceivedPacketInfo received_info;
305 received_info.largest_observed = 2;
306 received_info.missing_packets.insert(1);
307 ExpectAck(2);
308 manager_.OnIncomingAck(received_info, clock_.Now());
310 // Packet 1 is unacked, pending, but not retransmittable.
311 QuicPacketSequenceNumber unacked[] = { 1 };
312 VerifyUnackedPackets(unacked, arraysize(unacked));
313 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
314 VerifyRetransmittablePackets(NULL, 0);
317 TEST_F(QuicSentPacketManagerTest, RetransmitThenAckBeforeSend) {
318 SendDataPacket(1);
319 QuicSentPacketManagerPeer::MarkForRetransmission(
320 &manager_, 1, TLP_RETRANSMISSION);
321 EXPECT_TRUE(manager_.HasPendingRetransmissions());
323 // Ack 1.
324 ReceivedPacketInfo received_info;
325 received_info.largest_observed = 1;
326 ExpectAck(1);
327 manager_.OnIncomingAck(received_info, clock_.Now());
329 // There should no longer be a pending retransmission.
330 EXPECT_FALSE(manager_.HasPendingRetransmissions());
332 // No unacked packets remain.
333 VerifyUnackedPackets(NULL, 0);
334 VerifyRetransmittablePackets(NULL, 0);
335 EXPECT_EQ(0u, stats_.packets_spuriously_retransmitted);
338 TEST_F(QuicSentPacketManagerTest, RetransmitThenAckPrevious) {
339 SendDataPacket(1);
340 RetransmitPacket(1, 2);
341 QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
342 clock_.AdvanceTime(rtt);
344 // Ack 1 but not 2.
345 ExpectAck(1);
346 ReceivedPacketInfo received_info;
347 received_info.largest_observed = 1;
348 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
350 // 2 should be unacked, since it may provide an RTT measurement.
351 QuicPacketSequenceNumber unacked[] = { 2 };
352 VerifyUnackedPackets(unacked, arraysize(unacked));
353 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
354 VerifyRetransmittablePackets(NULL, 0);
356 // Verify that the retransmission alarm would not fire,
357 // since there is no retransmittable data outstanding.
358 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
359 EXPECT_EQ(1u, stats_.packets_spuriously_retransmitted);
362 TEST_F(QuicSentPacketManagerTest, RetransmitAndSendThenAckPrevious) {
363 SendDataPacket(1);
364 RetransmitAndSendPacket(1, 2);
365 QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
366 clock_.AdvanceTime(rtt);
368 // Ack 1 but not 2.
369 ExpectAck(1);
370 ReceivedPacketInfo received_info;
371 received_info.largest_observed = 1;
372 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
374 // 2 remains unacked, but no packets have retransmittable data.
375 QuicPacketSequenceNumber unacked[] = { 2 };
376 VerifyUnackedPackets(unacked, arraysize(unacked));
377 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
378 VerifyRetransmittablePackets(NULL, 0);
380 EXPECT_EQ(1u, stats_.packets_spuriously_retransmitted);
383 TEST_F(QuicSentPacketManagerTest, RetransmitThenAckPreviousThenNackRetransmit) {
384 SendDataPacket(1);
385 RetransmitPacket(1, 2);
386 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 2, _, _))
387 .WillOnce(Return(true));
388 manager_.OnPacketSent(2, clock_.ApproximateNow(), kDefaultLength,
389 LOSS_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA);
390 QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
391 clock_.AdvanceTime(rtt);
393 // First, ACK packet 1 which makes packet 2 non-retransmittable.
394 ExpectAck(1);
395 ReceivedPacketInfo received_info;
396 received_info.largest_observed = 1;
397 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
399 SendDataPacket(3);
400 SendDataPacket(4);
401 SendDataPacket(5);
402 clock_.AdvanceTime(rtt);
404 // Next, NACK packet 2 three times.
405 received_info.largest_observed = 3;
406 received_info.missing_packets.insert(2);
407 ExpectAck(3);
408 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
410 received_info.largest_observed = 4;
411 ExpectAck(4);
412 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
414 received_info.largest_observed = 5;
415 ExpectAckAndLoss(true, 5, 2);
416 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
418 // No packets remain unacked.
419 VerifyUnackedPackets(NULL, 0);
420 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
421 VerifyRetransmittablePackets(NULL, 0);
423 // Verify that the retransmission alarm would not fire,
424 // since there is no retransmittable data outstanding.
425 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
428 TEST_F(QuicSentPacketManagerTest, RetransmitTwiceThenAckPreviousBeforeSend) {
429 SendDataPacket(1);
430 RetransmitAndSendPacket(1, 2);
432 // Fire the RTO, which will mark 2 for retransmission (but will not send it).
433 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
434 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
435 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
436 .WillOnce(Return(2 * kDefaultTCPMSS));
437 manager_.OnRetransmissionTimeout();
438 EXPECT_TRUE(manager_.HasPendingRetransmissions());
440 // Ack 1 but not 2, before 2 is able to be sent.
441 // Since 1 has been retransmitted, it has already been lost, and so the
442 // send algorithm is not informed that it has been ACK'd.
443 ReceivedPacketInfo received_info;
444 received_info.largest_observed = 1;
445 ExpectUpdatedRtt(1);
446 EXPECT_CALL(*send_algorithm_, RevertRetransmissionTimeout());
447 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
449 // Since 2 was marked for retransmit, when 1 is acked, 2 is kept for RTT.
450 QuicPacketSequenceNumber unacked[] = { 2 };
451 VerifyUnackedPackets(unacked, arraysize(unacked));
452 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
453 VerifyRetransmittablePackets(NULL, 0);
455 // Verify that the retransmission alarm would not fire,
456 // since there is no retransmittable data outstanding.
457 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
460 TEST_F(QuicSentPacketManagerTest, RetransmitTwiceThenAckFirst) {
461 StrictMock<MockDebugDelegate> debug_delegate;
462 EXPECT_CALL(debug_delegate, OnSpuriousPacketRetransmition(
463 TLP_RETRANSMISSION, kDefaultLength)).Times(2);
464 manager_.set_debug_delegate(&debug_delegate);
466 SendDataPacket(1);
467 RetransmitAndSendPacket(1, 2);
468 RetransmitAndSendPacket(2, 3);
469 QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
470 clock_.AdvanceTime(rtt);
472 // Ack 1 but not 2 or 3.
473 ExpectAck(1);
474 ReceivedPacketInfo received_info;
475 received_info.largest_observed = 1;
476 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
478 // 2 and 3 remain unacked, but no packets have retransmittable data.
479 QuicPacketSequenceNumber unacked[] = { 2, 3 };
480 VerifyUnackedPackets(unacked, arraysize(unacked));
481 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
482 VerifyRetransmittablePackets(NULL, 0);
484 // Ensure packet 2 is lost when 4 is sent and 3 and 4 are acked.
485 SendDataPacket(4);
486 received_info.largest_observed = 4;
487 received_info.missing_packets.insert(2);
488 QuicPacketSequenceNumber acked[] = { 3, 4 };
489 ExpectAcksAndLosses(true, acked, arraysize(acked), NULL, 0);
490 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
492 QuicPacketSequenceNumber unacked2[] = { 2 };
493 VerifyUnackedPackets(unacked2, arraysize(unacked2));
494 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
496 SendDataPacket(5);
497 received_info.largest_observed = 5;
498 ExpectAckAndLoss(true, 5, 2);
499 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
501 VerifyUnackedPackets(NULL, 0);
502 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
503 EXPECT_EQ(2u, stats_.packets_spuriously_retransmitted);
506 TEST_F(QuicSentPacketManagerTest, LoseButDontRetransmitRevivedPacket) {
507 SendDataPacket(1);
508 SendDataPacket(2);
509 SendFecPacket(3);
510 SendDataPacket(4);
512 // Ack 2 and 3, and mark 1 as revived.
513 ReceivedPacketInfo received_info;
514 received_info.largest_observed = 3;
515 received_info.missing_packets.insert(1);
516 received_info.revived_packets.insert(1);
517 QuicPacketSequenceNumber acked[] = { 2, 3 };
518 ExpectAcksAndLosses(true, acked, arraysize(acked), NULL, 0);
519 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
521 EXPECT_FALSE(manager_.HasPendingRetransmissions());
522 QuicPacketSequenceNumber unacked[] = { 1, 4 };
523 VerifyUnackedPackets(unacked, arraysize(unacked));
524 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
525 QuicPacketSequenceNumber retransmittable[] = { 4 };
526 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
528 // Ack the 4th packet and expect the 1st to be considered lost.
529 received_info.largest_observed = 4;
530 ExpectAckAndLoss(true, 4, 1);
531 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
533 EXPECT_FALSE(manager_.HasPendingRetransmissions());
534 VerifyRetransmittablePackets(NULL, 0);
537 TEST_F(QuicSentPacketManagerTest, MarkLostThenReviveAndDontRetransmitPacket) {
538 SendDataPacket(1);
539 SendDataPacket(2);
540 SendDataPacket(3);
541 SendDataPacket(4);
542 SendFecPacket(5);
544 // Ack 2, 3, and 4, and expect the 1st to be considered lost.
545 ReceivedPacketInfo received_info;
546 received_info.largest_observed = 4;
547 received_info.missing_packets.insert(1);
548 QuicPacketSequenceNumber acked[] = { 2, 3, 4 };
549 QuicPacketSequenceNumber lost[] = { 1 };
550 ExpectAcksAndLosses(true, acked, arraysize(acked), lost, arraysize(lost));
551 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
553 EXPECT_TRUE(manager_.HasPendingRetransmissions());
554 QuicPacketSequenceNumber unacked[] = { 1, 5 };
555 VerifyUnackedPackets(unacked, arraysize(unacked));
556 QuicPacketSequenceNumber retransmittable[] = { 1 };
557 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
559 // Ack 5th packet (FEC) and revive 1st packet. 1st packet should now be
560 // removed from pending retransmissions map.
561 received_info.largest_observed = 5;
562 received_info.revived_packets.insert(1);
563 ExpectAck(5);
564 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
566 EXPECT_FALSE(manager_.HasPendingRetransmissions());
567 VerifyRetransmittablePackets(NULL, 0);
570 TEST_F(QuicSentPacketManagerTest, TruncatedAck) {
571 SendDataPacket(1);
572 RetransmitAndSendPacket(1, 2);
573 RetransmitAndSendPacket(2, 3);
574 RetransmitAndSendPacket(3, 4);
575 RetransmitAndSendPacket(4, 5);
577 // Truncated ack with 4 NACKs, so the first packet is lost.
578 ReceivedPacketInfo received_info;
579 received_info.largest_observed = 4;
580 received_info.missing_packets.insert(1);
581 received_info.missing_packets.insert(2);
582 received_info.missing_packets.insert(3);
583 received_info.missing_packets.insert(4);
584 received_info.is_truncated = true;
586 QuicPacketSequenceNumber lost[] = { 1 };
587 ExpectAcksAndLosses(true, NULL, 0, lost, arraysize(lost));
588 manager_.OnIncomingAck(received_info, clock_.Now());
590 // High water mark will be raised.
591 QuicPacketSequenceNumber unacked[] = { 2, 3, 4, 5 };
592 VerifyUnackedPackets(unacked, arraysize(unacked));
593 QuicPacketSequenceNumber retransmittable[] = { 5 };
594 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
597 TEST_F(QuicSentPacketManagerTest, AckPreviousTransmissionThenTruncatedAck) {
598 SendDataPacket(1);
599 RetransmitAndSendPacket(1, 2);
600 RetransmitAndSendPacket(2, 3);
601 RetransmitAndSendPacket(3, 4);
602 manager_.OnSerializedPacket(CreateDataPacket(5));
603 manager_.OnSerializedPacket(CreateDataPacket(6));
604 manager_.OnSerializedPacket(CreateDataPacket(7));
605 manager_.OnSerializedPacket(CreateDataPacket(8));
606 manager_.OnSerializedPacket(CreateDataPacket(9));
608 // Ack previous transmission
610 ReceivedPacketInfo received_info;
611 received_info.largest_observed = 2;
612 received_info.missing_packets.insert(1);
613 ExpectAck(2);
614 manager_.OnIncomingAck(received_info, clock_.Now());
615 EXPECT_TRUE(manager_.IsUnacked(4));
618 // Truncated ack with 4 NACKs
620 ReceivedPacketInfo received_info;
621 received_info.largest_observed = 6;
622 received_info.missing_packets.insert(3);
623 received_info.missing_packets.insert(4);
624 received_info.missing_packets.insert(5);
625 received_info.missing_packets.insert(6);
626 received_info.is_truncated = true;
627 ExpectAckAndLoss(false, 1, 3);
628 manager_.OnIncomingAck(received_info, clock_.Now());
631 // High water mark will be raised.
632 QuicPacketSequenceNumber unacked[] = { 4, 5, 6, 7, 8, 9 };
633 VerifyUnackedPackets(unacked, arraysize(unacked));
634 QuicPacketSequenceNumber retransmittable[] = { 5, 6, 7, 8, 9 };
635 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
638 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedSentPacket) {
639 EXPECT_EQ(0u, manager_.GetLeastUnackedSentPacket());
642 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedSentPacketUnacked) {
643 SerializedPacket serialized_packet(CreateDataPacket(1));
645 manager_.OnSerializedPacket(serialized_packet);
646 EXPECT_EQ(1u, manager_.GetLeastUnackedSentPacket());
649 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedSentPacketUnackedFec) {
650 SerializedPacket serialized_packet(CreateFecPacket(1));
652 manager_.OnSerializedPacket(serialized_packet);
653 EXPECT_EQ(1u, manager_.GetLeastUnackedSentPacket());
656 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedPacketAndDiscard) {
657 VerifyUnackedPackets(NULL, 0);
659 SerializedPacket serialized_packet(CreateFecPacket(1));
660 manager_.OnSerializedPacket(serialized_packet);
661 EXPECT_EQ(1u, manager_.GetLeastUnackedSentPacket());
663 SerializedPacket serialized_packet2(CreateFecPacket(2));
664 manager_.OnSerializedPacket(serialized_packet2);
665 EXPECT_EQ(1u, manager_.GetLeastUnackedSentPacket());
667 SerializedPacket serialized_packet3(CreateFecPacket(3));
668 manager_.OnSerializedPacket(serialized_packet3);
669 EXPECT_EQ(1u, manager_.GetLeastUnackedSentPacket());
671 QuicPacketSequenceNumber unacked[] = { 1, 2, 3 };
672 VerifyUnackedPackets(unacked, arraysize(unacked));
673 VerifyRetransmittablePackets(NULL, 0);
675 // Ack 2, which has never been sent, so there's no rtt update.
676 ReceivedPacketInfo received_info;
677 received_info.largest_observed = 2;
678 manager_.OnIncomingAck(received_info, clock_.Now());
680 EXPECT_EQ(3u, manager_.GetLeastUnackedSentPacket());
683 TEST_F(QuicSentPacketManagerTest, GetSentTime) {
684 VerifyUnackedPackets(NULL, 0);
686 SerializedPacket serialized_packet(CreateFecPacket(1));
687 manager_.OnSerializedPacket(serialized_packet);
688 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 1, _, _))
689 .Times(1).WillOnce(Return(true));
690 manager_.OnPacketSent(1, QuicTime::Zero(), kDefaultLength, NOT_RETRANSMISSION,
691 NO_RETRANSMITTABLE_DATA);
693 SerializedPacket serialized_packet2(CreateFecPacket(2));
694 QuicTime sent_time = QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(1));
695 manager_.OnSerializedPacket(serialized_packet2);
696 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 2, _, _))
697 .Times(1).WillOnce(Return(true));
698 manager_.OnPacketSent(2, sent_time, kDefaultLength, NOT_RETRANSMISSION,
699 NO_RETRANSMITTABLE_DATA);
701 QuicPacketSequenceNumber unacked[] = { 1, 2 };
702 VerifyUnackedPackets(unacked, arraysize(unacked));
703 VerifyRetransmittablePackets(NULL, 0);
705 EXPECT_TRUE(manager_.HasUnackedPackets());
706 EXPECT_EQ(QuicTime::Zero(),
707 QuicSentPacketManagerPeer::GetSentTime(&manager_, 1));
708 EXPECT_EQ(sent_time, QuicSentPacketManagerPeer::GetSentTime(&manager_, 2));
711 TEST_F(QuicSentPacketManagerTest, AckAckAndUpdateRtt) {
712 SendDataPacket(1);
713 SendAckPacket(2);
715 // Now ack the ack and expect an RTT update.
716 ReceivedPacketInfo received_info;
717 received_info.largest_observed = 2;
718 received_info.delta_time_largest_observed =
719 QuicTime::Delta::FromMilliseconds(5);
721 ExpectAck(1);
722 manager_.OnIncomingAck(received_info, clock_.Now());
724 SendAckPacket(3);
726 // Now ack the ack and expect only an RTT update.
727 received_info.largest_observed = 3;
728 ExpectUpdatedRtt(3);
729 manager_.OnIncomingAck(received_info, clock_.Now());
732 TEST_F(QuicSentPacketManagerTest, Rtt) {
733 QuicPacketSequenceNumber sequence_number = 1;
734 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(15);
735 SendDataPacket(sequence_number);
736 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
738 ExpectAck(sequence_number);
739 ReceivedPacketInfo received_info;
740 received_info.largest_observed = sequence_number;
741 received_info.delta_time_largest_observed =
742 QuicTime::Delta::FromMilliseconds(5);
743 manager_.OnIncomingAck(received_info, clock_.Now());
744 EXPECT_EQ(expected_rtt,
745 QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
748 TEST_F(QuicSentPacketManagerTest, RttWithInvalidDelta) {
749 // Expect that the RTT is equal to the local time elapsed, since the
750 // delta_time_largest_observed is larger than the local time elapsed
751 // and is hence invalid.
752 QuicPacketSequenceNumber sequence_number = 1;
753 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
754 SendDataPacket(sequence_number);
755 clock_.AdvanceTime(expected_rtt);
757 ExpectAck(sequence_number);
758 ReceivedPacketInfo received_info;
759 received_info.largest_observed = sequence_number;
760 received_info.delta_time_largest_observed =
761 QuicTime::Delta::FromMilliseconds(11);
762 manager_.OnIncomingAck(received_info, clock_.Now());
763 EXPECT_EQ(expected_rtt,
764 QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
767 TEST_F(QuicSentPacketManagerTest, RttWithInfiniteDelta) {
768 // Expect that the RTT is equal to the local time elapsed, since the
769 // delta_time_largest_observed is infinite, and is hence invalid.
770 QuicPacketSequenceNumber sequence_number = 1;
771 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
772 SendDataPacket(sequence_number);
773 clock_.AdvanceTime(expected_rtt);
775 ExpectAck(sequence_number);
776 ReceivedPacketInfo received_info;
777 received_info.largest_observed = sequence_number;
778 received_info.delta_time_largest_observed = QuicTime::Delta::Infinite();
779 manager_.OnIncomingAck(received_info, clock_.Now());
780 EXPECT_EQ(expected_rtt,
781 QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
784 TEST_F(QuicSentPacketManagerTest, RttZeroDelta) {
785 // Expect that the RTT is the time between send and receive since the
786 // delta_time_largest_observed is zero.
787 QuicPacketSequenceNumber sequence_number = 1;
788 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
789 SendDataPacket(sequence_number);
790 clock_.AdvanceTime(expected_rtt);
792 ExpectAck(sequence_number);
793 ReceivedPacketInfo received_info;
794 received_info.largest_observed = sequence_number;
795 received_info.delta_time_largest_observed = QuicTime::Delta::Zero();
796 manager_.OnIncomingAck(received_info, clock_.Now());
797 EXPECT_EQ(expected_rtt,
798 QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
801 TEST_F(QuicSentPacketManagerTest, TailLossProbeTimeout) {
802 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
804 // Send 1 packet.
805 QuicPacketSequenceNumber sequence_number = 1;
806 SendDataPacket(sequence_number);
808 // The first tail loss probe retransmits 1 packet.
809 manager_.OnRetransmissionTimeout();
810 EXPECT_EQ(QuicTime::Delta::Zero(),
811 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
812 EXPECT_FALSE(manager_.HasPendingRetransmissions());
813 manager_.MaybeRetransmitTailLossProbe();
814 EXPECT_TRUE(manager_.HasPendingRetransmissions());
815 RetransmitNextPacket(2);
816 EXPECT_FALSE(manager_.HasPendingRetransmissions());
818 // The second tail loss probe retransmits 1 packet.
819 manager_.OnRetransmissionTimeout();
820 EXPECT_EQ(QuicTime::Delta::Zero(),
821 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
822 EXPECT_FALSE(manager_.HasPendingRetransmissions());
823 manager_.MaybeRetransmitTailLossProbe();
824 EXPECT_TRUE(manager_.HasPendingRetransmissions());
825 RetransmitNextPacket(3);
826 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
827 QuicTime::Delta::Infinite()));
828 EXPECT_EQ(QuicTime::Delta::Infinite(),
829 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
830 EXPECT_FALSE(manager_.HasPendingRetransmissions());
832 // Ack the third and ensure the first two are still pending.
833 ExpectAck(3);
834 ReceivedPacketInfo received_info;
835 received_info.largest_observed = 3;
836 received_info.missing_packets.insert(1);
837 received_info.missing_packets.insert(2);
838 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
840 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
842 // Acking two more packets will lose both of them due to nacks.
843 received_info.largest_observed = 5;
844 QuicPacketSequenceNumber lost[] = { 1, 2 };
845 ExpectAcksAndLosses(false, NULL, 0, lost, arraysize(lost));
846 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
848 EXPECT_FALSE(manager_.HasPendingRetransmissions());
849 EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
850 EXPECT_EQ(2u, stats_.tlp_count);
851 EXPECT_EQ(0u, stats_.rto_count);
854 TEST_F(QuicSentPacketManagerTest, TailLossProbeThenRTO) {
855 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
857 // Send 100 packets.
858 const size_t kNumSentPackets = 100;
859 for (size_t i = 1; i <= kNumSentPackets; ++i) {
860 SendDataPacket(i);
863 // The first tail loss probe retransmits 1 packet.
864 manager_.OnRetransmissionTimeout();
865 EXPECT_EQ(QuicTime::Delta::Zero(),
866 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
867 EXPECT_FALSE(manager_.HasPendingRetransmissions());
868 manager_.MaybeRetransmitTailLossProbe();
869 EXPECT_TRUE(manager_.HasPendingRetransmissions());
870 RetransmitNextPacket(101);
871 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
872 QuicTime::Delta::Infinite()));
873 EXPECT_EQ(QuicTime::Delta::Infinite(),
874 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
875 EXPECT_FALSE(manager_.HasPendingRetransmissions());
877 // The second tail loss probe retransmits 1 packet.
878 manager_.OnRetransmissionTimeout();
879 EXPECT_EQ(QuicTime::Delta::Zero(),
880 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
881 EXPECT_FALSE(manager_.HasPendingRetransmissions());
882 EXPECT_TRUE(manager_.MaybeRetransmitTailLossProbe());
883 EXPECT_TRUE(manager_.HasPendingRetransmissions());
884 RetransmitNextPacket(102);
885 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
886 QuicTime::Delta::Infinite()));
887 EXPECT_EQ(QuicTime::Delta::Infinite(),
888 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
890 // Advance the time enough to ensure all packets are RTO'd.
891 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
893 // The final RTO abandons all of them.
894 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
895 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
896 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
897 .WillOnce(Return(2 * kDefaultTCPMSS));
898 manager_.OnRetransmissionTimeout();
899 EXPECT_TRUE(manager_.HasPendingRetransmissions());
900 EXPECT_EQ(2u, stats_.tlp_count);
901 EXPECT_EQ(1u, stats_.rto_count);
904 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeout) {
905 // Send 2 crypto packets and 3 data packets.
906 const size_t kNumSentCryptoPackets = 2;
907 for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
908 SendCryptoPacket(i);
910 const size_t kNumSentDataPackets = 3;
911 for (size_t i = 1; i <= kNumSentDataPackets; ++i) {
912 SendDataPacket(kNumSentCryptoPackets + i);
914 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
916 // The first retransmits 2 packets.
917 manager_.OnRetransmissionTimeout();
918 RetransmitNextPacket(6);
919 RetransmitNextPacket(7);
920 EXPECT_FALSE(manager_.HasPendingRetransmissions());
921 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
923 // The second retransmits 2 packets.
924 manager_.OnRetransmissionTimeout();
925 RetransmitNextPacket(8);
926 RetransmitNextPacket(9);
927 EXPECT_FALSE(manager_.HasPendingRetransmissions());
928 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
930 // Now ack the two crypto packets and the speculatively encrypted request,
931 // and ensure the first four crypto packets get abandoned, but not lost.
932 QuicPacketSequenceNumber acked[] = { 3, 4, 5, 8, 9 };
933 ExpectAcksAndLosses(true, acked, arraysize(acked), NULL, 0);
934 ReceivedPacketInfo received_info;
935 received_info.largest_observed = 9;
936 received_info.missing_packets.insert(1);
937 received_info.missing_packets.insert(2);
938 received_info.missing_packets.insert(6);
939 received_info.missing_packets.insert(7);
940 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
942 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
945 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeoutVersionNegotiation) {
946 // Send 2 crypto packets and 3 data packets.
947 const size_t kNumSentCryptoPackets = 2;
948 for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
949 SendCryptoPacket(i);
951 const size_t kNumSentDataPackets = 3;
952 for (size_t i = 1; i <= kNumSentDataPackets; ++i) {
953 SendDataPacket(kNumSentCryptoPackets + i);
955 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
957 // The first retransmission timeout retransmits 2 crypto packets.
958 manager_.OnRetransmissionTimeout();
959 RetransmitNextPacket(6);
960 RetransmitNextPacket(7);
961 EXPECT_FALSE(manager_.HasPendingRetransmissions());
962 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
964 // Now act like a version negotiation packet arrived, which would cause all
965 // unacked packets to be retransmitted.
966 manager_.RetransmitUnackedPackets(ALL_PACKETS);
968 // Ensure the first two pending packets are the crypto retransmits.
969 ASSERT_TRUE(manager_.HasPendingRetransmissions());
970 EXPECT_EQ(6u, manager_.NextPendingRetransmission().sequence_number);
971 RetransmitNextPacket(8);
972 EXPECT_EQ(7u, manager_.NextPendingRetransmission().sequence_number);
973 RetransmitNextPacket(9);
975 EXPECT_TRUE(manager_.HasPendingRetransmissions());
978 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeSpuriousRetransmission) {
979 // Send 1 crypto packet.
980 SendCryptoPacket(1);
981 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
983 // Retransmit the crypto packet as 2.
984 manager_.OnRetransmissionTimeout();
985 RetransmitNextPacket(2);
987 // Retransmit the crypto packet as 3.
988 manager_.OnRetransmissionTimeout();
989 RetransmitNextPacket(3);
991 // Now ack the second crypto packet, and ensure the first gets removed, but
992 // the third does not.
993 ExpectUpdatedRtt(2);
994 ReceivedPacketInfo received_info;
995 received_info.largest_observed = 2;
996 received_info.missing_packets.insert(1);
997 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
999 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1000 QuicPacketSequenceNumber unacked[] = { 3 };
1001 VerifyUnackedPackets(unacked, arraysize(unacked));
1004 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeoutUnsentDataPacket) {
1005 // Send 2 crypto packets and serialize 1 data packet.
1006 const size_t kNumSentCryptoPackets = 2;
1007 for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
1008 SendCryptoPacket(i);
1010 SerializedPacket packet(CreateDataPacket(3));
1011 manager_.OnSerializedPacket(packet);
1012 EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1014 // Retransmit 2 crypto packets, but not the serialized packet.
1015 manager_.OnRetransmissionTimeout();
1016 RetransmitNextPacket(6);
1017 RetransmitNextPacket(7);
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_PACKETS);
1035 QuicPacketSequenceNumber 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 QuicPacketSequenceNumber unacked[] = { 1, 2, 3};
1060 VerifyUnackedPackets(unacked, arraysize(unacked));
1061 VerifyRetransmittablePackets(NULL, 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 ReceivedPacketInfo received_info;
1068 received_info.largest_observed = 3;
1069 received_info.missing_packets.insert(1);
1070 received_info.missing_packets.insert(2);
1071 ExpectUpdatedRtt(3);
1072 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
1073 VerifyUnackedPackets(NULL, 0);
1074 VerifyRetransmittablePackets(NULL, 0);
1077 TEST_F(QuicSentPacketManagerTest, TailLossProbeTimeoutUnsentDataPacket) {
1078 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
1079 // Serialize two data packets and send the latter.
1080 SerializedPacket packet(CreateDataPacket(1));
1081 manager_.OnSerializedPacket(packet);
1082 SendDataPacket(2);
1083 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1084 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
1086 // Retransmit 1 unacked packets, but not the first serialized packet.
1087 manager_.OnRetransmissionTimeout();
1088 EXPECT_EQ(QuicTime::Delta::Zero(),
1089 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1090 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1091 manager_.MaybeRetransmitTailLossProbe();
1092 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1093 RetransmitNextPacket(3);
1094 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
1095 QuicTime::Delta::Infinite()));
1096 EXPECT_EQ(QuicTime::Delta::Infinite(),
1097 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1098 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1099 EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
1100 EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
1103 TEST_F(QuicSentPacketManagerTest, ResetRecentMinRTTWithEmptyWindow) {
1104 QuicTime::Delta min_rtt = QuicTime::Delta::FromMilliseconds(50);
1105 QuicSentPacketManagerPeer::GetRttStats(&manager_)->UpdateRtt(
1106 min_rtt, QuicTime::Delta::Zero(), QuicTime::Zero());
1107 EXPECT_EQ(min_rtt,
1108 QuicSentPacketManagerPeer::GetRttStats(&manager_)->min_rtt());
1109 EXPECT_EQ(min_rtt,
1110 QuicSentPacketManagerPeer::GetRttStats(
1111 &manager_)->recent_min_rtt());
1113 // Send two packets with no prior bytes in flight.
1114 SendDataPacket(1);
1115 SendDataPacket(2);
1117 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
1118 // Ack two packets with 100ms RTT observations.
1119 ReceivedPacketInfo received_info;
1120 received_info.delta_time_largest_observed = QuicTime::Delta::Zero();
1121 received_info.largest_observed = 1;
1122 ExpectAck(1);
1123 manager_.OnIncomingAck(received_info, clock_.Now());
1125 // First ack does not change recent min rtt.
1126 EXPECT_EQ(min_rtt,
1127 QuicSentPacketManagerPeer::GetRttStats(
1128 &manager_)->recent_min_rtt());
1130 received_info.largest_observed = 2;
1131 ExpectAck(2);
1132 manager_.OnIncomingAck(received_info, clock_.Now());
1134 EXPECT_EQ(min_rtt,
1135 QuicSentPacketManagerPeer::GetRttStats(&manager_)->min_rtt());
1136 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(100),
1137 QuicSentPacketManagerPeer::GetRttStats(
1138 &manager_)->recent_min_rtt());
1141 TEST_F(QuicSentPacketManagerTest, RetransmissionTimeout) {
1142 // Send 100 packets and then ensure all are abandoned when the RTO fires.
1143 const size_t kNumSentPackets = 100;
1144 for (size_t i = 1; i <= kNumSentPackets; ++i) {
1145 SendDataPacket(i);
1148 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1149 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
1150 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1151 .WillOnce(Return(2 * kDefaultTCPMSS));
1152 EXPECT_FALSE(manager_.MaybeRetransmitTailLossProbe());
1153 manager_.OnRetransmissionTimeout();
1156 TEST_F(QuicSentPacketManagerTest, GetTransmissionTime) {
1157 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
1160 TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeCryptoHandshake) {
1161 SendCryptoPacket(1);
1163 // Check the min.
1164 QuicSentPacketManagerPeer::GetRttStats(&manager_)->set_initial_rtt_us(
1165 1 * base::Time::kMicrosecondsPerMillisecond);
1166 EXPECT_EQ(clock_.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1167 manager_.GetRetransmissionTime());
1169 // Test with a standard smoothed RTT.
1170 QuicSentPacketManagerPeer::GetRttStats(&manager_)->set_initial_rtt_us(
1171 100 * base::Time::kMicrosecondsPerMillisecond);
1173 QuicTime::Delta srtt = manager_.GetRttStats()->SmoothedRtt();
1174 QuicTime expected_time = clock_.Now().Add(srtt.Multiply(1.5));
1175 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1177 // Retransmit the packet by invoking the retransmission timeout.
1178 clock_.AdvanceTime(srtt.Multiply(1.5));
1179 manager_.OnRetransmissionTimeout();
1180 RetransmitNextPacket(2);
1182 // The retransmission time should now be twice as far in the future.
1183 expected_time = clock_.Now().Add(srtt.Multiply(2).Multiply(1.5));
1184 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1187 TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeTailLossProbe) {
1188 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
1189 SendDataPacket(1);
1190 SendDataPacket(2);
1192 // Check the min.
1193 QuicSentPacketManagerPeer::GetRttStats(&manager_)->set_initial_rtt_us(
1194 1 * base::Time::kMicrosecondsPerMillisecond);
1195 EXPECT_EQ(clock_.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
1196 manager_.GetRetransmissionTime());
1198 // Test with a standard smoothed RTT.
1199 QuicSentPacketManagerPeer::GetRttStats(&manager_)->set_initial_rtt_us(
1200 100 * base::Time::kMicrosecondsPerMillisecond);
1201 QuicTime::Delta srtt = manager_.GetRttStats()->SmoothedRtt();
1202 QuicTime::Delta expected_tlp_delay = srtt.Multiply(2);
1203 QuicTime expected_time = clock_.Now().Add(expected_tlp_delay);
1204 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1206 // Retransmit the packet by invoking the retransmission timeout.
1207 clock_.AdvanceTime(expected_tlp_delay);
1208 manager_.OnRetransmissionTimeout();
1209 EXPECT_EQ(QuicTime::Delta::Zero(),
1210 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1211 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1212 EXPECT_TRUE(manager_.MaybeRetransmitTailLossProbe());
1213 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1214 RetransmitNextPacket(3);
1215 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
1216 QuicTime::Delta::Infinite()));
1217 EXPECT_EQ(QuicTime::Delta::Infinite(),
1218 manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
1219 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1221 expected_time = clock_.Now().Add(expected_tlp_delay);
1222 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1225 TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeRTO) {
1226 QuicSentPacketManagerPeer::GetRttStats(&manager_)->UpdateRtt(
1227 QuicTime::Delta::FromMilliseconds(100),
1228 QuicTime::Delta::Zero(),
1229 QuicTime::Zero());
1231 SendDataPacket(1);
1232 SendDataPacket(2);
1233 SendDataPacket(3);
1234 SendDataPacket(4);
1236 QuicTime::Delta expected_rto_delay = QuicTime::Delta::FromMilliseconds(500);
1237 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
1238 .WillRepeatedly(Return(expected_rto_delay));
1239 QuicTime expected_time = clock_.Now().Add(expected_rto_delay);
1240 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1242 // Retransmit the packet by invoking the retransmission timeout.
1243 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
1244 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1245 .WillOnce(Return(2 * kDefaultTCPMSS));
1246 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1247 clock_.AdvanceTime(expected_rto_delay);
1248 manager_.OnRetransmissionTimeout();
1249 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1250 RetransmitNextPacket(5);
1251 RetransmitNextPacket(6);
1252 EXPECT_EQ(2 * kDefaultLength,
1253 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1254 EXPECT_TRUE(manager_.HasPendingRetransmissions());
1256 // The delay should double the second time.
1257 expected_time = clock_.Now().Add(expected_rto_delay).Add(expected_rto_delay);
1258 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1260 // Ack a packet and ensure the RTO goes back to the original value.
1261 ReceivedPacketInfo received_info;
1262 received_info.largest_observed = 2;
1263 received_info.missing_packets.insert(1);
1264 ExpectUpdatedRtt(2);
1265 EXPECT_CALL(*send_algorithm_, RevertRetransmissionTimeout());
1266 manager_.OnIncomingAck(received_info, clock_.ApproximateNow());
1267 EXPECT_FALSE(manager_.HasPendingRetransmissions());
1268 EXPECT_EQ(4 * kDefaultLength,
1269 QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
1271 // Wait 2RTTs from now for the RTO, since it's the max of the RTO time
1272 // and the TLP time. In production, there would always be two TLP's first.
1273 expected_time = clock_.Now().Add(QuicTime::Delta::FromMilliseconds(200));
1274 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1277 TEST_F(QuicSentPacketManagerTest, GetTransmissionDelayMin) {
1278 SendDataPacket(1);
1279 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
1280 .WillRepeatedly(Return(QuicTime::Delta::FromMilliseconds(1)));
1281 QuicTime::Delta delay = QuicTime::Delta::FromMilliseconds(200);
1283 // If the delay is smaller than the min, ensure it exponentially backs off
1284 // from the min.
1285 for (int i = 0; i < 5; ++i) {
1286 EXPECT_EQ(delay,
1287 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
1288 delay = delay.Add(delay);
1289 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
1290 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1291 .WillOnce(Return(2 * kDefaultTCPMSS));
1292 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1293 manager_.OnRetransmissionTimeout();
1294 RetransmitNextPacket(i + 2);
1298 TEST_F(QuicSentPacketManagerTest, GetTransmissionDelayMax) {
1299 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
1300 .WillOnce(Return(QuicTime::Delta::FromSeconds(500)));
1302 EXPECT_EQ(QuicTime::Delta::FromSeconds(60),
1303 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
1306 TEST_F(QuicSentPacketManagerTest, GetTransmissionDelay) {
1307 SendDataPacket(1);
1308 QuicTime::Delta delay = QuicTime::Delta::FromMilliseconds(500);
1309 EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
1310 .WillRepeatedly(Return(delay));
1312 // Delay should back off exponentially.
1313 for (int i = 0; i < 5; ++i) {
1314 EXPECT_EQ(delay,
1315 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
1316 delay = delay.Add(delay);
1317 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
1318 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1319 .WillOnce(Return(2 * kDefaultTCPMSS));
1320 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1321 manager_.OnRetransmissionTimeout();
1322 RetransmitNextPacket(i + 2);
1326 TEST_F(QuicSentPacketManagerTest, GetLossDelay) {
1327 MockLossAlgorithm* loss_algorithm = new MockLossAlgorithm();
1328 QuicSentPacketManagerPeer::SetLossAlgorithm(&manager_, loss_algorithm);
1330 EXPECT_CALL(*loss_algorithm, GetLossTimeout())
1331 .WillRepeatedly(Return(QuicTime::Zero()));
1332 SendDataPacket(1);
1333 SendDataPacket(2);
1335 // Handle an ack which causes the loss algorithm to be evaluated and
1336 // set the loss timeout.
1337 ExpectAck(2);
1338 EXPECT_CALL(*loss_algorithm, DetectLostPackets(_, _, _, _))
1339 .WillOnce(Return(SequenceNumberSet()));
1340 ReceivedPacketInfo received_info;
1341 received_info.largest_observed = 2;
1342 received_info.missing_packets.insert(1);
1343 manager_.OnIncomingAck(received_info, clock_.Now());
1345 QuicTime timeout(clock_.Now().Add(QuicTime::Delta::FromMilliseconds(10)));
1346 EXPECT_CALL(*loss_algorithm, GetLossTimeout())
1347 .WillRepeatedly(Return(timeout));
1348 EXPECT_EQ(timeout, manager_.GetRetransmissionTime());
1350 // Fire the retransmission timeout and ensure the loss detection algorithm
1351 // is invoked.
1352 EXPECT_CALL(*loss_algorithm, DetectLostPackets(_, _, _, _))
1353 .WillOnce(Return(SequenceNumberSet()));
1354 manager_.OnRetransmissionTimeout();
1357 TEST_F(QuicSentPacketManagerTest, NegotiateTimeLossDetection) {
1358 EXPECT_EQ(kNack,
1359 QuicSentPacketManagerPeer::GetLossAlgorithm(
1360 &manager_)->GetLossDetectionType());
1362 QuicConfig config;
1363 QuicConfigPeer::SetReceivedLossDetection(&config, kTIME);
1364 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1365 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
1366 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1367 .WillOnce(Return(100 * kDefaultTCPMSS));
1368 manager_.SetFromConfig(config);
1370 EXPECT_EQ(kTime,
1371 QuicSentPacketManagerPeer::GetLossAlgorithm(
1372 &manager_)->GetLossDetectionType());
1375 TEST_F(QuicSentPacketManagerTest, NegotiateTimeLossDetectionFromOptions) {
1376 EXPECT_EQ(kNack,
1377 QuicSentPacketManagerPeer::GetLossAlgorithm(
1378 &manager_)->GetLossDetectionType());
1380 QuicConfig config;
1381 QuicTagVector options;
1382 options.push_back(kTIME);
1383 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1384 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1385 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
1386 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1387 .WillOnce(Return(100 * kDefaultTCPMSS));
1388 manager_.SetFromConfig(config);
1390 EXPECT_EQ(kTime,
1391 QuicSentPacketManagerPeer::GetLossAlgorithm(
1392 &manager_)->GetLossDetectionType());
1395 TEST_F(QuicSentPacketManagerTest, NegotiatePacingFromOptions) {
1396 ValueRestore<bool> old_flag(&FLAGS_enable_quic_pacing, true);
1397 EXPECT_FALSE(manager_.using_pacing());
1399 QuicConfig config;
1400 QuicTagVector options;
1401 options.push_back(kPACE);
1402 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1403 EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
1404 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1405 .WillOnce(Return(100 * kDefaultTCPMSS));
1406 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1407 manager_.SetFromConfig(config);
1409 EXPECT_TRUE(manager_.using_pacing());
1412 } // namespace
1413 } // namespace test
1414 } // namespace net