MacViews: Get c/b/ui/views/tabs to build on Mac
[chromium-blink-merge.git] / net / quic / congestion_control / pacing_sender_test.cc
blob0be0b0aac017466ee522ff1e3b71d7766d08d028
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 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));
38 virtual ~PacingSenderTest() {}
40 void CheckPacketIsSentImmediately() {
41 // In order for the packet to be sendable, the underlying sender must
42 // permit it to be sent immediately.
43 for (int i = 0; i < 2; ++i) {
44 EXPECT_CALL(*mock_sender_, TimeUntilSend(clock_.Now(),
45 kBytesInFlight,
46 HAS_RETRANSMITTABLE_DATA))
47 .WillOnce(Return(zero_time_));
48 // Verify that the packet can be sent immediately.
49 EXPECT_EQ(zero_time_,
50 pacing_sender_->TimeUntilSend(clock_.Now(),
51 kBytesInFlight,
52 HAS_RETRANSMITTABLE_DATA));
55 // Actually send the packet.
56 EXPECT_CALL(*mock_sender_,
57 OnPacketSent(clock_.Now(), kBytesInFlight, sequence_number_,
58 kMaxPacketSize, HAS_RETRANSMITTABLE_DATA));
59 pacing_sender_->OnPacketSent(clock_.Now(), kBytesInFlight,
60 sequence_number_++, kMaxPacketSize,
61 HAS_RETRANSMITTABLE_DATA);
64 void CheckAckIsSentImmediately() {
65 // In order for the ack to be sendable, the underlying sender must
66 // permit it to be sent immediately.
67 EXPECT_CALL(*mock_sender_, TimeUntilSend(clock_.Now(),
69 NO_RETRANSMITTABLE_DATA))
70 .WillOnce(Return(zero_time_));
71 // Verify that the ACK can be sent immediately.
72 EXPECT_EQ(zero_time_,
73 pacing_sender_->TimeUntilSend(clock_.Now(),
75 NO_RETRANSMITTABLE_DATA));
77 // Actually send the packet.
78 EXPECT_CALL(*mock_sender_,
79 OnPacketSent(clock_.Now(), 0, sequence_number_,
80 kMaxPacketSize, NO_RETRANSMITTABLE_DATA));
81 pacing_sender_->OnPacketSent(clock_.Now(), 0,
82 sequence_number_++, kMaxPacketSize,
83 NO_RETRANSMITTABLE_DATA);
86 void CheckPacketIsDelayed(QuicTime::Delta delay) {
87 // In order for the packet to be sendable, the underlying sender must
88 // permit it to be sent immediately.
89 for (int i = 0; i < 2; ++i) {
90 EXPECT_CALL(*mock_sender_, TimeUntilSend(clock_.Now(),
91 kBytesInFlight,
92 HAS_RETRANSMITTABLE_DATA))
93 .WillOnce(Return(zero_time_));
94 // Verify that the packet is delayed.
95 EXPECT_EQ(delay.ToMicroseconds(),
96 pacing_sender_->TimeUntilSend(
97 clock_.Now(), kBytesInFlight,
98 HAS_RETRANSMITTABLE_DATA).ToMicroseconds());
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_;
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.
138 EXPECT_CALL(*mock_sender_, PacingRate())
139 .WillRepeatedly(Return(QuicBandwidth::FromBytesAndTimeDelta(
140 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1))));
142 // Now update the RTT and verify that packets are actually paced.
143 EXPECT_CALL(*mock_sender_, OnCongestionEvent(true, kBytesInFlight, _, _));
144 SendAlgorithmInterface::CongestionVector empty_map;
145 pacing_sender_->OnCongestionEvent(true, kBytesInFlight, empty_map, empty_map);
147 CheckPacketIsSentImmediately();
148 CheckPacketIsSentImmediately();
149 CheckPacketIsSentImmediately();
151 // The first packet was a "make up", then we sent two packets "into the
152 // future", so the delay should be 2.
153 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
155 // Wake up on time.
156 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2));
157 CheckPacketIsSentImmediately();
158 CheckPacketIsSentImmediately();
159 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
160 CheckAckIsSentImmediately();
162 // Wake up late.
163 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(4));
164 CheckPacketIsSentImmediately();
165 CheckPacketIsSentImmediately();
166 CheckPacketIsSentImmediately();
167 CheckPacketIsSentImmediately();
168 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
170 // Wake up really late.
171 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
172 CheckPacketIsSentImmediately();
173 CheckPacketIsSentImmediately();
174 CheckPacketIsSentImmediately();
175 CheckPacketIsSentImmediately();
176 CheckPacketIsSentImmediately();
177 CheckPacketIsSentImmediately();
178 CheckPacketIsSentImmediately();
179 CheckPacketIsSentImmediately();
180 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
182 // Wake up really late again, but application pause partway through.
183 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
184 CheckPacketIsSentImmediately();
185 CheckPacketIsSentImmediately();
186 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
187 CheckPacketIsSentImmediately();
188 CheckPacketIsSentImmediately();
189 CheckPacketIsSentImmediately();
190 CheckPacketIsSentImmediately();
191 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
193 // Wake up too early.
194 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
196 // Wake up early, but after enough time has passed to permit a send.
197 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
198 CheckPacketIsSentImmediately();
199 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
202 TEST_F(PacingSenderTest, CongestionAvoidanceSending) {
203 // Configure pacing rate of 1 packet per 1 ms.
204 EXPECT_CALL(*mock_sender_, PacingRate())
205 .WillRepeatedly(Return(QuicBandwidth::FromBytesAndTimeDelta(
206 kMaxPacketSize * 1.25, QuicTime::Delta::FromMilliseconds(2))));
208 // Now update the RTT and verify that packets are actually paced.
209 EXPECT_CALL(*mock_sender_, OnCongestionEvent(true, kBytesInFlight, _, _));
210 SendAlgorithmInterface::CongestionVector empty_map;
211 pacing_sender_->OnCongestionEvent(true, kBytesInFlight, empty_map, empty_map);
213 CheckPacketIsSentImmediately();
214 CheckPacketIsSentImmediately();
216 // The first packet was a "make up", then we sent two packets "into the
217 // future", so the delay should be 2200us.
218 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2200));
220 // Wake up on time.
221 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(2200));
222 CheckPacketIsSentImmediately();
223 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1600));
224 CheckAckIsSentImmediately();
226 // Wake up late.
227 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(4));
228 CheckPacketIsSentImmediately();
229 CheckPacketIsSentImmediately();
230 CheckPacketIsSentImmediately();
231 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2400));
233 // Wake up really late.
234 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
235 CheckPacketIsSentImmediately();
236 CheckPacketIsSentImmediately();
237 CheckPacketIsSentImmediately();
238 CheckPacketIsSentImmediately();
239 CheckPacketIsSentImmediately();
240 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2400));
242 // Wake up really late again, but application pause partway through.
243 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
244 CheckPacketIsSentImmediately();
245 CheckPacketIsSentImmediately();
246 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
247 CheckPacketIsSentImmediately();
248 CheckPacketIsSentImmediately();
249 CheckPacketIsSentImmediately();
250 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2200));
252 // Wake up too early.
253 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2200));
255 // Wake up early, but after enough time has passed to permit a send.
256 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1200));
257 CheckPacketIsSentImmediately();
258 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(2600));
261 TEST_F(PacingSenderTest, InitialBurst) {
262 pacing_sender_.reset();
263 mock_sender_ = new StrictMock<MockSendAlgorithm>();
264 pacing_sender_.reset(new PacingSender(mock_sender_,
265 QuicTime::Delta::FromMilliseconds(1),
266 10));
268 // Configure pacing rate of 1 packet per 1 ms.
269 EXPECT_CALL(*mock_sender_, PacingRate())
270 .WillRepeatedly(Return(QuicBandwidth::FromBytesAndTimeDelta(
271 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1))));
273 // Update the RTT and verify that the first 10 packets aren't paced.
274 EXPECT_CALL(*mock_sender_, OnCongestionEvent(true, kBytesInFlight, _, _));
275 SendAlgorithmInterface::CongestionVector empty_map;
276 pacing_sender_->OnCongestionEvent(true, kBytesInFlight, empty_map, empty_map);
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", so the delay should be 2ms.
285 CheckPacketIsSentImmediately();
286 CheckPacketIsSentImmediately();
287 CheckPacketIsSentImmediately();
288 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
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 EXPECT_CALL(*mock_sender_,
296 TimeUntilSend(clock_.Now(),
298 HAS_RETRANSMITTABLE_DATA)).
299 WillOnce(Return(zero_time_));
300 EXPECT_EQ(zero_time_,
301 pacing_sender_->TimeUntilSend(clock_.Now(),
303 HAS_RETRANSMITTABLE_DATA));
304 for (int i = 0 ; i < kInitialBurstPackets; ++i) {
305 CheckPacketIsSentImmediately();
308 // The first packet was a "make up", then we sent two packets "into the
309 // future", so the delay should be 2ms.
310 CheckPacketIsSentImmediately();
311 CheckPacketIsSentImmediately();
312 CheckPacketIsSentImmediately();
313 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
316 TEST_F(PacingSenderTest, InitialBurstNoRttMeasurement) {
317 pacing_sender_.reset();
318 mock_sender_ = new StrictMock<MockSendAlgorithm>();
319 pacing_sender_.reset(new PacingSender(mock_sender_,
320 QuicTime::Delta::FromMilliseconds(1),
321 10));
323 // Configure pacing rate of 1 packet per 1 ms.
324 EXPECT_CALL(*mock_sender_, PacingRate())
325 .WillRepeatedly(Return(QuicBandwidth::FromBytesAndTimeDelta(
326 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1))));
328 // Send 10 packets, and verify that they are not paced.
329 for (int i = 0 ; i < kInitialBurstPackets; ++i) {
330 CheckPacketIsSentImmediately();
333 // The first packet was a "make up", then we sent two packets "into the
334 // future", so the delay should be 2ms.
335 CheckPacketIsSentImmediately();
336 CheckPacketIsSentImmediately();
337 CheckPacketIsSentImmediately();
338 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
341 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
342 CheckPacketIsSentImmediately();
344 // Next time TimeUntilSend is called with no bytes in flight, the tokens
345 // should be refilled and there should be no delay.
346 EXPECT_CALL(*mock_sender_,
347 TimeUntilSend(clock_.Now(),
349 HAS_RETRANSMITTABLE_DATA)).
350 WillOnce(Return(zero_time_));
351 EXPECT_EQ(zero_time_,
352 pacing_sender_->TimeUntilSend(clock_.Now(),
354 HAS_RETRANSMITTABLE_DATA));
355 // Send 10 packets, and verify that they are not paced.
356 for (int i = 0 ; i < kInitialBurstPackets; ++i) {
357 CheckPacketIsSentImmediately();
360 // The first packet was a "make up", then we sent two packets "into the
361 // future", so the delay should be 2ms.
362 CheckPacketIsSentImmediately();
363 CheckPacketIsSentImmediately();
364 CheckPacketIsSentImmediately();
365 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
368 } // namespace test
369 } // namespace net