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
;
21 const QuicByteCount kBytesInFlight
= 1024;
22 const int kInitialBurstPackets
= 10;
24 class PacingSenderTest
: public ::testing::Test
{
27 : zero_time_(QuicTime::Delta::Zero()),
28 infinite_time_(QuicTime::Delta::Infinite()),
30 mock_sender_(new StrictMock
<MockSendAlgorithm
>()),
31 pacing_sender_(new PacingSender(mock_sender_
,
32 QuicTime::Delta::FromMilliseconds(1),
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 // In order for the packet to be sendable, the underlying sender must
51 // permit it to be sent immediately.
52 for (int i
= 0; i
< 2; ++i
) {
53 EXPECT_CALL(*mock_sender_
, TimeUntilSend(clock_
.Now(), bytes_in_flight
,
54 retransmittable_data
))
55 .WillOnce(Return(zero_time_
));
56 // Verify that the packet can be sent immediately.
58 pacing_sender_
->TimeUntilSend(clock_
.Now(), bytes_in_flight
,
59 retransmittable_data
));
62 // Actually send the packet.
63 EXPECT_CALL(*mock_sender_
,
64 OnPacketSent(clock_
.Now(), bytes_in_flight
, sequence_number_
,
65 kMaxPacketSize
, retransmittable_data
));
66 pacing_sender_
->OnPacketSent(clock_
.Now(), bytes_in_flight
,
67 sequence_number_
++, kMaxPacketSize
,
68 retransmittable_data
);
71 void CheckPacketIsSentImmediately() {
72 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA
, kBytesInFlight
);
75 void CheckAckIsSentImmediately() {
76 CheckPacketIsSentImmediately(NO_RETRANSMITTABLE_DATA
, kBytesInFlight
);
79 void CheckPacketIsDelayed(QuicTime::Delta delay
) {
80 // In order for the packet to be sendable, the underlying sender must
81 // permit it to be sent immediately.
82 for (int i
= 0; i
< 2; ++i
) {
83 EXPECT_CALL(*mock_sender_
, TimeUntilSend(clock_
.Now(),
85 HAS_RETRANSMITTABLE_DATA
))
86 .WillOnce(Return(zero_time_
));
87 // Verify that the packet is delayed.
88 EXPECT_EQ(delay
.ToMicroseconds(),
89 pacing_sender_
->TimeUntilSend(
90 clock_
.Now(), kBytesInFlight
,
91 HAS_RETRANSMITTABLE_DATA
).ToMicroseconds());
96 EXPECT_CALL(*mock_sender_
, OnCongestionEvent(true, kBytesInFlight
, _
, _
));
97 SendAlgorithmInterface::CongestionVector empty_map
;
98 pacing_sender_
->OnCongestionEvent(true, kBytesInFlight
, empty_map
,
102 const QuicTime::Delta zero_time_
;
103 const QuicTime::Delta infinite_time_
;
105 QuicPacketSequenceNumber sequence_number_
;
106 StrictMock
<MockSendAlgorithm
>* mock_sender_
;
107 scoped_ptr
<PacingSender
> pacing_sender_
;
110 TEST_F(PacingSenderTest
, NoSend
) {
111 for (int i
= 0; i
< 2; ++i
) {
112 EXPECT_CALL(*mock_sender_
, TimeUntilSend(clock_
.Now(),
114 HAS_RETRANSMITTABLE_DATA
))
115 .WillOnce(Return(infinite_time_
));
116 EXPECT_EQ(infinite_time_
,
117 pacing_sender_
->TimeUntilSend(clock_
.Now(),
119 HAS_RETRANSMITTABLE_DATA
));
123 TEST_F(PacingSenderTest
, SendNow
) {
124 for (int i
= 0; i
< 2; ++i
) {
125 EXPECT_CALL(*mock_sender_
, TimeUntilSend(clock_
.Now(),
127 HAS_RETRANSMITTABLE_DATA
))
128 .WillOnce(Return(zero_time_
));
129 EXPECT_EQ(zero_time_
,
130 pacing_sender_
->TimeUntilSend(clock_
.Now(),
132 HAS_RETRANSMITTABLE_DATA
));
136 TEST_F(PacingSenderTest
, VariousSending
) {
137 // Configure pacing rate of 1 packet per 1 ms, no initial burst.
138 InitPacingRate(0, QuicBandwidth::FromBytesAndTimeDelta(
139 kMaxPacketSize
, QuicTime::Delta::FromMilliseconds(1)));
141 // Now update the RTT and verify that packets are actually paced.
144 CheckPacketIsSentImmediately();
145 CheckPacketIsSentImmediately();
147 // The first packet was a "make up", then we sent two packets "into the
148 // future", so the delay should be 2.
149 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
152 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(2));
153 CheckPacketIsSentImmediately();
154 CheckPacketIsSentImmediately();
155 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
156 CheckAckIsSentImmediately();
159 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(4));
160 CheckPacketIsSentImmediately();
161 CheckPacketIsSentImmediately();
162 CheckPacketIsSentImmediately();
163 CheckPacketIsSentImmediately();
164 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
166 // Wake up really late.
167 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
168 CheckPacketIsSentImmediately();
169 CheckPacketIsSentImmediately();
170 CheckPacketIsSentImmediately();
171 CheckPacketIsSentImmediately();
172 CheckPacketIsSentImmediately();
173 CheckPacketIsSentImmediately();
174 CheckPacketIsSentImmediately();
175 CheckPacketIsSentImmediately();
176 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
178 // Wake up really late again, but application pause partway through.
179 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
180 CheckPacketIsSentImmediately();
181 CheckPacketIsSentImmediately();
182 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
183 CheckPacketIsSentImmediately();
184 CheckPacketIsSentImmediately();
185 CheckPacketIsSentImmediately();
186 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
188 // Wake up too early.
189 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
191 // Wake up early, but after enough time has passed to permit a send.
192 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
193 CheckPacketIsSentImmediately();
194 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
197 // TODO(ianswett): Remove this test.
198 TEST_F(PacingSenderTest
, DISABLED_CongestionAvoidanceSending
) {
199 // Configure pacing rate of 1 packet per 1 ms.
200 EXPECT_CALL(*mock_sender_
, PacingRate())
201 .WillRepeatedly(Return(QuicBandwidth::FromBytesAndTimeDelta(
202 kMaxPacketSize
* 1.25, QuicTime::Delta::FromMilliseconds(2))));
204 // Now update the RTT and verify that packets are actually paced.
207 CheckPacketIsSentImmediately();
208 CheckPacketIsSentImmediately();
210 // The first packet was a "make up", then we sent two packets "into the
211 // future", so the delay should be 2200us.
212 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2200));
215 clock_
.AdvanceTime(QuicTime::Delta::FromMicroseconds(2200));
216 CheckPacketIsSentImmediately();
217 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1600));
218 CheckAckIsSentImmediately();
221 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(4));
222 CheckPacketIsSentImmediately();
223 CheckPacketIsSentImmediately();
224 CheckPacketIsSentImmediately();
225 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2400));
227 // Wake up really late.
228 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
229 CheckPacketIsSentImmediately();
230 CheckPacketIsSentImmediately();
231 CheckPacketIsSentImmediately();
232 CheckPacketIsSentImmediately();
233 CheckPacketIsSentImmediately();
234 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2400));
236 // Wake up really late again, but application pause partway through.
237 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
238 CheckPacketIsSentImmediately();
239 CheckPacketIsSentImmediately();
240 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
241 CheckPacketIsSentImmediately();
242 CheckPacketIsSentImmediately();
243 CheckPacketIsSentImmediately();
244 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2200));
246 // Wake up too early.
247 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2200));
249 // Wake up early, but after enough time has passed to permit a send.
250 clock_
.AdvanceTime(QuicTime::Delta::FromMicroseconds(1200));
251 CheckPacketIsSentImmediately();
252 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2600));
255 TEST_F(PacingSenderTest
, InitialBurst
) {
256 // Configure pacing rate of 1 packet per 1 ms.
257 InitPacingRate(10, QuicBandwidth::FromBytesAndTimeDelta(
258 kMaxPacketSize
, QuicTime::Delta::FromMilliseconds(1)));
260 // Update the RTT and verify that the first 10 packets aren't paced.
263 // Send 10 packets, and verify that they are not paced.
264 for (int i
= 0 ; i
< kInitialBurstPackets
; ++i
) {
265 CheckPacketIsSentImmediately();
268 // The first packet was a "make up", then we sent two packets "into the
269 // future", so the delay should be 2ms.
270 CheckPacketIsSentImmediately();
271 CheckPacketIsSentImmediately();
272 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
274 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
275 CheckPacketIsSentImmediately();
277 // Next time TimeUntilSend is called with no bytes in flight, pacing should
278 // allow a packet to be sent, and when it's sent, the tokens are refilled.
279 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA
, 0);
280 for (int i
= 0; i
< kInitialBurstPackets
- 1; ++i
) {
281 CheckPacketIsSentImmediately();
284 // The first packet was a "make up", then we sent two packets "into the
285 // future", so the delay should be 2ms.
286 CheckPacketIsSentImmediately();
287 CheckPacketIsSentImmediately();
288 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
291 TEST_F(PacingSenderTest
, InitialBurstNoRttMeasurement
) {
292 // Configure pacing rate of 1 packet per 1 ms.
293 InitPacingRate(10, QuicBandwidth::FromBytesAndTimeDelta(
294 kMaxPacketSize
, QuicTime::Delta::FromMilliseconds(1)));
296 // Send 10 packets, and verify that they are not paced.
297 for (int i
= 0 ; i
< kInitialBurstPackets
; ++i
) {
298 CheckPacketIsSentImmediately();
301 // The first packet was a "make up", then we sent two packets "into the
302 // future", so the delay should be 2ms.
303 CheckPacketIsSentImmediately();
304 CheckPacketIsSentImmediately();
305 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
308 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
309 CheckPacketIsSentImmediately();
311 // Next time TimeUntilSend is called with no bytes in flight, the tokens
312 // should be refilled and there should be no delay.
313 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA
, 0);
314 // Send 10 packets, and verify that they are not paced.
315 for (int i
= 0; i
< kInitialBurstPackets
- 1; ++i
) {
316 CheckPacketIsSentImmediately();
319 // The first packet was a "make up", then we sent two packets "into the
320 // future", so the delay should be 2ms.
321 CheckPacketIsSentImmediately();
322 CheckPacketIsSentImmediately();
323 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
326 TEST_F(PacingSenderTest
, FastSending
) {
327 // Ensure the pacing sender paces, even when the inter-packet spacing is less
328 // than the pacing granularity.
330 QuicBandwidth::FromBytesAndTimeDelta(
331 2 * kMaxPacketSize
, QuicTime::Delta::FromMilliseconds(1)));
333 // Update the RTT and verify that the first 10 packets aren't paced.
336 // Send 10 packets, and verify that they are not paced.
337 for (int i
= 0; i
< kInitialBurstPackets
; ++i
) {
338 CheckPacketIsSentImmediately();
341 // The first packet was a "make up", then we sent two packets "into the
342 // future", since it's 2 packets/ms, so the delay should be 1.5ms.
343 CheckPacketIsSentImmediately();
344 CheckPacketIsSentImmediately();
345 CheckPacketIsSentImmediately();
346 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1500));
348 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
349 CheckPacketIsSentImmediately();
351 // Next time TimeUntilSend is called with no bytes in flight, the tokens
352 // should be refilled and there should be no delay.
353 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA
, 0);
354 for (int i
= 0; i
< kInitialBurstPackets
- 1; ++i
) {
355 CheckPacketIsSentImmediately();
358 // The first packet was a "make up", then we sent two packets "into the
359 // future", so the delay should be 1.5ms.
360 CheckPacketIsSentImmediately();
361 CheckPacketIsSentImmediately();
362 CheckPacketIsSentImmediately();
363 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1500));