OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include "net/quic/congestion_control/tcp_loss_algorithm.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" |
| 11 #include "net/quic/quic_unacked_packet_map.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace net { |
| 15 namespace test { |
| 16 namespace { |
| 17 |
| 18 class TcpLossAlgorithmTest : public ::testing::Test { |
| 19 protected: |
| 20 TcpLossAlgorithmTest() |
| 21 : unacked_packets_(true), |
| 22 srtt_(QuicTime::Delta::FromMilliseconds(10)) { } |
| 23 |
| 24 void SendDataPacket(QuicPacketSequenceNumber sequence_number) { |
| 25 SerializedPacket packet(sequence_number, PACKET_1BYTE_SEQUENCE_NUMBER, |
| 26 NULL, 0, new RetransmittableFrames()); |
| 27 unacked_packets_.AddPacket(packet); |
| 28 unacked_packets_.SetPending(sequence_number, QuicTime::Zero(), 1000); |
| 29 } |
| 30 |
| 31 void VerifyLosses(QuicPacketSequenceNumber largest_observed, |
| 32 QuicPacketSequenceNumber* losses_expected, |
| 33 size_t num_losses) { |
| 34 SequenceNumberSet lost_packets = |
| 35 loss_algorithm_.DetectLostPackets( |
| 36 unacked_packets_, QuicTime::Zero(), largest_observed, srtt_); |
| 37 EXPECT_EQ(num_losses, lost_packets.size()); |
| 38 for (size_t i = 0; i < num_losses; ++i) { |
| 39 EXPECT_TRUE(ContainsKey(lost_packets, losses_expected[i])); |
| 40 } |
| 41 } |
| 42 |
| 43 QuicUnackedPacketMap unacked_packets_; |
| 44 TCPLossAlgorithm loss_algorithm_; |
| 45 QuicTime::Delta srtt_; |
| 46 }; |
| 47 |
| 48 TEST_F(TcpLossAlgorithmTest, NackRetransmit1Packet) { |
| 49 const size_t kNumSentPackets = 5; |
| 50 // Transmit 5 packets. |
| 51 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
| 52 SendDataPacket(i); |
| 53 } |
| 54 // No loss on one ack. |
| 55 unacked_packets_.SetNotPending(2); |
| 56 unacked_packets_.NackPacket(1, 1); |
| 57 VerifyLosses(2, NULL, 0); |
| 58 // No loss on two acks. |
| 59 unacked_packets_.SetNotPending(3); |
| 60 unacked_packets_.NackPacket(1, 2); |
| 61 VerifyLosses(3, NULL, 0); |
| 62 // Loss on three acks. |
| 63 unacked_packets_.SetNotPending(4); |
| 64 unacked_packets_.NackPacket(1, 3); |
| 65 QuicPacketSequenceNumber lost[] = { 1 }; |
| 66 VerifyLosses(4, lost, arraysize(lost)); |
| 67 } |
| 68 |
| 69 // A stretch ack is an ack that covers more than 1 packet of previously |
| 70 // unacknowledged data. |
| 71 TEST_F(TcpLossAlgorithmTest, NackRetransmit1PacketWith1StretchAck) { |
| 72 const size_t kNumSentPackets = 10; |
| 73 // Transmit 10 packets. |
| 74 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
| 75 SendDataPacket(i); |
| 76 } |
| 77 |
| 78 // Nack the first packet 3 times in a single StretchAck. |
| 79 unacked_packets_.NackPacket(1, 3); |
| 80 unacked_packets_.SetNotPending(2); |
| 81 unacked_packets_.SetNotPending(3); |
| 82 unacked_packets_.SetNotPending(4); |
| 83 QuicPacketSequenceNumber lost[] = { 1 }; |
| 84 VerifyLosses(4, lost, arraysize(lost)); |
| 85 } |
| 86 |
| 87 // Ack a packet 3 packets ahead, causing a retransmit. |
| 88 TEST_F(TcpLossAlgorithmTest, NackRetransmit1PacketSingleAck) { |
| 89 const size_t kNumSentPackets = 10; |
| 90 // Transmit 10 packets. |
| 91 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
| 92 SendDataPacket(i); |
| 93 } |
| 94 |
| 95 // Nack the first packet 3 times in an AckFrame with three missing packets. |
| 96 unacked_packets_.NackPacket(1, 3); |
| 97 unacked_packets_.NackPacket(2, 2); |
| 98 unacked_packets_.NackPacket(3, 1); |
| 99 unacked_packets_.SetNotPending(4); |
| 100 QuicPacketSequenceNumber lost[] = { 1 }; |
| 101 VerifyLosses(4, lost, arraysize(lost)); |
| 102 } |
| 103 |
| 104 TEST_F(TcpLossAlgorithmTest, EarlyRetransmit1Packet) { |
| 105 const size_t kNumSentPackets = 2; |
| 106 // Transmit 2 packets. |
| 107 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
| 108 SendDataPacket(i); |
| 109 } |
| 110 // Early retransmit when the final packet gets acked and the first is nacked. |
| 111 unacked_packets_.SetNotPending(2); |
| 112 unacked_packets_.NackPacket(1, 1); |
| 113 QuicPacketSequenceNumber lost[] = { 1 }; |
| 114 VerifyLosses(2, lost, arraysize(lost)); |
| 115 } |
| 116 |
| 117 TEST_F(TcpLossAlgorithmTest, EarlyRetransmitAllPackets) { |
| 118 const size_t kNumSentPackets = 5; |
| 119 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
| 120 SendDataPacket(i); |
| 121 } |
| 122 // Early retransmit when the final packet gets acked and the first 4 are |
| 123 // nacked multiple times via FACK. |
| 124 unacked_packets_.SetNotPending(kNumSentPackets); |
| 125 for (size_t i = 1; i < kNumSentPackets; ++i) { |
| 126 unacked_packets_.NackPacket(i, kNumSentPackets - i); |
| 127 } |
| 128 QuicPacketSequenceNumber lost[] = { 1, 2, 3, 4 }; |
| 129 VerifyLosses(kNumSentPackets, lost, arraysize(lost)); |
| 130 } |
| 131 |
| 132 TEST_F(TcpLossAlgorithmTest, DontEarlyRetransmitNeuteredPacket) { |
| 133 const size_t kNumSentPackets = 2; |
| 134 // Transmit 2 packets. |
| 135 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
| 136 SendDataPacket(i); |
| 137 } |
| 138 // Early retransmit when the final packet gets acked and the first is nacked. |
| 139 unacked_packets_.SetNotPending(2); |
| 140 unacked_packets_.NackPacket(1, 1); |
| 141 unacked_packets_.NeuterPacket(1); |
| 142 VerifyLosses(2, NULL, 0); |
| 143 } |
| 144 |
| 145 } // namespace |
| 146 } // namespace test |
| 147 } // namespace net |
OLD | NEW |