Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / congestion_control / pacing_sender_test.cc
blob25ca2a7c1b8d993f86d3225915058a648d398309
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 // 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.
57 EXPECT_EQ(zero_time_,
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, packet_number_,
65 kMaxPacketSize, retransmittable_data));
66 pacing_sender_->OnPacketSent(clock_.Now(), bytes_in_flight,
67 packet_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(),
84 kBytesInFlight,
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());
95 void UpdateRtt() {
96 EXPECT_CALL(*mock_sender_, OnCongestionEvent(true, kBytesInFlight, _, _));
97 SendAlgorithmInterface::CongestionVector empty_map;
98 pacing_sender_->OnCongestionEvent(true, kBytesInFlight, empty_map,
99 empty_map);
102 const QuicTime::Delta zero_time_;
103 const QuicTime::Delta infinite_time_;
104 MockClock clock_;
105 QuicPacketNumber packet_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(),
113 kBytesInFlight,
114 HAS_RETRANSMITTABLE_DATA))
115 .WillOnce(Return(infinite_time_));
116 EXPECT_EQ(infinite_time_,
117 pacing_sender_->TimeUntilSend(clock_.Now(),
118 kBytesInFlight,
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(),
126 kBytesInFlight,
127 HAS_RETRANSMITTABLE_DATA))
128 .WillOnce(Return(zero_time_));
129 EXPECT_EQ(zero_time_,
130 pacing_sender_->TimeUntilSend(clock_.Now(),
131 kBytesInFlight,
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.
142 UpdateRtt();
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));
151 // Wake up on time.
152 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2));
153 CheckPacketIsSentImmediately();
154 CheckPacketIsSentImmediately();
155 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
156 CheckAckIsSentImmediately();
158 // Wake up late.
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 EXPECT_CALL(*mock_sender_, GetCongestionWindow())
203 .WillOnce(Return(10 * kDefaultTCPMSS));
204 // Update the RTT and verify that the first 10 packets aren't paced.
205 UpdateRtt();
207 // Send 10 packets, and verify that they are not paced.
208 for (int i = 0 ; i < kInitialBurstPackets; ++i) {
209 CheckPacketIsSentImmediately();
212 // The first packet was a "make up", then we sent two packets "into the
213 // future", so the delay should be 2ms.
214 CheckPacketIsSentImmediately();
215 CheckPacketIsSentImmediately();
216 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
218 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
219 CheckPacketIsSentImmediately();
221 // Next time TimeUntilSend is called with no bytes in flight, pacing should
222 // allow a packet to be sent, and when it's sent, the tokens are refilled.
223 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0);
224 for (int i = 0; i < kInitialBurstPackets - 1; ++i) {
225 CheckPacketIsSentImmediately();
228 // The first packet was a "make up", then we sent two packets "into the
229 // future", so the delay should be 2ms.
230 CheckPacketIsSentImmediately();
231 CheckPacketIsSentImmediately();
232 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
235 TEST_F(PacingSenderTest, InitialBurstNoRttMeasurement) {
236 // Configure pacing rate of 1 packet per 1 ms.
237 InitPacingRate(10, QuicBandwidth::FromBytesAndTimeDelta(
238 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
240 EXPECT_CALL(*mock_sender_, GetCongestionWindow())
241 .WillOnce(Return(10 * kDefaultTCPMSS));
242 // Send 10 packets, and verify that they are not paced.
243 for (int i = 0 ; i < kInitialBurstPackets; ++i) {
244 CheckPacketIsSentImmediately();
247 // The first packet was a "make up", then we sent two packets "into the
248 // future", so the delay should be 2ms.
249 CheckPacketIsSentImmediately();
250 CheckPacketIsSentImmediately();
251 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
254 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
255 CheckPacketIsSentImmediately();
257 // Next time TimeUntilSend is called with no bytes in flight, the tokens
258 // should be refilled and there should be no delay.
259 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0);
260 // Send 10 packets, and verify that they are not paced.
261 for (int i = 0; i < kInitialBurstPackets - 1; ++i) {
262 CheckPacketIsSentImmediately();
265 // The first packet was a "make up", then we sent two packets "into the
266 // future", so the delay should be 2ms.
267 CheckPacketIsSentImmediately();
268 CheckPacketIsSentImmediately();
269 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
272 TEST_F(PacingSenderTest, FastSending) {
273 // Ensure the pacing sender paces, even when the inter-packet spacing is less
274 // than the pacing granularity.
275 InitPacingRate(10,
276 QuicBandwidth::FromBytesAndTimeDelta(
277 2 * kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
279 EXPECT_CALL(*mock_sender_, GetCongestionWindow())
280 .WillOnce(Return(10 * kDefaultTCPMSS));
281 // Update the RTT and verify that the first 10 packets aren't paced.
282 UpdateRtt();
284 // Send 10 packets, and verify that they are not paced.
285 for (int i = 0; i < kInitialBurstPackets; ++i) {
286 CheckPacketIsSentImmediately();
289 // The first packet was a "make up", then we sent two packets "into the
290 // future", since it's 2 packets/ms, so the delay should be 1.5ms.
291 CheckPacketIsSentImmediately();
292 CheckPacketIsSentImmediately();
293 CheckPacketIsSentImmediately();
294 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1500));
296 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
297 CheckPacketIsSentImmediately();
299 // Next time TimeUntilSend is called with no bytes in flight, the tokens
300 // should be refilled and there should be no delay.
301 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0);
302 for (int i = 0; i < kInitialBurstPackets - 1; ++i) {
303 CheckPacketIsSentImmediately();
306 // The first packet was a "make up", then we sent two packets "into the
307 // future", so the delay should be 1.5ms.
308 CheckPacketIsSentImmediately();
309 CheckPacketIsSentImmediately();
310 CheckPacketIsSentImmediately();
311 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1500));
314 } // namespace test
315 } // namespace net