Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / net / quic / congestion_control / tcp_cubic_sender_test.cc
blob82b216e68cd3b692214dce4951e5242b46473d0e
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/congestion_control/tcp_cubic_sender.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "net/quic/congestion_control/rtt_stats.h"
12 #include "net/quic/crypto/crypto_protocol.h"
13 #include "net/quic/proto/cached_network_parameters.pb.h"
14 #include "net/quic/quic_protocol.h"
15 #include "net/quic/quic_utils.h"
16 #include "net/quic/test_tools/mock_clock.h"
17 #include "net/quic/test_tools/quic_config_peer.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using std::min;
22 namespace net {
23 namespace test {
25 // TODO(ianswett): A number of theses tests were written with the assumption of
26 // an initial CWND of 10. They have carefully calculated values which should be
27 // updated to be based on kInitialCongestionWindowInsecure.
28 const uint32 kInitialCongestionWindowPackets = 10;
29 const uint32 kDefaultWindowTCP =
30 kInitialCongestionWindowPackets * kDefaultTCPMSS;
31 const float kRenoBeta = 0.7f; // Reno backoff factor.
33 class TcpCubicSenderPeer : public TcpCubicSender {
34 public:
35 TcpCubicSenderPeer(const QuicClock* clock,
36 bool reno,
37 QuicPacketCount max_tcp_congestion_window)
38 : TcpCubicSender(clock,
39 &rtt_stats_,
40 reno,
41 kInitialCongestionWindowPackets,
42 max_tcp_congestion_window,
43 &stats_) {}
45 QuicPacketCount congestion_window() {
46 return congestion_window_;
49 QuicPacketCount slowstart_threshold() {
50 return slowstart_threshold_;
53 const HybridSlowStart& hybrid_slow_start() const {
54 return hybrid_slow_start_;
57 float GetRenoBeta() const {
58 return RenoBeta();
61 RttStats rtt_stats_;
62 QuicConnectionStats stats_;
65 class TcpCubicSenderTest : public ::testing::Test {
66 protected:
67 TcpCubicSenderTest()
68 : one_ms_(QuicTime::Delta::FromMilliseconds(1)),
69 sender_(new TcpCubicSenderPeer(&clock_, true, kMaxCongestionWindow)),
70 packet_number_(1),
71 acked_packet_number_(0),
72 bytes_in_flight_(0) {
73 standard_packet_.bytes_sent = kDefaultTCPMSS;
76 int SendAvailableSendWindow() {
77 // Send as long as TimeUntilSend returns Zero.
78 int packets_sent = 0;
79 bool can_send = sender_->TimeUntilSend(
80 clock_.Now(), bytes_in_flight_, HAS_RETRANSMITTABLE_DATA).IsZero();
81 while (can_send) {
82 sender_->OnPacketSent(clock_.Now(), bytes_in_flight_, packet_number_++,
83 kDefaultTCPMSS, HAS_RETRANSMITTABLE_DATA);
84 ++packets_sent;
85 bytes_in_flight_ += kDefaultTCPMSS;
86 can_send = sender_->TimeUntilSend(
87 clock_.Now(), bytes_in_flight_, HAS_RETRANSMITTABLE_DATA).IsZero();
89 return packets_sent;
92 // Normal is that TCP acks every other segment.
93 void AckNPackets(int n) {
94 sender_->rtt_stats_.UpdateRtt(QuicTime::Delta::FromMilliseconds(60),
95 QuicTime::Delta::Zero(),
96 clock_.Now());
97 SendAlgorithmInterface::CongestionVector acked_packets;
98 SendAlgorithmInterface::CongestionVector lost_packets;
99 for (int i = 0; i < n; ++i) {
100 ++acked_packet_number_;
101 acked_packets.push_back(
102 std::make_pair(acked_packet_number_, standard_packet_));
104 sender_->OnCongestionEvent(
105 true, bytes_in_flight_, acked_packets, lost_packets);
106 bytes_in_flight_ -= n * kDefaultTCPMSS;
107 clock_.AdvanceTime(one_ms_);
110 void LoseNPackets(int n) {
111 SendAlgorithmInterface::CongestionVector acked_packets;
112 SendAlgorithmInterface::CongestionVector lost_packets;
113 for (int i = 0; i < n; ++i) {
114 ++acked_packet_number_;
115 lost_packets.push_back(
116 std::make_pair(acked_packet_number_, standard_packet_));
118 sender_->OnCongestionEvent(
119 false, bytes_in_flight_, acked_packets, lost_packets);
120 bytes_in_flight_ -= n * kDefaultTCPMSS;
123 // Does not increment acked_packet_number_.
124 void LosePacket(QuicPacketNumber packet_number) {
125 SendAlgorithmInterface::CongestionVector acked_packets;
126 SendAlgorithmInterface::CongestionVector lost_packets;
127 lost_packets.push_back(std::make_pair(packet_number, standard_packet_));
128 sender_->OnCongestionEvent(
129 false, bytes_in_flight_, acked_packets, lost_packets);
130 bytes_in_flight_ -= kDefaultTCPMSS;
133 const QuicTime::Delta one_ms_;
134 MockClock clock_;
135 scoped_ptr<TcpCubicSenderPeer> sender_;
136 QuicPacketNumber packet_number_;
137 QuicPacketNumber acked_packet_number_;
138 QuicByteCount bytes_in_flight_;
139 TransmissionInfo standard_packet_;
142 TEST_F(TcpCubicSenderTest, SimpleSender) {
143 // At startup make sure we are at the default.
144 EXPECT_EQ(kDefaultWindowTCP, sender_->GetCongestionWindow());
145 // At startup make sure we can send.
146 EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
148 HAS_RETRANSMITTABLE_DATA).IsZero());
149 // Make sure we can send.
150 EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
152 HAS_RETRANSMITTABLE_DATA).IsZero());
153 // And that window is un-affected.
154 EXPECT_EQ(kDefaultWindowTCP, sender_->GetCongestionWindow());
156 // Fill the send window with data, then verify that we can't send.
157 SendAvailableSendWindow();
158 EXPECT_FALSE(sender_->TimeUntilSend(clock_.Now(),
159 sender_->GetCongestionWindow(),
160 HAS_RETRANSMITTABLE_DATA).IsZero());
163 TEST_F(TcpCubicSenderTest, ApplicationLimitedSlowStart) {
164 // Send exactly 10 packets and ensure the CWND ends at 14 packets.
165 const int kNumberOfAcks = 5;
166 // At startup make sure we can send.
167 EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
169 HAS_RETRANSMITTABLE_DATA).IsZero());
170 // Make sure we can send.
171 EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
173 HAS_RETRANSMITTABLE_DATA).IsZero());
175 SendAvailableSendWindow();
176 for (int i = 0; i < kNumberOfAcks; ++i) {
177 AckNPackets(2);
179 QuicByteCount bytes_to_send = sender_->GetCongestionWindow();
180 // It's expected 2 acks will arrive when the bytes_in_flight are greater than
181 // half the CWND.
182 EXPECT_EQ(kDefaultWindowTCP + kDefaultTCPMSS * 2 * 2,
183 bytes_to_send);
186 TEST_F(TcpCubicSenderTest, ExponentialSlowStart) {
187 const int kNumberOfAcks = 20;
188 // At startup make sure we can send.
189 EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
191 HAS_RETRANSMITTABLE_DATA).IsZero());
192 EXPECT_EQ(QuicBandwidth::Zero(), sender_->BandwidthEstimate());
193 // Make sure we can send.
194 EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
196 HAS_RETRANSMITTABLE_DATA).IsZero());
198 for (int i = 0; i < kNumberOfAcks; ++i) {
199 // Send our full send window.
200 SendAvailableSendWindow();
201 AckNPackets(2);
203 const QuicByteCount cwnd = sender_->GetCongestionWindow();
204 EXPECT_EQ(kDefaultWindowTCP + kDefaultTCPMSS * 2 * kNumberOfAcks, cwnd);
205 EXPECT_EQ(QuicBandwidth::FromBytesAndTimeDelta(
206 cwnd, sender_->rtt_stats_.smoothed_rtt()),
207 sender_->BandwidthEstimate());
210 TEST_F(TcpCubicSenderTest, SlowStartPacketLoss) {
211 sender_->SetNumEmulatedConnections(1);
212 const int kNumberOfAcks = 10;
213 for (int i = 0; i < kNumberOfAcks; ++i) {
214 // Send our full send window.
215 SendAvailableSendWindow();
216 AckNPackets(2);
218 SendAvailableSendWindow();
219 QuicByteCount expected_send_window = kDefaultWindowTCP +
220 (kDefaultTCPMSS * 2 * kNumberOfAcks);
221 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
223 // Lose a packet to exit slow start.
224 LoseNPackets(1);
225 size_t packets_in_recovery_window = expected_send_window / kDefaultTCPMSS;
227 // We should now have fallen out of slow start with a reduced window.
228 expected_send_window *= kRenoBeta;
229 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
231 // Recovery phase. We need to ack every packet in the recovery window before
232 // we exit recovery.
233 size_t number_of_packets_in_window = expected_send_window / kDefaultTCPMSS;
234 DVLOG(1) << "number_packets: " << number_of_packets_in_window;
235 AckNPackets(packets_in_recovery_window);
236 SendAvailableSendWindow();
237 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
239 // We need to ack an entire window before we increase CWND by 1.
240 AckNPackets(number_of_packets_in_window - 2);
241 SendAvailableSendWindow();
242 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
244 // Next ack should increase cwnd by 1.
245 AckNPackets(1);
246 expected_send_window += kDefaultTCPMSS;
247 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
249 // Now RTO and ensure slow start gets reset.
250 EXPECT_TRUE(sender_->hybrid_slow_start().started());
251 sender_->OnRetransmissionTimeout(true);
252 EXPECT_FALSE(sender_->hybrid_slow_start().started());
255 TEST_F(TcpCubicSenderTest, NoPRRWhenLessThanOnePacketInFlight) {
256 SendAvailableSendWindow();
257 LoseNPackets(kInitialCongestionWindowPackets - 1);
258 AckNPackets(1);
259 // PRR will allow 2 packets for every ack during recovery.
260 EXPECT_EQ(2, SendAvailableSendWindow());
261 // Simulate abandoning all packets by supplying a bytes_in_flight of 0.
262 // PRR should now allow a packet to be sent, even though prr's state
263 // variables believe it has sent enough packets.
264 EXPECT_EQ(QuicTime::Delta::Zero(),
265 sender_->TimeUntilSend(clock_.Now(), 0, HAS_RETRANSMITTABLE_DATA));
268 TEST_F(TcpCubicSenderTest, SlowStartPacketLossPRR) {
269 sender_->SetNumEmulatedConnections(1);
270 // Test based on the first example in RFC6937.
271 // Ack 10 packets in 5 acks to raise the CWND to 20, as in the example.
272 const int kNumberOfAcks = 5;
273 for (int i = 0; i < kNumberOfAcks; ++i) {
274 // Send our full send window.
275 SendAvailableSendWindow();
276 AckNPackets(2);
278 SendAvailableSendWindow();
279 QuicByteCount expected_send_window = kDefaultWindowTCP +
280 (kDefaultTCPMSS * 2 * kNumberOfAcks);
281 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
283 LoseNPackets(1);
285 // We should now have fallen out of slow start with a reduced window.
286 size_t send_window_before_loss = expected_send_window;
287 expected_send_window *= kRenoBeta;
288 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
290 // Testing TCP proportional rate reduction.
291 // We should send packets paced over the received acks for the remaining
292 // outstanding packets. The number of packets before we exit recovery is the
293 // original CWND minus the packet that has been lost and the one which
294 // triggered the loss.
295 size_t remaining_packets_in_recovery =
296 send_window_before_loss / kDefaultTCPMSS - 2;
298 for (size_t i = 0; i < remaining_packets_in_recovery; ++i) {
299 AckNPackets(1);
300 SendAvailableSendWindow();
301 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
304 // We need to ack another window before we increase CWND by 1.
305 size_t number_of_packets_in_window = expected_send_window / kDefaultTCPMSS;
306 for (size_t i = 0; i < number_of_packets_in_window; ++i) {
307 AckNPackets(1);
308 EXPECT_EQ(1, SendAvailableSendWindow());
309 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
312 AckNPackets(1);
313 expected_send_window += kDefaultTCPMSS;
314 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
317 TEST_F(TcpCubicSenderTest, SlowStartBurstPacketLossPRR) {
318 sender_->SetNumEmulatedConnections(1);
319 // Test based on the second example in RFC6937, though we also implement
320 // forward acknowledgements, so the first two incoming acks will trigger
321 // PRR immediately.
322 // Ack 20 packets in 10 acks to raise the CWND to 30.
323 const int kNumberOfAcks = 10;
324 for (int i = 0; i < kNumberOfAcks; ++i) {
325 // Send our full send window.
326 SendAvailableSendWindow();
327 AckNPackets(2);
329 SendAvailableSendWindow();
330 QuicByteCount expected_send_window = kDefaultWindowTCP +
331 (kDefaultTCPMSS * 2 * kNumberOfAcks);
332 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
334 // Lose one more than the congestion window reduction, so that after loss,
335 // bytes_in_flight is lesser than the congestion window.
336 size_t send_window_after_loss = kRenoBeta * expected_send_window;
337 size_t num_packets_to_lose =
338 (expected_send_window - send_window_after_loss) / kDefaultTCPMSS + 1;
339 LoseNPackets(num_packets_to_lose);
340 // Immediately after the loss, ensure at least one packet can be sent.
341 // Losses without subsequent acks can occur with timer based loss detection.
342 EXPECT_TRUE(sender_->TimeUntilSend(
343 clock_.Now(), bytes_in_flight_, HAS_RETRANSMITTABLE_DATA).IsZero());
344 AckNPackets(1);
346 // We should now have fallen out of slow start with a reduced window.
347 expected_send_window *= kRenoBeta;
348 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
350 // Only 2 packets should be allowed to be sent, per PRR-SSRB
351 EXPECT_EQ(2, SendAvailableSendWindow());
353 // Ack the next packet, which triggers another loss.
354 LoseNPackets(1);
355 AckNPackets(1);
357 // Send 2 packets to simulate PRR-SSRB.
358 EXPECT_EQ(2, SendAvailableSendWindow());
360 // Ack the next packet, which triggers another loss.
361 LoseNPackets(1);
362 AckNPackets(1);
364 // Send 2 packets to simulate PRR-SSRB.
365 EXPECT_EQ(2, SendAvailableSendWindow());
367 // Exit recovery and return to sending at the new rate.
368 for (int i = 0; i < kNumberOfAcks; ++i) {
369 AckNPackets(1);
370 EXPECT_EQ(1, SendAvailableSendWindow());
374 TEST_F(TcpCubicSenderTest, RTOCongestionWindow) {
375 EXPECT_EQ(kDefaultWindowTCP, sender_->GetCongestionWindow());
376 EXPECT_EQ(kMaxCongestionWindow, sender_->slowstart_threshold());
378 // Expect the window to decrease to the minimum once the RTO fires
379 // and slow start threshold to be set to 1/2 of the CWND.
380 sender_->OnRetransmissionTimeout(true);
381 EXPECT_EQ(2 * kDefaultTCPMSS, sender_->GetCongestionWindow());
382 EXPECT_EQ(5u, sender_->slowstart_threshold());
385 TEST_F(TcpCubicSenderTest, RTOCongestionWindowNoRetransmission) {
386 EXPECT_EQ(kDefaultWindowTCP, sender_->GetCongestionWindow());
388 // Expect the window to remain unchanged if the RTO fires but no
389 // packets are retransmitted.
390 sender_->OnRetransmissionTimeout(false);
391 EXPECT_EQ(kDefaultWindowTCP, sender_->GetCongestionWindow());
394 TEST_F(TcpCubicSenderTest, RetransmissionDelay) {
395 const int64 kRttMs = 10;
396 const int64 kDeviationMs = 3;
397 EXPECT_EQ(QuicTime::Delta::Zero(), sender_->RetransmissionDelay());
399 sender_->rtt_stats_.UpdateRtt(QuicTime::Delta::FromMilliseconds(kRttMs),
400 QuicTime::Delta::Zero(), clock_.Now());
402 // Initial value is to set the median deviation to half of the initial
403 // rtt, the median in then multiplied by a factor of 4 and finally the
404 // smoothed rtt is added which is the initial rtt.
405 QuicTime::Delta expected_delay =
406 QuicTime::Delta::FromMilliseconds(kRttMs + kRttMs / 2 * 4);
407 EXPECT_EQ(expected_delay, sender_->RetransmissionDelay());
409 for (int i = 0; i < 100; ++i) {
410 // Run to make sure that we converge.
411 sender_->rtt_stats_.UpdateRtt(
412 QuicTime::Delta::FromMilliseconds(kRttMs + kDeviationMs),
413 QuicTime::Delta::Zero(), clock_.Now());
414 sender_->rtt_stats_.UpdateRtt(
415 QuicTime::Delta::FromMilliseconds(kRttMs - kDeviationMs),
416 QuicTime::Delta::Zero(), clock_.Now());
418 expected_delay = QuicTime::Delta::FromMilliseconds(kRttMs + kDeviationMs * 4);
420 EXPECT_NEAR(kRttMs, sender_->rtt_stats_.smoothed_rtt().ToMilliseconds(), 1);
421 EXPECT_NEAR(expected_delay.ToMilliseconds(),
422 sender_->RetransmissionDelay().ToMilliseconds(),
424 EXPECT_EQ(static_cast<int64>(
425 sender_->GetCongestionWindow() * kNumMicrosPerSecond /
426 sender_->rtt_stats_.smoothed_rtt().ToMicroseconds()),
427 sender_->BandwidthEstimate().ToBytesPerSecond());
430 TEST_F(TcpCubicSenderTest, SlowStartMaxSendWindow) {
431 const QuicPacketCount kMaxCongestionWindowTCP = 50;
432 const int kNumberOfAcks = 100;
433 sender_.reset(
434 new TcpCubicSenderPeer(&clock_, false, kMaxCongestionWindowTCP));
436 for (int i = 0; i < kNumberOfAcks; ++i) {
437 // Send our full send window.
438 SendAvailableSendWindow();
439 AckNPackets(2);
441 QuicByteCount expected_send_window = kMaxCongestionWindowTCP * kDefaultTCPMSS;
442 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
445 TEST_F(TcpCubicSenderTest, TcpRenoMaxCongestionWindow) {
446 const QuicPacketCount kMaxCongestionWindowTCP = 50;
447 const int kNumberOfAcks = 1000;
448 sender_.reset(new TcpCubicSenderPeer(&clock_, true, kMaxCongestionWindowTCP));
450 SendAvailableSendWindow();
451 AckNPackets(2);
452 // Make sure we fall out of slow start.
453 LoseNPackets(1);
455 for (int i = 0; i < kNumberOfAcks; ++i) {
456 // Send our full send window.
457 SendAvailableSendWindow();
458 AckNPackets(2);
461 QuicByteCount expected_send_window = kMaxCongestionWindowTCP * kDefaultTCPMSS;
462 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
465 TEST_F(TcpCubicSenderTest, TcpCubicMaxCongestionWindow) {
466 const QuicPacketCount kMaxCongestionWindowTCP = 50;
467 // Set to 10000 to compensate for small cubic alpha.
468 const int kNumberOfAcks = 10000;
470 sender_.reset(
471 new TcpCubicSenderPeer(&clock_, false, kMaxCongestionWindowTCP));
473 SendAvailableSendWindow();
474 AckNPackets(2);
475 // Make sure we fall out of slow start.
476 LoseNPackets(1);
478 for (int i = 0; i < kNumberOfAcks; ++i) {
479 // Send our full send window.
480 SendAvailableSendWindow();
481 AckNPackets(2);
484 QuicByteCount expected_send_window = kMaxCongestionWindowTCP * kDefaultTCPMSS;
485 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
488 TEST_F(TcpCubicSenderTest, MultipleLossesInOneWindow) {
489 SendAvailableSendWindow();
490 const QuicByteCount initial_window = sender_->GetCongestionWindow();
491 LosePacket(acked_packet_number_ + 1);
492 const QuicByteCount post_loss_window = sender_->GetCongestionWindow();
493 EXPECT_GT(initial_window, post_loss_window);
494 LosePacket(acked_packet_number_ + 3);
495 EXPECT_EQ(post_loss_window, sender_->GetCongestionWindow());
496 LosePacket(packet_number_ - 1);
497 EXPECT_EQ(post_loss_window, sender_->GetCongestionWindow());
499 // Lose a later packet and ensure the window decreases.
500 LosePacket(packet_number_);
501 EXPECT_GT(post_loss_window, sender_->GetCongestionWindow());
504 TEST_F(TcpCubicSenderTest, DontTrackAckPackets) {
505 // Send a packet with no retransmittable data, and ensure it's not tracked.
506 EXPECT_FALSE(sender_->OnPacketSent(clock_.Now(), bytes_in_flight_,
507 packet_number_++, kDefaultTCPMSS,
508 NO_RETRANSMITTABLE_DATA));
510 // Send a data packet with retransmittable data, and ensure it is tracked.
511 EXPECT_TRUE(sender_->OnPacketSent(clock_.Now(), bytes_in_flight_,
512 packet_number_++, kDefaultTCPMSS,
513 HAS_RETRANSMITTABLE_DATA));
516 TEST_F(TcpCubicSenderTest, ConfigureInitialWindow) {
517 QuicConfig config;
519 QuicTagVector options;
520 options.push_back(kIW03);
521 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
522 sender_->SetFromConfig(config, Perspective::IS_SERVER);
523 EXPECT_EQ(3u, sender_->congestion_window());
525 options.clear();
526 options.push_back(kIW10);
527 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
528 sender_->SetFromConfig(config, Perspective::IS_SERVER);
529 EXPECT_EQ(10u, sender_->congestion_window());
531 options.clear();
532 options.push_back(kIW20);
533 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
534 sender_->SetFromConfig(config, Perspective::IS_SERVER);
535 EXPECT_EQ(20u, sender_->congestion_window());
537 options.clear();
538 options.push_back(kIW50);
539 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
540 sender_->SetFromConfig(config, Perspective::IS_SERVER);
541 EXPECT_EQ(50u, sender_->congestion_window());
544 TEST_F(TcpCubicSenderTest, ConfigureMinimumWindow) {
545 QuicConfig config;
547 // Verify that kCOPT: kMIN1 forces the min CWND to 1 packet.
548 QuicTagVector options;
549 options.push_back(kMIN1);
550 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
551 sender_->SetFromConfig(config, Perspective::IS_SERVER);
552 sender_->OnRetransmissionTimeout(true);
553 EXPECT_EQ(1u, sender_->congestion_window());
556 TEST_F(TcpCubicSenderTest, 2ConnectionCongestionAvoidanceAtEndOfRecovery) {
557 sender_->SetNumEmulatedConnections(2);
558 // Ack 10 packets in 5 acks to raise the CWND to 20.
559 const int kNumberOfAcks = 5;
560 for (int i = 0; i < kNumberOfAcks; ++i) {
561 // Send our full send window.
562 SendAvailableSendWindow();
563 AckNPackets(2);
565 SendAvailableSendWindow();
566 QuicByteCount expected_send_window = kDefaultWindowTCP +
567 (kDefaultTCPMSS * 2 * kNumberOfAcks);
568 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
570 LoseNPackets(1);
572 // We should now have fallen out of slow start with a reduced window.
573 expected_send_window = expected_send_window * sender_->GetRenoBeta();
574 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
576 // No congestion window growth should occur in recovery phase, i.e., until the
577 // currently outstanding 20 packets are acked.
578 for (int i = 0; i < 10; ++i) {
579 // Send our full send window.
580 SendAvailableSendWindow();
581 EXPECT_TRUE(sender_->InRecovery());
582 AckNPackets(2);
583 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
585 EXPECT_FALSE(sender_->InRecovery());
587 // Out of recovery now. Congestion window should not grow for half an RTT.
588 size_t packets_in_send_window = expected_send_window / kDefaultTCPMSS;
589 SendAvailableSendWindow();
590 AckNPackets(packets_in_send_window / 2 - 2);
591 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
593 // Next ack should increase congestion window by 1MSS.
594 SendAvailableSendWindow();
595 AckNPackets(2);
596 expected_send_window += kDefaultTCPMSS;
597 packets_in_send_window += 1;
598 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
600 // Congestion window should remain steady again for half an RTT.
601 SendAvailableSendWindow();
602 AckNPackets(packets_in_send_window / 2 - 1);
603 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
605 // Next ack should cause congestion window to grow by 1MSS.
606 SendAvailableSendWindow();
607 AckNPackets(2);
608 expected_send_window += kDefaultTCPMSS;
609 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
612 TEST_F(TcpCubicSenderTest, 1ConnectionCongestionAvoidanceAtEndOfRecovery) {
613 sender_->SetNumEmulatedConnections(1);
614 // Ack 10 packets in 5 acks to raise the CWND to 20.
615 const int kNumberOfAcks = 5;
616 for (int i = 0; i < kNumberOfAcks; ++i) {
617 // Send our full send window.
618 SendAvailableSendWindow();
619 AckNPackets(2);
621 SendAvailableSendWindow();
622 QuicByteCount expected_send_window = kDefaultWindowTCP +
623 (kDefaultTCPMSS * 2 * kNumberOfAcks);
624 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
626 LoseNPackets(1);
628 // We should now have fallen out of slow start with a reduced window.
629 expected_send_window *= kRenoBeta;
630 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
632 // No congestion window growth should occur in recovery phase, i.e., until the
633 // currently outstanding 20 packets are acked.
634 for (int i = 0; i < 10; ++i) {
635 // Send our full send window.
636 SendAvailableSendWindow();
637 EXPECT_TRUE(sender_->InRecovery());
638 AckNPackets(2);
639 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
641 EXPECT_FALSE(sender_->InRecovery());
643 // Out of recovery now. Congestion window should not grow during RTT.
644 for (uint64 i = 0; i < expected_send_window / kDefaultTCPMSS - 2; i += 2) {
645 // Send our full send window.
646 SendAvailableSendWindow();
647 AckNPackets(2);
648 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
651 // Next ack should cause congestion window to grow by 1MSS.
652 SendAvailableSendWindow();
653 AckNPackets(2);
654 expected_send_window += kDefaultTCPMSS;
655 EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
658 TEST_F(TcpCubicSenderTest, BandwidthResumption) {
659 // Test that when provided with CachedNetworkParameters and opted in to the
660 // bandwidth resumption experiment, that the TcpCubicSender sets initial CWND
661 // appropriately.
663 // Set some common values.
664 CachedNetworkParameters cached_network_params;
665 const QuicPacketCount kNumberOfPackets = 123;
666 const int kBandwidthEstimateBytesPerSecond =
667 kNumberOfPackets * kMaxPacketSize;
668 cached_network_params.set_bandwidth_estimate_bytes_per_second(
669 kBandwidthEstimateBytesPerSecond);
670 cached_network_params.set_min_rtt_ms(1000);
672 // Make sure that a bandwidth estimate results in a changed CWND.
673 cached_network_params.set_timestamp(clock_.WallNow().ToUNIXSeconds() -
674 (kNumSecondsPerHour - 1));
675 sender_->ResumeConnectionState(cached_network_params, false);
676 EXPECT_EQ(kNumberOfPackets, sender_->congestion_window());
678 // Resumed CWND is limited to be in a sensible range.
679 cached_network_params.set_bandwidth_estimate_bytes_per_second(
680 (kMaxCongestionWindow + 1) * kMaxPacketSize);
681 sender_->ResumeConnectionState(cached_network_params, false);
682 EXPECT_EQ(kMaxCongestionWindow, sender_->congestion_window());
684 cached_network_params.set_bandwidth_estimate_bytes_per_second(
685 (kMinCongestionWindowForBandwidthResumption - 1) * kMaxPacketSize);
686 sender_->ResumeConnectionState(cached_network_params, false);
687 EXPECT_EQ(kMinCongestionWindowForBandwidthResumption,
688 sender_->congestion_window());
690 // Resume to the max value.
691 cached_network_params.set_max_bandwidth_estimate_bytes_per_second(
692 (kMinCongestionWindowForBandwidthResumption + 10) * kDefaultTCPMSS);
693 sender_->ResumeConnectionState(cached_network_params, true);
694 EXPECT_EQ((kMinCongestionWindowForBandwidthResumption + 10) * kDefaultTCPMSS,
695 sender_->GetCongestionWindow());
698 TEST_F(TcpCubicSenderTest, PaceBelowCWND) {
699 QuicConfig config;
701 // Verify that kCOPT: kMIN4 forces the min CWND to 1 packet, but allows up
702 // to 4 to be sent.
703 QuicTagVector options;
704 options.push_back(kMIN4);
705 QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
706 sender_->SetFromConfig(config, Perspective::IS_SERVER);
707 sender_->OnRetransmissionTimeout(true);
708 EXPECT_EQ(1u, sender_->congestion_window());
709 EXPECT_TRUE(sender_->TimeUntilSend(QuicTime::Zero(), kDefaultTCPMSS,
710 HAS_RETRANSMITTABLE_DATA).IsZero());
711 EXPECT_TRUE(sender_->TimeUntilSend(QuicTime::Zero(), 2 * kDefaultTCPMSS,
712 HAS_RETRANSMITTABLE_DATA).IsZero());
713 EXPECT_TRUE(sender_->TimeUntilSend(QuicTime::Zero(), 3 * kDefaultTCPMSS,
714 HAS_RETRANSMITTABLE_DATA).IsZero());
715 EXPECT_FALSE(sender_->TimeUntilSend(QuicTime::Zero(), 4 * kDefaultTCPMSS,
716 HAS_RETRANSMITTABLE_DATA).IsZero());
719 } // namespace test
720 } // namespace net