blob: d479c68ef032b24ab2c47c1da6045fb59f7fb797 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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.
4
5#include "net/third_party/quiche/src/quic/core/quic_sent_packet_manager.h"
6
7#include <memory>
bnc463f2352019-10-10 04:49:34 -07008#include <utility>
QUICHE teama6ef0a62019-03-07 20:34:33 -05009
QUICHE teama6ef0a62019-03-07 20:34:33 -050010#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050011#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
12#include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h"
13#include "net/third_party/quiche/src/quic/test_tools/quic_sent_packet_manager_peer.h"
14#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
bnc4e9283d2019-12-17 07:08:57 -080015#include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h"
dmcardlecf0bfcf2019-12-13 08:08:21 -080016#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050017
18using testing::_;
19using testing::AnyNumber;
20using testing::Invoke;
21using testing::InvokeWithoutArgs;
22using testing::IsEmpty;
23using testing::Not;
24using testing::Pointwise;
25using testing::Return;
26using testing::StrictMock;
27using testing::WithArgs;
28
29namespace quic {
30namespace test {
31namespace {
32// Default packet length.
33const uint32_t kDefaultLength = 1000;
34
35// Stream ID for data sent in CreatePacket().
36const QuicStreamId kStreamId = 7;
37
38// Matcher to check that the packet number matches the second argument.
39MATCHER(PacketNumberEq, "") {
40 return ::testing::get<0>(arg).packet_number ==
41 QuicPacketNumber(::testing::get<1>(arg));
42}
43
44class MockDebugDelegate : public QuicSentPacketManager::DebugDelegate {
45 public:
wub713afae2020-04-27 07:48:31 -070046 MOCK_METHOD(void,
47 OnSpuriousPacketRetransmission,
48 (TransmissionType transmission_type, QuicByteCount byte_size),
49 (override));
50 MOCK_METHOD(void,
51 OnPacketLoss,
52 (QuicPacketNumber lost_packet_number,
53 EncryptionLevel encryption_level,
54 TransmissionType transmission_type,
55 QuicTime detection_time),
56 (override));
QUICHE teama6ef0a62019-03-07 20:34:33 -050057};
58
fayangcff885a2019-10-22 07:39:04 -070059class QuicSentPacketManagerTest : public QuicTest {
QUICHE teama6ef0a62019-03-07 20:34:33 -050060 public:
61 void RetransmitCryptoPacket(uint64_t packet_number) {
62 EXPECT_CALL(
63 *send_algorithm_,
64 OnPacketSent(_, BytesInFlight(), QuicPacketNumber(packet_number),
65 kDefaultLength, HAS_RETRANSMITTABLE_DATA));
66 SerializedPacket packet(CreatePacket(packet_number, false));
67 packet.retransmittable_frames.push_back(
dmcardlecf0bfcf2019-12-13 08:08:21 -080068 QuicFrame(QuicStreamFrame(1, false, 0, quiche::QuicheStringPiece())));
QUICHE teama6ef0a62019-03-07 20:34:33 -050069 packet.has_crypto_handshake = IS_HANDSHAKE;
fayangcff885a2019-10-22 07:39:04 -070070 manager_.OnPacketSent(&packet, clock_.Now(), HANDSHAKE_RETRANSMISSION,
71 HAS_RETRANSMITTABLE_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -050072 }
73
fayang06bb1b02019-06-14 14:07:12 -070074 void RetransmitDataPacket(uint64_t packet_number,
75 TransmissionType type,
76 EncryptionLevel level) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050077 EXPECT_CALL(
78 *send_algorithm_,
79 OnPacketSent(_, BytesInFlight(), QuicPacketNumber(packet_number),
80 kDefaultLength, HAS_RETRANSMITTABLE_DATA));
81 SerializedPacket packet(CreatePacket(packet_number, true));
fayang06bb1b02019-06-14 14:07:12 -070082 packet.encryption_level = level;
fayangcff885a2019-10-22 07:39:04 -070083 manager_.OnPacketSent(&packet, clock_.Now(), type,
QUICHE teama6ef0a62019-03-07 20:34:33 -050084 HAS_RETRANSMITTABLE_DATA);
85 }
86
fayang06bb1b02019-06-14 14:07:12 -070087 void RetransmitDataPacket(uint64_t packet_number, TransmissionType type) {
88 RetransmitDataPacket(packet_number, type, ENCRYPTION_INITIAL);
89 }
90
QUICHE teama6ef0a62019-03-07 20:34:33 -050091 protected:
wub18773d62020-01-28 14:14:06 -080092 const CongestionControlType kInitialCongestionControlType = kCubicBytes;
QUICHE teama6ef0a62019-03-07 20:34:33 -050093 QuicSentPacketManagerTest()
QUICHE team73957f12019-04-18 16:21:52 -070094 : manager_(Perspective::IS_SERVER,
95 &clock_,
96 QuicRandom::GetInstance(),
97 &stats_,
fayang3be090d2020-02-11 14:05:08 -080098 kInitialCongestionControlType),
QUICHE teama6ef0a62019-03-07 20:34:33 -050099 send_algorithm_(new StrictMock<MockSendAlgorithm>),
100 network_change_visitor_(new StrictMock<MockNetworkChangeVisitor>) {
101 QuicSentPacketManagerPeer::SetSendAlgorithm(&manager_, send_algorithm_);
102 // Disable tail loss probes for most tests.
103 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 0);
104 // Advance the time 1s so the send times are never QuicTime::Zero.
105 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
106 manager_.SetNetworkChangeVisitor(network_change_visitor_.get());
107 manager_.SetSessionNotifier(&notifier_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500108
wub18773d62020-01-28 14:14:06 -0800109 EXPECT_CALL(*send_algorithm_, GetCongestionControlType())
110 .WillRepeatedly(Return(kInitialCongestionControlType));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500111 EXPECT_CALL(*send_algorithm_, BandwidthEstimate())
112 .Times(AnyNumber())
113 .WillRepeatedly(Return(QuicBandwidth::Zero()));
114 EXPECT_CALL(*send_algorithm_, InSlowStart()).Times(AnyNumber());
115 EXPECT_CALL(*send_algorithm_, InRecovery()).Times(AnyNumber());
wubf4ab9652020-02-20 14:45:43 -0800116 EXPECT_CALL(*send_algorithm_, OnPacketNeutered(_)).Times(AnyNumber());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500117 EXPECT_CALL(*network_change_visitor_, OnPathMtuIncreased(1000))
118 .Times(AnyNumber());
119 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(true));
120 EXPECT_CALL(notifier_, HasUnackedCryptoData())
121 .WillRepeatedly(Return(false));
122 EXPECT_CALL(notifier_, OnStreamFrameRetransmitted(_)).Times(AnyNumber());
QUICHE team9467db02019-05-30 09:38:45 -0700123 EXPECT_CALL(notifier_, OnFrameAcked(_, _, _)).WillRepeatedly(Return(true));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500124 }
125
126 ~QuicSentPacketManagerTest() override {}
127
ianswett9f459cb2019-04-21 06:39:59 -0700128 QuicByteCount BytesInFlight() { return manager_.GetBytesInFlight(); }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500129 void VerifyUnackedPackets(uint64_t* packets, size_t num_packets) {
130 if (num_packets == 0) {
131 EXPECT_TRUE(manager_.unacked_packets().empty());
132 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
133 &manager_));
134 return;
135 }
136
137 EXPECT_FALSE(manager_.unacked_packets().empty());
138 EXPECT_EQ(QuicPacketNumber(packets[0]), manager_.GetLeastUnacked());
139 for (size_t i = 0; i < num_packets; ++i) {
ianswette129b0c2019-04-20 00:27:00 -0700140 EXPECT_TRUE(
141 manager_.unacked_packets().IsUnacked(QuicPacketNumber(packets[i])))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500142 << packets[i];
143 }
144 }
145
146 void VerifyRetransmittablePackets(uint64_t* packets, size_t num_packets) {
147 EXPECT_EQ(
148 num_packets,
149 QuicSentPacketManagerPeer::GetNumRetransmittablePackets(&manager_));
150 for (size_t i = 0; i < num_packets; ++i) {
151 EXPECT_TRUE(QuicSentPacketManagerPeer::HasRetransmittableFrames(
152 &manager_, packets[i]))
153 << " packets[" << i << "]:" << packets[i];
154 }
155 }
156
157 void ExpectAck(uint64_t largest_observed) {
158 EXPECT_CALL(
159 *send_algorithm_,
160 // Ensure the AckedPacketVector argument contains largest_observed.
161 OnCongestionEvent(true, _, _,
162 Pointwise(PacketNumberEq(), {largest_observed}),
163 IsEmpty()));
164 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
165 }
166
dschinazi17d42422019-06-18 16:35:07 -0700167 void ExpectUpdatedRtt(uint64_t /*largest_observed*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500168 EXPECT_CALL(*send_algorithm_,
169 OnCongestionEvent(true, _, _, IsEmpty(), IsEmpty()));
170 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
171 }
172
173 void ExpectAckAndLoss(bool rtt_updated,
174 uint64_t largest_observed,
175 uint64_t lost_packet) {
176 EXPECT_CALL(
177 *send_algorithm_,
178 OnCongestionEvent(rtt_updated, _, _,
179 Pointwise(PacketNumberEq(), {largest_observed}),
180 Pointwise(PacketNumberEq(), {lost_packet})));
181 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
182 }
183
184 // |packets_acked| and |packets_lost| should be in packet number order.
185 void ExpectAcksAndLosses(bool rtt_updated,
186 uint64_t* packets_acked,
187 size_t num_packets_acked,
188 uint64_t* packets_lost,
189 size_t num_packets_lost) {
190 std::vector<QuicPacketNumber> ack_vector;
191 for (size_t i = 0; i < num_packets_acked; ++i) {
192 ack_vector.push_back(QuicPacketNumber(packets_acked[i]));
193 }
194 std::vector<QuicPacketNumber> lost_vector;
195 for (size_t i = 0; i < num_packets_lost; ++i) {
196 lost_vector.push_back(QuicPacketNumber(packets_lost[i]));
197 }
198 EXPECT_CALL(*send_algorithm_,
199 OnCongestionEvent(rtt_updated, _, _,
200 Pointwise(PacketNumberEq(), ack_vector),
201 Pointwise(PacketNumberEq(), lost_vector)));
202 EXPECT_CALL(*network_change_visitor_, OnCongestionChange())
203 .Times(AnyNumber());
204 }
205
206 void RetransmitAndSendPacket(uint64_t old_packet_number,
207 uint64_t new_packet_number) {
208 RetransmitAndSendPacket(old_packet_number, new_packet_number,
209 TLP_RETRANSMISSION);
210 }
211
212 void RetransmitAndSendPacket(uint64_t old_packet_number,
213 uint64_t new_packet_number,
214 TransmissionType transmission_type) {
215 bool is_lost = false;
fayangcff885a2019-10-22 07:39:04 -0700216 if (transmission_type == HANDSHAKE_RETRANSMISSION ||
217 transmission_type == TLP_RETRANSMISSION ||
218 transmission_type == RTO_RETRANSMISSION ||
219 transmission_type == PROBING_RETRANSMISSION) {
220 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
221 .WillOnce(WithArgs<1>(
222 Invoke([this, new_packet_number](TransmissionType type) {
223 RetransmitDataPacket(new_packet_number, type);
224 })));
225 } else {
226 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(1);
227 is_lost = true;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500228 }
229 QuicSentPacketManagerPeer::MarkForRetransmission(
230 &manager_, old_packet_number, transmission_type);
fayangcff885a2019-10-22 07:39:04 -0700231 if (!is_lost) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500232 return;
233 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500234 EXPECT_CALL(
235 *send_algorithm_,
236 OnPacketSent(_, BytesInFlight(), QuicPacketNumber(new_packet_number),
237 kDefaultLength, HAS_RETRANSMITTABLE_DATA));
fayangcff885a2019-10-22 07:39:04 -0700238 SerializedPacket packet(CreatePacket(new_packet_number, true));
239 manager_.OnPacketSent(&packet, clock_.Now(), transmission_type,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500240 HAS_RETRANSMITTABLE_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500241 }
242
243 SerializedPacket CreateDataPacket(uint64_t packet_number) {
244 return CreatePacket(packet_number, true);
245 }
246
247 SerializedPacket CreatePacket(uint64_t packet_number, bool retransmittable) {
248 SerializedPacket packet(QuicPacketNumber(packet_number),
249 PACKET_4BYTE_PACKET_NUMBER, nullptr, kDefaultLength,
250 false, false);
251 if (retransmittable) {
dmcardlecf0bfcf2019-12-13 08:08:21 -0800252 packet.retransmittable_frames.push_back(QuicFrame(
253 QuicStreamFrame(kStreamId, false, 0, quiche::QuicheStringPiece())));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500254 }
255 return packet;
256 }
257
zhongyi1b2f7832019-06-14 13:31:34 -0700258 SerializedPacket CreatePingPacket(uint64_t packet_number) {
259 SerializedPacket packet(QuicPacketNumber(packet_number),
260 PACKET_4BYTE_PACKET_NUMBER, nullptr, kDefaultLength,
261 false, false);
262 packet.retransmittable_frames.push_back(QuicFrame(QuicPingFrame()));
263 return packet;
264 }
265
QUICHE teama6ef0a62019-03-07 20:34:33 -0500266 void SendDataPacket(uint64_t packet_number) {
QUICHE teamc279cec2019-03-22 06:51:48 -0700267 SendDataPacket(packet_number, ENCRYPTION_INITIAL);
268 }
269
270 void SendDataPacket(uint64_t packet_number,
271 EncryptionLevel encryption_level) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500272 EXPECT_CALL(*send_algorithm_,
273 OnPacketSent(_, BytesInFlight(),
274 QuicPacketNumber(packet_number), _, _));
275 SerializedPacket packet(CreateDataPacket(packet_number));
QUICHE teamc279cec2019-03-22 06:51:48 -0700276 packet.encryption_level = encryption_level;
fayangcff885a2019-10-22 07:39:04 -0700277 manager_.OnPacketSent(&packet, clock_.Now(), NOT_RETRANSMISSION,
278 HAS_RETRANSMITTABLE_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500279 }
280
zhongyi1b2f7832019-06-14 13:31:34 -0700281 void SendPingPacket(uint64_t packet_number,
282 EncryptionLevel encryption_level) {
283 EXPECT_CALL(*send_algorithm_,
284 OnPacketSent(_, BytesInFlight(),
285 QuicPacketNumber(packet_number), _, _));
286 SerializedPacket packet(CreatePingPacket(packet_number));
287 packet.encryption_level = encryption_level;
fayangcff885a2019-10-22 07:39:04 -0700288 manager_.OnPacketSent(&packet, clock_.Now(), NOT_RETRANSMISSION,
289 HAS_RETRANSMITTABLE_DATA);
zhongyi1b2f7832019-06-14 13:31:34 -0700290 }
291
QUICHE teama6ef0a62019-03-07 20:34:33 -0500292 void SendCryptoPacket(uint64_t packet_number) {
293 EXPECT_CALL(
294 *send_algorithm_,
295 OnPacketSent(_, BytesInFlight(), QuicPacketNumber(packet_number),
296 kDefaultLength, HAS_RETRANSMITTABLE_DATA));
297 SerializedPacket packet(CreatePacket(packet_number, false));
298 packet.retransmittable_frames.push_back(
dmcardlecf0bfcf2019-12-13 08:08:21 -0800299 QuicFrame(QuicStreamFrame(1, false, 0, quiche::QuicheStringPiece())));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500300 packet.has_crypto_handshake = IS_HANDSHAKE;
fayangcff885a2019-10-22 07:39:04 -0700301 manager_.OnPacketSent(&packet, clock_.Now(), NOT_RETRANSMISSION,
302 HAS_RETRANSMITTABLE_DATA);
303 EXPECT_CALL(notifier_, HasUnackedCryptoData()).WillRepeatedly(Return(true));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500304 }
305
306 void SendAckPacket(uint64_t packet_number, uint64_t largest_acked) {
fayang06bb1b02019-06-14 14:07:12 -0700307 SendAckPacket(packet_number, largest_acked, ENCRYPTION_INITIAL);
308 }
309
310 void SendAckPacket(uint64_t packet_number,
311 uint64_t largest_acked,
312 EncryptionLevel level) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500313 EXPECT_CALL(
314 *send_algorithm_,
315 OnPacketSent(_, BytesInFlight(), QuicPacketNumber(packet_number),
316 kDefaultLength, NO_RETRANSMITTABLE_DATA));
317 SerializedPacket packet(CreatePacket(packet_number, false));
318 packet.largest_acked = QuicPacketNumber(largest_acked);
fayang06bb1b02019-06-14 14:07:12 -0700319 packet.encryption_level = level;
fayangcff885a2019-10-22 07:39:04 -0700320 manager_.OnPacketSent(&packet, clock_.Now(), NOT_RETRANSMISSION,
321 NO_RETRANSMITTABLE_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500322 }
323
fayangce0a3162019-08-15 09:05:36 -0700324 void EnablePto(QuicTag tag) {
fayangce0a3162019-08-15 09:05:36 -0700325 QuicConfig config;
326 QuicTagVector options;
327 options.push_back(tag);
328 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
329 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
330 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
331 manager_.SetFromConfig(config);
fayang62f867a2019-08-22 12:05:01 -0700332 EXPECT_TRUE(manager_.pto_enabled());
fayangce0a3162019-08-15 09:05:36 -0700333 }
334
QUICHE teama6ef0a62019-03-07 20:34:33 -0500335 QuicSentPacketManager manager_;
336 MockClock clock_;
337 QuicConnectionStats stats_;
338 MockSendAlgorithm* send_algorithm_;
339 std::unique_ptr<MockNetworkChangeVisitor> network_change_visitor_;
340 StrictMock<MockSessionNotifier> notifier_;
341};
342
fayangcff885a2019-10-22 07:39:04 -0700343TEST_F(QuicSentPacketManagerTest, IsUnacked) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500344 VerifyUnackedPackets(nullptr, 0);
345 SendDataPacket(1);
346
347 uint64_t unacked[] = {1};
bnc4e9283d2019-12-17 07:08:57 -0800348 VerifyUnackedPackets(unacked, QUICHE_ARRAYSIZE(unacked));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500349 uint64_t retransmittable[] = {1};
350 VerifyRetransmittablePackets(retransmittable,
bnc4e9283d2019-12-17 07:08:57 -0800351 QUICHE_ARRAYSIZE(retransmittable));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500352}
353
fayangcff885a2019-10-22 07:39:04 -0700354TEST_F(QuicSentPacketManagerTest, IsUnAckedRetransmit) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500355 SendDataPacket(1);
356 RetransmitAndSendPacket(1, 2);
357
358 EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(&manager_, 2));
359 uint64_t unacked[] = {1, 2};
bnc4e9283d2019-12-17 07:08:57 -0800360 VerifyUnackedPackets(unacked, QUICHE_ARRAYSIZE(unacked));
fayangcff885a2019-10-22 07:39:04 -0700361 std::vector<uint64_t> retransmittable = {1, 2};
QUICHE teama6ef0a62019-03-07 20:34:33 -0500362 VerifyRetransmittablePackets(&retransmittable[0], retransmittable.size());
363}
364
fayangcff885a2019-10-22 07:39:04 -0700365TEST_F(QuicSentPacketManagerTest, RetransmitThenAck) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500366 SendDataPacket(1);
367 RetransmitAndSendPacket(1, 2);
368
369 // Ack 2 but not 1.
370 ExpectAck(2);
371 manager_.OnAckFrameStart(QuicPacketNumber(2), QuicTime::Delta::Infinite(),
372 clock_.Now());
373 manager_.OnAckRange(QuicPacketNumber(2), QuicPacketNumber(3));
fayang3eb82212019-04-16 12:05:46 -0700374 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700375 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
376 ENCRYPTION_INITIAL));
fayangcff885a2019-10-22 07:39:04 -0700377 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500378 // Packet 1 is unacked, pending, but not retransmittable.
379 uint64_t unacked[] = {1};
bnc4e9283d2019-12-17 07:08:57 -0800380 VerifyUnackedPackets(unacked, QUICHE_ARRAYSIZE(unacked));
ianswett67d7a3c2019-04-19 10:58:47 -0700381 EXPECT_TRUE(manager_.HasInFlightPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500382 VerifyRetransmittablePackets(nullptr, 0);
383}
384
fayangcff885a2019-10-22 07:39:04 -0700385TEST_F(QuicSentPacketManagerTest, RetransmitThenAckBeforeSend) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500386 SendDataPacket(1);
fayangcff885a2019-10-22 07:39:04 -0700387 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
388 .WillOnce(WithArgs<1>(Invoke(
389 [this](TransmissionType type) { RetransmitDataPacket(2, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500390 QuicSentPacketManagerPeer::MarkForRetransmission(&manager_, 1,
391 TLP_RETRANSMISSION);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500392 // Ack 1.
393 ExpectAck(1);
394 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
395 clock_.Now());
396 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700397 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700398 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
399 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500400
fayangcff885a2019-10-22 07:39:04 -0700401 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
402 uint64_t unacked[] = {2};
bnc4e9283d2019-12-17 07:08:57 -0800403 VerifyUnackedPackets(unacked, QUICHE_ARRAYSIZE(unacked));
fayangcff885a2019-10-22 07:39:04 -0700404 // We do not know packet 2 is a spurious retransmission until it gets acked.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500405 VerifyRetransmittablePackets(nullptr, 0);
406 EXPECT_EQ(0u, stats_.packets_spuriously_retransmitted);
407}
408
fayangcff885a2019-10-22 07:39:04 -0700409TEST_F(QuicSentPacketManagerTest, RetransmitThenStopRetransmittingBeforeSend) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500410 SendDataPacket(1);
fayangcff885a2019-10-22 07:39:04 -0700411 EXPECT_CALL(notifier_, RetransmitFrames(_, _));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500412 QuicSentPacketManagerPeer::MarkForRetransmission(&manager_, 1,
413 TLP_RETRANSMISSION);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500414
fayangcff885a2019-10-22 07:39:04 -0700415 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500416
417 uint64_t unacked[] = {1};
bnc4e9283d2019-12-17 07:08:57 -0800418 VerifyUnackedPackets(unacked, QUICHE_ARRAYSIZE(unacked));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500419 VerifyRetransmittablePackets(nullptr, 0);
420 EXPECT_EQ(0u, stats_.packets_spuriously_retransmitted);
421}
422
fayangcff885a2019-10-22 07:39:04 -0700423TEST_F(QuicSentPacketManagerTest, RetransmitThenAckPrevious) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500424 SendDataPacket(1);
425 RetransmitAndSendPacket(1, 2);
426 QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
427 clock_.AdvanceTime(rtt);
428
429 // Ack 1 but not 2.
430 ExpectAck(1);
431 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
432 clock_.Now());
433 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700434 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700435 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
436 ENCRYPTION_INITIAL));
fayangcff885a2019-10-22 07:39:04 -0700437 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500438 // 2 remains unacked, but no packets have retransmittable data.
439 uint64_t unacked[] = {2};
bnc4e9283d2019-12-17 07:08:57 -0800440 VerifyUnackedPackets(unacked, QUICHE_ARRAYSIZE(unacked));
ianswett67d7a3c2019-04-19 10:58:47 -0700441 EXPECT_TRUE(manager_.HasInFlightPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500442 VerifyRetransmittablePackets(nullptr, 0);
fayangcff885a2019-10-22 07:39:04 -0700443 // Ack 2 causes 2 be considered as spurious retransmission.
444 EXPECT_CALL(notifier_, OnFrameAcked(_, _, _)).WillOnce(Return(false));
445 ExpectAck(2);
446 manager_.OnAckFrameStart(QuicPacketNumber(2), QuicTime::Delta::Infinite(),
447 clock_.Now());
448 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(3));
449 EXPECT_EQ(PACKETS_NEWLY_ACKED,
450 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(2),
451 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500452
453 EXPECT_EQ(1u, stats_.packets_spuriously_retransmitted);
454}
455
fayangcff885a2019-10-22 07:39:04 -0700456TEST_F(QuicSentPacketManagerTest, RetransmitThenAckPreviousThenNackRetransmit) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500457 SendDataPacket(1);
458 RetransmitAndSendPacket(1, 2);
459 QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
460 clock_.AdvanceTime(rtt);
461
462 // First, ACK packet 1 which makes packet 2 non-retransmittable.
463 ExpectAck(1);
464 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
465 clock_.Now());
466 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700467 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700468 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
469 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500470
471 SendDataPacket(3);
472 SendDataPacket(4);
473 SendDataPacket(5);
474 clock_.AdvanceTime(rtt);
475
476 // Next, NACK packet 2 three times.
fayang3be090d2020-02-11 14:05:08 -0800477 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
478 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(1);
479 ExpectAckAndLoss(true, 3, 2);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500480 manager_.OnAckFrameStart(QuicPacketNumber(3), QuicTime::Delta::Infinite(),
481 clock_.Now());
482 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(4));
483 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700484 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700485 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(2),
486 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500487
488 ExpectAck(4);
489 manager_.OnAckFrameStart(QuicPacketNumber(4), QuicTime::Delta::Infinite(),
490 clock_.Now());
491 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(5));
492 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700493 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700494 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(3),
495 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500496
fayang3be090d2020-02-11 14:05:08 -0800497 ExpectAck(5);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500498 manager_.OnAckFrameStart(QuicPacketNumber(5), QuicTime::Delta::Infinite(),
499 clock_.Now());
500 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(6));
501 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700502 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700503 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(4),
504 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500505
fayangcff885a2019-10-22 07:39:04 -0700506 uint64_t unacked[] = {2};
bnc4e9283d2019-12-17 07:08:57 -0800507 VerifyUnackedPackets(unacked, QUICHE_ARRAYSIZE(unacked));
ianswett67d7a3c2019-04-19 10:58:47 -0700508 EXPECT_FALSE(manager_.HasInFlightPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500509 VerifyRetransmittablePackets(nullptr, 0);
510
511 // Verify that the retransmission alarm would not fire,
512 // since there is no retransmittable data outstanding.
513 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
514}
515
fayangcff885a2019-10-22 07:39:04 -0700516TEST_F(QuicSentPacketManagerTest,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500517 DISABLED_RetransmitTwiceThenAckPreviousBeforeSend) {
518 SendDataPacket(1);
519 RetransmitAndSendPacket(1, 2);
520
521 // Fire the RTO, which will mark 2 for retransmission (but will not send it).
522 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
523 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
524 manager_.OnRetransmissionTimeout();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500525
526 // Ack 1 but not 2, before 2 is able to be sent.
527 // Since 1 has been retransmitted, it has already been lost, and so the
528 // send algorithm is not informed that it has been ACK'd.
529 ExpectUpdatedRtt(1);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500530 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
531 clock_.Now());
532 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700533 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700534 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
535 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500536
537 // Since 2 was marked for retransmit, when 1 is acked, 2 is kept for RTT.
538 uint64_t unacked[] = {2};
bnc4e9283d2019-12-17 07:08:57 -0800539 VerifyUnackedPackets(unacked, QUICHE_ARRAYSIZE(unacked));
ianswett67d7a3c2019-04-19 10:58:47 -0700540 EXPECT_FALSE(manager_.HasInFlightPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500541 VerifyRetransmittablePackets(nullptr, 0);
542
543 // Verify that the retransmission alarm would not fire,
544 // since there is no retransmittable data outstanding.
545 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
546}
547
fayangcff885a2019-10-22 07:39:04 -0700548TEST_F(QuicSentPacketManagerTest, RetransmitTwiceThenAckFirst) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500549 StrictMock<MockDebugDelegate> debug_delegate;
fayangcff885a2019-10-22 07:39:04 -0700550 EXPECT_CALL(debug_delegate, OnSpuriousPacketRetransmission(TLP_RETRANSMISSION,
551 kDefaultLength))
552 .Times(1);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500553 manager_.SetDebugDelegate(&debug_delegate);
554
555 SendDataPacket(1);
556 RetransmitAndSendPacket(1, 2);
557 RetransmitAndSendPacket(2, 3);
558 QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
559 clock_.AdvanceTime(rtt);
560
561 // Ack 1 but not 2 or 3.
562 ExpectAck(1);
563 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
564 clock_.Now());
565 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700566 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700567 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
568 ENCRYPTION_INITIAL));
fayangcff885a2019-10-22 07:39:04 -0700569 // Frames in packets 2 and 3 are acked.
570 EXPECT_CALL(notifier_, IsFrameOutstanding(_))
571 .Times(2)
572 .WillRepeatedly(Return(false));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500573
574 // 2 and 3 remain unacked, but no packets have retransmittable data.
575 uint64_t unacked[] = {2, 3};
bnc4e9283d2019-12-17 07:08:57 -0800576 VerifyUnackedPackets(unacked, QUICHE_ARRAYSIZE(unacked));
ianswett67d7a3c2019-04-19 10:58:47 -0700577 EXPECT_TRUE(manager_.HasInFlightPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500578 VerifyRetransmittablePackets(nullptr, 0);
579
580 // Ensure packet 2 is lost when 4 is sent and 3 and 4 are acked.
581 SendDataPacket(4);
fayangcff885a2019-10-22 07:39:04 -0700582 // No new data gets acked in packet 3.
583 EXPECT_CALL(notifier_, OnFrameAcked(_, _, _))
584 .WillOnce(Return(false))
585 .WillRepeatedly(Return(true));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500586 uint64_t acked[] = {3, 4};
bnc4e9283d2019-12-17 07:08:57 -0800587 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500588 manager_.OnAckFrameStart(QuicPacketNumber(4), QuicTime::Delta::Infinite(),
589 clock_.Now());
590 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(5));
591 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700592 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700593 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(2),
594 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500595
596 uint64_t unacked2[] = {2};
bnc4e9283d2019-12-17 07:08:57 -0800597 VerifyUnackedPackets(unacked2, QUICHE_ARRAYSIZE(unacked2));
ianswett67d7a3c2019-04-19 10:58:47 -0700598 EXPECT_TRUE(manager_.HasInFlightPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500599
600 SendDataPacket(5);
601 ExpectAckAndLoss(true, 5, 2);
602 EXPECT_CALL(debug_delegate,
vasilvv923b6d22020-03-23 11:17:58 -0700603 OnPacketLoss(QuicPacketNumber(2), _, LOSS_RETRANSMISSION, _));
fayangcff885a2019-10-22 07:39:04 -0700604 // Frames in all packets are acked.
605 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
606 // Notify session that stream frame in packet 2 gets lost although it is
607 // not outstanding.
608 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(1);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500609 manager_.OnAckFrameStart(QuicPacketNumber(5), QuicTime::Delta::Infinite(),
610 clock_.Now());
611 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(6));
612 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700613 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700614 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(3),
615 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500616
fayangcff885a2019-10-22 07:39:04 -0700617 uint64_t unacked3[] = {2};
bnc4e9283d2019-12-17 07:08:57 -0800618 VerifyUnackedPackets(unacked3, QUICHE_ARRAYSIZE(unacked3));
ianswett67d7a3c2019-04-19 10:58:47 -0700619 EXPECT_FALSE(manager_.HasInFlightPackets());
fayangcff885a2019-10-22 07:39:04 -0700620 // Spurious retransmission is detected when packet 3 gets acked. We cannot
621 // know packet 2 is a spurious until it gets acked.
622 EXPECT_EQ(1u, stats_.packets_spuriously_retransmitted);
wube330aa42020-02-27 07:49:09 -0800623 EXPECT_EQ(1u, stats_.packets_lost);
624 EXPECT_LT(QuicTime::Delta::Zero(), stats_.total_loss_detection_time);
wubaa51f0e2020-05-12 15:08:16 -0700625 EXPECT_LE(1u, stats_.sent_packets_max_sequence_reordering);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500626}
627
fayangcff885a2019-10-22 07:39:04 -0700628TEST_F(QuicSentPacketManagerTest, AckOriginalTransmission) {
vasilvv0fc587f2019-09-06 13:33:08 -0700629 auto loss_algorithm = std::make_unique<MockLossAlgorithm>();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500630 QuicSentPacketManagerPeer::SetLossAlgorithm(&manager_, loss_algorithm.get());
631
632 SendDataPacket(1);
633 RetransmitAndSendPacket(1, 2);
634
635 // Ack original transmission, but that wasn't lost via fast retransmit,
636 // so no call on OnSpuriousRetransmission is expected.
637 {
638 ExpectAck(1);
639 EXPECT_CALL(*loss_algorithm, DetectLosses(_, _, _, _, _, _));
640 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
641 clock_.Now());
642 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700643 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700644 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
645 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500646 }
647
648 SendDataPacket(3);
649 SendDataPacket(4);
650 // Ack 4, which causes 3 to be retransmitted.
651 {
652 ExpectAck(4);
653 EXPECT_CALL(*loss_algorithm, DetectLosses(_, _, _, _, _, _));
654 manager_.OnAckFrameStart(QuicPacketNumber(4), QuicTime::Delta::Infinite(),
655 clock_.Now());
656 manager_.OnAckRange(QuicPacketNumber(4), QuicPacketNumber(5));
657 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700658 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700659 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(2),
660 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500661 RetransmitAndSendPacket(3, 5, LOSS_RETRANSMISSION);
662 }
663
664 // Ack 3, which causes SpuriousRetransmitDetected to be called.
665 {
666 uint64_t acked[] = {3};
bnc4e9283d2019-12-17 07:08:57 -0800667 ExpectAcksAndLosses(false, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500668 EXPECT_CALL(*loss_algorithm, DetectLosses(_, _, _, _, _, _));
fayangcc4ea6a2019-10-25 08:44:03 -0700669 EXPECT_CALL(*loss_algorithm,
670 SpuriousLossDetected(_, _, _, QuicPacketNumber(3),
671 QuicPacketNumber(4)));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500672 manager_.OnAckFrameStart(QuicPacketNumber(4), QuicTime::Delta::Infinite(),
673 clock_.Now());
674 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(5));
675 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
wub02831dc2020-02-28 12:00:54 -0800676 EXPECT_EQ(0u, stats_.packet_spuriously_detected_lost);
fayang3eb82212019-04-16 12:05:46 -0700677 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700678 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(3),
679 ENCRYPTION_INITIAL));
wub02831dc2020-02-28 12:00:54 -0800680 EXPECT_EQ(1u, stats_.packet_spuriously_detected_lost);
fayangcff885a2019-10-22 07:39:04 -0700681 // Ack 3 will not cause 5 be considered as a spurious retransmission. Ack
682 // 5 will cause 5 be considered as a spurious retransmission as no new
683 // data gets acked.
684 ExpectAck(5);
685 EXPECT_CALL(*loss_algorithm, DetectLosses(_, _, _, _, _, _));
686 EXPECT_CALL(notifier_, OnFrameAcked(_, _, _)).WillOnce(Return(false));
687 manager_.OnAckFrameStart(QuicPacketNumber(5), QuicTime::Delta::Infinite(),
688 clock_.Now());
689 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(6));
690 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
691 EXPECT_EQ(PACKETS_NEWLY_ACKED,
692 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(4),
693 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500694 }
695}
696
fayangcff885a2019-10-22 07:39:04 -0700697TEST_F(QuicSentPacketManagerTest, GetLeastUnacked) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500698 EXPECT_EQ(QuicPacketNumber(1u), manager_.GetLeastUnacked());
699}
700
fayangcff885a2019-10-22 07:39:04 -0700701TEST_F(QuicSentPacketManagerTest, GetLeastUnackedUnacked) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500702 SendDataPacket(1);
703 EXPECT_EQ(QuicPacketNumber(1u), manager_.GetLeastUnacked());
704}
705
fayangcff885a2019-10-22 07:39:04 -0700706TEST_F(QuicSentPacketManagerTest, AckAckAndUpdateRtt) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500707 EXPECT_FALSE(manager_.largest_packet_peer_knows_is_acked().IsInitialized());
708 SendDataPacket(1);
709 SendAckPacket(2, 1);
710
711 // Now ack the ack and expect an RTT update.
712 uint64_t acked[] = {1, 2};
bnc4e9283d2019-12-17 07:08:57 -0800713 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500714 manager_.OnAckFrameStart(QuicPacketNumber(2),
715 QuicTime::Delta::FromMilliseconds(5), clock_.Now());
716 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(3));
fayang3eb82212019-04-16 12:05:46 -0700717 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700718 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
719 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500720 EXPECT_EQ(QuicPacketNumber(1), manager_.largest_packet_peer_knows_is_acked());
721
722 SendAckPacket(3, 3);
723
724 // Now ack the ack and expect only an RTT update.
725 uint64_t acked2[] = {3};
bnc4e9283d2019-12-17 07:08:57 -0800726 ExpectAcksAndLosses(true, acked2, QUICHE_ARRAYSIZE(acked2), nullptr, 0);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500727 manager_.OnAckFrameStart(QuicPacketNumber(3), QuicTime::Delta::Infinite(),
728 clock_.Now());
729 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(4));
fayang3eb82212019-04-16 12:05:46 -0700730 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700731 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(2),
732 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500733 EXPECT_EQ(QuicPacketNumber(3u),
734 manager_.largest_packet_peer_knows_is_acked());
735}
736
fayangcff885a2019-10-22 07:39:04 -0700737TEST_F(QuicSentPacketManagerTest, Rtt) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500738 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(20);
739 SendDataPacket(1);
740 clock_.AdvanceTime(expected_rtt);
741
742 ExpectAck(1);
743 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
744 clock_.Now());
745 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700746 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700747 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
748 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500749 EXPECT_EQ(expected_rtt, manager_.GetRttStats()->latest_rtt());
750}
751
fayangcff885a2019-10-22 07:39:04 -0700752TEST_F(QuicSentPacketManagerTest, RttWithInvalidDelta) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500753 // Expect that the RTT is equal to the local time elapsed, since the
754 // ack_delay_time is larger than the local time elapsed
755 // and is hence invalid.
756 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
757 SendDataPacket(1);
758 clock_.AdvanceTime(expected_rtt);
759
760 ExpectAck(1);
761 manager_.OnAckFrameStart(QuicPacketNumber(1),
762 QuicTime::Delta::FromMilliseconds(11), clock_.Now());
763 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700764 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700765 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
766 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500767 EXPECT_EQ(expected_rtt, manager_.GetRttStats()->latest_rtt());
768}
769
fayangcff885a2019-10-22 07:39:04 -0700770TEST_F(QuicSentPacketManagerTest, RttWithInfiniteDelta) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500771 // Expect that the RTT is equal to the local time elapsed, since the
772 // ack_delay_time is infinite, and is hence invalid.
773 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
774 SendDataPacket(1);
775 clock_.AdvanceTime(expected_rtt);
776
777 ExpectAck(1);
778 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
779 clock_.Now());
780 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700781 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700782 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
783 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500784 EXPECT_EQ(expected_rtt, manager_.GetRttStats()->latest_rtt());
785}
786
wub8ee29a02019-12-12 13:06:48 -0800787TEST_F(QuicSentPacketManagerTest, RttWithDeltaExceedingLimit) {
788 // Initialize min and smoothed rtt to 10ms.
789 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
790 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(10),
791 QuicTime::Delta::Zero(), QuicTime::Zero());
792
793 QuicTime::Delta send_delta = QuicTime::Delta::FromMilliseconds(100);
794 QuicTime::Delta ack_delay =
795 QuicTime::Delta::FromMilliseconds(5) + manager_.peer_max_ack_delay();
796 ASSERT_GT(send_delta - rtt_stats->min_rtt(), ack_delay);
797 SendDataPacket(1);
798 clock_.AdvanceTime(send_delta);
799
800 ExpectAck(1);
801 manager_.OnAckFrameStart(QuicPacketNumber(1), ack_delay, clock_.Now());
802 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
803 EXPECT_EQ(PACKETS_NEWLY_ACKED,
804 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
805 ENCRYPTION_FORWARD_SECURE));
806
807 QuicTime::Delta expected_rtt_sample =
wub81c9b662020-02-14 09:08:07 -0800808 send_delta - manager_.peer_max_ack_delay();
wub8ee29a02019-12-12 13:06:48 -0800809 EXPECT_EQ(expected_rtt_sample, manager_.GetRttStats()->latest_rtt());
810}
811
fayangcff885a2019-10-22 07:39:04 -0700812TEST_F(QuicSentPacketManagerTest, RttZeroDelta) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500813 // Expect that the RTT is the time between send and receive since the
814 // ack_delay_time is zero.
815 QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
816 SendDataPacket(1);
817 clock_.AdvanceTime(expected_rtt);
818
819 ExpectAck(1);
820 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Zero(),
821 clock_.Now());
822 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -0700823 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700824 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
825 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500826 EXPECT_EQ(expected_rtt, manager_.GetRttStats()->latest_rtt());
827}
828
fayangcff885a2019-10-22 07:39:04 -0700829TEST_F(QuicSentPacketManagerTest, TailLossProbeTimeout) {
fayang5d011982020-05-13 14:14:38 -0700830 if (GetQuicReloadableFlag(quic_default_on_pto)) {
831 return;
832 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500833 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
834
835 // Send 1 packet.
836 SendDataPacket(1);
837
838 // The first tail loss probe retransmits 1 packet.
839 manager_.OnRetransmissionTimeout();
840 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
fayangcff885a2019-10-22 07:39:04 -0700841 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
842 .WillOnce(WithArgs<1>(Invoke(
843 [this](TransmissionType type) { RetransmitDataPacket(2, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500844 manager_.MaybeRetransmitTailLossProbe();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500845
846 // The second tail loss probe retransmits 1 packet.
847 manager_.OnRetransmissionTimeout();
848 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
fayangcff885a2019-10-22 07:39:04 -0700849 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
850 .WillOnce(WithArgs<1>(Invoke(
851 [this](TransmissionType type) { RetransmitDataPacket(3, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500852 manager_.MaybeRetransmitTailLossProbe();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500853 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillOnce(Return(false));
854 EXPECT_EQ(QuicTime::Delta::Infinite(), manager_.TimeUntilSend(clock_.Now()));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500855
856 // Ack the third and ensure the first two are still pending.
857 ExpectAck(3);
858
859 manager_.OnAckFrameStart(QuicPacketNumber(3), QuicTime::Delta::Infinite(),
860 clock_.Now());
861 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(4));
fayang3eb82212019-04-16 12:05:46 -0700862 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700863 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
864 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500865
ianswett67d7a3c2019-04-19 10:58:47 -0700866 EXPECT_TRUE(manager_.HasInFlightPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500867
868 // Acking two more packets will lose both of them due to nacks.
869 SendDataPacket(4);
870 SendDataPacket(5);
871 uint64_t acked[] = {4, 5};
872 uint64_t lost[] = {1, 2};
bnc4e9283d2019-12-17 07:08:57 -0800873 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), lost,
874 QUICHE_ARRAYSIZE(lost));
fayangcff885a2019-10-22 07:39:04 -0700875 // Frames in all packets are acked.
876 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
877 // Notify session that stream frame in packets 1 and 2 get lost although
878 // they are not outstanding.
879 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(2);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500880 manager_.OnAckFrameStart(QuicPacketNumber(5), QuicTime::Delta::Infinite(),
881 clock_.Now());
882 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(6));
fayang3eb82212019-04-16 12:05:46 -0700883 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700884 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(2),
885 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500886
ianswett67d7a3c2019-04-19 10:58:47 -0700887 EXPECT_FALSE(manager_.HasInFlightPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500888 EXPECT_EQ(2u, stats_.tlp_count);
889 EXPECT_EQ(0u, stats_.rto_count);
890}
891
fayangcff885a2019-10-22 07:39:04 -0700892TEST_F(QuicSentPacketManagerTest, TailLossProbeThenRTO) {
fayang5d011982020-05-13 14:14:38 -0700893 if (GetQuicReloadableFlag(quic_default_on_pto)) {
894 return;
895 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500896 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
897
898 // Send 100 packets.
899 const size_t kNumSentPackets = 100;
900 for (size_t i = 1; i <= kNumSentPackets; ++i) {
901 SendDataPacket(i);
902 }
903 QuicTime rto_packet_time = clock_.Now();
904 // Advance the time.
905 clock_.AdvanceTime(manager_.GetRetransmissionTime() - clock_.Now());
906
907 // The first tail loss probe retransmits 1 packet.
908 manager_.OnRetransmissionTimeout();
909 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
fayangcff885a2019-10-22 07:39:04 -0700910 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
911 .WillOnce(WithArgs<1>(Invoke(
912 [this](TransmissionType type) { RetransmitDataPacket(101, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500913 manager_.MaybeRetransmitTailLossProbe();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500914 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillOnce(Return(false));
915 EXPECT_EQ(QuicTime::Delta::Infinite(), manager_.TimeUntilSend(clock_.Now()));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500916 clock_.AdvanceTime(manager_.GetRetransmissionTime() - clock_.Now());
917
918 // The second tail loss probe retransmits 1 packet.
919 manager_.OnRetransmissionTimeout();
920 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
fayangcff885a2019-10-22 07:39:04 -0700921 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
922 .WillOnce(WithArgs<1>(Invoke(
923 [this](TransmissionType type) { RetransmitDataPacket(102, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500924 EXPECT_TRUE(manager_.MaybeRetransmitTailLossProbe());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500925 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillOnce(Return(false));
926 EXPECT_EQ(QuicTime::Delta::Infinite(), manager_.TimeUntilSend(clock_.Now()));
927
928 // Ensure the RTO is set based on the correct packet.
929 rto_packet_time = clock_.Now();
930 EXPECT_EQ(rto_packet_time + QuicTime::Delta::FromMilliseconds(500),
931 manager_.GetRetransmissionTime());
932
933 // Advance the time enough to ensure all packets are RTO'd.
934 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
935
fayangcff885a2019-10-22 07:39:04 -0700936 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
937 .Times(2)
938 .WillOnce(WithArgs<1>(Invoke(
939 [this](TransmissionType type) { RetransmitDataPacket(103, type); })))
940 .WillOnce(WithArgs<1>(Invoke(
941 [this](TransmissionType type) { RetransmitDataPacket(104, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500942 manager_.OnRetransmissionTimeout();
943 EXPECT_EQ(2u, stats_.tlp_count);
944 EXPECT_EQ(1u, stats_.rto_count);
fayang2a379bf2020-05-18 11:49:25 -0700945 EXPECT_EQ(0u, stats_.max_consecutive_rto_with_forward_progress);
fayangcff885a2019-10-22 07:39:04 -0700946 // There are 2 RTO retransmissions.
947 EXPECT_EQ(104 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500948 QuicPacketNumber largest_acked = QuicPacketNumber(103);
949 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
950 EXPECT_CALL(*send_algorithm_,
951 OnCongestionEvent(
952 true, _, _, Pointwise(PacketNumberEq(), {largest_acked}), _));
953 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
fayangcff885a2019-10-22 07:39:04 -0700954 // Although frames in packet 3 gets acked, it would be kept for another
955 // RTT.
956 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(true));
957 // Packets [1, 102] are lost, although stream frame in packet 3 is not
958 // outstanding.
959 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(102);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500960 manager_.OnAckFrameStart(QuicPacketNumber(103), QuicTime::Delta::Infinite(),
961 clock_.Now());
962 manager_.OnAckRange(QuicPacketNumber(103), QuicPacketNumber(104));
fayang3eb82212019-04-16 12:05:46 -0700963 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -0700964 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
965 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500966 // All packets before 103 should be lost.
fayangcff885a2019-10-22 07:39:04 -0700967 // Packet 104 is still in flight.
968 EXPECT_EQ(1000u, manager_.GetBytesInFlight());
fayang2a379bf2020-05-18 11:49:25 -0700969 EXPECT_EQ(1u, stats_.max_consecutive_rto_with_forward_progress);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500970}
971
fayangcff885a2019-10-22 07:39:04 -0700972TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeout) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500973 // Send 2 crypto packets and 3 data packets.
974 const size_t kNumSentCryptoPackets = 2;
975 for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
976 SendCryptoPacket(i);
977 }
978 const size_t kNumSentDataPackets = 3;
979 for (size_t i = 1; i <= kNumSentDataPackets; ++i) {
980 SendDataPacket(kNumSentCryptoPackets + i);
981 }
ianswett0cc36802019-04-20 03:04:09 -0700982 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
ianswett898306b2019-04-23 11:05:57 -0700983 EXPECT_EQ(5 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500984
985 // The first retransmits 2 packets.
fayangcff885a2019-10-22 07:39:04 -0700986 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
987 .Times(2)
988 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(6); }))
989 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(7); }));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500990 manager_.OnRetransmissionTimeout();
ianswett898306b2019-04-23 11:05:57 -0700991 // Expect all 4 handshake packets to be in flight and 3 data packets.
ianswett04004652019-09-03 18:46:57 -0700992 EXPECT_EQ(7 * kDefaultLength, manager_.GetBytesInFlight());
ianswett0cc36802019-04-20 03:04:09 -0700993 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500994
995 // The second retransmits 2 packets.
fayangcff885a2019-10-22 07:39:04 -0700996 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
997 .Times(2)
998 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(8); }))
999 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(9); }));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001000 manager_.OnRetransmissionTimeout();
ianswett04004652019-09-03 18:46:57 -07001001 EXPECT_EQ(9 * kDefaultLength, manager_.GetBytesInFlight());
ianswett0cc36802019-04-20 03:04:09 -07001002 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001003
1004 // Now ack the two crypto packets and the speculatively encrypted request,
1005 // and ensure the first four crypto packets get abandoned, but not lost.
ianswett04004652019-09-03 18:46:57 -07001006 // Crypto packets remain in flight, so any that aren't acked will be lost.
1007 uint64_t acked[] = {3, 4, 5, 8, 9};
1008 uint64_t lost[] = {1, 2, 6};
bnc4e9283d2019-12-17 07:08:57 -08001009 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), lost,
1010 QUICHE_ARRAYSIZE(lost));
fayangcff885a2019-10-22 07:39:04 -07001011 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(3);
1012 EXPECT_CALL(notifier_, HasUnackedCryptoData()).WillRepeatedly(Return(false));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001013 manager_.OnAckFrameStart(QuicPacketNumber(9), QuicTime::Delta::Infinite(),
1014 clock_.Now());
1015 manager_.OnAckRange(QuicPacketNumber(8), QuicPacketNumber(10));
1016 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(6));
fayang3eb82212019-04-16 12:05:46 -07001017 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07001018 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
1019 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001020
ianswett0cc36802019-04-20 03:04:09 -07001021 EXPECT_FALSE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001022}
1023
fayangcff885a2019-10-22 07:39:04 -07001024TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeoutVersionNegotiation) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001025 // Send 2 crypto packets and 3 data packets.
1026 const size_t kNumSentCryptoPackets = 2;
1027 for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
1028 SendCryptoPacket(i);
1029 }
1030 const size_t kNumSentDataPackets = 3;
1031 for (size_t i = 1; i <= kNumSentDataPackets; ++i) {
1032 SendDataPacket(kNumSentCryptoPackets + i);
1033 }
ianswett0cc36802019-04-20 03:04:09 -07001034 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001035
fayangcff885a2019-10-22 07:39:04 -07001036 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1037 .Times(2)
1038 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(6); }))
1039 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(7); }));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001040 manager_.OnRetransmissionTimeout();
ianswett0cc36802019-04-20 03:04:09 -07001041 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001042
1043 // Now act like a version negotiation packet arrived, which would cause all
1044 // unacked packets to be retransmitted.
fayangcff885a2019-10-22 07:39:04 -07001045 // Mark packets [1, 7] lost. And the frames in 6 and 7 are same as packets 1
1046 // and 2, respectively.
1047 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(7);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001048 manager_.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION);
1049
1050 // Ensure the first two pending packets are the crypto retransmits.
fayangcff885a2019-10-22 07:39:04 -07001051 RetransmitCryptoPacket(8);
1052 RetransmitCryptoPacket(9);
1053 RetransmitDataPacket(10, ALL_UNACKED_RETRANSMISSION);
1054 RetransmitDataPacket(11, ALL_UNACKED_RETRANSMISSION);
1055 RetransmitDataPacket(12, ALL_UNACKED_RETRANSMISSION);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001056
1057 EXPECT_EQ(QuicPacketNumber(1u), manager_.GetLeastUnacked());
1058 // Least unacked isn't raised until an ack is received, so ack the
1059 // crypto packets.
1060 uint64_t acked[] = {8, 9};
bnc4e9283d2019-12-17 07:08:57 -08001061 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001062 manager_.OnAckFrameStart(QuicPacketNumber(9), QuicTime::Delta::Infinite(),
1063 clock_.Now());
1064 manager_.OnAckRange(QuicPacketNumber(8), QuicPacketNumber(10));
fayang3eb82212019-04-16 12:05:46 -07001065 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07001066 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
1067 ENCRYPTION_INITIAL));
fayangcff885a2019-10-22 07:39:04 -07001068 EXPECT_CALL(notifier_, HasUnackedCryptoData()).WillRepeatedly(Return(false));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001069 EXPECT_EQ(QuicPacketNumber(10u), manager_.GetLeastUnacked());
1070}
1071
fayangcff885a2019-10-22 07:39:04 -07001072TEST_F(QuicSentPacketManagerTest, CryptoHandshakeSpuriousRetransmission) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001073 // Send 1 crypto packet.
1074 SendCryptoPacket(1);
ianswett0cc36802019-04-20 03:04:09 -07001075 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001076
1077 // Retransmit the crypto packet as 2.
fayangcff885a2019-10-22 07:39:04 -07001078 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1079 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(2); }));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001080 manager_.OnRetransmissionTimeout();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001081
1082 // Retransmit the crypto packet as 3.
fayangcff885a2019-10-22 07:39:04 -07001083 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1084 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(3); }));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001085 manager_.OnRetransmissionTimeout();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001086
1087 // Now ack the second crypto packet, and ensure the first gets removed, but
1088 // the third does not.
1089 uint64_t acked[] = {2};
bnc4e9283d2019-12-17 07:08:57 -08001090 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
fayangcff885a2019-10-22 07:39:04 -07001091 EXPECT_CALL(notifier_, HasUnackedCryptoData()).WillRepeatedly(Return(false));
1092 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001093 manager_.OnAckFrameStart(QuicPacketNumber(2), QuicTime::Delta::Infinite(),
1094 clock_.Now());
1095 manager_.OnAckRange(QuicPacketNumber(2), QuicPacketNumber(3));
fayang3eb82212019-04-16 12:05:46 -07001096 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07001097 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
1098 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001099
ianswett0cc36802019-04-20 03:04:09 -07001100 EXPECT_FALSE(manager_.HasUnackedCryptoPackets());
ianswett04004652019-09-03 18:46:57 -07001101 uint64_t unacked[] = {1, 3};
bnc4e9283d2019-12-17 07:08:57 -08001102 VerifyUnackedPackets(unacked, QUICHE_ARRAYSIZE(unacked));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001103}
1104
fayangcff885a2019-10-22 07:39:04 -07001105TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeoutUnsentDataPacket) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001106 // Send 2 crypto packets and 1 data packet.
1107 const size_t kNumSentCryptoPackets = 2;
1108 for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
1109 SendCryptoPacket(i);
1110 }
1111 SendDataPacket(3);
ianswett0cc36802019-04-20 03:04:09 -07001112 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001113
1114 // Retransmit 2 crypto packets, but not the serialized packet.
fayangcff885a2019-10-22 07:39:04 -07001115 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1116 .Times(2)
1117 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(4); }))
1118 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(5); }));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001119 manager_.OnRetransmissionTimeout();
ianswett0cc36802019-04-20 03:04:09 -07001120 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001121}
1122
fayangcff885a2019-10-22 07:39:04 -07001123TEST_F(QuicSentPacketManagerTest,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001124 CryptoHandshakeRetransmissionThenRetransmitAll) {
1125 // Send 1 crypto packet.
1126 SendCryptoPacket(1);
1127
ianswett0cc36802019-04-20 03:04:09 -07001128 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001129
1130 // Retransmit the crypto packet as 2.
fayangcff885a2019-10-22 07:39:04 -07001131 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1132 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(2); }));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001133 manager_.OnRetransmissionTimeout();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001134 // Now retransmit all the unacked packets, which occurs when there is a
1135 // version negotiation.
fayangcff885a2019-10-22 07:39:04 -07001136 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(2);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001137 manager_.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION);
fayangcff885a2019-10-22 07:39:04 -07001138 // Both packets 1 and 2 are unackable.
1139 EXPECT_FALSE(manager_.unacked_packets().IsUnacked(QuicPacketNumber(1)));
1140 EXPECT_FALSE(manager_.unacked_packets().IsUnacked(QuicPacketNumber(2)));
ianswett0cc36802019-04-20 03:04:09 -07001141 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
ianswett67d7a3c2019-04-19 10:58:47 -07001142 EXPECT_FALSE(manager_.HasInFlightPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001143}
1144
fayangcff885a2019-10-22 07:39:04 -07001145TEST_F(QuicSentPacketManagerTest,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001146 CryptoHandshakeRetransmissionThenNeuterAndAck) {
1147 // Send 1 crypto packet.
1148 SendCryptoPacket(1);
1149
ianswett0cc36802019-04-20 03:04:09 -07001150 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001151
1152 // Retransmit the crypto packet as 2.
fayangcff885a2019-10-22 07:39:04 -07001153 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1154 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(2); }));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001155 manager_.OnRetransmissionTimeout();
ianswett0cc36802019-04-20 03:04:09 -07001156 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001157
1158 // Retransmit the crypto packet as 3.
fayangcff885a2019-10-22 07:39:04 -07001159 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1160 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(3); }));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001161 manager_.OnRetransmissionTimeout();
ianswett0cc36802019-04-20 03:04:09 -07001162 EXPECT_TRUE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001163
1164 // Now neuter all unacked unencrypted packets, which occurs when the
1165 // connection goes forward secure.
fayangcff885a2019-10-22 07:39:04 -07001166 EXPECT_CALL(notifier_, HasUnackedCryptoData()).WillRepeatedly(Return(false));
1167 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
fayang93c83942019-11-07 10:41:16 -08001168 manager_.NeuterUnencryptedPackets();
ianswett0cc36802019-04-20 03:04:09 -07001169 EXPECT_FALSE(manager_.HasUnackedCryptoPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001170 uint64_t unacked[] = {1, 2, 3};
bnc4e9283d2019-12-17 07:08:57 -08001171 VerifyUnackedPackets(unacked, QUICHE_ARRAYSIZE(unacked));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001172 VerifyRetransmittablePackets(nullptr, 0);
ianswett0cc36802019-04-20 03:04:09 -07001173 EXPECT_FALSE(manager_.HasUnackedCryptoPackets());
ianswett67d7a3c2019-04-19 10:58:47 -07001174 EXPECT_FALSE(manager_.HasInFlightPackets());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001175
1176 // Ensure both packets get discarded when packet 2 is acked.
1177 uint64_t acked[] = {3};
bnc4e9283d2019-12-17 07:08:57 -08001178 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001179 manager_.OnAckFrameStart(QuicPacketNumber(3), QuicTime::Delta::Infinite(),
1180 clock_.Now());
1181 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(4));
fayang3eb82212019-04-16 12:05:46 -07001182 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07001183 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
1184 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001185 VerifyUnackedPackets(nullptr, 0);
1186 VerifyRetransmittablePackets(nullptr, 0);
1187}
1188
fayangcff885a2019-10-22 07:39:04 -07001189TEST_F(QuicSentPacketManagerTest, RetransmissionTimeout) {
fayang5d011982020-05-13 14:14:38 -07001190 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1191 return;
1192 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001193 StrictMock<MockDebugDelegate> debug_delegate;
1194 manager_.SetDebugDelegate(&debug_delegate);
1195
1196 // Send 100 packets.
1197 const size_t kNumSentPackets = 100;
1198 for (size_t i = 1; i <= kNumSentPackets; ++i) {
1199 SendDataPacket(i);
1200 }
1201
1202 EXPECT_FALSE(manager_.MaybeRetransmitTailLossProbe());
fayangcff885a2019-10-22 07:39:04 -07001203 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1204 .Times(2)
1205 .WillOnce(WithArgs<1>(Invoke(
1206 [this](TransmissionType type) { RetransmitDataPacket(101, type); })))
1207 .WillOnce(WithArgs<1>(Invoke(
1208 [this](TransmissionType type) { RetransmitDataPacket(102, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001209 manager_.OnRetransmissionTimeout();
fayangcff885a2019-10-22 07:39:04 -07001210 EXPECT_EQ(102 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001211
1212 // Ack a retransmission.
1213 // Ensure no packets are lost.
1214 QuicPacketNumber largest_acked = QuicPacketNumber(102);
1215 EXPECT_CALL(*send_algorithm_,
1216 OnCongestionEvent(true, _, _,
1217 Pointwise(PacketNumberEq(), {largest_acked}),
1218 /*lost_packets=*/IsEmpty()));
1219 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1220 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1221 // RTO's use loss detection instead of immediately declaring retransmitted
1222 // packets lost.
1223 for (int i = 1; i <= 99; ++i) {
1224 EXPECT_CALL(debug_delegate,
vasilvv923b6d22020-03-23 11:17:58 -07001225 OnPacketLoss(QuicPacketNumber(i), _, LOSS_RETRANSMISSION, _));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001226 }
fayangcff885a2019-10-22 07:39:04 -07001227 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(true));
1228 // Packets [1, 99] are considered as lost, although stream frame in packet
1229 // 2 is not outstanding.
1230 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(99);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001231 manager_.OnAckFrameStart(QuicPacketNumber(102), QuicTime::Delta::Zero(),
1232 clock_.Now());
1233 manager_.OnAckRange(QuicPacketNumber(102), QuicPacketNumber(103));
fayang3eb82212019-04-16 12:05:46 -07001234 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07001235 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
1236 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001237}
1238
fayangcff885a2019-10-22 07:39:04 -07001239TEST_F(QuicSentPacketManagerTest, RetransmissionTimeoutOnePacket) {
fayang5d011982020-05-13 14:14:38 -07001240 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1241 return;
1242 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001243 // Set the 1RTO connection option.
1244 QuicConfig client_config;
1245 QuicTagVector options;
1246 options.push_back(k1RTO);
1247 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1248 client_config.SetConnectionOptionsToSend(options);
1249 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1250 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1251 EXPECT_CALL(*send_algorithm_, PacingRate(_))
1252 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1253 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1254 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
1255 manager_.SetFromConfig(client_config);
1256 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true));
1257
1258 StrictMock<MockDebugDelegate> debug_delegate;
1259 manager_.SetDebugDelegate(&debug_delegate);
1260
1261 // Send 100 packets.
1262 const size_t kNumSentPackets = 100;
1263 for (size_t i = 1; i <= kNumSentPackets; ++i) {
1264 SendDataPacket(i);
1265 }
1266
1267 EXPECT_FALSE(manager_.MaybeRetransmitTailLossProbe());
fayangcff885a2019-10-22 07:39:04 -07001268 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1269 .Times(1)
1270 .WillOnce(WithArgs<1>(Invoke(
1271 [this](TransmissionType type) { RetransmitDataPacket(101, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001272 manager_.OnRetransmissionTimeout();
fayangcff885a2019-10-22 07:39:04 -07001273 EXPECT_EQ(101 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001274}
1275
fayangcff885a2019-10-22 07:39:04 -07001276TEST_F(QuicSentPacketManagerTest, NewRetransmissionTimeout) {
fayang5d011982020-05-13 14:14:38 -07001277 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1278 return;
1279 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001280 QuicConfig client_config;
1281 QuicTagVector options;
1282 options.push_back(kNRTO);
1283 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1284 client_config.SetConnectionOptionsToSend(options);
1285 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1286 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1287 EXPECT_CALL(*send_algorithm_, PacingRate(_))
1288 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1289 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1290 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
1291 manager_.SetFromConfig(client_config);
1292 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
1293 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true));
1294
1295 // Send 100 packets.
1296 const size_t kNumSentPackets = 100;
1297 for (size_t i = 1; i <= kNumSentPackets; ++i) {
1298 SendDataPacket(i);
1299 }
1300
1301 EXPECT_FALSE(manager_.MaybeRetransmitTailLossProbe());
fayangcff885a2019-10-22 07:39:04 -07001302 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1303 .Times(2)
1304 .WillOnce(WithArgs<1>(Invoke(
1305 [this](TransmissionType type) { RetransmitDataPacket(101, type); })))
1306 .WillOnce(WithArgs<1>(Invoke(
1307 [this](TransmissionType type) { RetransmitDataPacket(102, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001308 manager_.OnRetransmissionTimeout();
fayangcff885a2019-10-22 07:39:04 -07001309 EXPECT_EQ(102 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001310
1311 // Ack a retransmission and expect no call to OnRetransmissionTimeout.
1312 // This will include packets in the lost packet map.
1313 QuicPacketNumber largest_acked = QuicPacketNumber(102);
1314 EXPECT_CALL(*send_algorithm_,
1315 OnCongestionEvent(true, _, _,
1316 Pointwise(PacketNumberEq(), {largest_acked}),
1317 /*lost_packets=*/Not(IsEmpty())));
1318 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
fayangcff885a2019-10-22 07:39:04 -07001319 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(true));
1320 // Packets [1, 99] are considered as lost, although stream frame in packet
1321 // 2 is not outstanding.
1322 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(99);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001323 manager_.OnAckFrameStart(QuicPacketNumber(102), QuicTime::Delta::Zero(),
1324 clock_.Now());
1325 manager_.OnAckRange(QuicPacketNumber(102), QuicPacketNumber(103));
fayang3eb82212019-04-16 12:05:46 -07001326 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07001327 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
1328 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001329}
1330
fayangcff885a2019-10-22 07:39:04 -07001331TEST_F(QuicSentPacketManagerTest, TwoRetransmissionTimeoutsAckSecond) {
fayang5d011982020-05-13 14:14:38 -07001332 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1333 return;
1334 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001335 // Send 1 packet.
1336 SendDataPacket(1);
1337
fayangcff885a2019-10-22 07:39:04 -07001338 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1339 .WillOnce(WithArgs<1>(Invoke(
1340 [this](TransmissionType type) { RetransmitDataPacket(2, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001341 manager_.OnRetransmissionTimeout();
fayangcff885a2019-10-22 07:39:04 -07001342 EXPECT_EQ(2 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001343
1344 // Rto a second time.
fayangcff885a2019-10-22 07:39:04 -07001345 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1346 .WillOnce(WithArgs<1>(Invoke(
1347 [this](TransmissionType type) { RetransmitDataPacket(3, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001348 manager_.OnRetransmissionTimeout();
fayangcff885a2019-10-22 07:39:04 -07001349 EXPECT_EQ(3 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001350
1351 // Ack a retransmission and ensure OnRetransmissionTimeout is called.
1352 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1353 ExpectAck(2);
1354 manager_.OnAckFrameStart(QuicPacketNumber(2), QuicTime::Delta::Zero(),
1355 clock_.Now());
1356 manager_.OnAckRange(QuicPacketNumber(2), QuicPacketNumber(3));
fayang3eb82212019-04-16 12:05:46 -07001357 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07001358 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
1359 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001360
1361 // The original packet and newest should be outstanding.
ianswett9f459cb2019-04-21 06:39:59 -07001362 EXPECT_EQ(2 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001363}
1364
fayangcff885a2019-10-22 07:39:04 -07001365TEST_F(QuicSentPacketManagerTest, TwoRetransmissionTimeoutsAckFirst) {
fayang5d011982020-05-13 14:14:38 -07001366 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1367 return;
1368 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001369 // Send 1 packet.
1370 SendDataPacket(1);
1371
fayangcff885a2019-10-22 07:39:04 -07001372 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1373 .WillOnce(WithArgs<1>(Invoke(
1374 [this](TransmissionType type) { RetransmitDataPacket(2, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001375 manager_.OnRetransmissionTimeout();
fayangcff885a2019-10-22 07:39:04 -07001376 EXPECT_EQ(2 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001377
1378 // Rto a second time.
fayangcff885a2019-10-22 07:39:04 -07001379 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1380 .WillOnce(WithArgs<1>(Invoke(
1381 [this](TransmissionType type) { RetransmitDataPacket(3, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001382 manager_.OnRetransmissionTimeout();
fayangcff885a2019-10-22 07:39:04 -07001383 EXPECT_EQ(3 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001384
1385 // Ack a retransmission and ensure OnRetransmissionTimeout is called.
1386 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
1387 ExpectAck(3);
1388 manager_.OnAckFrameStart(QuicPacketNumber(3), QuicTime::Delta::Zero(),
1389 clock_.Now());
1390 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(4));
fayang3eb82212019-04-16 12:05:46 -07001391 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07001392 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
1393 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001394
1395 // The first two packets should still be outstanding.
ianswett9f459cb2019-04-21 06:39:59 -07001396 EXPECT_EQ(2 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001397}
1398
fayangcff885a2019-10-22 07:39:04 -07001399TEST_F(QuicSentPacketManagerTest, GetTransmissionTime) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001400 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
1401}
1402
fayangcff885a2019-10-22 07:39:04 -07001403TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeCryptoHandshake) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001404 QuicTime crypto_packet_send_time = clock_.Now();
1405 SendCryptoPacket(1);
1406
1407 // Check the min.
1408 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
1409 rtt_stats->set_initial_rtt(QuicTime::Delta::FromMilliseconds(1));
1410 EXPECT_EQ(clock_.Now() + QuicTime::Delta::FromMilliseconds(10),
1411 manager_.GetRetransmissionTime());
1412
1413 // Test with a standard smoothed RTT.
1414 rtt_stats->set_initial_rtt(QuicTime::Delta::FromMilliseconds(100));
1415
1416 QuicTime::Delta srtt = rtt_stats->initial_rtt();
1417 QuicTime expected_time = clock_.Now() + 1.5 * srtt;
1418 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1419
1420 // Retransmit the packet by invoking the retransmission timeout.
1421 clock_.AdvanceTime(1.5 * srtt);
fayangcff885a2019-10-22 07:39:04 -07001422 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1423 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(2); }));
1424 // When session decides what to write, crypto_packet_send_time gets updated.
1425 crypto_packet_send_time = clock_.Now();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001426 manager_.OnRetransmissionTimeout();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001427
1428 // The retransmission time should now be twice as far in the future.
1429 expected_time = crypto_packet_send_time + srtt * 2 * 1.5;
1430 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1431
1432 // Retransmit the packet for the 2nd time.
1433 clock_.AdvanceTime(2 * 1.5 * srtt);
fayangcff885a2019-10-22 07:39:04 -07001434 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1435 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(3); }));
1436 // When session decides what to write, crypto_packet_send_time gets updated.
1437 crypto_packet_send_time = clock_.Now();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001438 manager_.OnRetransmissionTimeout();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001439
1440 // Verify exponential backoff of the retransmission timeout.
1441 expected_time = crypto_packet_send_time + srtt * 4 * 1.5;
1442 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1443}
1444
fayangcff885a2019-10-22 07:39:04 -07001445TEST_F(QuicSentPacketManagerTest,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001446 GetConservativeTransmissionTimeCryptoHandshake) {
1447 QuicConfig config;
1448 QuicTagVector options;
1449 options.push_back(kCONH);
1450 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1451 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1452 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1453 manager_.SetFromConfig(config);
1454 // Calling SetFromConfig requires mocking out some send algorithm methods.
1455 EXPECT_CALL(*send_algorithm_, PacingRate(_))
1456 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1457 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1458 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
1459
1460 QuicTime crypto_packet_send_time = clock_.Now();
1461 SendCryptoPacket(1);
1462
1463 // Check the min.
1464 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
1465 rtt_stats->set_initial_rtt(QuicTime::Delta::FromMilliseconds(1));
1466 EXPECT_EQ(clock_.Now() + QuicTime::Delta::FromMilliseconds(25),
1467 manager_.GetRetransmissionTime());
1468
1469 // Test with a standard smoothed RTT.
1470 rtt_stats->set_initial_rtt(QuicTime::Delta::FromMilliseconds(100));
1471
1472 QuicTime::Delta srtt = rtt_stats->initial_rtt();
1473 QuicTime expected_time = clock_.Now() + 2 * srtt;
1474 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1475
1476 // Retransmit the packet by invoking the retransmission timeout.
1477 clock_.AdvanceTime(2 * srtt);
fayangcff885a2019-10-22 07:39:04 -07001478 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1479 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(2); }));
1480 crypto_packet_send_time = clock_.Now();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001481 manager_.OnRetransmissionTimeout();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001482
1483 // The retransmission time should now be twice as far in the future.
1484 expected_time = crypto_packet_send_time + srtt * 2 * 2;
1485 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1486}
1487
fayangcff885a2019-10-22 07:39:04 -07001488TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeTailLossProbe) {
fayang5d011982020-05-13 14:14:38 -07001489 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1490 return;
1491 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001492 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
1493 SendDataPacket(1);
1494 SendDataPacket(2);
1495
1496 // Check the min.
1497 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
1498 rtt_stats->set_initial_rtt(QuicTime::Delta::FromMilliseconds(1));
1499 EXPECT_EQ(clock_.Now() + QuicTime::Delta::FromMilliseconds(10),
1500 manager_.GetRetransmissionTime());
1501
1502 // Test with a standard smoothed RTT.
1503 rtt_stats->set_initial_rtt(QuicTime::Delta::FromMilliseconds(100));
1504 QuicTime::Delta srtt = rtt_stats->initial_rtt();
1505 QuicTime::Delta expected_tlp_delay = 2 * srtt;
1506 QuicTime expected_time = clock_.Now() + expected_tlp_delay;
1507 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1508
1509 // Retransmit the packet by invoking the retransmission timeout.
1510 clock_.AdvanceTime(expected_tlp_delay);
1511 manager_.OnRetransmissionTimeout();
1512 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
fayangcff885a2019-10-22 07:39:04 -07001513 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1514 .WillOnce(WithArgs<1>(Invoke(
1515 [this](TransmissionType type) { RetransmitDataPacket(3, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001516 EXPECT_TRUE(manager_.MaybeRetransmitTailLossProbe());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001517 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillOnce(Return(false));
1518 EXPECT_EQ(QuicTime::Delta::Infinite(), manager_.TimeUntilSend(clock_.Now()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001519
1520 expected_time = clock_.Now() + expected_tlp_delay;
1521 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1522}
1523
fayangcff885a2019-10-22 07:39:04 -07001524TEST_F(QuicSentPacketManagerTest, TLPRWithPendingStreamData) {
fayang5d011982020-05-13 14:14:38 -07001525 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1526 return;
1527 }
zhongyi1b2f7832019-06-14 13:31:34 -07001528 QuicConfig config;
1529 QuicTagVector options;
1530
1531 options.push_back(kTLPR);
1532 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1533 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1534 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1535 EXPECT_CALL(*send_algorithm_, PacingRate(_))
1536 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1537 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1538 .WillOnce(Return(10 * kDefaultTCPMSS));
1539 manager_.SetFromConfig(config);
1540 EXPECT_TRUE(
1541 QuicSentPacketManagerPeer::GetEnableHalfRttTailLossProbe(&manager_));
1542
1543 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
1544
1545 SendDataPacket(1);
1546 SendDataPacket(2);
1547
1548 // Test with a standard smoothed RTT.
1549 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
1550 rtt_stats->set_initial_rtt(QuicTime::Delta::FromMilliseconds(100));
1551 QuicTime::Delta srtt = rtt_stats->initial_rtt();
1552 // With pending stream data, TLPR is used.
1553 QuicTime::Delta expected_tlp_delay = 0.5 * srtt;
1554 EXPECT_CALL(notifier_, HasUnackedStreamData()).WillRepeatedly(Return(true));
1555
1556 EXPECT_EQ(expected_tlp_delay,
1557 manager_.GetRetransmissionTime() - clock_.Now());
1558
1559 // Retransmit the packet by invoking the retransmission timeout.
1560 clock_.AdvanceTime(expected_tlp_delay);
1561 manager_.OnRetransmissionTimeout();
1562 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
zhongyi1b2f7832019-06-14 13:31:34 -07001563 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1564 .WillOnce(WithArgs<1>(Invoke(
1565 [this](TransmissionType type) { RetransmitDataPacket(3, type); })));
1566 EXPECT_TRUE(manager_.MaybeRetransmitTailLossProbe());
1567
1568 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillOnce(Return(false));
1569 EXPECT_EQ(QuicTime::Delta::Infinite(), manager_.TimeUntilSend(clock_.Now()));
zhongyi1b2f7832019-06-14 13:31:34 -07001570
1571 // 2nd TLP.
1572 expected_tlp_delay = 2 * srtt;
1573 EXPECT_EQ(expected_tlp_delay,
1574 manager_.GetRetransmissionTime() - clock_.Now());
1575}
1576
fayangcff885a2019-10-22 07:39:04 -07001577TEST_F(QuicSentPacketManagerTest, TLPRWithoutPendingStreamData) {
fayang5d011982020-05-13 14:14:38 -07001578 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1579 return;
1580 }
zhongyi1b2f7832019-06-14 13:31:34 -07001581 QuicConfig config;
1582 QuicTagVector options;
1583
1584 options.push_back(kTLPR);
1585 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1586 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1587 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1588 EXPECT_CALL(*send_algorithm_, PacingRate(_))
1589 .WillRepeatedly(Return(QuicBandwidth::Zero()));
1590 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
1591 .WillOnce(Return(10 * kDefaultTCPMSS));
1592 manager_.SetFromConfig(config);
1593 EXPECT_TRUE(
1594 QuicSentPacketManagerPeer::GetEnableHalfRttTailLossProbe(&manager_));
1595 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
1596
1597 SendPingPacket(1, ENCRYPTION_INITIAL);
1598 SendPingPacket(2, ENCRYPTION_INITIAL);
1599
1600 // Test with a standard smoothed RTT.
1601 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
1602 rtt_stats->set_initial_rtt(QuicTime::Delta::FromMilliseconds(100));
1603 QuicTime::Delta srtt = rtt_stats->initial_rtt();
1604 QuicTime::Delta expected_tlp_delay = 0.5 * srtt;
zhongyi9fa2be32019-09-10 12:39:01 -07001605 // With no pending stream data, TLPR is ignored.
1606 expected_tlp_delay = 2 * srtt;
zhongyi1b2f7832019-06-14 13:31:34 -07001607 EXPECT_CALL(notifier_, HasUnackedStreamData()).WillRepeatedly(Return(false));
1608 EXPECT_EQ(expected_tlp_delay,
1609 manager_.GetRetransmissionTime() - clock_.Now());
1610
1611 // Retransmit the packet by invoking the retransmission timeout.
1612 clock_.AdvanceTime(expected_tlp_delay);
1613 manager_.OnRetransmissionTimeout();
1614 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
zhongyi1b2f7832019-06-14 13:31:34 -07001615 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1616 .WillOnce(WithArgs<1>(Invoke(
1617 [this](TransmissionType type) { RetransmitDataPacket(3, type); })));
1618 EXPECT_TRUE(manager_.MaybeRetransmitTailLossProbe());
1619 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillOnce(Return(false));
1620 EXPECT_EQ(QuicTime::Delta::Infinite(), manager_.TimeUntilSend(clock_.Now()));
zhongyi1b2f7832019-06-14 13:31:34 -07001621
1622 // 2nd TLP.
1623 expected_tlp_delay = 2 * srtt;
1624 EXPECT_EQ(expected_tlp_delay,
1625 manager_.GetRetransmissionTime() - clock_.Now());
1626}
1627
fayangcff885a2019-10-22 07:39:04 -07001628TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeSpuriousRTO) {
fayang5d011982020-05-13 14:14:38 -07001629 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1630 return;
1631 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001632 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
1633 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
1634 QuicTime::Delta::Zero(), QuicTime::Zero());
1635
1636 SendDataPacket(1);
1637 SendDataPacket(2);
1638 SendDataPacket(3);
1639 SendDataPacket(4);
1640
1641 QuicTime::Delta expected_rto_delay =
1642 rtt_stats->smoothed_rtt() + 4 * rtt_stats->mean_deviation();
1643 QuicTime expected_time = clock_.Now() + expected_rto_delay;
1644 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1645
1646 // Retransmit the packet by invoking the retransmission timeout.
1647 clock_.AdvanceTime(expected_rto_delay);
fayangcff885a2019-10-22 07:39:04 -07001648 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1649 .Times(2)
1650 .WillOnce(WithArgs<1>(Invoke(
1651 [this](TransmissionType type) { RetransmitDataPacket(5, type); })))
1652 .WillOnce(WithArgs<1>(Invoke(
1653 [this](TransmissionType type) { RetransmitDataPacket(6, type); })));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001654 manager_.OnRetransmissionTimeout();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001655 // All previous packets are inflight, plus two rto retransmissions.
ianswett9f459cb2019-04-21 06:39:59 -07001656 EXPECT_EQ(6 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001657
1658 // The delay should double the second time.
1659 expected_time = clock_.Now() + expected_rto_delay + expected_rto_delay;
1660 // Once we always base the timer on the right edge, leaving the older packets
1661 // in flight doesn't change the timeout.
1662 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1663
1664 // Ack a packet before the first RTO and ensure the RTO timeout returns to the
1665 // original value and OnRetransmissionTimeout is not called or reverted.
fayang01983412019-12-07 05:50:22 -08001666 uint64_t acked[] = {1, 2};
bnc4e9283d2019-12-17 07:08:57 -08001667 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
wub8ee29a02019-12-12 13:06:48 -08001668 manager_.OnAckFrameStart(QuicPacketNumber(2), QuicTime::Delta::Zero(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05001669 clock_.Now());
fayang01983412019-12-07 05:50:22 -08001670 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(3));
fayang3eb82212019-04-16 12:05:46 -07001671 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07001672 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
1673 ENCRYPTION_INITIAL));
fayang01983412019-12-07 05:50:22 -08001674 EXPECT_EQ(4 * kDefaultLength, manager_.GetBytesInFlight());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001675
1676 // Wait 2RTTs from now for the RTO, since it's the max of the RTO time
1677 // and the TLP time. In production, there would always be two TLP's first.
1678 // Since retransmission was spurious, smoothed_rtt_ is expired, and replaced
1679 // by the latest RTT sample of 500ms.
1680 expected_time = clock_.Now() + QuicTime::Delta::FromMilliseconds(1000);
1681 // Once we always base the timer on the right edge, leaving the older packets
1682 // in flight doesn't change the timeout.
1683 EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
1684}
1685
fayangcff885a2019-10-22 07:39:04 -07001686TEST_F(QuicSentPacketManagerTest, GetTransmissionDelayMin) {
fayang5d011982020-05-13 14:14:38 -07001687 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1688 return;
1689 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001690 SendDataPacket(1);
1691 // Provide a 1ms RTT sample.
1692 const_cast<RttStats*>(manager_.GetRttStats())
1693 ->UpdateRtt(QuicTime::Delta::FromMilliseconds(1), QuicTime::Delta::Zero(),
1694 QuicTime::Zero());
1695 QuicTime::Delta delay = QuicTime::Delta::FromMilliseconds(200);
1696
1697 // If the delay is smaller than the min, ensure it exponentially backs off
1698 // from the min.
1699 for (int i = 0; i < 5; ++i) {
1700 EXPECT_EQ(delay,
1701 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001702 delay = delay + delay;
fayangcff885a2019-10-22 07:39:04 -07001703 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1704 .WillOnce(WithArgs<1>(Invoke([this, i](TransmissionType type) {
1705 RetransmitDataPacket(i + 2, type);
1706 })));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001707 manager_.OnRetransmissionTimeout();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001708 }
1709}
1710
fayangcff885a2019-10-22 07:39:04 -07001711TEST_F(QuicSentPacketManagerTest, GetTransmissionDelayMax) {
fayang5d011982020-05-13 14:14:38 -07001712 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1713 return;
1714 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001715 SendDataPacket(1);
1716 // Provide a 60s RTT sample.
1717 const_cast<RttStats*>(manager_.GetRttStats())
1718 ->UpdateRtt(QuicTime::Delta::FromSeconds(60), QuicTime::Delta::Zero(),
1719 QuicTime::Zero());
1720
1721 EXPECT_EQ(QuicTime::Delta::FromSeconds(60),
1722 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001723}
1724
fayangcff885a2019-10-22 07:39:04 -07001725TEST_F(QuicSentPacketManagerTest, GetTransmissionDelayExponentialBackoff) {
fayang5d011982020-05-13 14:14:38 -07001726 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1727 return;
1728 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001729 SendDataPacket(1);
1730 QuicTime::Delta delay = QuicTime::Delta::FromMilliseconds(500);
1731
1732 // Delay should back off exponentially.
1733 for (int i = 0; i < 5; ++i) {
1734 EXPECT_EQ(delay,
1735 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001736 delay = delay + delay;
fayangcff885a2019-10-22 07:39:04 -07001737 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
1738 .WillOnce(WithArgs<1>(Invoke([this, i](TransmissionType type) {
1739 RetransmitDataPacket(i + 2, type);
1740 })));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001741 manager_.OnRetransmissionTimeout();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001742 }
1743}
1744
fayangcff885a2019-10-22 07:39:04 -07001745TEST_F(QuicSentPacketManagerTest, RetransmissionDelay) {
fayang5d011982020-05-13 14:14:38 -07001746 if (GetQuicReloadableFlag(quic_default_on_pto)) {
1747 return;
1748 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001749 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
1750 const int64_t kRttMs = 250;
1751 const int64_t kDeviationMs = 5;
1752
1753 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kRttMs),
1754 QuicTime::Delta::Zero(), clock_.Now());
1755
1756 // Initial value is to set the median deviation to half of the initial rtt,
1757 // the median in then multiplied by a factor of 4 and finally the smoothed rtt
1758 // is added which is the initial rtt.
1759 QuicTime::Delta expected_delay =
1760 QuicTime::Delta::FromMilliseconds(kRttMs + kRttMs / 2 * 4);
1761 EXPECT_EQ(expected_delay,
1762 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001763
1764 for (int i = 0; i < 100; ++i) {
1765 // Run to make sure that we converge.
1766 rtt_stats->UpdateRtt(
1767 QuicTime::Delta::FromMilliseconds(kRttMs + kDeviationMs),
1768 QuicTime::Delta::Zero(), clock_.Now());
1769 rtt_stats->UpdateRtt(
1770 QuicTime::Delta::FromMilliseconds(kRttMs - kDeviationMs),
1771 QuicTime::Delta::Zero(), clock_.Now());
1772 }
1773 expected_delay = QuicTime::Delta::FromMilliseconds(kRttMs + kDeviationMs * 4);
1774
1775 EXPECT_NEAR(kRttMs, rtt_stats->smoothed_rtt().ToMilliseconds(), 1);
1776 EXPECT_NEAR(expected_delay.ToMilliseconds(),
1777 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_)
1778 .ToMilliseconds(),
1779 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001780}
1781
fayangcff885a2019-10-22 07:39:04 -07001782TEST_F(QuicSentPacketManagerTest, GetLossDelay) {
vasilvv0fc587f2019-09-06 13:33:08 -07001783 auto loss_algorithm = std::make_unique<MockLossAlgorithm>();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001784 QuicSentPacketManagerPeer::SetLossAlgorithm(&manager_, loss_algorithm.get());
1785
1786 EXPECT_CALL(*loss_algorithm, GetLossTimeout())
1787 .WillRepeatedly(Return(QuicTime::Zero()));
1788 SendDataPacket(1);
1789 SendDataPacket(2);
1790
1791 // Handle an ack which causes the loss algorithm to be evaluated and
1792 // set the loss timeout.
1793 ExpectAck(2);
1794 EXPECT_CALL(*loss_algorithm, DetectLosses(_, _, _, _, _, _));
1795 manager_.OnAckFrameStart(QuicPacketNumber(2), QuicTime::Delta::Infinite(),
1796 clock_.Now());
1797 manager_.OnAckRange(QuicPacketNumber(2), QuicPacketNumber(3));
fayang3eb82212019-04-16 12:05:46 -07001798 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07001799 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
1800 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001801
1802 QuicTime timeout(clock_.Now() + QuicTime::Delta::FromMilliseconds(10));
1803 EXPECT_CALL(*loss_algorithm, GetLossTimeout())
1804 .WillRepeatedly(Return(timeout));
1805 EXPECT_EQ(timeout, manager_.GetRetransmissionTime());
1806
1807 // Fire the retransmission timeout and ensure the loss detection algorithm
1808 // is invoked.
1809 EXPECT_CALL(*loss_algorithm, DetectLosses(_, _, _, _, _, _));
1810 manager_.OnRetransmissionTimeout();
1811}
1812
fayangcff885a2019-10-22 07:39:04 -07001813TEST_F(QuicSentPacketManagerTest, NegotiateIetfLossDetectionFromOptions) {
fayang3be090d2020-02-11 14:05:08 -08001814 EXPECT_TRUE(
1815 QuicSentPacketManagerPeer::AdaptiveReorderingThresholdEnabled(&manager_));
1816 EXPECT_FALSE(
1817 QuicSentPacketManagerPeer::AdaptiveTimeThresholdEnabled(&manager_));
1818 EXPECT_EQ(kDefaultLossDelayShift,
1819 QuicSentPacketManagerPeer::GetReorderingShift(&manager_));
fayangb0c7b4b2019-09-12 06:45:24 -07001820
1821 QuicConfig config;
1822 QuicTagVector options;
1823 options.push_back(kILD0);
1824 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1825 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1826 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1827 manager_.SetFromConfig(config);
1828
fayangb0c7b4b2019-09-12 06:45:24 -07001829 EXPECT_EQ(3, QuicSentPacketManagerPeer::GetReorderingShift(&manager_));
1830 EXPECT_FALSE(
1831 QuicSentPacketManagerPeer::AdaptiveReorderingThresholdEnabled(&manager_));
1832}
1833
fayangcff885a2019-10-22 07:39:04 -07001834TEST_F(QuicSentPacketManagerTest,
fayangb0c7b4b2019-09-12 06:45:24 -07001835 NegotiateIetfLossDetectionOneFourthRttFromOptions) {
fayang3be090d2020-02-11 14:05:08 -08001836 EXPECT_TRUE(
1837 QuicSentPacketManagerPeer::AdaptiveReorderingThresholdEnabled(&manager_));
1838 EXPECT_FALSE(
1839 QuicSentPacketManagerPeer::AdaptiveTimeThresholdEnabled(&manager_));
1840 EXPECT_EQ(kDefaultLossDelayShift,
1841 QuicSentPacketManagerPeer::GetReorderingShift(&manager_));
fayangb0c7b4b2019-09-12 06:45:24 -07001842
1843 QuicConfig config;
1844 QuicTagVector options;
1845 options.push_back(kILD1);
1846 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1847 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1848 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1849 manager_.SetFromConfig(config);
1850
fayangb0c7b4b2019-09-12 06:45:24 -07001851 EXPECT_EQ(kDefaultLossDelayShift,
1852 QuicSentPacketManagerPeer::GetReorderingShift(&manager_));
1853 EXPECT_FALSE(
1854 QuicSentPacketManagerPeer::AdaptiveReorderingThresholdEnabled(&manager_));
1855}
1856
fayangcff885a2019-10-22 07:39:04 -07001857TEST_F(QuicSentPacketManagerTest,
fayangb0c7b4b2019-09-12 06:45:24 -07001858 NegotiateIetfLossDetectionAdaptiveReorderingThreshold) {
fayang3be090d2020-02-11 14:05:08 -08001859 EXPECT_TRUE(
1860 QuicSentPacketManagerPeer::AdaptiveReorderingThresholdEnabled(&manager_));
1861 EXPECT_FALSE(
1862 QuicSentPacketManagerPeer::AdaptiveTimeThresholdEnabled(&manager_));
1863 EXPECT_EQ(kDefaultLossDelayShift,
1864 QuicSentPacketManagerPeer::GetReorderingShift(&manager_));
fayangb0c7b4b2019-09-12 06:45:24 -07001865
1866 QuicConfig config;
1867 QuicTagVector options;
1868 options.push_back(kILD2);
1869 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1870 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1871 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1872 manager_.SetFromConfig(config);
1873
fayangb0c7b4b2019-09-12 06:45:24 -07001874 EXPECT_EQ(3, QuicSentPacketManagerPeer::GetReorderingShift(&manager_));
1875 EXPECT_TRUE(
1876 QuicSentPacketManagerPeer::AdaptiveReorderingThresholdEnabled(&manager_));
1877}
1878
fayangcff885a2019-10-22 07:39:04 -07001879TEST_F(QuicSentPacketManagerTest,
fayangb0c7b4b2019-09-12 06:45:24 -07001880 NegotiateIetfLossDetectionAdaptiveReorderingThreshold2) {
fayang3be090d2020-02-11 14:05:08 -08001881 EXPECT_TRUE(
1882 QuicSentPacketManagerPeer::AdaptiveReorderingThresholdEnabled(&manager_));
1883 EXPECT_FALSE(
1884 QuicSentPacketManagerPeer::AdaptiveTimeThresholdEnabled(&manager_));
1885 EXPECT_EQ(kDefaultLossDelayShift,
1886 QuicSentPacketManagerPeer::GetReorderingShift(&manager_));
fayangb0c7b4b2019-09-12 06:45:24 -07001887
1888 QuicConfig config;
1889 QuicTagVector options;
1890 options.push_back(kILD3);
1891 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1892 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1893 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1894 manager_.SetFromConfig(config);
fayangb0c7b4b2019-09-12 06:45:24 -07001895 EXPECT_EQ(kDefaultLossDelayShift,
1896 QuicSentPacketManagerPeer::GetReorderingShift(&manager_));
1897 EXPECT_TRUE(
1898 QuicSentPacketManagerPeer::AdaptiveReorderingThresholdEnabled(&manager_));
1899}
1900
fayangdb095652019-11-04 07:37:36 -08001901TEST_F(QuicSentPacketManagerTest,
1902 NegotiateIetfLossDetectionAdaptiveReorderingAndTimeThreshold) {
fayang3be090d2020-02-11 14:05:08 -08001903 EXPECT_TRUE(
1904 QuicSentPacketManagerPeer::AdaptiveReorderingThresholdEnabled(&manager_));
1905 EXPECT_FALSE(
1906 QuicSentPacketManagerPeer::AdaptiveTimeThresholdEnabled(&manager_));
1907 EXPECT_EQ(kDefaultLossDelayShift,
1908 QuicSentPacketManagerPeer::GetReorderingShift(&manager_));
fayangdb095652019-11-04 07:37:36 -08001909
1910 QuicConfig config;
1911 QuicTagVector options;
1912 options.push_back(kILD4);
1913 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1914 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1915 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1916 manager_.SetFromConfig(config);
1917
fayangdb095652019-11-04 07:37:36 -08001918 EXPECT_EQ(kDefaultLossDelayShift,
1919 QuicSentPacketManagerPeer::GetReorderingShift(&manager_));
1920 EXPECT_TRUE(
1921 QuicSentPacketManagerPeer::AdaptiveReorderingThresholdEnabled(&manager_));
1922 EXPECT_TRUE(
1923 QuicSentPacketManagerPeer::AdaptiveTimeThresholdEnabled(&manager_));
1924}
1925
fayangcff885a2019-10-22 07:39:04 -07001926TEST_F(QuicSentPacketManagerTest, NegotiateCongestionControlFromOptions) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001927 QuicConfig config;
1928 QuicTagVector options;
1929
1930 options.push_back(kRENO);
1931 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1932 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1933 manager_.SetFromConfig(config);
1934 EXPECT_EQ(kRenoBytes, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_)
1935 ->GetCongestionControlType());
1936
1937 options.clear();
1938 options.push_back(kTBBR);
1939 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1940 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1941 manager_.SetFromConfig(config);
1942 EXPECT_EQ(kBBR, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_)
1943 ->GetCongestionControlType());
1944
1945 options.clear();
1946 options.push_back(kBYTE);
1947 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1948 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1949 manager_.SetFromConfig(config);
1950 EXPECT_EQ(kCubicBytes, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_)
1951 ->GetCongestionControlType());
1952 options.clear();
1953 options.push_back(kRENO);
1954 options.push_back(kBYTE);
1955 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
1956 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1957 manager_.SetFromConfig(config);
1958 EXPECT_EQ(kRenoBytes, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_)
1959 ->GetCongestionControlType());
1960}
1961
fayangcff885a2019-10-22 07:39:04 -07001962TEST_F(QuicSentPacketManagerTest, NegotiateClientCongestionControlFromOptions) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001963 QuicConfig config;
1964 QuicTagVector options;
1965
1966 // No change if the server receives client options.
1967 const SendAlgorithmInterface* mock_sender =
1968 QuicSentPacketManagerPeer::GetSendAlgorithm(manager_);
1969 options.push_back(kRENO);
1970 config.SetClientConnectionOptions(options);
1971 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
1972 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1973 manager_.SetFromConfig(config);
1974 EXPECT_EQ(mock_sender, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_));
1975
1976 // Change the congestion control on the client with client options.
1977 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
1978 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1979 manager_.SetFromConfig(config);
1980 EXPECT_EQ(kRenoBytes, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_)
1981 ->GetCongestionControlType());
1982
1983 options.clear();
1984 options.push_back(kTBBR);
1985 config.SetClientConnectionOptions(options);
1986 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1987 manager_.SetFromConfig(config);
1988 EXPECT_EQ(kBBR, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_)
1989 ->GetCongestionControlType());
1990
1991 options.clear();
1992 options.push_back(kBYTE);
1993 config.SetClientConnectionOptions(options);
1994 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
1995 manager_.SetFromConfig(config);
1996 EXPECT_EQ(kCubicBytes, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_)
1997 ->GetCongestionControlType());
1998
1999 options.clear();
2000 options.push_back(kRENO);
2001 options.push_back(kBYTE);
2002 config.SetClientConnectionOptions(options);
2003 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2004 manager_.SetFromConfig(config);
2005 EXPECT_EQ(kRenoBytes, QuicSentPacketManagerPeer::GetSendAlgorithm(manager_)
2006 ->GetCongestionControlType());
2007}
2008
fayangcff885a2019-10-22 07:39:04 -07002009TEST_F(QuicSentPacketManagerTest, NegotiateNoMinTLPFromOptionsAtServer) {
fayang5d011982020-05-13 14:14:38 -07002010 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2011 return;
2012 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002013 QuicConfig config;
2014 QuicTagVector options;
2015
2016 options.push_back(kMAD2);
2017 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
2018 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2019 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2020 EXPECT_CALL(*send_algorithm_, PacingRate(_))
2021 .WillRepeatedly(Return(QuicBandwidth::Zero()));
2022 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
2023 .WillOnce(Return(10 * kDefaultTCPMSS));
2024 manager_.SetFromConfig(config);
2025 // Set the initial RTT to 1us.
2026 QuicSentPacketManagerPeer::GetRttStats(&manager_)->set_initial_rtt(
2027 QuicTime::Delta::FromMicroseconds(1));
2028 // The TLP with fewer than 2 packets outstanding includes 1/2 min RTO(200ms).
2029 EXPECT_EQ(QuicTime::Delta::FromMicroseconds(100002),
2030 QuicSentPacketManagerPeer::GetTailLossProbeDelay(&manager_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002031
ianswettad65ab92019-10-28 07:19:07 -07002032 // Send two packets, and the TLP should be 1ms.
2033 QuicTime::Delta expected_tlp_delay = QuicTime::Delta::FromMilliseconds(1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002034 SendDataPacket(1);
2035 SendDataPacket(2);
ianswettdcc474e2019-08-06 08:22:38 -07002036 EXPECT_EQ(expected_tlp_delay,
QUICHE teama6ef0a62019-03-07 20:34:33 -05002037 QuicSentPacketManagerPeer::GetTailLossProbeDelay(&manager_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002038}
2039
fayangcff885a2019-10-22 07:39:04 -07002040TEST_F(QuicSentPacketManagerTest, NegotiateNoMinTLPFromOptionsAtClient) {
fayang5d011982020-05-13 14:14:38 -07002041 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2042 return;
2043 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002044 QuicConfig client_config;
2045 QuicTagVector options;
2046
2047 options.push_back(kMAD2);
2048 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
2049 client_config.SetConnectionOptionsToSend(options);
2050 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2051 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2052 EXPECT_CALL(*send_algorithm_, PacingRate(_))
2053 .WillRepeatedly(Return(QuicBandwidth::Zero()));
2054 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
2055 .WillOnce(Return(10 * kDefaultTCPMSS));
2056 manager_.SetFromConfig(client_config);
2057 // Set the initial RTT to 1us.
2058 QuicSentPacketManagerPeer::GetRttStats(&manager_)->set_initial_rtt(
2059 QuicTime::Delta::FromMicroseconds(1));
2060 // The TLP with fewer than 2 packets outstanding includes 1/2 min RTO(200ms).
2061 EXPECT_EQ(QuicTime::Delta::FromMicroseconds(100002),
2062 QuicSentPacketManagerPeer::GetTailLossProbeDelay(&manager_));
ianswettad65ab92019-10-28 07:19:07 -07002063 // Send two packets, and the TLP should be 1ms.
2064 QuicTime::Delta expected_tlp_delay = QuicTime::Delta::FromMilliseconds(1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002065 SendDataPacket(1);
2066 SendDataPacket(2);
ianswettdcc474e2019-08-06 08:22:38 -07002067 EXPECT_EQ(expected_tlp_delay,
QUICHE teama6ef0a62019-03-07 20:34:33 -05002068 QuicSentPacketManagerPeer::GetTailLossProbeDelay(&manager_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002069}
2070
fayangcff885a2019-10-22 07:39:04 -07002071TEST_F(QuicSentPacketManagerTest, NegotiateNoMinRTOFromOptionsAtServer) {
fayang5d011982020-05-13 14:14:38 -07002072 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2073 return;
2074 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002075 QuicConfig config;
2076 QuicTagVector options;
2077
2078 options.push_back(kMAD3);
2079 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
2080 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2081 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2082 manager_.SetFromConfig(config);
2083 // Provide one RTT measurement, because otherwise we use the default of 500ms.
2084 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
2085 rtt_stats->UpdateRtt(QuicTime::Delta::FromMicroseconds(1),
2086 QuicTime::Delta::Zero(), QuicTime::Zero());
ianswettad65ab92019-10-28 07:19:07 -07002087 QuicTime::Delta expected_rto_delay = QuicTime::Delta::FromMilliseconds(1);
ianswettdcc474e2019-08-06 08:22:38 -07002088 EXPECT_EQ(expected_rto_delay,
QUICHE teama6ef0a62019-03-07 20:34:33 -05002089 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002090 // The TLP with fewer than 2 packets outstanding includes 1/2 min RTO(0ms).
ianswettad65ab92019-10-28 07:19:07 -07002091 QuicTime::Delta expected_tlp_delay = QuicTime::Delta::FromMicroseconds(502);
ianswettdcc474e2019-08-06 08:22:38 -07002092 EXPECT_EQ(expected_tlp_delay,
QUICHE teama6ef0a62019-03-07 20:34:33 -05002093 QuicSentPacketManagerPeer::GetTailLossProbeDelay(&manager_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002094}
2095
fayangcff885a2019-10-22 07:39:04 -07002096TEST_F(QuicSentPacketManagerTest, NegotiateNoMinRTOFromOptionsAtClient) {
fayang5d011982020-05-13 14:14:38 -07002097 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2098 return;
2099 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002100 QuicConfig client_config;
2101 QuicTagVector options;
2102
2103 options.push_back(kMAD3);
2104 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
2105 client_config.SetConnectionOptionsToSend(options);
2106 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2107 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2108 manager_.SetFromConfig(client_config);
2109 // Provide one RTT measurement, because otherwise we use the default of 500ms.
2110 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
2111 rtt_stats->UpdateRtt(QuicTime::Delta::FromMicroseconds(1),
2112 QuicTime::Delta::Zero(), QuicTime::Zero());
ianswettad65ab92019-10-28 07:19:07 -07002113 QuicTime::Delta expected_rto_delay = QuicTime::Delta::FromMilliseconds(1);
ianswettdcc474e2019-08-06 08:22:38 -07002114 EXPECT_EQ(expected_rto_delay,
QUICHE teama6ef0a62019-03-07 20:34:33 -05002115 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002116 // The TLP with fewer than 2 packets outstanding includes 1/2 min RTO(0ms).
ianswettad65ab92019-10-28 07:19:07 -07002117 QuicTime::Delta expected_tlp_delay = QuicTime::Delta::FromMicroseconds(502);
ianswettdcc474e2019-08-06 08:22:38 -07002118 EXPECT_EQ(expected_tlp_delay,
QUICHE teama6ef0a62019-03-07 20:34:33 -05002119 QuicSentPacketManagerPeer::GetTailLossProbeDelay(&manager_));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002120}
2121
fayangcff885a2019-10-22 07:39:04 -07002122TEST_F(QuicSentPacketManagerTest, NegotiateNoTLPFromOptionsAtServer) {
fayang5d011982020-05-13 14:14:38 -07002123 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2124 return;
2125 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002126 QuicConfig config;
2127 QuicTagVector options;
2128
2129 options.push_back(kNTLP);
2130 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
2131 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2132 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2133 manager_.SetFromConfig(config);
2134 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_));
2135}
2136
fayangcff885a2019-10-22 07:39:04 -07002137TEST_F(QuicSentPacketManagerTest, NegotiateNoTLPFromOptionsAtClient) {
fayang5d011982020-05-13 14:14:38 -07002138 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2139 return;
2140 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002141 QuicConfig client_config;
2142 QuicTagVector options;
2143
2144 options.push_back(kNTLP);
2145 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
2146 client_config.SetConnectionOptionsToSend(options);
2147 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2148 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2149 manager_.SetFromConfig(client_config);
2150 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_));
2151}
2152
fayangcff885a2019-10-22 07:39:04 -07002153TEST_F(QuicSentPacketManagerTest, Negotiate1TLPFromOptionsAtServer) {
fayang5d011982020-05-13 14:14:38 -07002154 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2155 return;
2156 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002157 QuicConfig config;
2158 QuicTagVector options;
2159
2160 options.push_back(k1TLP);
2161 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
2162 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2163 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2164 manager_.SetFromConfig(config);
2165 EXPECT_EQ(1u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_));
2166}
2167
fayangcff885a2019-10-22 07:39:04 -07002168TEST_F(QuicSentPacketManagerTest, Negotiate1TLPFromOptionsAtClient) {
fayang5d011982020-05-13 14:14:38 -07002169 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2170 return;
2171 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002172 QuicConfig client_config;
2173 QuicTagVector options;
2174
2175 options.push_back(k1TLP);
2176 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
2177 client_config.SetConnectionOptionsToSend(options);
2178 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2179 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2180 manager_.SetFromConfig(client_config);
2181 EXPECT_EQ(1u, QuicSentPacketManagerPeer::GetMaxTailLossProbes(&manager_));
2182}
2183
fayangcff885a2019-10-22 07:39:04 -07002184TEST_F(QuicSentPacketManagerTest, NegotiateTLPRttFromOptionsAtServer) {
fayang5d011982020-05-13 14:14:38 -07002185 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2186 return;
2187 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002188 QuicConfig config;
2189 QuicTagVector options;
2190
2191 options.push_back(kTLPR);
2192 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
2193 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2194 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2195 manager_.SetFromConfig(config);
2196 EXPECT_TRUE(
2197 QuicSentPacketManagerPeer::GetEnableHalfRttTailLossProbe(&manager_));
2198}
2199
fayangcff885a2019-10-22 07:39:04 -07002200TEST_F(QuicSentPacketManagerTest, NegotiateTLPRttFromOptionsAtClient) {
fayang5d011982020-05-13 14:14:38 -07002201 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2202 return;
2203 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002204 QuicConfig client_config;
2205 QuicTagVector options;
2206
2207 options.push_back(kTLPR);
2208 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
2209 client_config.SetConnectionOptionsToSend(options);
2210 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2211 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2212 manager_.SetFromConfig(client_config);
2213 EXPECT_TRUE(
2214 QuicSentPacketManagerPeer::GetEnableHalfRttTailLossProbe(&manager_));
2215}
2216
fayangcff885a2019-10-22 07:39:04 -07002217TEST_F(QuicSentPacketManagerTest, NegotiateNewRTOFromOptionsAtServer) {
fayang5d011982020-05-13 14:14:38 -07002218 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2219 return;
2220 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002221 EXPECT_FALSE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
2222 QuicConfig config;
2223 QuicTagVector options;
2224
2225 options.push_back(kNRTO);
2226 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
2227 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2228 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2229 manager_.SetFromConfig(config);
2230 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
2231}
2232
fayangcff885a2019-10-22 07:39:04 -07002233TEST_F(QuicSentPacketManagerTest, NegotiateNewRTOFromOptionsAtClient) {
fayang5d011982020-05-13 14:14:38 -07002234 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2235 return;
2236 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002237 EXPECT_FALSE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
2238 QuicConfig client_config;
2239 QuicTagVector options;
2240
2241 options.push_back(kNRTO);
2242 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
2243 client_config.SetConnectionOptionsToSend(options);
2244 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2245 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2246 manager_.SetFromConfig(client_config);
2247 EXPECT_TRUE(QuicSentPacketManagerPeer::GetUseNewRto(&manager_));
2248}
2249
fayangcff885a2019-10-22 07:39:04 -07002250TEST_F(QuicSentPacketManagerTest, UseInitialRoundTripTimeToSend) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002251 QuicTime::Delta initial_rtt = QuicTime::Delta::FromMilliseconds(325);
2252 EXPECT_NE(initial_rtt, manager_.GetRttStats()->smoothed_rtt());
2253
2254 QuicConfig config;
2255 config.SetInitialRoundTripTimeUsToSend(initial_rtt.ToMicroseconds());
2256 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2257 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2258 manager_.SetFromConfig(config);
2259
2260 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.GetRttStats()->smoothed_rtt());
2261 EXPECT_EQ(initial_rtt, manager_.GetRttStats()->initial_rtt());
2262}
2263
fayangcff885a2019-10-22 07:39:04 -07002264TEST_F(QuicSentPacketManagerTest, ResumeConnectionState) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002265 // The sent packet manager should use the RTT from CachedNetworkParameters if
2266 // it is provided.
2267 const QuicTime::Delta kRtt = QuicTime::Delta::FromMilliseconds(1234);
2268 CachedNetworkParameters cached_network_params;
2269 cached_network_params.set_min_rtt_ms(kRtt.ToMilliseconds());
2270
QUICHE teamb4e187c2019-11-14 06:22:50 -08002271 SendAlgorithmInterface::NetworkParams params;
2272 params.bandwidth = QuicBandwidth::Zero();
2273 params.allow_cwnd_to_decrease = false;
2274 params.rtt = kRtt;
2275
2276 EXPECT_CALL(*send_algorithm_, AdjustNetworkParameters(params));
fayangbe83ecd2019-04-26 13:58:09 -07002277 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
2278 .Times(testing::AnyNumber());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002279 manager_.ResumeConnectionState(cached_network_params, false);
2280 EXPECT_EQ(kRtt, manager_.GetRttStats()->initial_rtt());
2281}
2282
fayangcff885a2019-10-22 07:39:04 -07002283TEST_F(QuicSentPacketManagerTest, ConnectionMigrationUnspecifiedChange) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002284 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
2285 QuicTime::Delta default_init_rtt = rtt_stats->initial_rtt();
2286 rtt_stats->set_initial_rtt(default_init_rtt * 2);
2287 EXPECT_EQ(2 * default_init_rtt, rtt_stats->initial_rtt());
2288
2289 QuicSentPacketManagerPeer::SetConsecutiveRtoCount(&manager_, 1);
2290 EXPECT_EQ(1u, manager_.GetConsecutiveRtoCount());
2291 QuicSentPacketManagerPeer::SetConsecutiveTlpCount(&manager_, 2);
2292 EXPECT_EQ(2u, manager_.GetConsecutiveTlpCount());
2293
2294 EXPECT_CALL(*send_algorithm_, OnConnectionMigration());
2295 manager_.OnConnectionMigration(IPV4_TO_IPV4_CHANGE);
2296
2297 EXPECT_EQ(default_init_rtt, rtt_stats->initial_rtt());
2298 EXPECT_EQ(0u, manager_.GetConsecutiveRtoCount());
2299 EXPECT_EQ(0u, manager_.GetConsecutiveTlpCount());
2300}
2301
fayangcff885a2019-10-22 07:39:04 -07002302TEST_F(QuicSentPacketManagerTest, ConnectionMigrationIPSubnetChange) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002303 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
2304 QuicTime::Delta default_init_rtt = rtt_stats->initial_rtt();
2305 rtt_stats->set_initial_rtt(default_init_rtt * 2);
2306 EXPECT_EQ(2 * default_init_rtt, rtt_stats->initial_rtt());
2307
2308 QuicSentPacketManagerPeer::SetConsecutiveRtoCount(&manager_, 1);
2309 EXPECT_EQ(1u, manager_.GetConsecutiveRtoCount());
2310 QuicSentPacketManagerPeer::SetConsecutiveTlpCount(&manager_, 2);
2311 EXPECT_EQ(2u, manager_.GetConsecutiveTlpCount());
2312
2313 manager_.OnConnectionMigration(IPV4_SUBNET_CHANGE);
2314
2315 EXPECT_EQ(2 * default_init_rtt, rtt_stats->initial_rtt());
2316 EXPECT_EQ(1u, manager_.GetConsecutiveRtoCount());
2317 EXPECT_EQ(2u, manager_.GetConsecutiveTlpCount());
2318}
2319
fayangcff885a2019-10-22 07:39:04 -07002320TEST_F(QuicSentPacketManagerTest, ConnectionMigrationPortChange) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002321 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
2322 QuicTime::Delta default_init_rtt = rtt_stats->initial_rtt();
2323 rtt_stats->set_initial_rtt(default_init_rtt * 2);
2324 EXPECT_EQ(2 * default_init_rtt, rtt_stats->initial_rtt());
2325
2326 QuicSentPacketManagerPeer::SetConsecutiveRtoCount(&manager_, 1);
2327 EXPECT_EQ(1u, manager_.GetConsecutiveRtoCount());
2328 QuicSentPacketManagerPeer::SetConsecutiveTlpCount(&manager_, 2);
2329 EXPECT_EQ(2u, manager_.GetConsecutiveTlpCount());
2330
2331 manager_.OnConnectionMigration(PORT_CHANGE);
2332
2333 EXPECT_EQ(2 * default_init_rtt, rtt_stats->initial_rtt());
2334 EXPECT_EQ(1u, manager_.GetConsecutiveRtoCount());
2335 EXPECT_EQ(2u, manager_.GetConsecutiveTlpCount());
2336}
2337
fayangcff885a2019-10-22 07:39:04 -07002338TEST_F(QuicSentPacketManagerTest, PathMtuIncreased) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002339 EXPECT_CALL(*send_algorithm_,
2340 OnPacketSent(_, BytesInFlight(), QuicPacketNumber(1), _, _));
2341 SerializedPacket packet(QuicPacketNumber(1), PACKET_4BYTE_PACKET_NUMBER,
2342 nullptr, kDefaultLength + 100, false, false);
fayangcff885a2019-10-22 07:39:04 -07002343 manager_.OnPacketSent(&packet, clock_.Now(), NOT_RETRANSMISSION,
2344 HAS_RETRANSMITTABLE_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002345
2346 // Ack the large packet and expect the path MTU to increase.
2347 ExpectAck(1);
2348 EXPECT_CALL(*network_change_visitor_,
2349 OnPathMtuIncreased(kDefaultLength + 100));
2350 QuicAckFrame ack_frame = InitAckFrame(1);
2351 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
2352 clock_.Now());
2353 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -07002354 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002355 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
2356 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002357}
2358
fayangcff885a2019-10-22 07:39:04 -07002359TEST_F(QuicSentPacketManagerTest, OnAckRangeSlowPath) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002360 // Send packets 1 - 20.
2361 for (size_t i = 1; i <= 20; ++i) {
2362 SendDataPacket(i);
2363 }
2364 // Ack [5, 7), [10, 12), [15, 17).
2365 uint64_t acked1[] = {5, 6, 10, 11, 15, 16};
2366 uint64_t lost1[] = {1, 2, 3, 4, 7, 8, 9, 12, 13};
bnc4e9283d2019-12-17 07:08:57 -08002367 ExpectAcksAndLosses(true, acked1, QUICHE_ARRAYSIZE(acked1), lost1,
2368 QUICHE_ARRAYSIZE(lost1));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002369 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(AnyNumber());
2370 manager_.OnAckFrameStart(QuicPacketNumber(16), QuicTime::Delta::Infinite(),
2371 clock_.Now());
2372 manager_.OnAckRange(QuicPacketNumber(15), QuicPacketNumber(17));
2373 manager_.OnAckRange(QuicPacketNumber(10), QuicPacketNumber(12));
2374 manager_.OnAckRange(QuicPacketNumber(5), QuicPacketNumber(7));
2375 // Make sure empty range does not harm.
2376 manager_.OnAckRange(QuicPacketNumber(4), QuicPacketNumber(4));
fayang3eb82212019-04-16 12:05:46 -07002377 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002378 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
2379 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002380
2381 // Ack [4, 8), [9, 13), [14, 21).
2382 uint64_t acked2[] = {4, 7, 9, 12, 14, 17, 18, 19, 20};
bnc4e9283d2019-12-17 07:08:57 -08002383 ExpectAcksAndLosses(true, acked2, QUICHE_ARRAYSIZE(acked2), nullptr, 0);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002384 manager_.OnAckFrameStart(QuicPacketNumber(20), QuicTime::Delta::Infinite(),
2385 clock_.Now());
2386 manager_.OnAckRange(QuicPacketNumber(14), QuicPacketNumber(21));
2387 manager_.OnAckRange(QuicPacketNumber(9), QuicPacketNumber(13));
2388 manager_.OnAckRange(QuicPacketNumber(4), QuicPacketNumber(8));
fayang3eb82212019-04-16 12:05:46 -07002389 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002390 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(2),
2391 ENCRYPTION_INITIAL));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002392}
2393
fayangcff885a2019-10-22 07:39:04 -07002394TEST_F(QuicSentPacketManagerTest, TolerateReneging) {
QUICHE team9929cc42019-03-13 08:17:43 -07002395 // Send packets 1 - 20.
2396 for (size_t i = 1; i <= 20; ++i) {
2397 SendDataPacket(i);
2398 }
2399 // Ack [5, 7), [10, 12), [15, 17).
2400 uint64_t acked1[] = {5, 6, 10, 11, 15, 16};
2401 uint64_t lost1[] = {1, 2, 3, 4, 7, 8, 9, 12, 13};
bnc4e9283d2019-12-17 07:08:57 -08002402 ExpectAcksAndLosses(true, acked1, QUICHE_ARRAYSIZE(acked1), lost1,
2403 QUICHE_ARRAYSIZE(lost1));
QUICHE team9929cc42019-03-13 08:17:43 -07002404 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(AnyNumber());
2405 manager_.OnAckFrameStart(QuicPacketNumber(16), QuicTime::Delta::Infinite(),
2406 clock_.Now());
2407 manager_.OnAckRange(QuicPacketNumber(15), QuicPacketNumber(17));
2408 manager_.OnAckRange(QuicPacketNumber(10), QuicPacketNumber(12));
2409 manager_.OnAckRange(QuicPacketNumber(5), QuicPacketNumber(7));
fayang3eb82212019-04-16 12:05:46 -07002410 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002411 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
2412 ENCRYPTION_INITIAL));
QUICHE team9929cc42019-03-13 08:17:43 -07002413
2414 // Making sure reneged ACK does not harm. Ack [4, 8), [9, 13).
2415 uint64_t acked2[] = {4, 7, 9, 12};
bnc4e9283d2019-12-17 07:08:57 -08002416 ExpectAcksAndLosses(true, acked2, QUICHE_ARRAYSIZE(acked2), nullptr, 0);
QUICHE team9929cc42019-03-13 08:17:43 -07002417 manager_.OnAckFrameStart(QuicPacketNumber(12), QuicTime::Delta::Infinite(),
2418 clock_.Now());
2419 manager_.OnAckRange(QuicPacketNumber(9), QuicPacketNumber(13));
2420 manager_.OnAckRange(QuicPacketNumber(4), QuicPacketNumber(8));
fayang3eb82212019-04-16 12:05:46 -07002421 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002422 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(2),
2423 ENCRYPTION_INITIAL));
QUICHE team9929cc42019-03-13 08:17:43 -07002424 EXPECT_EQ(QuicPacketNumber(16), manager_.GetLargestObserved());
2425}
2426
fayangcff885a2019-10-22 07:39:04 -07002427TEST_F(QuicSentPacketManagerTest, MultiplePacketNumberSpaces) {
QUICHE teamc279cec2019-03-22 06:51:48 -07002428 manager_.EnableMultiplePacketNumberSpacesSupport();
fayang533cb1b2020-01-28 08:05:08 -08002429 const QuicUnackedPacketMap* unacked_packets =
2430 QuicSentPacketManagerPeer::GetUnackedPacketMap(&manager_);
QUICHE teamc279cec2019-03-22 06:51:48 -07002431 EXPECT_FALSE(
fayang533cb1b2020-01-28 08:05:08 -08002432 unacked_packets
2433 ->GetLargestSentRetransmittableOfPacketNumberSpace(INITIAL_DATA)
2434 .IsInitialized());
QUICHE teamc279cec2019-03-22 06:51:48 -07002435 EXPECT_FALSE(
2436 manager_.GetLargestAckedPacket(ENCRYPTION_INITIAL).IsInitialized());
2437 // Send packet 1.
2438 SendDataPacket(1, ENCRYPTION_INITIAL);
2439 EXPECT_EQ(QuicPacketNumber(1),
fayang533cb1b2020-01-28 08:05:08 -08002440 unacked_packets->GetLargestSentRetransmittableOfPacketNumberSpace(
2441 INITIAL_DATA));
QUICHE teamc279cec2019-03-22 06:51:48 -07002442 EXPECT_FALSE(
fayang533cb1b2020-01-28 08:05:08 -08002443 unacked_packets
2444 ->GetLargestSentRetransmittableOfPacketNumberSpace(HANDSHAKE_DATA)
2445 .IsInitialized());
QUICHE teamc279cec2019-03-22 06:51:48 -07002446 // Ack packet 1.
2447 ExpectAck(1);
2448 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
2449 clock_.Now());
2450 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
fayang3eb82212019-04-16 12:05:46 -07002451 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002452 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
2453 ENCRYPTION_INITIAL));
QUICHE teamc279cec2019-03-22 06:51:48 -07002454 EXPECT_EQ(QuicPacketNumber(1),
2455 manager_.GetLargestAckedPacket(ENCRYPTION_INITIAL));
2456 EXPECT_FALSE(
2457 manager_.GetLargestAckedPacket(ENCRYPTION_HANDSHAKE).IsInitialized());
2458 // Send packets 2 and 3.
2459 SendDataPacket(2, ENCRYPTION_HANDSHAKE);
2460 SendDataPacket(3, ENCRYPTION_HANDSHAKE);
2461 EXPECT_EQ(QuicPacketNumber(1),
fayang533cb1b2020-01-28 08:05:08 -08002462 unacked_packets->GetLargestSentRetransmittableOfPacketNumberSpace(
2463 INITIAL_DATA));
QUICHE teamc279cec2019-03-22 06:51:48 -07002464 EXPECT_EQ(QuicPacketNumber(3),
fayang533cb1b2020-01-28 08:05:08 -08002465 unacked_packets->GetLargestSentRetransmittableOfPacketNumberSpace(
2466 HANDSHAKE_DATA));
QUICHE teamc279cec2019-03-22 06:51:48 -07002467 EXPECT_FALSE(
fayang533cb1b2020-01-28 08:05:08 -08002468 unacked_packets
2469 ->GetLargestSentRetransmittableOfPacketNumberSpace(APPLICATION_DATA)
2470 .IsInitialized());
QUICHE teamc279cec2019-03-22 06:51:48 -07002471 // Ack packet 2.
2472 ExpectAck(2);
2473 manager_.OnAckFrameStart(QuicPacketNumber(2), QuicTime::Delta::Infinite(),
2474 clock_.Now());
2475 manager_.OnAckRange(QuicPacketNumber(2), QuicPacketNumber(3));
fayang3eb82212019-04-16 12:05:46 -07002476 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002477 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(2),
2478 ENCRYPTION_HANDSHAKE));
QUICHE teamc279cec2019-03-22 06:51:48 -07002479 EXPECT_EQ(QuicPacketNumber(2),
2480 manager_.GetLargestAckedPacket(ENCRYPTION_HANDSHAKE));
2481 EXPECT_FALSE(
2482 manager_.GetLargestAckedPacket(ENCRYPTION_ZERO_RTT).IsInitialized());
2483 // Ack packet 3.
2484 ExpectAck(3);
2485 manager_.OnAckFrameStart(QuicPacketNumber(3), QuicTime::Delta::Infinite(),
2486 clock_.Now());
2487 manager_.OnAckRange(QuicPacketNumber(2), QuicPacketNumber(4));
fayang3eb82212019-04-16 12:05:46 -07002488 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002489 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(3),
2490 ENCRYPTION_HANDSHAKE));
QUICHE teamc279cec2019-03-22 06:51:48 -07002491 EXPECT_EQ(QuicPacketNumber(3),
2492 manager_.GetLargestAckedPacket(ENCRYPTION_HANDSHAKE));
2493 EXPECT_FALSE(
2494 manager_.GetLargestAckedPacket(ENCRYPTION_ZERO_RTT).IsInitialized());
2495 // Send packets 4 and 5.
2496 SendDataPacket(4, ENCRYPTION_ZERO_RTT);
2497 SendDataPacket(5, ENCRYPTION_ZERO_RTT);
2498 EXPECT_EQ(QuicPacketNumber(1),
fayang533cb1b2020-01-28 08:05:08 -08002499 unacked_packets->GetLargestSentRetransmittableOfPacketNumberSpace(
2500 INITIAL_DATA));
QUICHE teamc279cec2019-03-22 06:51:48 -07002501 EXPECT_EQ(QuicPacketNumber(3),
fayang533cb1b2020-01-28 08:05:08 -08002502 unacked_packets->GetLargestSentRetransmittableOfPacketNumberSpace(
2503 HANDSHAKE_DATA));
QUICHE teamc279cec2019-03-22 06:51:48 -07002504 EXPECT_EQ(QuicPacketNumber(5),
fayang533cb1b2020-01-28 08:05:08 -08002505 unacked_packets->GetLargestSentRetransmittableOfPacketNumberSpace(
2506 APPLICATION_DATA));
QUICHE teamc279cec2019-03-22 06:51:48 -07002507 // Ack packet 5.
2508 ExpectAck(5);
2509 manager_.OnAckFrameStart(QuicPacketNumber(5), QuicTime::Delta::Infinite(),
2510 clock_.Now());
2511 manager_.OnAckRange(QuicPacketNumber(5), QuicPacketNumber(6));
fayang3eb82212019-04-16 12:05:46 -07002512 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002513 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(4),
2514 ENCRYPTION_FORWARD_SECURE));
QUICHE teamc279cec2019-03-22 06:51:48 -07002515 EXPECT_EQ(QuicPacketNumber(3),
2516 manager_.GetLargestAckedPacket(ENCRYPTION_HANDSHAKE));
2517 EXPECT_EQ(QuicPacketNumber(5),
2518 manager_.GetLargestAckedPacket(ENCRYPTION_ZERO_RTT));
2519 EXPECT_EQ(QuicPacketNumber(5),
2520 manager_.GetLargestAckedPacket(ENCRYPTION_FORWARD_SECURE));
2521
2522 // Send packets 6 - 8.
2523 SendDataPacket(6, ENCRYPTION_FORWARD_SECURE);
2524 SendDataPacket(7, ENCRYPTION_FORWARD_SECURE);
2525 SendDataPacket(8, ENCRYPTION_FORWARD_SECURE);
2526 EXPECT_EQ(QuicPacketNumber(1),
fayang533cb1b2020-01-28 08:05:08 -08002527 unacked_packets->GetLargestSentRetransmittableOfPacketNumberSpace(
2528 INITIAL_DATA));
QUICHE teamc279cec2019-03-22 06:51:48 -07002529 EXPECT_EQ(QuicPacketNumber(3),
fayang533cb1b2020-01-28 08:05:08 -08002530 unacked_packets->GetLargestSentRetransmittableOfPacketNumberSpace(
2531 HANDSHAKE_DATA));
QUICHE teamc279cec2019-03-22 06:51:48 -07002532 EXPECT_EQ(QuicPacketNumber(8),
fayang533cb1b2020-01-28 08:05:08 -08002533 unacked_packets->GetLargestSentRetransmittableOfPacketNumberSpace(
2534 APPLICATION_DATA));
QUICHE teamc279cec2019-03-22 06:51:48 -07002535 // Ack all packets.
2536 uint64_t acked[] = {4, 6, 7, 8};
bnc4e9283d2019-12-17 07:08:57 -08002537 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
QUICHE teamc279cec2019-03-22 06:51:48 -07002538 manager_.OnAckFrameStart(QuicPacketNumber(8), QuicTime::Delta::Infinite(),
2539 clock_.Now());
2540 manager_.OnAckRange(QuicPacketNumber(4), QuicPacketNumber(9));
fayang3eb82212019-04-16 12:05:46 -07002541 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002542 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(5),
2543 ENCRYPTION_FORWARD_SECURE));
QUICHE teamc279cec2019-03-22 06:51:48 -07002544 EXPECT_EQ(QuicPacketNumber(3),
2545 manager_.GetLargestAckedPacket(ENCRYPTION_HANDSHAKE));
2546 EXPECT_EQ(QuicPacketNumber(8),
2547 manager_.GetLargestAckedPacket(ENCRYPTION_ZERO_RTT));
2548 EXPECT_EQ(QuicPacketNumber(8),
2549 manager_.GetLargestAckedPacket(ENCRYPTION_FORWARD_SECURE));
2550}
2551
fayangcff885a2019-10-22 07:39:04 -07002552TEST_F(QuicSentPacketManagerTest, PacketsGetAckedInWrongPacketNumberSpace) {
fayang3eb82212019-04-16 12:05:46 -07002553 manager_.EnableMultiplePacketNumberSpacesSupport();
2554 // Send packet 1.
2555 SendDataPacket(1, ENCRYPTION_INITIAL);
2556 // Send packets 2 and 3.
2557 SendDataPacket(2, ENCRYPTION_HANDSHAKE);
2558 SendDataPacket(3, ENCRYPTION_HANDSHAKE);
2559
2560 // ACK packets 2 and 3 in the wrong packet number space.
2561 manager_.OnAckFrameStart(QuicPacketNumber(3), QuicTime::Delta::Infinite(),
2562 clock_.Now());
2563 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(4));
2564 EXPECT_EQ(PACKETS_ACKED_IN_WRONG_PACKET_NUMBER_SPACE,
fayangf8e918b2019-07-16 13:03:16 -07002565 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
2566 ENCRYPTION_INITIAL));
fayang3eb82212019-04-16 12:05:46 -07002567}
2568
fayangcff885a2019-10-22 07:39:04 -07002569TEST_F(QuicSentPacketManagerTest, PacketsGetAckedInWrongPacketNumberSpace2) {
fayang3eb82212019-04-16 12:05:46 -07002570 manager_.EnableMultiplePacketNumberSpacesSupport();
2571 // Send packet 1.
2572 SendDataPacket(1, ENCRYPTION_INITIAL);
2573 // Send packets 2 and 3.
2574 SendDataPacket(2, ENCRYPTION_HANDSHAKE);
2575 SendDataPacket(3, ENCRYPTION_HANDSHAKE);
2576
2577 // ACK packet 1 in the wrong packet number space.
2578 manager_.OnAckFrameStart(QuicPacketNumber(3), QuicTime::Delta::Infinite(),
2579 clock_.Now());
2580 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(4));
2581 EXPECT_EQ(PACKETS_ACKED_IN_WRONG_PACKET_NUMBER_SPACE,
fayangf8e918b2019-07-16 13:03:16 -07002582 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
2583 ENCRYPTION_HANDSHAKE));
fayang3eb82212019-04-16 12:05:46 -07002584}
2585
fayangcff885a2019-10-22 07:39:04 -07002586TEST_F(QuicSentPacketManagerTest,
fayang3eb82212019-04-16 12:05:46 -07002587 ToleratePacketsGetAckedInWrongPacketNumberSpace) {
fayang3eb82212019-04-16 12:05:46 -07002588 manager_.EnableMultiplePacketNumberSpacesSupport();
2589 // Send packet 1.
2590 SendDataPacket(1, ENCRYPTION_INITIAL);
2591 // Ack packet 1.
2592 ExpectAck(1);
2593 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
2594 clock_.Now());
2595 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
2596 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002597 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
2598 ENCRYPTION_INITIAL));
fayang3eb82212019-04-16 12:05:46 -07002599
2600 // Send packets 2 and 3.
2601 SendDataPacket(2, ENCRYPTION_HANDSHAKE);
2602 SendDataPacket(3, ENCRYPTION_HANDSHAKE);
2603
2604 // Packet 1 gets acked in the wrong packet number space. Since packet 1 has
2605 // been acked in the correct packet number space, tolerate it.
2606 uint64_t acked[] = {2, 3};
bnc4e9283d2019-12-17 07:08:57 -08002607 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
fayang3eb82212019-04-16 12:05:46 -07002608 manager_.OnAckFrameStart(QuicPacketNumber(3), QuicTime::Delta::Infinite(),
2609 clock_.Now());
2610 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(4));
2611 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002612 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(2),
2613 ENCRYPTION_HANDSHAKE));
fayang3eb82212019-04-16 12:05:46 -07002614}
2615
fayang06bb1b02019-06-14 14:07:12 -07002616// Regression test for b/133771183.
fayangcff885a2019-10-22 07:39:04 -07002617TEST_F(QuicSentPacketManagerTest, PacketInLimbo) {
fayang5d011982020-05-13 14:14:38 -07002618 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2619 return;
2620 }
fayang06bb1b02019-06-14 14:07:12 -07002621 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
2622 // Send SHLO.
2623 SendCryptoPacket(1);
2624 // Send data packet.
2625 SendDataPacket(2, ENCRYPTION_FORWARD_SECURE);
2626 // Send Ack Packet.
2627 SendAckPacket(3, 1, ENCRYPTION_FORWARD_SECURE);
2628 // Retransmit SHLO.
2629 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
2630 .WillOnce(InvokeWithoutArgs([this]() { RetransmitCryptoPacket(4); }));
2631 manager_.OnRetransmissionTimeout();
2632
2633 // Successfully decrypt a forward secure packet.
2634 manager_.SetHandshakeConfirmed();
2635 EXPECT_CALL(notifier_, HasUnackedCryptoData()).WillRepeatedly(Return(false));
2636 // Send Ack packet.
2637 SendAckPacket(5, 2, ENCRYPTION_FORWARD_SECURE);
2638
2639 // Retransmission alarm fires.
2640 manager_.OnRetransmissionTimeout();
2641 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
2642 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
2643 RetransmitDataPacket(6, type, ENCRYPTION_FORWARD_SECURE);
2644 })));
2645 manager_.MaybeRetransmitTailLossProbe();
2646
2647 // Received Ack of packets 1, 3 and 4.
2648 uint64_t acked[] = {1, 3, 4};
bnc4e9283d2019-12-17 07:08:57 -08002649 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
fayang06bb1b02019-06-14 14:07:12 -07002650 manager_.OnAckFrameStart(QuicPacketNumber(4), QuicTime::Delta::Infinite(),
2651 clock_.Now());
2652 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(5));
2653 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
2654 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002655 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
2656 ENCRYPTION_INITIAL));
fayang06bb1b02019-06-14 14:07:12 -07002657
2658 uint64_t acked2[] = {5, 6};
2659 uint64_t loss[] = {2};
fayang897605d2019-08-21 13:26:45 -07002660 // Verify packet 2 is detected lost.
2661 EXPECT_CALL(notifier_, OnFrameLost(_)).Times(1);
bnc4e9283d2019-12-17 07:08:57 -08002662 ExpectAcksAndLosses(true, acked2, QUICHE_ARRAYSIZE(acked2), loss,
2663 QUICHE_ARRAYSIZE(loss));
fayang06bb1b02019-06-14 14:07:12 -07002664 manager_.OnAckFrameStart(QuicPacketNumber(6), QuicTime::Delta::Infinite(),
2665 clock_.Now());
2666 manager_.OnAckRange(QuicPacketNumber(3), QuicPacketNumber(7));
2667 EXPECT_EQ(PACKETS_NEWLY_ACKED,
fayangf8e918b2019-07-16 13:03:16 -07002668 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(2),
2669 ENCRYPTION_INITIAL));
fayang06bb1b02019-06-14 14:07:12 -07002670}
2671
fayangcff885a2019-10-22 07:39:04 -07002672TEST_F(QuicSentPacketManagerTest, RtoFiresNoPacketToRetransmit) {
fayang5d011982020-05-13 14:14:38 -07002673 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2674 return;
2675 }
fayanga29eb242019-07-16 12:25:38 -07002676 // Send 10 packets.
2677 for (size_t i = 1; i <= 10; ++i) {
2678 SendDataPacket(i);
2679 }
2680 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
2681 .Times(2)
2682 .WillOnce(WithArgs<1>(Invoke(
2683 [this](TransmissionType type) { RetransmitDataPacket(11, type); })))
2684 .WillOnce(WithArgs<1>(Invoke(
2685 [this](TransmissionType type) { RetransmitDataPacket(12, type); })));
2686 manager_.OnRetransmissionTimeout();
2687 EXPECT_EQ(1u, stats_.rto_count);
2688 EXPECT_EQ(0u, manager_.pending_timer_transmission_count());
2689
2690 // RTO fires again, but there is no packet to be RTO retransmitted.
2691 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
2692 EXPECT_CALL(notifier_, RetransmitFrames(_, _)).Times(0);
2693 manager_.OnRetransmissionTimeout();
2694 EXPECT_EQ(2u, stats_.rto_count);
fayange861aee2019-10-16 13:40:39 -07002695 // Verify a credit is raised up.
2696 EXPECT_EQ(1u, manager_.pending_timer_transmission_count());
fayanga29eb242019-07-16 12:25:38 -07002697}
2698
fayangcff885a2019-10-22 07:39:04 -07002699TEST_F(QuicSentPacketManagerTest, ComputingProbeTimeout) {
fayangce0a3162019-08-15 09:05:36 -07002700 EnablePto(k2PTO);
2701 EXPECT_CALL(*send_algorithm_, PacingRate(_))
2702 .WillRepeatedly(Return(QuicBandwidth::Zero()));
2703 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
2704 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
2705 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
2706 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
2707 QuicTime::Delta::Zero(), QuicTime::Zero());
2708 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
2709
2710 SendDataPacket(1, ENCRYPTION_FORWARD_SECURE);
2711 // Verify PTO is correctly set.
fayang5d011982020-05-13 14:14:38 -07002712 int pto_rttvar_multiplier =
2713 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayangce0a3162019-08-15 09:05:36 -07002714 QuicTime::Delta expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07002715 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayangce0a3162019-08-15 09:05:36 -07002716 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
fayang5d011982020-05-13 14:14:38 -07002717 QuicTime packet1_sent_time = clock_.Now();
fayangce0a3162019-08-15 09:05:36 -07002718 EXPECT_EQ(clock_.Now() + expected_pto_delay,
2719 manager_.GetRetransmissionTime());
2720
2721 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
2722 SendDataPacket(2, ENCRYPTION_FORWARD_SECURE);
2723 // Verify PTO is correctly set based on sent time of packet 2.
fayang5d011982020-05-13 14:14:38 -07002724 QuicTime deadline = clock_.Now() + expected_pto_delay;
2725 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2726 // Verify PTO is set based on left edge.
2727 deadline = packet1_sent_time + expected_pto_delay;
2728 }
2729 EXPECT_EQ(deadline, manager_.GetRetransmissionTime());
fayangce0a3162019-08-15 09:05:36 -07002730 EXPECT_EQ(0u, stats_.pto_count);
2731
2732 // Invoke PTO.
fayang5d011982020-05-13 14:14:38 -07002733 clock_.AdvanceTime(deadline - clock_.Now());
fayangce0a3162019-08-15 09:05:36 -07002734 manager_.OnRetransmissionTimeout();
2735 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
2736 EXPECT_EQ(1u, stats_.pto_count);
fayang2a379bf2020-05-18 11:49:25 -07002737 EXPECT_EQ(0u, stats_.max_consecutive_rto_with_forward_progress);
fayangce0a3162019-08-15 09:05:36 -07002738
2739 // Verify two probe packets get sent.
2740 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
2741 .Times(2)
2742 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
2743 RetransmitDataPacket(3, type, ENCRYPTION_FORWARD_SECURE);
2744 })))
2745 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
2746 RetransmitDataPacket(4, type, ENCRYPTION_FORWARD_SECURE);
2747 })));
2748 manager_.MaybeSendProbePackets();
2749 // Verify PTO period gets set to twice the current value.
2750 QuicTime sent_time = clock_.Now();
2751 EXPECT_EQ(sent_time + expected_pto_delay * 2,
2752 manager_.GetRetransmissionTime());
2753
2754 // Received ACK for packets 1 and 2.
2755 uint64_t acked[] = {1, 2};
bnc4e9283d2019-12-17 07:08:57 -08002756 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
fayangce0a3162019-08-15 09:05:36 -07002757 manager_.OnAckFrameStart(QuicPacketNumber(2), QuicTime::Delta::Infinite(),
2758 clock_.Now());
2759 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(3));
2760 EXPECT_EQ(PACKETS_NEWLY_ACKED,
2761 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
2762 ENCRYPTION_FORWARD_SECURE));
2763 expected_pto_delay =
2764 rtt_stats->SmoothedOrInitialRtt() +
fayang5d011982020-05-13 14:14:38 -07002765 std::max(pto_rttvar_multiplier * rtt_stats->mean_deviation(),
fayangce0a3162019-08-15 09:05:36 -07002766 QuicTime::Delta::FromMilliseconds(1)) +
2767 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
2768
2769 // Verify PTO is correctly re-armed based on sent time of packet 4.
2770 EXPECT_EQ(sent_time + expected_pto_delay, manager_.GetRetransmissionTime());
fayang2a379bf2020-05-18 11:49:25 -07002771 EXPECT_EQ(1u, stats_.max_consecutive_rto_with_forward_progress);
fayangce0a3162019-08-15 09:05:36 -07002772}
2773
fayangcff885a2019-10-22 07:39:04 -07002774TEST_F(QuicSentPacketManagerTest, SendOneProbePacket) {
fayangce0a3162019-08-15 09:05:36 -07002775 EnablePto(k1PTO);
2776 EXPECT_CALL(*send_algorithm_, PacingRate(_))
2777 .WillRepeatedly(Return(QuicBandwidth::Zero()));
2778 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
2779 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
2780
2781 SendDataPacket(1, ENCRYPTION_FORWARD_SECURE);
fayang5d011982020-05-13 14:14:38 -07002782 QuicTime packet1_sent_time = clock_.Now();
fayangce0a3162019-08-15 09:05:36 -07002783 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
2784 SendDataPacket(2, ENCRYPTION_FORWARD_SECURE);
2785
2786 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
2787 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
2788 QuicTime::Delta::Zero(), QuicTime::Zero());
2789 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
2790 // Verify PTO period is correctly set.
fayang5d011982020-05-13 14:14:38 -07002791 int pto_rttvar_multiplier =
2792 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayangce0a3162019-08-15 09:05:36 -07002793 QuicTime::Delta expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07002794 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayangce0a3162019-08-15 09:05:36 -07002795 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
fayang5d011982020-05-13 14:14:38 -07002796 QuicTime deadline = clock_.Now() + expected_pto_delay;
2797 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2798 // Verify PTO is set based on left edge.
2799 deadline = packet1_sent_time + expected_pto_delay;
2800 }
2801 EXPECT_EQ(deadline, manager_.GetRetransmissionTime());
fayangce0a3162019-08-15 09:05:36 -07002802
2803 // Invoke PTO.
fayang5d011982020-05-13 14:14:38 -07002804 clock_.AdvanceTime(deadline - clock_.Now());
fayangce0a3162019-08-15 09:05:36 -07002805 manager_.OnRetransmissionTimeout();
2806 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
2807
2808 // Verify one probe packet gets sent.
2809 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
2810 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
2811 RetransmitDataPacket(3, type, ENCRYPTION_FORWARD_SECURE);
2812 })));
2813 manager_.MaybeSendProbePackets();
2814}
2815
fayangcff885a2019-10-22 07:39:04 -07002816TEST_F(QuicSentPacketManagerTest, DisableHandshakeModeClient) {
fayang5f135052019-08-22 17:59:40 -07002817 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
fayangaf9903b2020-05-14 14:34:28 -07002818 manager_.EnableMultiplePacketNumberSpacesSupport();
fayang5f135052019-08-22 17:59:40 -07002819 // Send CHLO.
2820 SendCryptoPacket(1);
2821 EXPECT_NE(QuicTime::Zero(), manager_.GetRetransmissionTime());
2822 // Ack packet 1.
2823 ExpectAck(1);
2824 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
2825 clock_.Now());
2826 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
2827 EXPECT_EQ(PACKETS_NEWLY_ACKED,
2828 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
2829 ENCRYPTION_INITIAL));
2830 EXPECT_EQ(0u, manager_.GetBytesInFlight());
2831 // Verify retransmission timeout is not zero because handshake is not
2832 // confirmed although there is no in flight packet.
2833 EXPECT_NE(QuicTime::Zero(), manager_.GetRetransmissionTime());
2834 // Fire PTO.
2835 EXPECT_EQ(QuicSentPacketManager::PTO_MODE,
2836 manager_.OnRetransmissionTimeout());
2837}
2838
fayangcff885a2019-10-22 07:39:04 -07002839TEST_F(QuicSentPacketManagerTest, DisableHandshakeModeServer) {
fayang08a6c952019-09-18 14:29:45 -07002840 manager_.EnableIetfPtoAndLossDetection();
fayang5f135052019-08-22 17:59:40 -07002841 // Send SHLO.
2842 SendCryptoPacket(1);
2843 EXPECT_NE(QuicTime::Zero(), manager_.GetRetransmissionTime());
2844 // Ack packet 1.
2845 ExpectAck(1);
2846 manager_.OnAckFrameStart(QuicPacketNumber(1), QuicTime::Delta::Infinite(),
2847 clock_.Now());
2848 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2));
2849 EXPECT_EQ(PACKETS_NEWLY_ACKED,
2850 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
2851 ENCRYPTION_INITIAL));
2852 EXPECT_EQ(0u, manager_.GetBytesInFlight());
2853 // Verify retransmission timeout is not set on server side because there is
2854 // nothing in flight.
2855 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
2856}
2857
fayang4c908f02019-11-01 07:26:17 -07002858TEST_F(QuicSentPacketManagerTest, PtoTimeoutIncludesMaxAckDelay) {
2859 EnablePto(k1PTO);
2860 // Use PTOS and PTOA.
2861 QuicConfig config;
2862 QuicTagVector options;
2863 options.push_back(kPTOS);
2864 options.push_back(kPTOA);
2865 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
2866 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2867 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2868 manager_.SetFromConfig(config);
2869 EXPECT_TRUE(manager_.skip_packet_number_for_pto());
2870 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true));
2871
2872 EXPECT_CALL(*send_algorithm_, PacingRate(_))
2873 .WillRepeatedly(Return(QuicBandwidth::Zero()));
2874 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
2875 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
2876 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
2877 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
2878 QuicTime::Delta::Zero(), QuicTime::Zero());
2879 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
2880
2881 SendDataPacket(1, ENCRYPTION_FORWARD_SECURE);
fayang5d011982020-05-13 14:14:38 -07002882 QuicTime packet1_sent_time = clock_.Now();
fayang4c908f02019-11-01 07:26:17 -07002883 // Verify PTO is correctly set and ack delay is included.
fayang5d011982020-05-13 14:14:38 -07002884 int pto_rttvar_multiplier =
2885 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayang4c908f02019-11-01 07:26:17 -07002886 QuicTime::Delta expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07002887 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayang4c908f02019-11-01 07:26:17 -07002888 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
2889 EXPECT_EQ(clock_.Now() + expected_pto_delay,
2890 manager_.GetRetransmissionTime());
2891
2892 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
2893 SendDataPacket(2, ENCRYPTION_FORWARD_SECURE);
2894 // Verify PTO is correctly set based on sent time of packet 2 but ack delay is
2895 // not included as an immediate ACK is expected.
2896 expected_pto_delay = expected_pto_delay - QuicTime::Delta::FromMilliseconds(
2897 kDefaultDelayedAckTimeMs);
fayang5d011982020-05-13 14:14:38 -07002898 QuicTime deadline = clock_.Now() + expected_pto_delay;
2899 if (GetQuicReloadableFlag(quic_default_on_pto)) {
2900 deadline = packet1_sent_time + expected_pto_delay;
2901 }
2902 EXPECT_EQ(deadline, manager_.GetRetransmissionTime());
fayang4c908f02019-11-01 07:26:17 -07002903 EXPECT_EQ(0u, stats_.pto_count);
2904
2905 // Invoke PTO.
fayang5d011982020-05-13 14:14:38 -07002906 clock_.AdvanceTime(deadline - clock_.Now());
fayang4c908f02019-11-01 07:26:17 -07002907 manager_.OnRetransmissionTimeout();
2908 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
2909 EXPECT_EQ(1u, stats_.pto_count);
2910
2911 // Verify 1 probe packets get sent and packet number gets skipped.
2912 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
2913 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
2914 RetransmitDataPacket(4, type, ENCRYPTION_FORWARD_SECURE);
2915 })));
2916 manager_.MaybeSendProbePackets();
2917 // Verify PTO period gets set to twice the current value. Also, ack delay is
2918 // not included.
2919 QuicTime sent_time = clock_.Now();
2920 EXPECT_EQ(sent_time + expected_pto_delay * 2,
2921 manager_.GetRetransmissionTime());
2922
2923 // Received ACK for packets 1 and 2.
2924 uint64_t acked[] = {1, 2};
bnc4e9283d2019-12-17 07:08:57 -08002925 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
fayang4c908f02019-11-01 07:26:17 -07002926 manager_.OnAckFrameStart(QuicPacketNumber(2), QuicTime::Delta::Infinite(),
2927 clock_.Now());
2928 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(3));
2929 EXPECT_EQ(PACKETS_NEWLY_ACKED,
2930 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
2931 ENCRYPTION_FORWARD_SECURE));
2932 expected_pto_delay =
2933 rtt_stats->SmoothedOrInitialRtt() +
fayang5d011982020-05-13 14:14:38 -07002934 std::max(pto_rttvar_multiplier * rtt_stats->mean_deviation(),
fayang4c908f02019-11-01 07:26:17 -07002935 QuicTime::Delta::FromMilliseconds(1)) +
2936 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
2937
2938 // Verify PTO is correctly re-armed based on sent time of packet 4. Because of
2939 // PTOS turns out to be spurious, ACK delay is included.
2940 EXPECT_EQ(sent_time + expected_pto_delay, manager_.GetRetransmissionTime());
2941
2942 // Received ACK for packets 4.
2943 ExpectAck(4);
2944 manager_.OnAckFrameStart(QuicPacketNumber(4), QuicTime::Delta::Infinite(),
2945 clock_.Now());
2946 manager_.OnAckRange(QuicPacketNumber(4), QuicPacketNumber(5));
2947 EXPECT_EQ(PACKETS_NEWLY_ACKED,
2948 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(4),
2949 ENCRYPTION_FORWARD_SECURE));
2950 EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
2951 // Send more packets, such that peer will do ack decimation.
2952 std::vector<uint64_t> acked2;
2953 for (size_t i = 5; i <= 100; ++i) {
2954 SendDataPacket(i, ENCRYPTION_FORWARD_SECURE);
2955 acked2.push_back(i);
2956 }
2957 // Received ACK for all sent packets.
2958 ExpectAcksAndLosses(true, &acked2[0], acked2.size(), nullptr, 0);
2959 manager_.OnAckFrameStart(QuicPacketNumber(100), QuicTime::Delta::Infinite(),
2960 clock_.Now());
2961 manager_.OnAckRange(QuicPacketNumber(5), QuicPacketNumber(101));
2962 EXPECT_EQ(PACKETS_NEWLY_ACKED,
2963 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(100),
2964 ENCRYPTION_FORWARD_SECURE));
2965
2966 expected_pto_delay =
2967 rtt_stats->SmoothedOrInitialRtt() +
fayang5d011982020-05-13 14:14:38 -07002968 std::max(pto_rttvar_multiplier * rtt_stats->mean_deviation(),
fayang4c908f02019-11-01 07:26:17 -07002969 QuicTime::Delta::FromMilliseconds(1)) +
2970 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
2971 for (size_t i = 101; i < 110; i++) {
2972 SendDataPacket(i, ENCRYPTION_FORWARD_SECURE);
2973 // Verify PTO timeout includes ACK delay as there are less than 10 packets
2974 // outstanding.
2975 EXPECT_EQ(clock_.Now() + expected_pto_delay,
2976 manager_.GetRetransmissionTime());
2977 }
2978 expected_pto_delay = expected_pto_delay - QuicTime::Delta::FromMilliseconds(
2979 kDefaultDelayedAckTimeMs);
2980 SendDataPacket(110, ENCRYPTION_FORWARD_SECURE);
2981 // Verify ACK delay is excluded.
2982 EXPECT_EQ(clock_.Now() + expected_pto_delay,
2983 manager_.GetRetransmissionTime());
2984}
2985
fayang7085a6d2019-11-04 07:03:57 -08002986TEST_F(QuicSentPacketManagerTest, StartExponentialBackoffSince2ndPto) {
2987 EnablePto(k2PTO);
2988 QuicConfig config;
2989 QuicTagVector options;
2990 options.push_back(kPEB2);
2991 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
2992 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
2993 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
2994 manager_.SetFromConfig(config);
2995
2996 EXPECT_CALL(*send_algorithm_, PacingRate(_))
2997 .WillRepeatedly(Return(QuicBandwidth::Zero()));
2998 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
2999 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
3000 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3001 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
3002 QuicTime::Delta::Zero(), QuicTime::Zero());
3003 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
3004
3005 SendDataPacket(1, ENCRYPTION_FORWARD_SECURE);
fayang5d011982020-05-13 14:14:38 -07003006 QuicTime packet1_sent_time = clock_.Now();
fayang7085a6d2019-11-04 07:03:57 -08003007 // Verify PTO is correctly set.
fayang5d011982020-05-13 14:14:38 -07003008 int pto_rttvar_multiplier =
3009 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayang7085a6d2019-11-04 07:03:57 -08003010 QuicTime::Delta expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07003011 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayang7085a6d2019-11-04 07:03:57 -08003012 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
fayang5d011982020-05-13 14:14:38 -07003013 EXPECT_EQ(packet1_sent_time + expected_pto_delay,
fayang7085a6d2019-11-04 07:03:57 -08003014 manager_.GetRetransmissionTime());
3015
3016 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3017 SendDataPacket(2, ENCRYPTION_FORWARD_SECURE);
3018 // Verify PTO is correctly set based on sent time of packet 2.
fayang5d011982020-05-13 14:14:38 -07003019 QuicTime deadline = clock_.Now() + expected_pto_delay;
3020 if (GetQuicReloadableFlag(quic_default_on_pto)) {
3021 // Verify PTO is set based on left edge.
3022 deadline = packet1_sent_time + expected_pto_delay;
3023 }
3024 EXPECT_EQ(deadline, manager_.GetRetransmissionTime());
fayang7085a6d2019-11-04 07:03:57 -08003025 EXPECT_EQ(0u, stats_.pto_count);
3026
3027 // Invoke PTO.
fayang5d011982020-05-13 14:14:38 -07003028 clock_.AdvanceTime(deadline - clock_.Now());
fayang7085a6d2019-11-04 07:03:57 -08003029 manager_.OnRetransmissionTimeout();
3030 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
3031 EXPECT_EQ(1u, stats_.pto_count);
3032
3033 // Verify two probe packets get sent.
3034 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3035 .Times(2)
3036 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3037 RetransmitDataPacket(3, type, ENCRYPTION_FORWARD_SECURE);
3038 })))
3039 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3040 RetransmitDataPacket(4, type, ENCRYPTION_FORWARD_SECURE);
3041 })));
3042 manager_.MaybeSendProbePackets();
3043 // Verify no exponential backoff.
3044 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3045 manager_.GetRetransmissionTime());
3046
3047 // Invoke 2nd PTO.
3048 clock_.AdvanceTime(expected_pto_delay);
3049 manager_.OnRetransmissionTimeout();
3050 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
3051 EXPECT_EQ(2u, stats_.pto_count);
3052
3053 // Verify two probe packets get sent.
3054 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3055 .Times(2)
3056 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3057 RetransmitDataPacket(5, type, ENCRYPTION_FORWARD_SECURE);
3058 })))
3059 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3060 RetransmitDataPacket(6, type, ENCRYPTION_FORWARD_SECURE);
3061 })));
3062 manager_.MaybeSendProbePackets();
3063 // Verify still no exponential backoff.
3064 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3065 manager_.GetRetransmissionTime());
3066
3067 // Invoke 3rd PTO.
3068 clock_.AdvanceTime(expected_pto_delay);
3069 manager_.OnRetransmissionTimeout();
3070 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
3071 EXPECT_EQ(3u, stats_.pto_count);
3072
3073 // Verify two probe packets get sent.
3074 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3075 .Times(2)
3076 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3077 RetransmitDataPacket(7, type, ENCRYPTION_FORWARD_SECURE);
3078 })))
3079 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3080 RetransmitDataPacket(8, type, ENCRYPTION_FORWARD_SECURE);
3081 })));
3082 manager_.MaybeSendProbePackets();
3083 // Verify exponential backoff starts.
3084 EXPECT_EQ(clock_.Now() + expected_pto_delay * 2,
3085 manager_.GetRetransmissionTime());
3086
3087 // Invoke 4th PTO.
3088 clock_.AdvanceTime(expected_pto_delay * 2);
3089 manager_.OnRetransmissionTimeout();
3090 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
3091 EXPECT_EQ(4u, stats_.pto_count);
3092
3093 // Verify two probe packets get sent.
3094 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3095 .Times(2)
3096 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3097 RetransmitDataPacket(9, type, ENCRYPTION_FORWARD_SECURE);
3098 })))
3099 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3100 RetransmitDataPacket(10, type, ENCRYPTION_FORWARD_SECURE);
3101 })));
3102 manager_.MaybeSendProbePackets();
3103 // Verify exponential backoff continues.
3104 EXPECT_EQ(clock_.Now() + expected_pto_delay * 4,
3105 manager_.GetRetransmissionTime());
3106}
3107
fayang75db4342019-11-04 13:29:14 -08003108TEST_F(QuicSentPacketManagerTest, PtoTimeoutRttVarMultiple) {
3109 EnablePto(k1PTO);
3110 // Use 2 * rttvar
3111 QuicConfig config;
3112 QuicTagVector options;
3113 options.push_back(kPVS1);
3114 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
3115 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
3116 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
3117 manager_.SetFromConfig(config);
3118
3119 EXPECT_CALL(*send_algorithm_, PacingRate(_))
3120 .WillRepeatedly(Return(QuicBandwidth::Zero()));
3121 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
3122 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
3123 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3124 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
3125 QuicTime::Delta::Zero(), QuicTime::Zero());
3126 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
3127
3128 SendDataPacket(1, ENCRYPTION_FORWARD_SECURE);
3129 // Verify PTO is correctly set based on 2 times rtt var.
3130 QuicTime::Delta expected_pto_delay =
3131 srtt + 2 * rtt_stats->mean_deviation() +
3132 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3133 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3134 manager_.GetRetransmissionTime());
3135}
3136
fayang93c83942019-11-07 10:41:16 -08003137// Regression test for b/143962153
3138TEST_F(QuicSentPacketManagerTest, RtoNotInFlightPacket) {
fayang5d011982020-05-13 14:14:38 -07003139 if (GetQuicReloadableFlag(quic_default_on_pto)) {
3140 return;
3141 }
fayang93c83942019-11-07 10:41:16 -08003142 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
3143 // Send SHLO.
dmcardlecf0bfcf2019-12-13 08:08:21 -08003144 QuicStreamFrame crypto_frame(1, false, 0, quiche::QuicheStringPiece());
fayang93c83942019-11-07 10:41:16 -08003145 SendCryptoPacket(1);
3146 // Send data packet.
3147 SendDataPacket(2, ENCRYPTION_FORWARD_SECURE);
3148
3149 // Successfully decrypt a forward secure packet.
fayange1e81d22020-01-06 11:41:34 -08003150 EXPECT_CALL(notifier_, OnFrameAcked(_, _, _)).Times(1);
fayang93c83942019-11-07 10:41:16 -08003151 manager_.SetHandshakeConfirmed();
3152
3153 // 1st TLP.
3154 manager_.OnRetransmissionTimeout();
3155 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3156 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3157 RetransmitDataPacket(3, type, ENCRYPTION_FORWARD_SECURE);
3158 })));
3159 manager_.MaybeRetransmitTailLossProbe();
3160
3161 // 2nd TLP.
3162 manager_.OnRetransmissionTimeout();
3163 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3164 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3165 RetransmitDataPacket(4, type, ENCRYPTION_FORWARD_SECURE);
3166 })));
3167 manager_.MaybeRetransmitTailLossProbe();
3168
3169 // RTO retransmits SHLO although it is not in flight.
fayang93c83942019-11-07 10:41:16 -08003170 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
fayangac35d452019-11-08 12:35:15 -08003171 .WillOnce(WithArgs<0>(Invoke([&crypto_frame](const QuicFrames& frames) {
3172 EXPECT_EQ(1u, frames.size());
fayange1e81d22020-01-06 11:41:34 -08003173 EXPECT_NE(crypto_frame, frames[0].stream_frame);
fayangac35d452019-11-08 12:35:15 -08003174 })));
fayang93c83942019-11-07 10:41:16 -08003175 manager_.OnRetransmissionTimeout();
3176}
3177
fayang4aa22402020-01-07 11:36:07 -08003178TEST_F(QuicSentPacketManagerTest, Aggressive1Pto) {
3179 EnablePto(k1PTO);
3180 // Let the first PTO be aggressive.
3181 QuicConfig config;
3182 QuicTagVector options;
3183 options.push_back(kPTOS);
3184 options.push_back(kPAG1);
3185 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
3186 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
3187 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
3188 manager_.SetFromConfig(config);
3189
3190 EXPECT_CALL(*send_algorithm_, PacingRate(_))
3191 .WillRepeatedly(Return(QuicBandwidth::Zero()));
3192 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
3193 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
3194 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3195 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
3196 QuicTime::Delta::Zero(), QuicTime::Zero());
3197 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
3198 SendDataPacket(1, ENCRYPTION_FORWARD_SECURE);
3199 // Verify PTO is correctly set.
3200 QuicTime::Delta expected_pto_delay = 2 * srtt;
3201 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3202 manager_.GetRetransmissionTime());
3203
3204 // Invoke PTO.
3205 clock_.AdvanceTime(expected_pto_delay);
3206 manager_.OnRetransmissionTimeout();
3207 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
3208 EXPECT_EQ(1u, stats_.pto_count);
3209
3210 // Verify 1 probe packets get sent and packet number gets skipped.
3211 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3212 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3213 RetransmitDataPacket(3, type, ENCRYPTION_FORWARD_SECURE);
3214 })));
3215 manager_.MaybeSendProbePackets();
3216
3217 // Verify PTO period gets set correctly.
3218 QuicTime sent_time = clock_.Now();
fayang5d011982020-05-13 14:14:38 -07003219 int pto_rttvar_multiplier =
3220 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayang4aa22402020-01-07 11:36:07 -08003221 expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07003222 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayang4aa22402020-01-07 11:36:07 -08003223 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3224 EXPECT_EQ(sent_time + expected_pto_delay * 2,
3225 manager_.GetRetransmissionTime());
3226}
3227
3228TEST_F(QuicSentPacketManagerTest, Aggressive2Ptos) {
3229 EnablePto(k1PTO);
3230 // Let the first PTO be aggressive.
3231 QuicConfig config;
3232 QuicTagVector options;
3233 options.push_back(kPTOS);
3234 options.push_back(kPAG2);
3235 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
3236 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
3237 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
3238 manager_.SetFromConfig(config);
3239
3240 EXPECT_CALL(*send_algorithm_, PacingRate(_))
3241 .WillRepeatedly(Return(QuicBandwidth::Zero()));
3242 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
3243 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
3244 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3245 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
3246 QuicTime::Delta::Zero(), QuicTime::Zero());
3247 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
3248 SendDataPacket(1, ENCRYPTION_FORWARD_SECURE);
3249 // Verify PTO is correctly set.
3250 QuicTime::Delta expected_pto_delay = 2 * srtt;
3251 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3252 manager_.GetRetransmissionTime());
3253
3254 // Invoke PTO.
3255 clock_.AdvanceTime(expected_pto_delay);
3256 manager_.OnRetransmissionTimeout();
3257 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
3258 EXPECT_EQ(1u, stats_.pto_count);
3259
3260 // Verify 1 probe packets get sent and packet number gets skipped.
3261 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3262 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3263 RetransmitDataPacket(3, type, ENCRYPTION_FORWARD_SECURE);
3264 })));
3265 manager_.MaybeSendProbePackets();
3266
3267 // Verify PTO period gets set correctly.
3268 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3269 manager_.GetRetransmissionTime());
3270
3271 // Invoke 2nd PTO.
3272 clock_.AdvanceTime(expected_pto_delay);
3273 manager_.OnRetransmissionTimeout();
3274 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
3275 EXPECT_EQ(2u, stats_.pto_count);
3276
3277 // Verify 1 probe packets get sent and packet number gets skipped.
3278 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3279 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3280 RetransmitDataPacket(5, type, ENCRYPTION_FORWARD_SECURE);
3281 })));
3282 manager_.MaybeSendProbePackets();
fayang5d011982020-05-13 14:14:38 -07003283 int pto_rttvar_multiplier =
3284 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayang4aa22402020-01-07 11:36:07 -08003285 expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07003286 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayang4aa22402020-01-07 11:36:07 -08003287 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3288
3289 // Verify PTO period gets set correctly.
3290 EXPECT_EQ(clock_.Now() + expected_pto_delay * 4,
3291 manager_.GetRetransmissionTime());
3292}
3293
fayang402103c2020-01-10 13:36:24 -08003294TEST_F(QuicSentPacketManagerTest, IW10ForUpAndDown) {
3295 SetQuicReloadableFlag(quic_bbr_mitigate_overly_large_bandwidth_sample, true);
3296 QuicConfig config;
3297 QuicTagVector options;
3298 options.push_back(kBWS5);
3299 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
3300 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
3301 EXPECT_CALL(*send_algorithm_, SetInitialCongestionWindowInPackets(10));
3302 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
3303 manager_.SetFromConfig(config);
3304
3305 EXPECT_EQ(10u, manager_.initial_congestion_window());
3306}
3307
fayang18ff23b2020-01-28 09:19:00 -08003308TEST_F(QuicSentPacketManagerTest, ClientMultiplePacketNumberSpacePtoTimeout) {
3309 manager_.EnableMultiplePacketNumberSpacesSupport();
3310 EnablePto(k1PTO);
3311 EXPECT_CALL(*send_algorithm_, PacingRate(_))
3312 .WillRepeatedly(Return(QuicBandwidth::Zero()));
3313 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
3314 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
3315 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3316 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
3317 QuicTime::Delta::Zero(), QuicTime::Zero());
3318 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
fayang9a0e1bd2020-02-19 13:13:04 -08003319 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
fayang18ff23b2020-01-28 09:19:00 -08003320
3321 // Send packet 1.
3322 SendDataPacket(1, ENCRYPTION_INITIAL);
3323 // Verify PTO is correctly set.
fayang5d011982020-05-13 14:14:38 -07003324 int pto_rttvar_multiplier =
3325 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayang18ff23b2020-01-28 09:19:00 -08003326 QuicTime::Delta expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07003327 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayang18ff23b2020-01-28 09:19:00 -08003328 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3329 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3330 manager_.GetRetransmissionTime());
3331
3332 // Discard initial key and send packet 2 in handshake.
3333 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3334 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
3335 manager_.NeuterUnencryptedPackets();
3336
3337 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(true));
3338 SendDataPacket(2, ENCRYPTION_HANDSHAKE);
3339 // Verify PTO is correctly set based on sent time of packet 2.
3340 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3341 manager_.GetRetransmissionTime());
3342 // Invoke PTO.
3343 clock_.AdvanceTime(expected_pto_delay);
3344 manager_.OnRetransmissionTimeout();
3345 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
3346 EXPECT_EQ(1u, stats_.pto_count);
fayangaf9903b2020-05-14 14:34:28 -07003347 EXPECT_EQ(1u, stats_.crypto_retransmit_count);
fayang18ff23b2020-01-28 09:19:00 -08003348
3349 // Verify probe packet gets sent.
3350 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3351 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3352 RetransmitDataPacket(3, type, ENCRYPTION_HANDSHAKE);
3353 })));
3354 manager_.MaybeSendProbePackets();
3355 // Verify PTO period gets set to twice the current value.
3356 const QuicTime packet3_sent_time = clock_.Now();
3357 EXPECT_EQ(packet3_sent_time + expected_pto_delay * 2,
3358 manager_.GetRetransmissionTime());
3359
fayang9a0e1bd2020-02-19 13:13:04 -08003360 // Send packet 4 in application data with 0-RTT.
fayang18ff23b2020-01-28 09:19:00 -08003361 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
fayang9a0e1bd2020-02-19 13:13:04 -08003362 SendDataPacket(4, ENCRYPTION_ZERO_RTT);
fayang42834942020-03-30 10:56:15 -07003363 const QuicTime packet4_sent_time = clock_.Now();
fayang18ff23b2020-01-28 09:19:00 -08003364 // Verify PTO timeout is still based on packet 3.
3365 EXPECT_EQ(packet3_sent_time + expected_pto_delay * 2,
3366 manager_.GetRetransmissionTime());
3367
3368 // Send packet 5 in handshake.
3369 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3370 SendDataPacket(5, ENCRYPTION_HANDSHAKE);
fayang9a0e1bd2020-02-19 13:13:04 -08003371 const QuicTime packet5_sent_time = clock_.Now();
3372 // Verify PTO timeout is now based on packet 5 because packet 4 should be
3373 // ignored.
fayang18ff23b2020-01-28 09:19:00 -08003374 EXPECT_EQ(clock_.Now() + expected_pto_delay * 2,
3375 manager_.GetRetransmissionTime());
3376
fayang9a0e1bd2020-02-19 13:13:04 -08003377 // Send packet 6 in 1-RTT.
3378 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3379 SendDataPacket(6, ENCRYPTION_FORWARD_SECURE);
3380 const QuicTime packet6_sent_time = clock_.Now();
3381 // Verify PTO timeout is now based on packet 5.
3382 EXPECT_EQ(packet5_sent_time + expected_pto_delay * 2,
3383 manager_.GetRetransmissionTime());
3384
3385 // Send packet 7 in handshake.
3386 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3387 SendDataPacket(7, ENCRYPTION_HANDSHAKE);
3388 // Verify PTO timeout is now based on packet 6.
3389 EXPECT_EQ(packet6_sent_time + expected_pto_delay * 2,
3390 manager_.GetRetransmissionTime());
3391
fayang18ff23b2020-01-28 09:19:00 -08003392 // Neuter handshake key.
3393 manager_.SetHandshakeConfirmed();
fayang42834942020-03-30 10:56:15 -07003394 // Forward progress has been made, verify PTO counter gets reset. PTO timeout
3395 // is armed by left edge.
3396 EXPECT_EQ(packet4_sent_time + expected_pto_delay,
fayang18ff23b2020-01-28 09:19:00 -08003397 manager_.GetRetransmissionTime());
3398}
3399
3400TEST_F(QuicSentPacketManagerTest, ServerMultiplePacketNumberSpacePtoTimeout) {
3401 manager_.EnableMultiplePacketNumberSpacesSupport();
3402 EnablePto(k1PTO);
3403 EXPECT_CALL(*send_algorithm_, PacingRate(_))
3404 .WillRepeatedly(Return(QuicBandwidth::Zero()));
3405 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
3406 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
3407 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3408 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
3409 QuicTime::Delta::Zero(), QuicTime::Zero());
3410 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
3411
3412 // Send packet 1.
3413 SendDataPacket(1, ENCRYPTION_INITIAL);
3414 const QuicTime packet1_sent_time = clock_.Now();
3415 // Verify PTO is correctly set.
fayang5d011982020-05-13 14:14:38 -07003416 int pto_rttvar_multiplier =
3417 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayang18ff23b2020-01-28 09:19:00 -08003418 QuicTime::Delta expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07003419 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayang18ff23b2020-01-28 09:19:00 -08003420 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3421 EXPECT_EQ(packet1_sent_time + expected_pto_delay,
3422 manager_.GetRetransmissionTime());
3423
3424 // Send packet 2 in handshake.
3425 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3426 SendDataPacket(2, ENCRYPTION_HANDSHAKE);
fayang9a0e1bd2020-02-19 13:13:04 -08003427 const QuicTime packet2_sent_time = clock_.Now();
fayang18ff23b2020-01-28 09:19:00 -08003428 // Verify PTO timeout is still based on packet 1.
3429 EXPECT_EQ(packet1_sent_time + expected_pto_delay,
3430 manager_.GetRetransmissionTime());
3431
fayang9a0e1bd2020-02-19 13:13:04 -08003432 // Discard initial keys.
fayang18ff23b2020-01-28 09:19:00 -08003433 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
3434 manager_.NeuterUnencryptedPackets();
fayang18ff23b2020-01-28 09:19:00 -08003435
fayang9a0e1bd2020-02-19 13:13:04 -08003436 // Send packet 3 in 1-RTT.
fayang18ff23b2020-01-28 09:19:00 -08003437 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3438 SendDataPacket(3, ENCRYPTION_FORWARD_SECURE);
fayang9a0e1bd2020-02-19 13:13:04 -08003439 // Verify PTO timeout is based on packet 2.
3440 const QuicTime packet3_sent_time = clock_.Now();
3441 EXPECT_EQ(packet2_sent_time + expected_pto_delay,
3442 manager_.GetRetransmissionTime());
3443
3444 // Send packet 4 in handshake.
3445 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3446 SendDataPacket(4, ENCRYPTION_HANDSHAKE);
3447 // Verify PTO timeout is based on packet 4 as application data is ignored.
fayang18ff23b2020-01-28 09:19:00 -08003448 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3449 manager_.GetRetransmissionTime());
fayang9a0e1bd2020-02-19 13:13:04 -08003450
3451 // Discard handshake keys.
3452 manager_.SetHandshakeConfirmed();
3453 // Verify PTO timeout is now based on packet 3 as handshake is
3454 // complete/confirmed.
3455 EXPECT_EQ(packet3_sent_time + expected_pto_delay,
3456 manager_.GetRetransmissionTime());
fayang18ff23b2020-01-28 09:19:00 -08003457}
3458
fayang2ccfbcf2020-02-28 12:37:08 -08003459TEST_F(QuicSentPacketManagerTest, ComputingProbeTimeoutByLeftEdge) {
fayang2ccfbcf2020-02-28 12:37:08 -08003460 EnablePto(k1PTO);
3461 // Use PTOS and PLE1.
3462 QuicConfig config;
3463 QuicTagVector options;
3464 options.push_back(kPTOS);
3465 options.push_back(kPLE1);
3466 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
3467 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
3468 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
3469 manager_.SetFromConfig(config);
3470 EXPECT_TRUE(manager_.skip_packet_number_for_pto());
3471 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true));
3472 EXPECT_CALL(*send_algorithm_, PacingRate(_))
3473 .WillRepeatedly(Return(QuicBandwidth::Zero()));
3474 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
3475 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
3476 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3477 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
3478 QuicTime::Delta::Zero(), QuicTime::Zero());
3479 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
3480
3481 SendDataPacket(1, ENCRYPTION_FORWARD_SECURE);
3482 // Verify PTO is correctly set.
fayang5d011982020-05-13 14:14:38 -07003483 int pto_rttvar_multiplier =
3484 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayang2ccfbcf2020-02-28 12:37:08 -08003485 QuicTime::Delta expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07003486 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayang2ccfbcf2020-02-28 12:37:08 -08003487 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3488 const QuicTime packet1_sent_time = clock_.Now();
3489 EXPECT_EQ(packet1_sent_time + expected_pto_delay,
3490 manager_.GetRetransmissionTime());
3491
3492 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3493 SendDataPacket(2, ENCRYPTION_FORWARD_SECURE);
3494 // Verify PTO is still based on packet 1.
3495 EXPECT_EQ(packet1_sent_time + expected_pto_delay,
3496 manager_.GetRetransmissionTime());
3497 EXPECT_EQ(0u, stats_.pto_count);
3498
3499 // Invoke PTO.
3500 clock_.AdvanceTime(expected_pto_delay);
3501 manager_.OnRetransmissionTimeout();
3502 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
3503 EXPECT_EQ(1u, stats_.pto_count);
3504
3505 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3506 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3507 RetransmitDataPacket(3, type, ENCRYPTION_FORWARD_SECURE);
3508 })));
3509 manager_.MaybeSendProbePackets();
3510 // Verify PTO period gets set to twice the current value and based on packet3.
3511 QuicTime packet3_sent_time = clock_.Now();
3512 EXPECT_EQ(packet3_sent_time + expected_pto_delay * 2,
3513 manager_.GetRetransmissionTime());
3514
3515 // Received ACK for packets 1 and 2.
3516 uint64_t acked[] = {1, 2};
3517 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
3518 manager_.OnAckFrameStart(QuicPacketNumber(2), QuicTime::Delta::Infinite(),
3519 clock_.Now());
3520 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(3));
3521 EXPECT_EQ(PACKETS_NEWLY_ACKED,
3522 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
3523 ENCRYPTION_FORWARD_SECURE));
3524 expected_pto_delay =
3525 rtt_stats->SmoothedOrInitialRtt() +
fayang5d011982020-05-13 14:14:38 -07003526 std::max(pto_rttvar_multiplier * rtt_stats->mean_deviation(),
fayang2ccfbcf2020-02-28 12:37:08 -08003527 QuicTime::Delta::FromMilliseconds(1)) +
3528 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3529
3530 // Verify PTO is correctly re-armed based on sent time of packet 4.
3531 EXPECT_EQ(packet3_sent_time + expected_pto_delay,
3532 manager_.GetRetransmissionTime());
3533}
3534
fayangc5d9b2c2020-03-02 11:12:26 -08003535TEST_F(QuicSentPacketManagerTest, ComputingProbeTimeoutByLeftEdge2) {
fayangc5d9b2c2020-03-02 11:12:26 -08003536 EnablePto(k1PTO);
3537 // Use PTOS and PLE2.
3538 QuicConfig config;
3539 QuicTagVector options;
3540 options.push_back(kPTOS);
3541 options.push_back(kPLE2);
3542 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
3543 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
3544 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
3545 manager_.SetFromConfig(config);
3546 EXPECT_TRUE(manager_.skip_packet_number_for_pto());
3547 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true));
3548 EXPECT_CALL(*send_algorithm_, PacingRate(_))
3549 .WillRepeatedly(Return(QuicBandwidth::Zero()));
3550 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
3551 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
3552 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3553 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
3554 QuicTime::Delta::Zero(), QuicTime::Zero());
3555 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
3556
3557 SendDataPacket(1, ENCRYPTION_FORWARD_SECURE);
3558 // Verify PTO is correctly set.
fayang5d011982020-05-13 14:14:38 -07003559 int pto_rttvar_multiplier =
3560 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayangc5d9b2c2020-03-02 11:12:26 -08003561 QuicTime::Delta expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07003562 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayangc5d9b2c2020-03-02 11:12:26 -08003563 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3564 const QuicTime packet1_sent_time = clock_.Now();
3565 EXPECT_EQ(packet1_sent_time + expected_pto_delay,
3566 manager_.GetRetransmissionTime());
3567
3568 // Sent a packet 10ms before PTO expiring.
3569 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(
3570 expected_pto_delay.ToMilliseconds() - 10));
3571 SendDataPacket(2, ENCRYPTION_FORWARD_SECURE);
3572 // Verify PTO expands to packet 2 sent time + 1.5 * srtt.
3573 expected_pto_delay = 1.5 * rtt_stats->smoothed_rtt();
3574 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3575 manager_.GetRetransmissionTime());
3576 EXPECT_EQ(0u, stats_.pto_count);
3577
3578 // Invoke PTO.
3579 clock_.AdvanceTime(expected_pto_delay);
3580 manager_.OnRetransmissionTimeout();
3581 EXPECT_EQ(QuicTime::Delta::Zero(), manager_.TimeUntilSend(clock_.Now()));
3582 EXPECT_EQ(1u, stats_.pto_count);
3583
3584 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3585 .WillOnce(WithArgs<1>(Invoke([this](TransmissionType type) {
3586 RetransmitDataPacket(3, type, ENCRYPTION_FORWARD_SECURE);
3587 })));
3588 manager_.MaybeSendProbePackets();
3589 // Verify PTO period gets set to twice the expected value and based on
3590 // packet3 (right edge).
3591 expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07003592 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayangc5d9b2c2020-03-02 11:12:26 -08003593 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3594 QuicTime packet3_sent_time = clock_.Now();
3595 EXPECT_EQ(packet3_sent_time + expected_pto_delay * 2,
3596 manager_.GetRetransmissionTime());
3597
3598 // Received ACK for packets 1 and 2.
3599 uint64_t acked[] = {1, 2};
3600 ExpectAcksAndLosses(true, acked, QUICHE_ARRAYSIZE(acked), nullptr, 0);
3601 manager_.OnAckFrameStart(QuicPacketNumber(2), QuicTime::Delta::Infinite(),
3602 clock_.Now());
3603 manager_.OnAckRange(QuicPacketNumber(1), QuicPacketNumber(3));
3604 EXPECT_EQ(PACKETS_NEWLY_ACKED,
3605 manager_.OnAckFrameEnd(clock_.Now(), QuicPacketNumber(1),
3606 ENCRYPTION_FORWARD_SECURE));
3607 expected_pto_delay =
3608 rtt_stats->SmoothedOrInitialRtt() +
fayang5d011982020-05-13 14:14:38 -07003609 std::max(pto_rttvar_multiplier * rtt_stats->mean_deviation(),
fayangc5d9b2c2020-03-02 11:12:26 -08003610 QuicTime::Delta::FromMilliseconds(1)) +
3611 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3612
3613 // Verify PTO is correctly re-armed based on sent time of packet 3 (left
3614 // edge).
3615 EXPECT_EQ(packet3_sent_time + expected_pto_delay,
3616 manager_.GetRetransmissionTime());
3617}
3618
fayangc2fd6102020-03-02 17:27:38 -08003619TEST_F(QuicSentPacketManagerTest, ComputingProbeTimeoutUsingStandardDeviation) {
fayangc2fd6102020-03-02 17:27:38 -08003620 EnablePto(k1PTO);
3621 // Use PTOS and PSDA.
3622 QuicConfig config;
3623 QuicTagVector options;
3624 options.push_back(kPTOS);
3625 options.push_back(kPSDA);
3626 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
3627 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
3628 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
3629 manager_.SetFromConfig(config);
3630 EXPECT_TRUE(manager_.skip_packet_number_for_pto());
3631 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true));
3632 EXPECT_CALL(*send_algorithm_, PacingRate(_))
3633 .WillRepeatedly(Return(QuicBandwidth::Zero()));
3634 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
3635 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
3636 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3637 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
3638 QuicTime::Delta::Zero(), QuicTime::Zero());
3639 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(50),
3640 QuicTime::Delta::Zero(), QuicTime::Zero());
3641 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(50),
3642 QuicTime::Delta::Zero(), QuicTime::Zero());
3643 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(75),
3644 QuicTime::Delta::Zero(), QuicTime::Zero());
3645 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
3646
3647 SendDataPacket(1, ENCRYPTION_FORWARD_SECURE);
3648 // Verify PTO is correctly set using standard deviation.
fayang5d011982020-05-13 14:14:38 -07003649 int pto_rttvar_multiplier =
3650 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayangc2fd6102020-03-02 17:27:38 -08003651 QuicTime::Delta expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07003652 srtt + pto_rttvar_multiplier * rtt_stats->GetStandardOrMeanDeviation() +
fayangc2fd6102020-03-02 17:27:38 -08003653 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3654 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3655 manager_.GetRetransmissionTime());
3656}
3657
fayang2ccfbcf2020-02-28 12:37:08 -08003658TEST_F(QuicSentPacketManagerTest,
3659 ComputingProbeTimeoutByLeftEdgeMultiplePacketNumberSpaces) {
fayang2ccfbcf2020-02-28 12:37:08 -08003660 manager_.EnableMultiplePacketNumberSpacesSupport();
3661 EnablePto(k1PTO);
3662 // Use PTOS and PLE1.
3663 QuicConfig config;
3664 QuicTagVector options;
3665 options.push_back(kPTOS);
3666 options.push_back(kPLE1);
3667 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
3668 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
3669 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
3670 manager_.SetFromConfig(config);
3671 EXPECT_TRUE(manager_.skip_packet_number_for_pto());
3672 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true));
3673 EXPECT_CALL(*send_algorithm_, PacingRate(_))
3674 .WillRepeatedly(Return(QuicBandwidth::Zero()));
3675 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
3676 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
3677 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3678 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
3679 QuicTime::Delta::Zero(), QuicTime::Zero());
3680 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
3681
3682 // Send packet 1.
3683 SendDataPacket(1, ENCRYPTION_INITIAL);
3684 const QuicTime packet1_sent_time = clock_.Now();
3685 // Verify PTO is correctly set.
fayang5d011982020-05-13 14:14:38 -07003686 int pto_rttvar_multiplier =
3687 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayang2ccfbcf2020-02-28 12:37:08 -08003688 QuicTime::Delta expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07003689 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayang2ccfbcf2020-02-28 12:37:08 -08003690 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3691 EXPECT_EQ(packet1_sent_time + expected_pto_delay,
3692 manager_.GetRetransmissionTime());
3693
3694 // Send packet 2 in handshake.
3695 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3696 SendDataPacket(2, ENCRYPTION_HANDSHAKE);
3697 const QuicTime packet2_sent_time = clock_.Now();
3698 // Verify PTO timeout is still based on packet 1.
3699 EXPECT_EQ(packet1_sent_time + expected_pto_delay,
3700 manager_.GetRetransmissionTime());
3701
3702 // Discard initial keys.
3703 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
3704 manager_.NeuterUnencryptedPackets();
3705
3706 // Send packet 3 in 1-RTT.
3707 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3708 SendDataPacket(3, ENCRYPTION_FORWARD_SECURE);
3709 // Verify PTO timeout is based on packet 2.
3710 const QuicTime packet3_sent_time = clock_.Now();
3711 EXPECT_EQ(packet2_sent_time + expected_pto_delay,
3712 manager_.GetRetransmissionTime());
3713
3714 // Send packet 4 in handshake.
3715 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3716 SendDataPacket(4, ENCRYPTION_HANDSHAKE);
3717 // Verify PTO timeout is based on packet 4 as application data is ignored.
3718 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3719 manager_.GetRetransmissionTime());
3720
3721 // Discard handshake keys.
3722 manager_.SetHandshakeConfirmed();
3723 // Verify PTO timeout is now based on packet 3 as handshake is
3724 // complete/confirmed.
3725 EXPECT_EQ(packet3_sent_time + expected_pto_delay,
3726 manager_.GetRetransmissionTime());
3727
3728 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3729 SendDataPacket(5, ENCRYPTION_FORWARD_SECURE);
3730 // Verify PTO timeout is still based on packet 3.
3731 EXPECT_EQ(packet3_sent_time + expected_pto_delay,
3732 manager_.GetRetransmissionTime());
3733}
3734
fayangc5d9b2c2020-03-02 11:12:26 -08003735TEST_F(QuicSentPacketManagerTest,
3736 ComputingProbeTimeoutByLeftEdge2MultiplePacketNumberSpaces) {
fayangc5d9b2c2020-03-02 11:12:26 -08003737 manager_.EnableMultiplePacketNumberSpacesSupport();
3738 EnablePto(k1PTO);
3739 // Use PTOS and PLE2.
3740 QuicConfig config;
3741 QuicTagVector options;
3742 options.push_back(kPTOS);
3743 options.push_back(kPLE2);
3744 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
3745 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
3746 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
3747 manager_.SetFromConfig(config);
3748 EXPECT_TRUE(manager_.skip_packet_number_for_pto());
3749 EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true));
3750 EXPECT_CALL(*send_algorithm_, PacingRate(_))
3751 .WillRepeatedly(Return(QuicBandwidth::Zero()));
3752 EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
3753 .WillRepeatedly(Return(10 * kDefaultTCPMSS));
3754 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3755 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
3756 QuicTime::Delta::Zero(), QuicTime::Zero());
3757 QuicTime::Delta srtt = rtt_stats->smoothed_rtt();
3758
3759 // Send packet 1.
3760 SendDataPacket(1, ENCRYPTION_INITIAL);
3761 const QuicTime packet1_sent_time = clock_.Now();
3762 // Verify PTO is correctly set.
fayang5d011982020-05-13 14:14:38 -07003763 int pto_rttvar_multiplier =
3764 GetQuicReloadableFlag(quic_default_on_pto) ? 2 : 4;
fayangc5d9b2c2020-03-02 11:12:26 -08003765 QuicTime::Delta expected_pto_delay =
fayang5d011982020-05-13 14:14:38 -07003766 srtt + pto_rttvar_multiplier * rtt_stats->mean_deviation() +
fayangc5d9b2c2020-03-02 11:12:26 -08003767 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
3768 EXPECT_EQ(packet1_sent_time + expected_pto_delay,
3769 manager_.GetRetransmissionTime());
3770
3771 // Send packet 2 in handshake.
3772 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3773 SendDataPacket(2, ENCRYPTION_HANDSHAKE);
3774 const QuicTime packet2_sent_time = clock_.Now();
3775 // Verify PTO timeout is still based on packet 1.
3776 EXPECT_EQ(packet1_sent_time + expected_pto_delay,
3777 manager_.GetRetransmissionTime());
3778
3779 // Discard initial keys.
3780 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
3781 manager_.NeuterUnencryptedPackets();
3782
3783 // Send packet 3 in 1-RTT.
3784 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3785 SendDataPacket(3, ENCRYPTION_FORWARD_SECURE);
3786 // Verify PTO timeout is based on packet 2.
3787 const QuicTime packet3_sent_time = clock_.Now();
3788 EXPECT_EQ(packet2_sent_time + expected_pto_delay,
3789 manager_.GetRetransmissionTime());
3790
3791 // Send packet 4 in handshake.
3792 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3793 SendDataPacket(4, ENCRYPTION_HANDSHAKE);
3794 // Verify PTO timeout is based on packet 4 as application data is ignored.
3795 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3796 manager_.GetRetransmissionTime());
3797
3798 // Discard handshake keys.
3799 manager_.SetHandshakeConfirmed();
3800 // Verify PTO timeout is now based on packet 3 as handshake is
3801 // complete/confirmed.
3802 EXPECT_EQ(packet3_sent_time + expected_pto_delay,
3803 manager_.GetRetransmissionTime());
3804
3805 // Send packet 5 10ms before PTO expiring.
3806 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(
3807 expected_pto_delay.ToMilliseconds() - 10));
3808 SendDataPacket(5, ENCRYPTION_FORWARD_SECURE);
3809 // Verify PTO timeout expands to packet 5 sent time + 1.5 * srtt.
3810 EXPECT_EQ(clock_.Now() + 1.5 * rtt_stats->smoothed_rtt(),
3811 manager_.GetRetransmissionTime());
3812}
3813
wubf4ab9652020-02-20 14:45:43 -08003814TEST_F(QuicSentPacketManagerTest, SetHandshakeConfirmed) {
3815 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
3816 manager_.EnableMultiplePacketNumberSpacesSupport();
3817
3818 SendDataPacket(1, ENCRYPTION_INITIAL);
3819
3820 SendDataPacket(2, ENCRYPTION_HANDSHAKE);
3821
3822 EXPECT_CALL(notifier_, OnFrameAcked(_, _, _))
3823 .WillOnce(
3824 Invoke([](const QuicFrame& /*frame*/, QuicTime::Delta ack_delay_time,
3825 QuicTime receive_timestamp) {
3826 EXPECT_TRUE(ack_delay_time.IsZero());
3827 EXPECT_EQ(receive_timestamp, QuicTime::Zero());
3828 return true;
3829 }));
3830
3831 if (GetQuicReloadableFlag(
3832 quic_avoid_overestimate_bandwidth_with_aggregation)) {
3833 EXPECT_CALL(*send_algorithm_, OnPacketNeutered(QuicPacketNumber(2)))
3834 .Times(1);
3835 }
3836 manager_.SetHandshakeConfirmed();
3837}
3838
fayangd5c2ffb2020-02-04 11:49:05 -08003839// Regresstion test for b/148841700.
3840TEST_F(QuicSentPacketManagerTest, NeuterUnencryptedPackets) {
3841 SendCryptoPacket(1);
3842 SendPingPacket(2, ENCRYPTION_INITIAL);
3843 // Crypto data has been discarded but ping does not.
fayang1f8619f2020-03-16 08:39:58 -07003844 EXPECT_CALL(notifier_, OnFrameAcked(_, _, _))
3845 .Times(2)
3846 .WillOnce(Return(false))
3847 .WillOnce(Return(true));
fayangd5c2ffb2020-02-04 11:49:05 -08003848 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
wubf4ab9652020-02-20 14:45:43 -08003849 if (GetQuicReloadableFlag(
3850 quic_avoid_overestimate_bandwidth_with_aggregation)) {
3851 EXPECT_CALL(*send_algorithm_, OnPacketNeutered(QuicPacketNumber(1)))
3852 .Times(1);
3853 }
fayangd5c2ffb2020-02-04 11:49:05 -08003854 manager_.NeuterUnencryptedPackets();
3855}
3856
fayang84427fc2020-03-09 12:43:26 -07003857TEST_F(QuicSentPacketManagerTest, NoPacketThresholdDetectionForRuntPackets) {
3858 EXPECT_TRUE(
3859 QuicSentPacketManagerPeer::UsePacketThresholdForRuntPackets(&manager_));
3860
fayang84427fc2020-03-09 12:43:26 -07003861 QuicConfig config;
3862 QuicTagVector options;
3863 options.push_back(kRUNT);
3864 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
3865 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
3866 EXPECT_CALL(*network_change_visitor_, OnCongestionChange());
3867 manager_.SetFromConfig(config);
3868
3869 EXPECT_FALSE(
3870 QuicSentPacketManagerPeer::UsePacketThresholdForRuntPackets(&manager_));
3871}
3872
fayang1640cc22020-03-21 14:11:02 -07003873TEST_F(QuicSentPacketManagerTest, GetPathDegradingDelay) {
3874 QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
3875 // Before RTT sample is available.
3876 // 2 TLPs + 2 RTOs.
3877 QuicTime::Delta expected_delay = QuicTime::Delta::Zero();
3878 for (size_t i = 0; i < 2; ++i) {
3879 QuicSentPacketManagerPeer::SetConsecutiveTlpCount(&manager_, i);
3880 expected_delay =
3881 expected_delay +
3882 QuicSentPacketManagerPeer::GetTailLossProbeDelay(&manager_);
3883 }
3884 for (size_t i = 0; i < 2; ++i) {
3885 QuicSentPacketManagerPeer::SetConsecutiveRtoCount(&manager_, i);
3886 expected_delay =
3887 expected_delay +
3888 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_);
3889 }
3890 EXPECT_EQ(expected_delay, manager_.GetPathDegradingDelay());
3891
3892 expected_delay = QuicTime::Delta::Zero();
3893 QuicSentPacketManagerPeer::SetConsecutiveTlpCount(&manager_, 0);
3894 QuicSentPacketManagerPeer::SetConsecutiveRtoCount(&manager_, 0);
3895
3896 // After RTT sample is available.
3897 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3898 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
3899 QuicTime::Delta::Zero(), QuicTime::Zero());
3900 // 2 TLPs + 2 RTOs.
3901 for (size_t i = 0; i < 2; ++i) {
3902 QuicSentPacketManagerPeer::SetConsecutiveTlpCount(&manager_, i);
3903 expected_delay =
3904 expected_delay +
3905 QuicSentPacketManagerPeer::GetTailLossProbeDelay(&manager_);
3906 }
3907 for (size_t i = 0; i < 2; ++i) {
3908 QuicSentPacketManagerPeer::SetConsecutiveRtoCount(&manager_, i);
3909 expected_delay =
3910 expected_delay +
3911 QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_);
3912 }
3913 EXPECT_EQ(expected_delay, manager_.GetPathDegradingDelay());
3914}
3915
fayang6c7313c2020-04-23 12:10:35 -07003916// Regression test for b/154050235.
3917TEST_F(QuicSentPacketManagerTest, ExponentialBackoffWithNoRttMeasurement) {
3918 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
fayangaf9903b2020-05-14 14:34:28 -07003919 manager_.EnableMultiplePacketNumberSpacesSupport();
fayang6c7313c2020-04-23 12:10:35 -07003920 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3921 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kInitialRttMs),
3922 rtt_stats->initial_rtt());
3923 EXPECT_TRUE(rtt_stats->smoothed_rtt().IsZero());
3924
3925 SendCryptoPacket(1);
3926 QuicTime::Delta expected_pto_delay =
3927 QuicTime::Delta::FromMilliseconds(3 * kInitialRttMs);
3928 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3929 manager_.GetRetransmissionTime());
3930
3931 // Invoke PTO.
3932 clock_.AdvanceTime(expected_pto_delay);
3933 manager_.OnRetransmissionTimeout();
3934
3935 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3936 .WillOnce(WithArgs<1>(Invoke([this]() { RetransmitCryptoPacket(3); })));
3937 manager_.MaybeSendProbePackets();
fayang5d011982020-05-13 14:14:38 -07003938 if (GetQuicReloadableFlag(quic_default_on_pto)) {
3939 manager_.AdjustPendingTimerTransmissions();
3940 }
fayang6c7313c2020-04-23 12:10:35 -07003941 // Verify exponential backoff of the PTO timeout.
3942 EXPECT_EQ(clock_.Now() + 2 * expected_pto_delay,
3943 manager_.GetRetransmissionTime());
3944}
3945
3946TEST_F(QuicSentPacketManagerTest, PtoDelayWithTinyInitialRtt) {
fayangaf9903b2020-05-14 14:34:28 -07003947 manager_.EnableMultiplePacketNumberSpacesSupport();
fayang6c7313c2020-04-23 12:10:35 -07003948 RttStats* rtt_stats = const_cast<RttStats*>(manager_.GetRttStats());
3949 // Assume client provided a tiny initial RTT.
3950 rtt_stats->set_initial_rtt(QuicTime::Delta::FromMicroseconds(1));
3951 EXPECT_EQ(QuicTime::Delta::FromMicroseconds(1), rtt_stats->initial_rtt());
3952 EXPECT_TRUE(rtt_stats->smoothed_rtt().IsZero());
3953
3954 SendCryptoPacket(1);
3955 QuicTime::Delta expected_pto_delay = QuicTime::Delta::FromMilliseconds(10);
3956 // Verify kMinHandshakeTimeoutMs is respected.
3957 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3958 manager_.GetRetransmissionTime());
3959
3960 // Invoke PTO.
3961 clock_.AdvanceTime(expected_pto_delay);
3962 manager_.OnRetransmissionTimeout();
3963
3964 EXPECT_CALL(notifier_, RetransmitFrames(_, _))
3965 .WillOnce(WithArgs<1>(Invoke([this]() { RetransmitCryptoPacket(3); })));
3966 manager_.MaybeSendProbePackets();
fayang5d011982020-05-13 14:14:38 -07003967 if (GetQuicReloadableFlag(quic_default_on_pto)) {
3968 manager_.AdjustPendingTimerTransmissions();
3969 }
fayang6c7313c2020-04-23 12:10:35 -07003970 // Verify exponential backoff of the PTO timeout.
3971 EXPECT_EQ(clock_.Now() + 2 * expected_pto_delay,
3972 manager_.GetRetransmissionTime());
3973}
3974
fayangaecfd532020-04-28 12:35:12 -07003975TEST_F(QuicSentPacketManagerTest, HandshakeAckCausesInitialKeyDropping) {
3976 manager_.EnableMultiplePacketNumberSpacesSupport();
3977 QuicSentPacketManagerPeer::SetPerspective(&manager_, Perspective::IS_CLIENT);
3978 // Send INITIAL packet 1.
3979 SendDataPacket(1, ENCRYPTION_INITIAL);
3980 QuicTime::Delta expected_pto_delay =
3981 QuicTime::Delta::FromMilliseconds(3 * kInitialRttMs);
3982 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3983 manager_.GetRetransmissionTime());
3984 // Send HANDSHAKE ack.
3985 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
3986 SendAckPacket(2, /*largest_acked=*/1, ENCRYPTION_HANDSHAKE);
3987 // Sending HANDSHAKE packet causes dropping of INITIAL key.
3988 EXPECT_CALL(notifier_, HasUnackedCryptoData()).WillRepeatedly(Return(false));
3989 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
3990 manager_.NeuterUnencryptedPackets();
3991 // There is no in flight packets.
3992 EXPECT_FALSE(manager_.HasInFlightPackets());
3993 // Verify PTO timer gets rearmed from now because of anti-amplification.
3994 EXPECT_EQ(clock_.Now() + expected_pto_delay,
3995 manager_.GetRetransmissionTime());
3996
3997 // Invoke PTO.
3998 clock_.AdvanceTime(expected_pto_delay);
3999 manager_.OnRetransmissionTimeout();
4000 // Verify nothing to probe (and connection will send PING for current
4001 // encryption level).
4002 EXPECT_CALL(notifier_, RetransmitFrames(_, _)).Times(0);
4003 manager_.MaybeSendProbePackets();
4004}
4005
QUICHE teama6ef0a62019-03-07 20:34:33 -05004006} // namespace
4007} // namespace test
4008} // namespace quic