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 TEST_F(PacingSenderTest
, InitialBurst
) {
198 // Configure pacing rate of 1 packet per 1 ms.
199 InitPacingRate(10, QuicBandwidth::FromBytesAndTimeDelta(
200 kMaxPacketSize
, QuicTime::Delta::FromMilliseconds(1)));
202 // Update the RTT and verify that the first 10 packets aren't paced.
205 // Send 10 packets, and verify that they are not paced.
206 for (int i
= 0 ; i
< kInitialBurstPackets
; ++i
) {
207 CheckPacketIsSentImmediately();
210 // The first packet was a "make up", then we sent two packets "into the
211 // future", so the delay should be 2ms.
212 CheckPacketIsSentImmediately();
213 CheckPacketIsSentImmediately();
214 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
216 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
217 CheckPacketIsSentImmediately();
219 // Next time TimeUntilSend is called with no bytes in flight, pacing should
220 // allow a packet to be sent, and when it's sent, the tokens are refilled.
221 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA
, 0);
222 for (int i
= 0; i
< kInitialBurstPackets
- 1; ++i
) {
223 CheckPacketIsSentImmediately();
226 // The first packet was a "make up", then we sent two packets "into the
227 // future", so the delay should be 2ms.
228 CheckPacketIsSentImmediately();
229 CheckPacketIsSentImmediately();
230 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
233 TEST_F(PacingSenderTest
, InitialBurstNoRttMeasurement
) {
234 // Configure pacing rate of 1 packet per 1 ms.
235 InitPacingRate(10, QuicBandwidth::FromBytesAndTimeDelta(
236 kMaxPacketSize
, QuicTime::Delta::FromMilliseconds(1)));
238 // Send 10 packets, and verify that they are not paced.
239 for (int i
= 0 ; i
< kInitialBurstPackets
; ++i
) {
240 CheckPacketIsSentImmediately();
243 // The first packet was a "make up", then we sent two packets "into the
244 // future", so the delay should be 2ms.
245 CheckPacketIsSentImmediately();
246 CheckPacketIsSentImmediately();
247 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
250 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
251 CheckPacketIsSentImmediately();
253 // Next time TimeUntilSend is called with no bytes in flight, the tokens
254 // should be refilled and there should be no delay.
255 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA
, 0);
256 // Send 10 packets, and verify that they are not paced.
257 for (int i
= 0; i
< kInitialBurstPackets
- 1; ++i
) {
258 CheckPacketIsSentImmediately();
261 // The first packet was a "make up", then we sent two packets "into the
262 // future", so the delay should be 2ms.
263 CheckPacketIsSentImmediately();
264 CheckPacketIsSentImmediately();
265 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
268 TEST_F(PacingSenderTest
, FastSending
) {
269 // Ensure the pacing sender paces, even when the inter-packet spacing is less
270 // than the pacing granularity.
272 QuicBandwidth::FromBytesAndTimeDelta(
273 2 * kMaxPacketSize
, QuicTime::Delta::FromMilliseconds(1)));
275 // Update the RTT and verify that the first 10 packets aren't paced.
278 // Send 10 packets, and verify that they are not paced.
279 for (int i
= 0; i
< kInitialBurstPackets
; ++i
) {
280 CheckPacketIsSentImmediately();
283 // The first packet was a "make up", then we sent two packets "into the
284 // future", since it's 2 packets/ms, so the delay should be 1.5ms.
285 CheckPacketIsSentImmediately();
286 CheckPacketIsSentImmediately();
287 CheckPacketIsSentImmediately();
288 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1500));
290 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
291 CheckPacketIsSentImmediately();
293 // Next time TimeUntilSend is called with no bytes in flight, the tokens
294 // should be refilled and there should be no delay.
295 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA
, 0);
296 for (int i
= 0; i
< kInitialBurstPackets
- 1; ++i
) {
297 CheckPacketIsSentImmediately();
300 // The first packet was a "make up", then we sent two packets "into the
301 // future", so the delay should be 1.5ms.
302 CheckPacketIsSentImmediately();
303 CheckPacketIsSentImmediately();
304 CheckPacketIsSentImmediately();
305 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1500));