summaryrefslogtreecommitdiff
path: root/chromium/net/quic/congestion_control/tcp_cubic_sender_test.cc
blob: e9f889374267f16aa097d30b6b2f395738bf4787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "net/quic/congestion_control/tcp_cubic_sender.h"
#include "net/quic/congestion_control/tcp_receiver.h"
#include "net/quic/test_tools/mock_clock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {
namespace test {

const uint32 kDefaultWindowTCP = 10 * kMaxPacketSize;
const QuicByteCount kNoNBytesInFlight = 0;

class TcpCubicSenderPeer : public TcpCubicSender {
 public:
  // TODO(ianswett): Remove 10000 once b/10075719 is fixed.
  TcpCubicSenderPeer(const QuicClock* clock, bool reno)
      : TcpCubicSender(clock, reno, 10000) {
  }
  using TcpCubicSender::AvailableCongestionWindow;
  using TcpCubicSender::CongestionWindow;
  using TcpCubicSender::AckAccounting;
};

class TcpCubicSenderTest : public ::testing::Test {
 protected:
  TcpCubicSenderTest()
     : rtt_(QuicTime::Delta::FromMilliseconds(60)),
       one_ms_(QuicTime::Delta::FromMilliseconds(1)),
       sender_(new TcpCubicSenderPeer(&clock_, true)),
       receiver_(new TcpReceiver()),
       sequence_number_(1),
       acked_sequence_number_(0) {
  }

  void SendAvailableCongestionWindow() {
    QuicByteCount bytes_to_send = sender_->AvailableCongestionWindow();
    while (bytes_to_send > 0) {
      QuicByteCount bytes_in_packet = std::min(kMaxPacketSize, bytes_to_send);
      sender_->SentPacket(clock_.Now(), sequence_number_++, bytes_in_packet,
                          NOT_RETRANSMISSION);
      bytes_to_send -= bytes_in_packet;
      if (bytes_to_send > 0) {
        EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(), NOT_RETRANSMISSION,
                        HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero());
      }
    }
  }
  // Normal is that TCP acks every other segment.
  void AckNPackets(int n) {
    for (int i = 0; i < n; ++i) {
      acked_sequence_number_++;
      sender_->OnIncomingAck(acked_sequence_number_, kMaxPacketSize, rtt_);
    }
    clock_.AdvanceTime(one_ms_);  // 1 millisecond.
  }

  const QuicTime::Delta rtt_;
  const QuicTime::Delta one_ms_;
  MockClock clock_;
  SendAlgorithmInterface::SentPacketsMap not_used_;
  scoped_ptr<TcpCubicSenderPeer> sender_;
  scoped_ptr<TcpReceiver> receiver_;
  QuicPacketSequenceNumber sequence_number_;
  QuicPacketSequenceNumber acked_sequence_number_;
};

TEST_F(TcpCubicSenderTest, SimpleSender) {
  QuicCongestionFeedbackFrame feedback;
  // At startup make sure we are at the default.
  EXPECT_EQ(kDefaultWindowTCP,
            sender_->AvailableCongestionWindow());
  // At startup make sure we can send.
  EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
      NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero());
  // Get default QuicCongestionFeedbackFrame from receiver.
  ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
  sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now(),
                                                 not_used_);
  // Make sure we can send.
  EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
      NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero());
  // And that window is un-affected.
  EXPECT_EQ(kDefaultWindowTCP, sender_->AvailableCongestionWindow());

  // A retransmitt should always retun 0.
  EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
      IS_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero());
}

TEST_F(TcpCubicSenderTest, ExponentialSlowStart) {
  const int kNumberOfAck = 20;
  QuicCongestionFeedbackFrame feedback;
  // At startup make sure we can send.
  EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
      NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero());
  // Get default QuicCongestionFeedbackFrame from receiver.
  ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
  sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now(),
                                                 not_used_);
  // Make sure we can send.
  EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
      NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero());

  for (int n = 0; n < kNumberOfAck; ++n) {
    // Send our full congestion window.
    SendAvailableCongestionWindow();
    AckNPackets(2);
  }
  QuicByteCount bytes_to_send = sender_->CongestionWindow();
  EXPECT_EQ(kDefaultWindowTCP + kMaxPacketSize * 2 * kNumberOfAck,
            bytes_to_send);
}

TEST_F(TcpCubicSenderTest, SlowStartAckTrain) {
  // Make sure that we fall out of slow start when we send ACK train longer
  // than half the RTT, in this test case 30ms, which is more than 30 calls to
  // Ack2Packets in one round.
  // Since we start at 10 packet first round will be 5 second round 10 etc
  // Hence we should pass 30 at 65 = 5 + 10 + 20 + 30
  const int kNumberOfAck = 65;
  QuicCongestionFeedbackFrame feedback;
  // At startup make sure we can send.
  EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
      NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero());
  // Get default QuicCongestionFeedbackFrame from receiver.
  ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
  sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now(),
                                                 not_used_);
  // Make sure we can send.
  EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
      NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero());

  for (int n = 0; n < kNumberOfAck; ++n) {
    // Send our full congestion window.
    SendAvailableCongestionWindow();
    AckNPackets(2);
  }
  QuicByteCount expected_congestion_window =
      kDefaultWindowTCP + (kMaxPacketSize * 2 * kNumberOfAck);
  EXPECT_EQ(expected_congestion_window, sender_->CongestionWindow());
  // We should now have fallen out of slow start.
  SendAvailableCongestionWindow();
  AckNPackets(2);
  expected_congestion_window += kMaxPacketSize;
  EXPECT_EQ(expected_congestion_window, sender_->CongestionWindow());

  // Testing Reno phase.
  // We should need 141(65*2+1+10) ACK:ed packets before increasing window by
  // one.
  for (int m = 0; m < 70; ++m) {
    SendAvailableCongestionWindow();
    AckNPackets(2);
    EXPECT_EQ(expected_congestion_window, sender_->CongestionWindow());
  }
  SendAvailableCongestionWindow();
  AckNPackets(2);
  expected_congestion_window += kMaxPacketSize;
  EXPECT_EQ(expected_congestion_window, sender_->CongestionWindow());
}

TEST_F(TcpCubicSenderTest, SlowStartPacketLoss) {
  // Make sure that we fall out of slow start when we encounter a packet loss.
  const int kNumberOfAck = 10;
  QuicCongestionFeedbackFrame feedback;
  // At startup make sure we can send.
  EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
      NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero());
  // Get default QuicCongestionFeedbackFrame from receiver.
  ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
  sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now(),
                                                 not_used_);
  // Make sure we can send.
  EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
      NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero());

  for (int i = 0; i < kNumberOfAck; ++i) {
    // Send our full congestion window.
    SendAvailableCongestionWindow();
    AckNPackets(2);
  }
  SendAvailableCongestionWindow();
  QuicByteCount expected_congestion_window = kDefaultWindowTCP +
      (kMaxPacketSize * 2 * kNumberOfAck);
  EXPECT_EQ(expected_congestion_window, sender_->CongestionWindow());

  sender_->OnIncomingLoss(clock_.Now());

  // Make sure that we should not send right now.
  EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(), NOT_RETRANSMISSION,
      HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsInfinite());

  // We should now have fallen out of slow start.
  // We expect window to be cut in half.
  expected_congestion_window /= 2;
  EXPECT_EQ(expected_congestion_window, sender_->CongestionWindow());

  // Testing Reno phase.
  // We need to ack half of the pending packet before we can send agin.
  int number_of_packets_in_window = expected_congestion_window / kMaxPacketSize;
  AckNPackets(number_of_packets_in_window);
  EXPECT_EQ(expected_congestion_window, sender_->CongestionWindow());
  EXPECT_EQ(0u, sender_->AvailableCongestionWindow());

  AckNPackets(1);
  expected_congestion_window += kMaxPacketSize;
  number_of_packets_in_window++;
  EXPECT_EQ(expected_congestion_window, sender_->CongestionWindow());

  // We should need number_of_packets_in_window ACK:ed packets before
  // increasing window by one.
  for (int k = 0; k < number_of_packets_in_window; ++k) {
    SendAvailableCongestionWindow();
    AckNPackets(1);
    EXPECT_EQ(expected_congestion_window, sender_->CongestionWindow());
  }
  SendAvailableCongestionWindow();
  AckNPackets(1);
  expected_congestion_window += kMaxPacketSize;
  EXPECT_EQ(expected_congestion_window, sender_->CongestionWindow());
}

TEST_F(TcpCubicSenderTest, RetransmissionDelay) {
  const int64 kRttMs = 10;
  const int64 kDeviationMs = 3;
  EXPECT_EQ(QuicTime::Delta::Zero(), sender_->RetransmissionDelay());

  sender_->AckAccounting(QuicTime::Delta::FromMilliseconds(kRttMs));

  // Initial value is to set the median deviation to half of the initial
  // rtt, the median in then multiplied by a factor of 4 and finaly the
  // smoothed rtt is added which is the inital rtt.
  QuicTime::Delta expected_delay =
      QuicTime::Delta::FromMilliseconds(kRttMs + kRttMs / 2 * 4);
  EXPECT_EQ(expected_delay, sender_->RetransmissionDelay());

  for (int i = 0; i < 100; ++i) {
    // Run to make sure that we converge.
    sender_->AckAccounting(
        QuicTime::Delta::FromMilliseconds(kRttMs + kDeviationMs));
    sender_->AckAccounting(
        QuicTime::Delta::FromMilliseconds(kRttMs - kDeviationMs));
  }
  expected_delay = QuicTime::Delta::FromMilliseconds(kRttMs + kDeviationMs * 4);

  EXPECT_NEAR(kRttMs, sender_->SmoothedRtt().ToMilliseconds(), 1);
  EXPECT_NEAR(expected_delay.ToMilliseconds(),
              sender_->RetransmissionDelay().ToMilliseconds(),
              1);
}
}  // namespace test
}  // namespace net