Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / quic / congestion_control / pacing_sender_test.cc
blobcf069efe8066103f81b19a8a4bfb2256543640b9
1 // Copyright (c) 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.
5 #include "net/quic/congestion_control/pacing_sender.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "net/quic/quic_protocol.h"
10 #include "net/quic/test_tools/mock_clock.h"
11 #include "net/quic/test_tools/quic_test_utils.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using testing::Return;
15 using testing::StrictMock;
16 using testing::_;
18 namespace net {
19 namespace test {
21 const QuicByteCount kBytesInFlight = 1024;
22 const int kInitialBurstPackets = 10;
24 class PacingSenderTest : public ::testing::Test {
25 protected:
26 PacingSenderTest()
27 : zero_time_(QuicTime::Delta::Zero()),
28 infinite_time_(QuicTime::Delta::Infinite()),
29 packet_number_(1),
30 mock_sender_(new StrictMock<MockSendAlgorithm>()),
31 pacing_sender_(new PacingSender(mock_sender_,
32 QuicTime::Delta::FromMilliseconds(1),
33 0)) {
34 // Pick arbitrary time.
35 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(9));
38 ~PacingSenderTest() override {}
40 void InitPacingRate(QuicPacketCount burst_size, QuicBandwidth bandwidth) {
41 pacing_sender_.reset();
42 mock_sender_ = new StrictMock<MockSendAlgorithm>();
43 pacing_sender_.reset(new PacingSender(
44 mock_sender_, QuicTime::Delta::FromMilliseconds(1), burst_size));
45 EXPECT_CALL(*mock_sender_, PacingRate()).WillRepeatedly(Return(bandwidth));
48 void CheckPacketIsSentImmediately(HasRetransmittableData retransmittable_data,
49 QuicByteCount bytes_in_flight,
50 bool in_recovery) {
51 // In order for the packet to be sendable, the underlying sender must
52 // permit it to be sent immediately.
53 for (int i = 0; i < 2; ++i) {
54 EXPECT_CALL(*mock_sender_, TimeUntilSend(clock_.Now(), bytes_in_flight,
55 retransmittable_data))
56 .WillOnce(Return(zero_time_));
57 // Verify that the packet can be sent immediately.
58 EXPECT_EQ(zero_time_,
59 pacing_sender_->TimeUntilSend(clock_.Now(), bytes_in_flight,
60 retransmittable_data));
63 // Actually send the packet.
64 if (bytes_in_flight == 0) {
65 EXPECT_CALL(*mock_sender_, InRecovery()).WillOnce(Return(in_recovery));
67 EXPECT_CALL(*mock_sender_,
68 OnPacketSent(clock_.Now(), bytes_in_flight, packet_number_,
69 kMaxPacketSize, retransmittable_data));
70 pacing_sender_->OnPacketSent(clock_.Now(), bytes_in_flight,
71 packet_number_++, kMaxPacketSize,
72 retransmittable_data);
75 void CheckPacketIsSentImmediately() {
76 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, kBytesInFlight,
77 false);
80 void CheckAckIsSentImmediately() {
81 CheckPacketIsSentImmediately(NO_RETRANSMITTABLE_DATA, kBytesInFlight,
82 false);
85 void CheckPacketIsDelayed(QuicTime::Delta delay) {
86 // In order for the packet to be sendable, the underlying sender must
87 // permit it to be sent immediately.
88 for (int i = 0; i < 2; ++i) {
89 EXPECT_CALL(*mock_sender_, TimeUntilSend(clock_.Now(),
90 kBytesInFlight,
91 HAS_RETRANSMITTABLE_DATA))
92 .WillOnce(Return(zero_time_));
93 // Verify that the packet is delayed.
94 EXPECT_EQ(delay.ToMicroseconds(),
95 pacing_sender_->TimeUntilSend(
96 clock_.Now(), kBytesInFlight,
97 HAS_RETRANSMITTABLE_DATA).ToMicroseconds());
101 void UpdateRtt() {
102 EXPECT_CALL(*mock_sender_, OnCongestionEvent(true, kBytesInFlight, _, _));
103 SendAlgorithmInterface::CongestionVector empty_map;
104 pacing_sender_->OnCongestionEvent(true, kBytesInFlight, empty_map,
105 empty_map);
108 const QuicTime::Delta zero_time_;
109 const QuicTime::Delta infinite_time_;
110 MockClock clock_;
111 QuicPacketNumber packet_number_;
112 StrictMock<MockSendAlgorithm>* mock_sender_;
113 scoped_ptr<PacingSender> pacing_sender_;
116 TEST_F(PacingSenderTest, NoSend) {
117 for (int i = 0; i < 2; ++i) {
118 EXPECT_CALL(*mock_sender_, TimeUntilSend(clock_.Now(),
119 kBytesInFlight,
120 HAS_RETRANSMITTABLE_DATA))
121 .WillOnce(Return(infinite_time_));
122 EXPECT_EQ(infinite_time_,
123 pacing_sender_->TimeUntilSend(clock_.Now(),
124 kBytesInFlight,
125 HAS_RETRANSMITTABLE_DATA));
129 TEST_F(PacingSenderTest, SendNow) {
130 for (int i = 0; i < 2; ++i) {
131 EXPECT_CALL(*mock_sender_, TimeUntilSend(clock_.Now(),
132 kBytesInFlight,
133 HAS_RETRANSMITTABLE_DATA))
134 .WillOnce(Return(zero_time_));
135 EXPECT_EQ(zero_time_,
136 pacing_sender_->TimeUntilSend(clock_.Now(),
137 kBytesInFlight,
138 HAS_RETRANSMITTABLE_DATA));
142 TEST_F(PacingSenderTest, VariousSending) {
143 // Configure pacing rate of 1 packet per 1 ms, no initial burst.
144 InitPacingRate(0, QuicBandwidth::FromBytesAndTimeDelta(
145 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
147 // Now update the RTT and verify that packets are actually paced.
148 UpdateRtt();
150 CheckPacketIsSentImmediately();
151 CheckPacketIsSentImmediately();
153 // The first packet was a "make up", then we sent two packets "into the
154 // future", so the delay should be 2.
155 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
157 // Wake up on time.
158 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2));
159 CheckPacketIsSentImmediately();
160 CheckPacketIsSentImmediately();
161 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
162 CheckAckIsSentImmediately();
164 // Wake up late.
165 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(4));
166 CheckPacketIsSentImmediately();
167 CheckPacketIsSentImmediately();
168 CheckPacketIsSentImmediately();
169 CheckPacketIsSentImmediately();
170 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
172 // Wake up really late.
173 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
174 CheckPacketIsSentImmediately();
175 CheckPacketIsSentImmediately();
176 CheckPacketIsSentImmediately();
177 CheckPacketIsSentImmediately();
178 CheckPacketIsSentImmediately();
179 CheckPacketIsSentImmediately();
180 CheckPacketIsSentImmediately();
181 CheckPacketIsSentImmediately();
182 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
184 // Wake up really late again, but application pause partway through.
185 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
186 CheckPacketIsSentImmediately();
187 CheckPacketIsSentImmediately();
188 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
189 CheckPacketIsSentImmediately();
190 CheckPacketIsSentImmediately();
191 CheckPacketIsSentImmediately();
192 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
194 // Wake up too early.
195 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
197 // Wake up early, but after enough time has passed to permit a send.
198 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
199 CheckPacketIsSentImmediately();
200 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
203 TEST_F(PacingSenderTest, InitialBurst) {
204 // Configure pacing rate of 1 packet per 1 ms.
205 InitPacingRate(10, QuicBandwidth::FromBytesAndTimeDelta(
206 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
208 EXPECT_CALL(*mock_sender_, GetCongestionWindow())
209 .WillOnce(Return(10 * kDefaultTCPMSS));
210 // Update the RTT and verify that the first 10 packets aren't paced.
211 UpdateRtt();
213 // Send 10 packets, and verify that they are not paced.
214 for (int i = 0 ; i < kInitialBurstPackets; ++i) {
215 CheckPacketIsSentImmediately();
218 // The first packet was a "make up", then we sent two packets "into the
219 // future", so the delay should be 2ms.
220 CheckPacketIsSentImmediately();
221 CheckPacketIsSentImmediately();
222 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
224 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
225 CheckPacketIsSentImmediately();
227 // Next time TimeUntilSend is called with no bytes in flight, pacing should
228 // allow a packet to be sent, and when it's sent, the tokens are refilled.
229 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0, false);
230 for (int i = 0; i < kInitialBurstPackets - 1; ++i) {
231 CheckPacketIsSentImmediately();
234 // The first packet was a "make up", then we sent two packets "into the
235 // future", so the delay should be 2ms.
236 CheckPacketIsSentImmediately();
237 CheckPacketIsSentImmediately();
238 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
241 TEST_F(PacingSenderTest, InitialBurstNoRttMeasurement) {
242 // Configure pacing rate of 1 packet per 1 ms.
243 InitPacingRate(10, QuicBandwidth::FromBytesAndTimeDelta(
244 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
246 EXPECT_CALL(*mock_sender_, GetCongestionWindow())
247 .WillOnce(Return(10 * kDefaultTCPMSS));
248 // Send 10 packets, and verify that they are not paced.
249 for (int i = 0 ; i < kInitialBurstPackets; ++i) {
250 CheckPacketIsSentImmediately();
253 // The first packet was a "make up", then we sent two packets "into the
254 // future", so the delay should be 2ms.
255 CheckPacketIsSentImmediately();
256 CheckPacketIsSentImmediately();
257 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
260 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
261 CheckPacketIsSentImmediately();
263 // Next time TimeUntilSend is called with no bytes in flight, the tokens
264 // should be refilled and there should be no delay.
265 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0, false);
266 // Send 10 packets, and verify that they are not paced.
267 for (int i = 0; i < kInitialBurstPackets - 1; ++i) {
268 CheckPacketIsSentImmediately();
271 // The first packet was a "make up", then we sent two packets "into the
272 // future", so the delay should be 2ms.
273 CheckPacketIsSentImmediately();
274 CheckPacketIsSentImmediately();
275 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
278 TEST_F(PacingSenderTest, FastSending) {
279 // Ensure the pacing sender paces, even when the inter-packet spacing is less
280 // than the pacing granularity.
281 InitPacingRate(10,
282 QuicBandwidth::FromBytesAndTimeDelta(
283 2 * kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
285 EXPECT_CALL(*mock_sender_, GetCongestionWindow())
286 .WillOnce(Return(10 * kDefaultTCPMSS));
287 // Update the RTT and verify that the first 10 packets aren't paced.
288 UpdateRtt();
290 // Send 10 packets, and verify that they are not paced.
291 for (int i = 0; i < kInitialBurstPackets; ++i) {
292 CheckPacketIsSentImmediately();
295 // The first packet was a "make up", then we sent two packets "into the
296 // future", since it's 2 packets/ms, so the delay should be 1.5ms.
297 CheckPacketIsSentImmediately();
298 CheckPacketIsSentImmediately();
299 CheckPacketIsSentImmediately();
300 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1500));
302 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
303 CheckPacketIsSentImmediately();
305 // Next time TimeUntilSend is called with no bytes in flight, the tokens
306 // should be refilled and there should be no delay.
307 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0, false);
308 for (int i = 0; i < kInitialBurstPackets - 1; ++i) {
309 CheckPacketIsSentImmediately();
312 // The first packet was a "make up", then we sent two packets "into the
313 // future", so the delay should be 1.5ms.
314 CheckPacketIsSentImmediately();
315 CheckPacketIsSentImmediately();
316 CheckPacketIsSentImmediately();
317 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1500));
320 TEST_F(PacingSenderTest, NoBurstInRecovery) {
321 // Configure pacing rate of 1 packet per 1 ms with no burst tokens.
322 InitPacingRate(0, QuicBandwidth::FromBytesAndTimeDelta(
323 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
325 UpdateRtt();
327 // Ensure only one packet is sent immediately and the rest are paced.
328 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0, true);
329 CheckPacketIsSentImmediately();
330 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
333 } // namespace test
334 } // namespace net