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.
4 
5 #include "quiche/quic/core/congestion_control/pacing_sender.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "quiche/quic/core/quic_constants.h"
11 #include "quiche/quic/core/quic_packets.h"
12 #include "quiche/quic/platform/api/quic_flag_utils.h"
13 #include "quiche/quic/platform/api/quic_flags.h"
14 #include "quiche/quic/platform/api/quic_logging.h"
15 #include "quiche/quic/platform/api/quic_test.h"
16 #include "quiche/quic/test_tools/mock_clock.h"
17 #include "quiche/quic/test_tools/quic_test_utils.h"
18 
19 using testing::_;
20 using testing::AtMost;
21 using testing::Return;
22 using testing::StrictMock;
23 
24 namespace quic {
25 namespace test {
26 
27 const QuicByteCount kBytesInFlight = 1024;
28 const int kInitialBurstPackets = 10;
29 
30 class TestPacingSender : public PacingSender {
31  public:
32   using PacingSender::lumpy_tokens;
33   using PacingSender::PacingSender;
34 
ideal_next_packet_send_time() const35   QuicTime ideal_next_packet_send_time() const {
36     return GetNextReleaseTime().release_time;
37   }
38 };
39 
40 class PacingSenderTest : public QuicTest {
41  protected:
PacingSenderTest()42   PacingSenderTest()
43       : zero_time_(QuicTime::Delta::Zero()),
44         infinite_time_(QuicTime::Delta::Infinite()),
45         packet_number_(1),
46         mock_sender_(new StrictMock<MockSendAlgorithm>()),
47         pacing_sender_(new TestPacingSender) {
48     pacing_sender_->set_sender(mock_sender_.get());
49     // Pick arbitrary time.
50     clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(9));
51   }
52 
~PacingSenderTest()53   ~PacingSenderTest() override {}
54 
InitPacingRate(QuicPacketCount burst_size,QuicBandwidth bandwidth)55   void InitPacingRate(QuicPacketCount burst_size, QuicBandwidth bandwidth) {
56     mock_sender_ = std::make_unique<StrictMock<MockSendAlgorithm>>();
57     pacing_sender_ = std::make_unique<TestPacingSender>();
58     pacing_sender_->set_sender(mock_sender_.get());
59     EXPECT_CALL(*mock_sender_, PacingRate(_)).WillRepeatedly(Return(bandwidth));
60     EXPECT_CALL(*mock_sender_, BandwidthEstimate())
61         .WillRepeatedly(Return(bandwidth));
62     if (burst_size == 0) {
63       EXPECT_CALL(*mock_sender_, OnCongestionEvent(_, _, _, _, _, _, _));
64       LostPacketVector lost_packets;
65       lost_packets.push_back(
66           LostPacket(QuicPacketNumber(1), kMaxOutgoingPacketSize));
67       AckedPacketVector empty;
68       pacing_sender_->OnCongestionEvent(true, 1234, clock_.Now(), empty,
69                                         lost_packets, 0, 0);
70     } else if (burst_size != kInitialBurstPackets) {
71       QUIC_LOG(FATAL) << "Unsupported burst_size " << burst_size
72                       << " specificied, only 0 and " << kInitialBurstPackets
73                       << " are supported.";
74     }
75   }
76 
CheckPacketIsSentImmediately(HasRetransmittableData retransmittable_data,QuicByteCount prior_in_flight,bool in_recovery,QuicPacketCount cwnd)77   void CheckPacketIsSentImmediately(HasRetransmittableData retransmittable_data,
78                                     QuicByteCount prior_in_flight,
79                                     bool in_recovery, QuicPacketCount cwnd) {
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_, CanSend(prior_in_flight))
84           .WillOnce(Return(true));
85       // Verify that the packet can be sent immediately.
86       EXPECT_EQ(zero_time_,
87                 pacing_sender_->TimeUntilSend(clock_.Now(), prior_in_flight))
88           << "Next packet to send is " << packet_number_;
89     }
90 
91     // Actually send the packet.
92     if (prior_in_flight == 0 &&
93         !GetQuicReloadableFlag(quic_pacing_remove_non_initial_burst)) {
94       EXPECT_CALL(*mock_sender_, InRecovery()).WillOnce(Return(in_recovery));
95     }
96     EXPECT_CALL(*mock_sender_,
97                 OnPacketSent(clock_.Now(), prior_in_flight, packet_number_,
98                              kMaxOutgoingPacketSize, retransmittable_data));
99     EXPECT_CALL(*mock_sender_, GetCongestionWindow())
100         .WillRepeatedly(Return(cwnd * kDefaultTCPMSS));
101     EXPECT_CALL(*mock_sender_,
102                 CanSend(prior_in_flight + kMaxOutgoingPacketSize))
103         .Times(AtMost(1))
104         .WillRepeatedly(Return((prior_in_flight + kMaxOutgoingPacketSize) <
105                                (cwnd * kDefaultTCPMSS)));
106     pacing_sender_->OnPacketSent(clock_.Now(), prior_in_flight,
107                                  packet_number_++, kMaxOutgoingPacketSize,
108                                  retransmittable_data);
109   }
110 
CheckPacketIsSentImmediately()111   void CheckPacketIsSentImmediately() {
112     CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, kBytesInFlight,
113                                  false, 10);
114   }
115 
CheckPacketIsDelayed(QuicTime::Delta delay)116   void CheckPacketIsDelayed(QuicTime::Delta delay) {
117     // In order for the packet to be sendable, the underlying sender must
118     // permit it to be sent immediately.
119     for (int i = 0; i < 2; ++i) {
120       EXPECT_CALL(*mock_sender_, CanSend(kBytesInFlight))
121           .WillOnce(Return(true));
122       // Verify that the packet is delayed.
123       EXPECT_EQ(delay.ToMicroseconds(),
124                 pacing_sender_->TimeUntilSend(clock_.Now(), kBytesInFlight)
125                     .ToMicroseconds());
126     }
127   }
128 
UpdateRtt()129   void UpdateRtt() {
130     EXPECT_CALL(*mock_sender_,
131                 OnCongestionEvent(true, kBytesInFlight, _, _, _, _, _));
132     AckedPacketVector empty_acked;
133     LostPacketVector empty_lost;
134     pacing_sender_->OnCongestionEvent(true, kBytesInFlight, clock_.Now(),
135                                       empty_acked, empty_lost, 0, 0);
136   }
137 
OnApplicationLimited()138   void OnApplicationLimited() { pacing_sender_->OnApplicationLimited(); }
139 
140   const QuicTime::Delta zero_time_;
141   const QuicTime::Delta infinite_time_;
142   MockClock clock_;
143   QuicPacketNumber packet_number_;
144   std::unique_ptr<StrictMock<MockSendAlgorithm>> mock_sender_;
145   std::unique_ptr<TestPacingSender> pacing_sender_;
146 };
147 
TEST_F(PacingSenderTest,NoSend)148 TEST_F(PacingSenderTest, NoSend) {
149   for (int i = 0; i < 2; ++i) {
150     EXPECT_CALL(*mock_sender_, CanSend(kBytesInFlight)).WillOnce(Return(false));
151     EXPECT_EQ(infinite_time_,
152               pacing_sender_->TimeUntilSend(clock_.Now(), kBytesInFlight));
153   }
154 }
155 
TEST_F(PacingSenderTest,SendNow)156 TEST_F(PacingSenderTest, SendNow) {
157   for (int i = 0; i < 2; ++i) {
158     EXPECT_CALL(*mock_sender_, CanSend(kBytesInFlight)).WillOnce(Return(true));
159     EXPECT_EQ(zero_time_,
160               pacing_sender_->TimeUntilSend(clock_.Now(), kBytesInFlight));
161   }
162 }
163 
TEST_F(PacingSenderTest,VariousSending)164 TEST_F(PacingSenderTest, VariousSending) {
165   // Configure pacing rate of 1 packet per 1 ms, no initial burst.
166   InitPacingRate(
167       0, QuicBandwidth::FromBytesAndTimeDelta(
168              kMaxOutgoingPacketSize, QuicTime::Delta::FromMilliseconds(1)));
169 
170   // Now update the RTT and verify that packets are actually paced.
171   UpdateRtt();
172 
173   CheckPacketIsSentImmediately();
174   CheckPacketIsSentImmediately();
175 
176   // The first packet was a "make up", then we sent two packets "into the
177   // future", so the delay should be 2.
178   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
179 
180   // Wake up on time.
181   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2));
182   CheckPacketIsSentImmediately();
183   CheckPacketIsSentImmediately();
184   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
185 
186   // Wake up late.
187   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(4));
188   CheckPacketIsSentImmediately();
189   CheckPacketIsSentImmediately();
190   CheckPacketIsSentImmediately();
191   CheckPacketIsSentImmediately();
192   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
193 
194   // Wake up really late.
195   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
196   CheckPacketIsSentImmediately();
197   CheckPacketIsSentImmediately();
198   CheckPacketIsSentImmediately();
199   CheckPacketIsSentImmediately();
200   CheckPacketIsSentImmediately();
201   CheckPacketIsSentImmediately();
202   CheckPacketIsSentImmediately();
203   CheckPacketIsSentImmediately();
204   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
205 
206   // Wake up really late again, but application pause partway through.
207   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
208   CheckPacketIsSentImmediately();
209   CheckPacketIsSentImmediately();
210   OnApplicationLimited();
211   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
212   CheckPacketIsSentImmediately();
213   CheckPacketIsSentImmediately();
214   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
215   // Wake up early, but after enough time has passed to permit a send.
216   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
217   CheckPacketIsSentImmediately();
218 }
219 
TEST_F(PacingSenderTest,InitialBurst)220 TEST_F(PacingSenderTest, InitialBurst) {
221   // Configure pacing rate of 1 packet per 1 ms.
222   InitPacingRate(
223       10, QuicBandwidth::FromBytesAndTimeDelta(
224               kMaxOutgoingPacketSize, QuicTime::Delta::FromMilliseconds(1)));
225 
226   // Update the RTT and verify that the first 10 packets aren't paced.
227   UpdateRtt();
228 
229   // Send 10 packets, and verify that they are not paced.
230   for (int i = 0; i < kInitialBurstPackets; ++i) {
231     CheckPacketIsSentImmediately();
232   }
233 
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));
239 
240   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
241 
242   if (GetQuicReloadableFlag(quic_pacing_remove_non_initial_burst)) {
243     // Can send some packets immediately to make up for 5ms of lost time.
244     for (int i = 0; i < 6; ++i) {
245       CheckPacketIsSentImmediately();
246     }
247     CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(3));
248     return;
249   }
250 
251   CheckPacketIsSentImmediately();
252   // Next time TimeUntilSend is called with no bytes in flight, pacing should
253   // allow a packet to be sent, and when it's sent, the tokens are refilled.
254   CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0, false, 10);
255   for (int i = 0; i < kInitialBurstPackets - 1; ++i) {
256     CheckPacketIsSentImmediately();
257   }
258 
259   // The first packet was a "make up", then we sent two packets "into the
260   // future", so the delay should be 2ms.
261   CheckPacketIsSentImmediately();
262   CheckPacketIsSentImmediately();
263   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
264 }
265 
TEST_F(PacingSenderTest,InitialBurstNoRttMeasurement)266 TEST_F(PacingSenderTest, InitialBurstNoRttMeasurement) {
267   // Configure pacing rate of 1 packet per 1 ms.
268   InitPacingRate(
269       10, QuicBandwidth::FromBytesAndTimeDelta(
270               kMaxOutgoingPacketSize, QuicTime::Delta::FromMilliseconds(1)));
271 
272   // Send 10 packets, and verify that they are not paced.
273   for (int i = 0; i < kInitialBurstPackets; ++i) {
274     CheckPacketIsSentImmediately();
275   }
276 
277   // The first packet was a "make up", then we sent two packets "into the
278   // future", so the delay should be 2ms.
279   CheckPacketIsSentImmediately();
280   CheckPacketIsSentImmediately();
281   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
282 
283   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
284 
285   if (GetQuicReloadableFlag(quic_pacing_remove_non_initial_burst)) {
286     // Can send some packets immediately to make up for 5ms of lost time.
287     for (int i = 0; i < 6; ++i) {
288       CheckPacketIsSentImmediately();
289     }
290     CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(3));
291     return;
292   }
293 
294   CheckPacketIsSentImmediately();
295 
296   // Next time TimeUntilSend is called with no bytes in flight, the tokens
297   // should be refilled and there should be no delay.
298   CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0, false, 10);
299   // Send 10 packets, and verify that they are not paced.
300   for (int i = 0; i < kInitialBurstPackets - 1; ++i) {
301     CheckPacketIsSentImmediately();
302   }
303 
304   // The first packet was a "make up", then we sent two packets "into the
305   // future", so the delay should be 2ms.
306   CheckPacketIsSentImmediately();
307   CheckPacketIsSentImmediately();
308   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
309 }
310 
TEST_F(PacingSenderTest,FastSending)311 TEST_F(PacingSenderTest, FastSending) {
312   // Ensure the pacing sender paces, even when the inter-packet spacing(0.5ms)
313   // is less than the pacing granularity(1ms).
314   InitPacingRate(10, QuicBandwidth::FromBytesAndTimeDelta(
315                          2 * kMaxOutgoingPacketSize,
316                          QuicTime::Delta::FromMilliseconds(1)));
317   // Update the RTT and verify that the first 10 packets aren't paced.
318   UpdateRtt();
319 
320   // Send 10 packets, and verify that they are not paced.
321   for (int i = 0; i < kInitialBurstPackets; ++i) {
322     CheckPacketIsSentImmediately();
323   }
324 
325   CheckPacketIsSentImmediately();  // Make up
326   CheckPacketIsSentImmediately();  // Lumpy token
327   CheckPacketIsSentImmediately();  // "In the future" but within granularity.
328   CheckPacketIsSentImmediately();  // Lumpy token
329   CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2000));
330 
331   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
332 
333   if (GetQuicReloadableFlag(quic_pacing_remove_non_initial_burst)) {
334     // Can send some packets immediately to make up for 5ms of lost time.
335     for (int i = 0; i < 10; ++i) {
336       CheckPacketIsSentImmediately();
337     }
338     CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
339     return;
340   }
341 
342   CheckPacketIsSentImmediately();
343 
344   // Next time TimeUntilSend is called with no bytes in flight, the tokens
345   // should be refilled and there should be no delay.
346   CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0, false, 10);
347   for (int i = 0; i < kInitialBurstPackets - 1; ++i) {
348     CheckPacketIsSentImmediately();
349   }
350 
351   // The first packet was a "make up", then we sent two packets "into the
352   // future", so the delay should be 1.5ms.
353   CheckPacketIsSentImmediately();  // Make up
354   CheckPacketIsSentImmediately();  // Lumpy token
355   CheckPacketIsSentImmediately();  // "In the future" but within granularity.
356   CheckPacketIsSentImmediately();  // Lumpy token
357   CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2000));
358 }
359 
TEST_F(PacingSenderTest,NoBurstEnteringRecovery)360 TEST_F(PacingSenderTest, NoBurstEnteringRecovery) {
361   // Configure pacing rate of 1 packet per 1 ms with no burst tokens.
362   InitPacingRate(
363       0, QuicBandwidth::FromBytesAndTimeDelta(
364              kMaxOutgoingPacketSize, QuicTime::Delta::FromMilliseconds(1)));
365   // Sending a packet will set burst tokens.
366   CheckPacketIsSentImmediately();
367 
368   // Losing a packet will set clear burst tokens.
369   LostPacketVector lost_packets;
370   lost_packets.push_back(
371       LostPacket(QuicPacketNumber(1), kMaxOutgoingPacketSize));
372   AckedPacketVector empty_acked;
373   EXPECT_CALL(*mock_sender_, OnCongestionEvent(true, kMaxOutgoingPacketSize, _,
374                                                testing::IsEmpty(), _, _, _));
375   pacing_sender_->OnCongestionEvent(true, kMaxOutgoingPacketSize, clock_.Now(),
376                                     empty_acked, lost_packets, 0, 0);
377   // One packet is sent immediately, because of 1ms pacing granularity.
378   CheckPacketIsSentImmediately();
379   // Ensure packets are immediately paced.
380   EXPECT_CALL(*mock_sender_, CanSend(kMaxOutgoingPacketSize))
381       .WillOnce(Return(true));
382   // Verify the next packet is paced and delayed 2ms due to granularity.
383   EXPECT_EQ(
384       QuicTime::Delta::FromMilliseconds(2),
385       pacing_sender_->TimeUntilSend(clock_.Now(), kMaxOutgoingPacketSize));
386   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
387 }
388 
TEST_F(PacingSenderTest,NoBurstInRecovery)389 TEST_F(PacingSenderTest, NoBurstInRecovery) {
390   // Configure pacing rate of 1 packet per 1 ms with no burst tokens.
391   InitPacingRate(
392       0, QuicBandwidth::FromBytesAndTimeDelta(
393              kMaxOutgoingPacketSize, QuicTime::Delta::FromMilliseconds(1)));
394 
395   UpdateRtt();
396 
397   // Ensure only one packet is sent immediately and the rest are paced.
398   CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0, true, 10);
399   CheckPacketIsSentImmediately();
400   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
401 }
402 
TEST_F(PacingSenderTest,CwndLimited)403 TEST_F(PacingSenderTest, CwndLimited) {
404   // Configure pacing rate of 1 packet per 1 ms, no initial burst.
405   InitPacingRate(
406       0, QuicBandwidth::FromBytesAndTimeDelta(
407              kMaxOutgoingPacketSize, QuicTime::Delta::FromMilliseconds(1)));
408 
409   UpdateRtt();
410 
411   CheckPacketIsSentImmediately();
412   CheckPacketIsSentImmediately();
413   // Packet 3 will be delayed 2ms.
414   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
415 
416   // Wake up on time.
417   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2));
418   // After sending packet 3, cwnd is limited.
419   // This test is slightly odd because bytes_in_flight is calculated using
420   // kMaxOutgoingPacketSize and CWND is calculated using kDefaultTCPMSS,
421   // which is 8 bytes larger, so 3 packets can be sent for a CWND of 2.
422   CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA,
423                                2 * kMaxOutgoingPacketSize, false, 2);
424 
425   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
426   // Verify pacing sender stops making up for lost time after sending packet 3.
427   // Packet 6 will be delayed 2ms.
428   CheckPacketIsSentImmediately();
429   CheckPacketIsSentImmediately();
430   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
431 }
432 
TEST_F(PacingSenderTest,LumpyPacingWithInitialBurstToken)433 TEST_F(PacingSenderTest, LumpyPacingWithInitialBurstToken) {
434   // Set lumpy size to be 3, and cwnd faction to 0.5
435   SetQuicFlag(quic_lumpy_pacing_size, 3);
436   SetQuicFlag(quic_lumpy_pacing_cwnd_fraction, 0.5f);
437   // Configure pacing rate of 1 packet per 1 ms.
438   InitPacingRate(
439       10, QuicBandwidth::FromBytesAndTimeDelta(
440               kMaxOutgoingPacketSize, QuicTime::Delta::FromMilliseconds(1)));
441   UpdateRtt();
442 
443   // Send 10 packets, and verify that they are not paced.
444   for (int i = 0; i < kInitialBurstPackets; ++i) {
445     CheckPacketIsSentImmediately();
446   }
447 
448   CheckPacketIsSentImmediately();
449   CheckPacketIsSentImmediately();
450   CheckPacketIsSentImmediately();
451   // Packet 14 will be delayed 3ms.
452   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(3));
453 
454   // Wake up on time.
455   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3));
456   CheckPacketIsSentImmediately();
457   CheckPacketIsSentImmediately();
458   CheckPacketIsSentImmediately();
459   // Packet 17 will be delayed 3ms.
460   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(3));
461 
462   // Application throttles sending.
463   OnApplicationLimited();
464   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
465   CheckPacketIsSentImmediately();
466   CheckPacketIsSentImmediately();
467   CheckPacketIsSentImmediately();
468   // Packet 20 will be delayed 3ms.
469   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(3));
470 
471   // Wake up on time.
472   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3));
473   CheckPacketIsSentImmediately();
474   // After sending packet 21, cwnd is limited.
475   // This test is slightly odd because bytes_in_flight is calculated using
476   // kMaxOutgoingPacketSize and CWND is calculated using kDefaultTCPMSS,
477   // which is 8 bytes larger, so 21 packets can be sent for a CWND of 20.
478   CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA,
479                                20 * kMaxOutgoingPacketSize, false, 20);
480 
481   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
482   // Suppose cwnd size is 5, so that lumpy size becomes 2.
483   CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, kBytesInFlight, false,
484                                5);
485   CheckPacketIsSentImmediately();
486   // Packet 24 will be delayed 2ms.
487   CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
488 }
489 
TEST_F(PacingSenderTest,NoLumpyPacingForLowBandwidthFlows)490 TEST_F(PacingSenderTest, NoLumpyPacingForLowBandwidthFlows) {
491   // Set lumpy size to be 3, and cwnd fraction to 0.5
492   SetQuicFlag(quic_lumpy_pacing_size, 3);
493   SetQuicFlag(quic_lumpy_pacing_cwnd_fraction, 0.5f);
494 
495   // Configure pacing rate of 1 packet per 100 ms.
496   QuicTime::Delta inter_packet_delay = QuicTime::Delta::FromMilliseconds(100);
497   InitPacingRate(kInitialBurstPackets,
498                  QuicBandwidth::FromBytesAndTimeDelta(kMaxOutgoingPacketSize,
499                                                       inter_packet_delay));
500   UpdateRtt();
501 
502   // Send kInitialBurstPackets packets, and verify that they are not paced.
503   for (int i = 0; i < kInitialBurstPackets; ++i) {
504     CheckPacketIsSentImmediately();
505   }
506 
507   // The first packet after burst token exhausted is also sent immediately,
508   // because ideal_next_packet_send_time has not been set yet.
509   CheckPacketIsSentImmediately();
510 
511   for (int i = 0; i < 200; ++i) {
512     CheckPacketIsDelayed(inter_packet_delay);
513   }
514 }
515 
516 // Regression test for b/184471302 to ensure that ACKs received back-to-back
517 // don't cause bursts in sending.
TEST_F(PacingSenderTest,NoBurstsForLumpyPacingWithAckAggregation)518 TEST_F(PacingSenderTest, NoBurstsForLumpyPacingWithAckAggregation) {
519   // Configure pacing rate of 1 packet per millisecond.
520   QuicTime::Delta inter_packet_delay = QuicTime::Delta::FromMilliseconds(1);
521   InitPacingRate(kInitialBurstPackets,
522                  QuicBandwidth::FromBytesAndTimeDelta(kMaxOutgoingPacketSize,
523                                                       inter_packet_delay));
524   UpdateRtt();
525 
526   // Send kInitialBurstPackets packets, and verify that they are not paced.
527   for (int i = 0; i < kInitialBurstPackets; ++i) {
528     CheckPacketIsSentImmediately();
529   }
530   // The last packet of the burst causes the sender to be CWND limited.
531   CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA,
532                                10 * kMaxOutgoingPacketSize, false, 10);
533 
534   // The last sent packet made the connection CWND limited, so no lumpy tokens
535   // should be available.
536   EXPECT_EQ(0u, pacing_sender_->lumpy_tokens());
537   CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA,
538                                10 * kMaxOutgoingPacketSize, false, 10);
539   EXPECT_EQ(0u, pacing_sender_->lumpy_tokens());
540   CheckPacketIsDelayed(2 * inter_packet_delay);
541 }
542 
TEST_F(PacingSenderTest,IdealNextPacketSendTimeWithLumpyPacing)543 TEST_F(PacingSenderTest, IdealNextPacketSendTimeWithLumpyPacing) {
544   // Set lumpy size to be 3, and cwnd faction to 0.5
545   SetQuicFlag(quic_lumpy_pacing_size, 3);
546   SetQuicFlag(quic_lumpy_pacing_cwnd_fraction, 0.5f);
547 
548   // Configure pacing rate of 1 packet per millisecond.
549   QuicTime::Delta inter_packet_delay = QuicTime::Delta::FromMilliseconds(1);
550   InitPacingRate(kInitialBurstPackets,
551                  QuicBandwidth::FromBytesAndTimeDelta(kMaxOutgoingPacketSize,
552                                                       inter_packet_delay));
553 
554   // Send kInitialBurstPackets packets, and verify that they are not paced.
555   for (int i = 0; i < kInitialBurstPackets; ++i) {
556     CheckPacketIsSentImmediately();
557   }
558 
559   CheckPacketIsSentImmediately();
560   EXPECT_EQ(pacing_sender_->ideal_next_packet_send_time(),
561             clock_.Now() + inter_packet_delay);
562   EXPECT_EQ(pacing_sender_->lumpy_tokens(), 2u);
563 
564   CheckPacketIsSentImmediately();
565   EXPECT_EQ(pacing_sender_->ideal_next_packet_send_time(),
566             clock_.Now() + 2 * inter_packet_delay);
567   EXPECT_EQ(pacing_sender_->lumpy_tokens(), 1u);
568 
569   CheckPacketIsSentImmediately();
570   EXPECT_EQ(pacing_sender_->ideal_next_packet_send_time(),
571             clock_.Now() + 3 * inter_packet_delay);
572   EXPECT_EQ(pacing_sender_->lumpy_tokens(), 0u);
573 
574   CheckPacketIsDelayed(3 * inter_packet_delay);
575 
576   // Wake up on time.
577   clock_.AdvanceTime(3 * inter_packet_delay);
578   CheckPacketIsSentImmediately();
579   EXPECT_EQ(pacing_sender_->ideal_next_packet_send_time(),
580             clock_.Now() + inter_packet_delay);
581   EXPECT_EQ(pacing_sender_->lumpy_tokens(), 2u);
582 
583   CheckPacketIsSentImmediately();
584   EXPECT_EQ(pacing_sender_->ideal_next_packet_send_time(),
585             clock_.Now() + 2 * inter_packet_delay);
586   EXPECT_EQ(pacing_sender_->lumpy_tokens(), 1u);
587 
588   CheckPacketIsSentImmediately();
589   EXPECT_EQ(pacing_sender_->ideal_next_packet_send_time(),
590             clock_.Now() + 3 * inter_packet_delay);
591   EXPECT_EQ(pacing_sender_->lumpy_tokens(), 0u);
592 
593   CheckPacketIsDelayed(3 * inter_packet_delay);
594 
595   // Wake up late.
596   clock_.AdvanceTime(4.5 * inter_packet_delay);
597   CheckPacketIsSentImmediately();
598   EXPECT_EQ(pacing_sender_->ideal_next_packet_send_time(),
599             clock_.Now() - 0.5 * inter_packet_delay);
600   EXPECT_EQ(pacing_sender_->lumpy_tokens(), 2u);
601 
602   CheckPacketIsSentImmediately();
603   EXPECT_EQ(pacing_sender_->ideal_next_packet_send_time(),
604             clock_.Now() + 0.5 * inter_packet_delay);
605   EXPECT_EQ(pacing_sender_->lumpy_tokens(), 1u);
606 
607   CheckPacketIsSentImmediately();
608   EXPECT_EQ(pacing_sender_->ideal_next_packet_send_time(),
609             clock_.Now() + 1.5 * inter_packet_delay);
610   EXPECT_EQ(pacing_sender_->lumpy_tokens(), 0u);
611 
612   CheckPacketIsDelayed(1.5 * inter_packet_delay);
613 }
614 
615 }  // namespace test
616 }  // namespace quic
617