Add explicit |forceOnlineSignin| to user pod status
[chromium-blink-merge.git] / net / quic / congestion_control / pacing_sender_test.cc
blobdfc78b84ecec4e29dddbe55c313381b78775bb56
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;
17 namespace net {
18 namespace test {
20 class PacingSenderTest : public ::testing::Test {
21 protected:
22 PacingSenderTest()
23 : zero_time_(QuicTime::Delta::Zero()),
24 infinite_time_(QuicTime::Delta::Infinite()),
25 sequence_number_(1),
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(),
39 NOT_RETRANSMISSION,
40 HAS_RETRANSMITTABLE_DATA,
41 NOT_HANDSHAKE))
42 .WillOnce(Return(zero_time_));
43 // Verify that the packet can be sent immediately.
44 EXPECT_EQ(zero_time_,
45 pacing_sender_->TimeUntilSend(clock_.Now(), NOT_RETRANSMISSION,
46 HAS_RETRANSMITTABLE_DATA,
47 NOT_HANDSHAKE));
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(),
62 NOT_RETRANSMISSION,
63 NO_RETRANSMITTABLE_DATA,
64 NOT_HANDSHAKE))
65 .WillOnce(Return(zero_time_));
66 // Verify that the ACK can be sent immediately.
67 EXPECT_EQ(zero_time_,
68 pacing_sender_->TimeUntilSend(clock_.Now(), NOT_RETRANSMISSION,
69 NO_RETRANSMITTABLE_DATA,
70 NOT_HANDSHAKE));
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(),
85 NOT_RETRANSMISSION,
86 HAS_RETRANSMITTABLE_DATA,
87 NOT_HANDSHAKE))
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_;
98 MockClock clock_;
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(),
106 NOT_RETRANSMISSION,
107 HAS_RETRANSMITTABLE_DATA,
108 NOT_HANDSHAKE))
109 .WillOnce(Return(infinite_time_));
110 EXPECT_EQ(infinite_time_,
111 pacing_sender_->TimeUntilSend(clock_.Now(), NOT_RETRANSMISSION,
112 HAS_RETRANSMITTABLE_DATA,
113 NOT_HANDSHAKE));
116 TEST_F(PacingSenderTest, SendNow) {
117 EXPECT_CALL(*mock_sender_, TimeUntilSend(clock_.Now(),
118 NOT_RETRANSMISSION,
119 HAS_RETRANSMITTABLE_DATA,
120 NOT_HANDSHAKE))
121 .WillOnce(Return(zero_time_));
122 EXPECT_EQ(zero_time_,
123 pacing_sender_->TimeUntilSend(clock_.Now(), NOT_RETRANSMISSION,
124 HAS_RETRANSMITTABLE_DATA,
125 NOT_HANDSHAKE));
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));
143 // Wake up on time.
144 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2));
145 CheckPacketIsSentImmediately();
146 CheckPacketIsSentImmediately();
147 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
148 CheckAckIsSentImmediately();
150 // Wake up late.
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));
167 } // namespace test
168 } // namespace net