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