OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/congestion_control/tcp_loss_algorithm.h" | 5 #include "net/quic/congestion_control/tcp_loss_algorithm.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "net/quic/congestion_control/rtt_stats.h" |
11 #include "net/quic/quic_unacked_packet_map.h" | 12 #include "net/quic/quic_unacked_packet_map.h" |
12 #include "net/quic/test_tools/mock_clock.h" | 13 #include "net/quic/test_tools/mock_clock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace net { | 16 namespace net { |
16 namespace test { | 17 namespace test { |
17 namespace { | 18 namespace { |
18 | 19 |
19 class TcpLossAlgorithmTest : public ::testing::Test { | 20 class TcpLossAlgorithmTest : public ::testing::Test { |
20 protected: | 21 protected: |
21 TcpLossAlgorithmTest() | 22 TcpLossAlgorithmTest() |
22 : unacked_packets_(true), | 23 : unacked_packets_() { |
23 srtt_(QuicTime::Delta::FromMilliseconds(100)) { } | 24 rtt_stats_.UpdateRtt(QuicTime::Delta::FromMilliseconds(100), |
| 25 QuicTime::Delta::Zero()); |
| 26 } |
24 | 27 |
25 void SendDataPacket(QuicPacketSequenceNumber sequence_number) { | 28 void SendDataPacket(QuicPacketSequenceNumber sequence_number) { |
26 SerializedPacket packet(sequence_number, PACKET_1BYTE_SEQUENCE_NUMBER, | 29 SerializedPacket packet(sequence_number, PACKET_1BYTE_SEQUENCE_NUMBER, |
27 NULL, 0, new RetransmittableFrames()); | 30 NULL, 0, new RetransmittableFrames()); |
28 unacked_packets_.AddPacket(packet); | 31 unacked_packets_.AddPacket(packet); |
29 unacked_packets_.SetPending(sequence_number, clock_.Now(), 1000); | 32 unacked_packets_.SetPending(sequence_number, clock_.Now(), 1000); |
30 } | 33 } |
31 | 34 |
32 void VerifyLosses(QuicPacketSequenceNumber largest_observed, | 35 void VerifyLosses(QuicPacketSequenceNumber largest_observed, |
33 QuicPacketSequenceNumber* losses_expected, | 36 QuicPacketSequenceNumber* losses_expected, |
34 size_t num_losses) { | 37 size_t num_losses) { |
35 SequenceNumberSet lost_packets = | 38 SequenceNumberSet lost_packets = |
36 loss_algorithm_.DetectLostPackets( | 39 loss_algorithm_.DetectLostPackets( |
37 unacked_packets_, clock_.Now(), largest_observed, srtt_, srtt_); | 40 unacked_packets_, clock_.Now(), largest_observed, rtt_stats_); |
38 EXPECT_EQ(num_losses, lost_packets.size()); | 41 EXPECT_EQ(num_losses, lost_packets.size()); |
39 for (size_t i = 0; i < num_losses; ++i) { | 42 for (size_t i = 0; i < num_losses; ++i) { |
40 EXPECT_TRUE(ContainsKey(lost_packets, losses_expected[i])); | 43 EXPECT_TRUE(ContainsKey(lost_packets, losses_expected[i])); |
41 } | 44 } |
42 } | 45 } |
43 | 46 |
44 QuicUnackedPacketMap unacked_packets_; | 47 QuicUnackedPacketMap unacked_packets_; |
45 TCPLossAlgorithm loss_algorithm_; | 48 TCPLossAlgorithm loss_algorithm_; |
46 QuicTime::Delta srtt_; | 49 RttStats rtt_stats_; |
47 MockClock clock_; | 50 MockClock clock_; |
48 }; | 51 }; |
49 | 52 |
50 TEST_F(TcpLossAlgorithmTest, NackRetransmit1Packet) { | 53 TEST_F(TcpLossAlgorithmTest, NackRetransmit1Packet) { |
51 const size_t kNumSentPackets = 5; | 54 const size_t kNumSentPackets = 5; |
52 // Transmit 5 packets. | 55 // Transmit 5 packets. |
53 for (size_t i = 1; i <= kNumSentPackets; ++i) { | 56 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
54 SendDataPacket(i); | 57 SendDataPacket(i); |
55 } | 58 } |
56 // No loss on one ack. | 59 // No loss on one ack. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 TEST_F(TcpLossAlgorithmTest, EarlyRetransmit1Packet) { | 112 TEST_F(TcpLossAlgorithmTest, EarlyRetransmit1Packet) { |
110 const size_t kNumSentPackets = 2; | 113 const size_t kNumSentPackets = 2; |
111 // Transmit 2 packets. | 114 // Transmit 2 packets. |
112 for (size_t i = 1; i <= kNumSentPackets; ++i) { | 115 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
113 SendDataPacket(i); | 116 SendDataPacket(i); |
114 } | 117 } |
115 // Early retransmit when the final packet gets acked and the first is nacked. | 118 // Early retransmit when the final packet gets acked and the first is nacked. |
116 unacked_packets_.SetNotPending(2); | 119 unacked_packets_.SetNotPending(2); |
117 unacked_packets_.NackPacket(1, 1); | 120 unacked_packets_.NackPacket(1, 1); |
118 VerifyLosses(2, NULL, 0); | 121 VerifyLosses(2, NULL, 0); |
119 EXPECT_EQ(clock_.Now().Add(srtt_.Multiply(1.25)), | 122 EXPECT_EQ(clock_.Now().Add(rtt_stats_.SmoothedRtt().Multiply(1.25)), |
120 loss_algorithm_.GetLossTimeout()); | 123 loss_algorithm_.GetLossTimeout()); |
121 | 124 |
122 clock_.AdvanceTime(srtt_.Multiply(1.25)); | 125 clock_.AdvanceTime(rtt_stats_.latest_rtt().Multiply(1.25)); |
123 QuicPacketSequenceNumber lost[] = { 1 }; | 126 QuicPacketSequenceNumber lost[] = { 1 }; |
124 VerifyLosses(2, lost, arraysize(lost)); | 127 VerifyLosses(2, lost, arraysize(lost)); |
125 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); | 128 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); |
126 } | 129 } |
127 | 130 |
128 TEST_F(TcpLossAlgorithmTest, EarlyRetransmitAllPackets) { | 131 TEST_F(TcpLossAlgorithmTest, EarlyRetransmitAllPackets) { |
129 const size_t kNumSentPackets = 5; | 132 const size_t kNumSentPackets = 5; |
130 for (size_t i = 1; i <= kNumSentPackets; ++i) { | 133 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
131 SendDataPacket(i); | 134 SendDataPacket(i); |
| 135 // Advance the time 1/4 RTT between 3 and 4. |
| 136 if (i == 3) { |
| 137 clock_.AdvanceTime(rtt_stats_.SmoothedRtt().Multiply(0.25)); |
| 138 } |
132 } | 139 } |
133 // Early retransmit when the final packet gets acked and the first 4 are | 140 |
134 // nacked multiple times via FACK. | 141 // Early retransmit when the final packet gets acked and 1.25 RTTs have |
| 142 // elapsed since the packets were sent. |
135 unacked_packets_.SetNotPending(kNumSentPackets); | 143 unacked_packets_.SetNotPending(kNumSentPackets); |
| 144 // This simulates a single ack following multiple missing packets with FACK. |
136 for (size_t i = 1; i < kNumSentPackets; ++i) { | 145 for (size_t i = 1; i < kNumSentPackets; ++i) { |
137 unacked_packets_.NackPacket(i, kNumSentPackets - i); | 146 unacked_packets_.NackPacket(i, kNumSentPackets - i); |
138 } | 147 } |
139 QuicPacketSequenceNumber lost[] = { 1, 2 }; | 148 QuicPacketSequenceNumber lost[] = { 1, 2 }; |
140 VerifyLosses(kNumSentPackets, lost, arraysize(lost)); | 149 VerifyLosses(kNumSentPackets, lost, arraysize(lost)); |
141 EXPECT_EQ(clock_.Now().Add(srtt_.Multiply(1.25)), | 150 // The time has already advanced 1/4 an RTT, so ensure the timeout is set |
| 151 // 1.25 RTTs after the earliest pending packet(3), not the last(4). |
| 152 EXPECT_EQ(clock_.Now().Add(rtt_stats_.SmoothedRtt()), |
142 loss_algorithm_.GetLossTimeout()); | 153 loss_algorithm_.GetLossTimeout()); |
143 | 154 |
144 clock_.AdvanceTime(srtt_.Multiply(1.25)); | 155 clock_.AdvanceTime(rtt_stats_.SmoothedRtt()); |
145 QuicPacketSequenceNumber lost2[] = { 1, 2, 3, 4 }; | 156 QuicPacketSequenceNumber lost2[] = { 1, 2, 3 }; |
146 VerifyLosses(kNumSentPackets, lost2, arraysize(lost2)); | 157 VerifyLosses(kNumSentPackets, lost2, arraysize(lost2)); |
| 158 EXPECT_EQ(clock_.Now().Add(rtt_stats_.SmoothedRtt().Multiply(0.25)), |
| 159 loss_algorithm_.GetLossTimeout()); |
| 160 clock_.AdvanceTime(rtt_stats_.SmoothedRtt().Multiply(0.25)); |
| 161 QuicPacketSequenceNumber lost3[] = { 1, 2, 3, 4 }; |
| 162 VerifyLosses(kNumSentPackets, lost3, arraysize(lost3)); |
147 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); | 163 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); |
148 } | 164 } |
149 | 165 |
150 TEST_F(TcpLossAlgorithmTest, DontEarlyRetransmitNeuteredPacket) { | 166 TEST_F(TcpLossAlgorithmTest, DontEarlyRetransmitNeuteredPacket) { |
151 const size_t kNumSentPackets = 2; | 167 const size_t kNumSentPackets = 2; |
152 // Transmit 2 packets. | 168 // Transmit 2 packets. |
153 for (size_t i = 1; i <= kNumSentPackets; ++i) { | 169 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
154 SendDataPacket(i); | 170 SendDataPacket(i); |
155 } | 171 } |
156 // Early retransmit when the final packet gets acked and the first is nacked. | 172 // Early retransmit when the final packet gets acked and the first is nacked. |
157 unacked_packets_.SetNotPending(2); | 173 unacked_packets_.SetNotPending(2); |
158 unacked_packets_.NackPacket(1, 1); | 174 unacked_packets_.NackPacket(1, 1); |
159 unacked_packets_.NeuterPacket(1); | 175 unacked_packets_.NeuterPacket(1); |
160 VerifyLosses(2, NULL, 0); | 176 VerifyLosses(2, NULL, 0); |
161 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); | 177 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); |
162 } | 178 } |
163 | 179 |
164 } // namespace | 180 } // namespace |
165 } // namespace test | 181 } // namespace test |
166 } // namespace net | 182 } // namespace net |
OLD | NEW |