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
;
20 class PacingSenderTest
: public ::testing::Test
{
23 : zero_time_(QuicTime::Delta::Zero()),
24 infinite_time_(QuicTime::Delta::Infinite()),
26 mock_sender_(new StrictMock
<MockSendAlgorithm
>()),
27 pacing_sender_(new PacingSender(mock_sender_
,
28 QuicTime::Delta::FromMilliseconds(1))) {
29 // Pick arbitrary time.
30 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(9));
33 virtual ~PacingSenderTest() {}
35 void CheckPacketIsSentImmediately() {
36 // In order for the packet to be sendable, the underlying sender must
37 // permit it to be sent immediately.
38 EXPECT_CALL(*mock_sender_
, TimeUntilSend(clock_
.Now(),
40 HAS_RETRANSMITTABLE_DATA
,
42 .WillOnce(Return(zero_time_
));
43 // Verify that the packet can be sent immediately.
45 pacing_sender_
->TimeUntilSend(clock_
.Now(), NOT_RETRANSMISSION
,
46 HAS_RETRANSMITTABLE_DATA
,
49 // Actually send the packet.
50 EXPECT_CALL(*mock_sender_
,
51 OnPacketSent(clock_
.Now(), sequence_number_
, kMaxPacketSize
,
52 NOT_RETRANSMISSION
, HAS_RETRANSMITTABLE_DATA
));
53 pacing_sender_
->OnPacketSent(clock_
.Now(), sequence_number_
++,
54 kMaxPacketSize
, NOT_RETRANSMISSION
,
55 HAS_RETRANSMITTABLE_DATA
);
58 void CheckAckIsSentImmediately() {
59 // In order for the ack to be sendable, the underlying sender must
60 // permit it to be sent immediately.
61 EXPECT_CALL(*mock_sender_
, TimeUntilSend(clock_
.Now(),
63 NO_RETRANSMITTABLE_DATA
,
65 .WillOnce(Return(zero_time_
));
66 // Verify that the ACK can be sent immediately.
68 pacing_sender_
->TimeUntilSend(clock_
.Now(), NOT_RETRANSMISSION
,
69 NO_RETRANSMITTABLE_DATA
,
72 // Actually send the packet.
73 EXPECT_CALL(*mock_sender_
,
74 OnPacketSent(clock_
.Now(), sequence_number_
, kMaxPacketSize
,
75 NOT_RETRANSMISSION
, NO_RETRANSMITTABLE_DATA
));
76 pacing_sender_
->OnPacketSent(clock_
.Now(), sequence_number_
++,
77 kMaxPacketSize
, NOT_RETRANSMISSION
,
78 NO_RETRANSMITTABLE_DATA
);
81 void CheckPacketIsDelayed(QuicTime::Delta delay
) {
82 // In order for the packet to be sendable, the underlying sender must
83 // permit it to be sent immediately.
84 EXPECT_CALL(*mock_sender_
, TimeUntilSend(clock_
.Now(),
86 HAS_RETRANSMITTABLE_DATA
,
88 .WillOnce(Return(zero_time_
));
89 // Verify that the packet is delayed.
90 EXPECT_EQ(delay
.ToMicroseconds(),
91 pacing_sender_
->TimeUntilSend(clock_
.Now(), NOT_RETRANSMISSION
,
92 HAS_RETRANSMITTABLE_DATA
,
93 NOT_HANDSHAKE
).ToMicroseconds());
96 const QuicTime::Delta zero_time_
;
97 const QuicTime::Delta infinite_time_
;
99 QuicPacketSequenceNumber sequence_number_
;
100 StrictMock
<MockSendAlgorithm
>* mock_sender_
;
101 scoped_ptr
<PacingSender
> pacing_sender_
;
104 TEST_F(PacingSenderTest
, NoSend
) {
105 EXPECT_CALL(*mock_sender_
, TimeUntilSend(clock_
.Now(),
107 HAS_RETRANSMITTABLE_DATA
,
109 .WillOnce(Return(infinite_time_
));
110 EXPECT_EQ(infinite_time_
,
111 pacing_sender_
->TimeUntilSend(clock_
.Now(), NOT_RETRANSMISSION
,
112 HAS_RETRANSMITTABLE_DATA
,
116 TEST_F(PacingSenderTest
, SendNow
) {
117 EXPECT_CALL(*mock_sender_
, TimeUntilSend(clock_
.Now(),
119 HAS_RETRANSMITTABLE_DATA
,
121 .WillOnce(Return(zero_time_
));
122 EXPECT_EQ(zero_time_
,
123 pacing_sender_
->TimeUntilSend(clock_
.Now(), NOT_RETRANSMISSION
,
124 HAS_RETRANSMITTABLE_DATA
,
128 TEST_F(PacingSenderTest
, VariousSending
) {
129 // Configure bandwith of 1 packet per 2 ms, for which the pacing rate
130 // will be 1 packet per 1 ms.
131 EXPECT_CALL(*mock_sender_
, BandwidthEstimate())
132 .WillRepeatedly(Return(QuicBandwidth::FromBytesAndTimeDelta(
133 kMaxPacketSize
, QuicTime::Delta::FromMilliseconds(2))));
135 CheckPacketIsSentImmediately();
136 CheckPacketIsSentImmediately();
137 CheckPacketIsSentImmediately();
139 // The first packet was a "make up", then we sent two packets "into the
140 // future", so the delay should be 2.
141 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
144 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(2));
145 CheckPacketIsSentImmediately();
146 CheckPacketIsSentImmediately();
147 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
148 CheckAckIsSentImmediately();
151 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(4));
152 CheckPacketIsSentImmediately();
153 CheckPacketIsSentImmediately();
154 CheckPacketIsSentImmediately();
155 CheckPacketIsSentImmediately();
156 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
158 // Wake up too early.
159 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
161 // Wake up early, but after enough time has passed to permit a send.
162 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
163 CheckPacketIsSentImmediately();
164 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));