OLD | NEW |
| (Empty) |
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 "net/quic/congestion_control/pacing_sender.h" | |
6 | |
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" | |
13 | |
14 using testing::Return; | |
15 using testing::StrictMock; | |
16 using testing::_; | |
17 | |
18 namespace net { | |
19 namespace test { | |
20 | |
21 const QuicByteCount kBytesInFlight = 1024; | |
22 const int kInitialBurstPackets = 10; | |
23 | |
24 class PacingSenderTest : public ::testing::Test { | |
25 protected: | |
26 PacingSenderTest() | |
27 : zero_time_(QuicTime::Delta::Zero()), | |
28 infinite_time_(QuicTime::Delta::Infinite()), | |
29 sequence_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)); | |
36 } | |
37 | |
38 ~PacingSenderTest() override {} | |
39 | |
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)); | |
46 } | |
47 | |
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)); | |
60 } | |
61 | |
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); | |
69 } | |
70 | |
71 void CheckPacketIsSentImmediately() { | |
72 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, kBytesInFlight); | |
73 } | |
74 | |
75 void CheckAckIsSentImmediately() { | |
76 CheckPacketIsSentImmediately(NO_RETRANSMITTABLE_DATA, kBytesInFlight); | |
77 } | |
78 | |
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()); | |
92 } | |
93 } | |
94 | |
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); | |
100 } | |
101 | |
102 const QuicTime::Delta zero_time_; | |
103 const QuicTime::Delta infinite_time_; | |
104 MockClock clock_; | |
105 QuicPacketSequenceNumber sequence_number_; | |
106 StrictMock<MockSendAlgorithm>* mock_sender_; | |
107 scoped_ptr<PacingSender> pacing_sender_; | |
108 }; | |
109 | |
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)); | |
120 } | |
121 } | |
122 | |
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)); | |
133 } | |
134 } | |
135 | |
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))); | |
140 | |
141 // Now update the RTT and verify that packets are actually paced. | |
142 UpdateRtt(); | |
143 | |
144 CheckPacketIsSentImmediately(); | |
145 CheckPacketIsSentImmediately(); | |
146 | |
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)); | |
150 | |
151 // Wake up on time. | |
152 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2)); | |
153 CheckPacketIsSentImmediately(); | |
154 CheckPacketIsSentImmediately(); | |
155 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2)); | |
156 CheckAckIsSentImmediately(); | |
157 | |
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)); | |
165 | |
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)); | |
177 | |
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)); | |
187 | |
188 // Wake up too early. | |
189 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2)); | |
190 | |
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)); | |
195 } | |
196 | |
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)))); | |
203 | |
204 // Now update the RTT and verify that packets are actually paced. | |
205 UpdateRtt(); | |
206 | |
207 CheckPacketIsSentImmediately(); | |
208 CheckPacketIsSentImmediately(); | |
209 | |
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)); | |
213 | |
214 // Wake up on time. | |
215 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(2200)); | |
216 CheckPacketIsSentImmediately(); | |
217 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1600)); | |
218 CheckAckIsSentImmediately(); | |
219 | |
220 // Wake up late. | |
221 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(4)); | |
222 CheckPacketIsSentImmediately(); | |
223 CheckPacketIsSentImmediately(); | |
224 CheckPacketIsSentImmediately(); | |
225 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2400)); | |
226 | |
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)); | |
235 | |
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)); | |
245 | |
246 // Wake up too early. | |
247 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2200)); | |
248 | |
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)); | |
253 } | |
254 | |
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))); | |
259 | |
260 // Update the RTT and verify that the first 10 packets aren't paced. | |
261 UpdateRtt(); | |
262 | |
263 // Send 10 packets, and verify that they are not paced. | |
264 for (int i = 0 ; i < kInitialBurstPackets; ++i) { | |
265 CheckPacketIsSentImmediately(); | |
266 } | |
267 | |
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)); | |
273 | |
274 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); | |
275 CheckPacketIsSentImmediately(); | |
276 | |
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(); | |
282 } | |
283 | |
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)); | |
289 } | |
290 | |
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))); | |
295 | |
296 // Send 10 packets, and verify that they are not paced. | |
297 for (int i = 0 ; i < kInitialBurstPackets; ++i) { | |
298 CheckPacketIsSentImmediately(); | |
299 } | |
300 | |
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)); | |
306 | |
307 | |
308 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); | |
309 CheckPacketIsSentImmediately(); | |
310 | |
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(); | |
317 } | |
318 | |
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)); | |
324 } | |
325 | |
326 TEST_F(PacingSenderTest, FastSending) { | |
327 // Ensure the pacing sender paces, even when the inter-packet spacing is less | |
328 // than the pacing granularity. | |
329 InitPacingRate(10, | |
330 QuicBandwidth::FromBytesAndTimeDelta( | |
331 2 * kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1))); | |
332 | |
333 // Update the RTT and verify that the first 10 packets aren't paced. | |
334 UpdateRtt(); | |
335 | |
336 // Send 10 packets, and verify that they are not paced. | |
337 for (int i = 0; i < kInitialBurstPackets; ++i) { | |
338 CheckPacketIsSentImmediately(); | |
339 } | |
340 | |
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)); | |
347 | |
348 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); | |
349 CheckPacketIsSentImmediately(); | |
350 | |
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(); | |
356 } | |
357 | |
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)); | |
364 } | |
365 | |
366 } // namespace test | |
367 } // namespace net | |
OLD | NEW |