Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / net / quic / quic_flow_controller_test.cc
blobdffbd56d7f62794ff25e05d186d5e9f8821d42e5
1 // Copyright 2014 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/quic_flow_controller.h"
7 #include "base/format_macros.h"
8 #include "base/strings/stringprintf.h"
9 #include "net/quic/quic_flags.h"
10 #include "net/quic/quic_utils.h"
11 #include "net/quic/test_tools/quic_connection_peer.h"
12 #include "net/quic/test_tools/quic_flow_controller_peer.h"
13 #include "net/quic/test_tools/quic_sent_packet_manager_peer.h"
14 #include "net/quic/test_tools/quic_test_utils.h"
15 #include "net/test/gtest_util.h"
16 #include "testing/gmock/include/gmock/gmock.h"
18 namespace net {
19 namespace test {
21 // Receive window auto-tuning uses RTT in its logic.
22 const int64 kRtt = 100;
24 class QuicFlowControllerTest : public ::testing::Test {
25 public:
26 QuicFlowControllerTest()
27 : stream_id_(1234),
28 send_window_(kInitialSessionFlowControlWindowForTest),
29 receive_window_(kInitialSessionFlowControlWindowForTest),
30 connection_(Perspective::IS_CLIENT) {}
32 void Initialize() {
33 flow_controller_.reset(
34 new QuicFlowController(&connection_, stream_id_, Perspective::IS_CLIENT,
35 send_window_, receive_window_, false));
38 protected:
39 QuicStreamId stream_id_;
40 QuicByteCount send_window_;
41 QuicByteCount receive_window_;
42 scoped_ptr<QuicFlowController> flow_controller_;
43 MockConnection connection_;
46 TEST_F(QuicFlowControllerTest, SendingBytes) {
47 Initialize();
49 EXPECT_FALSE(flow_controller_->IsBlocked());
50 EXPECT_FALSE(flow_controller_->FlowControlViolation());
51 EXPECT_EQ(send_window_, flow_controller_->SendWindowSize());
53 // Send some bytes, but not enough to block.
54 flow_controller_->AddBytesSent(send_window_ / 2);
55 EXPECT_FALSE(flow_controller_->IsBlocked());
56 EXPECT_EQ(send_window_ / 2, flow_controller_->SendWindowSize());
58 // Send enough bytes to block.
59 flow_controller_->AddBytesSent(send_window_ / 2);
60 EXPECT_TRUE(flow_controller_->IsBlocked());
61 EXPECT_EQ(0u, flow_controller_->SendWindowSize());
63 // BLOCKED frame should get sent.
64 EXPECT_CALL(connection_, SendBlocked(stream_id_)).Times(1);
65 flow_controller_->MaybeSendBlocked();
67 // Update the send window, and verify this has unblocked.
68 EXPECT_TRUE(flow_controller_->UpdateSendWindowOffset(2 * send_window_));
69 EXPECT_FALSE(flow_controller_->IsBlocked());
70 EXPECT_EQ(send_window_, flow_controller_->SendWindowSize());
72 // Updating with a smaller offset doesn't change anything.
73 EXPECT_FALSE(flow_controller_->UpdateSendWindowOffset(send_window_ / 10));
74 EXPECT_EQ(send_window_, flow_controller_->SendWindowSize());
76 // Try to send more bytes, violating flow control.
77 EXPECT_CALL(connection_,
78 SendConnectionClose(QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA));
79 EXPECT_DFATAL(
80 flow_controller_->AddBytesSent(send_window_ * 10),
81 base::StringPrintf("Trying to send an extra %" PRIu64 " bytes",
82 send_window_ * 10));
83 EXPECT_TRUE(flow_controller_->IsBlocked());
84 EXPECT_EQ(0u, flow_controller_->SendWindowSize());
87 TEST_F(QuicFlowControllerTest, ReceivingBytes) {
88 Initialize();
90 EXPECT_FALSE(flow_controller_->IsBlocked());
91 EXPECT_FALSE(flow_controller_->FlowControlViolation());
92 EXPECT_EQ(kInitialSessionFlowControlWindowForTest,
93 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
95 // Receive some bytes, updating highest received offset, but not enough to
96 // fill flow control receive window.
97 EXPECT_TRUE(
98 flow_controller_->UpdateHighestReceivedOffset(1 + receive_window_ / 2));
99 EXPECT_FALSE(flow_controller_->FlowControlViolation());
100 EXPECT_EQ((receive_window_ / 2) - 1,
101 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
103 // Consume enough bytes to send a WINDOW_UPDATE frame.
104 EXPECT_CALL(connection_, SendWindowUpdate(stream_id_, ::testing::_)).Times(1);
106 flow_controller_->AddBytesConsumed(1 + receive_window_ / 2);
108 // Result is that once again we have a fully open receive window.
109 EXPECT_FALSE(flow_controller_->FlowControlViolation());
110 EXPECT_EQ(kInitialSessionFlowControlWindowForTest,
111 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
114 TEST_F(QuicFlowControllerTest, OnlySendBlockedFrameOncePerOffset) {
115 Initialize();
117 // Test that we don't send duplicate BLOCKED frames. We should only send one
118 // BLOCKED frame at a given send window offset.
119 EXPECT_FALSE(flow_controller_->IsBlocked());
120 EXPECT_FALSE(flow_controller_->FlowControlViolation());
121 EXPECT_EQ(send_window_, flow_controller_->SendWindowSize());
123 // Send enough bytes to block.
124 flow_controller_->AddBytesSent(send_window_);
125 EXPECT_TRUE(flow_controller_->IsBlocked());
126 EXPECT_EQ(0u, flow_controller_->SendWindowSize());
128 // Expect that 2 BLOCKED frames should get sent in total.
129 EXPECT_CALL(connection_, SendBlocked(stream_id_)).Times(2);
131 // BLOCKED frame should get sent.
132 flow_controller_->MaybeSendBlocked();
134 // BLOCKED frame should not get sent again until our send offset changes.
135 flow_controller_->MaybeSendBlocked();
136 flow_controller_->MaybeSendBlocked();
137 flow_controller_->MaybeSendBlocked();
138 flow_controller_->MaybeSendBlocked();
139 flow_controller_->MaybeSendBlocked();
141 // Update the send window, then send enough bytes to block again.
142 EXPECT_TRUE(flow_controller_->UpdateSendWindowOffset(2 * send_window_));
143 EXPECT_FALSE(flow_controller_->IsBlocked());
144 EXPECT_EQ(send_window_, flow_controller_->SendWindowSize());
145 flow_controller_->AddBytesSent(send_window_);
146 EXPECT_TRUE(flow_controller_->IsBlocked());
147 EXPECT_EQ(0u, flow_controller_->SendWindowSize());
149 // BLOCKED frame should get sent as send offset has changed.
150 flow_controller_->MaybeSendBlocked();
153 TEST_F(QuicFlowControllerTest, ReceivingBytesFastIncreasesFlowWindow) {
154 ValueRestore<bool> old_flag(&FLAGS_quic_auto_tune_receive_window, true);
155 // This test will generate two WINDOW_UPDATE frames.
156 EXPECT_CALL(connection_, SendWindowUpdate(stream_id_, ::testing::_)).Times(2);
158 Initialize();
159 flow_controller_->set_auto_tune_receive_window(true);
161 // Make sure clock is inititialized.
162 connection_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
164 QuicSentPacketManager* manager =
165 QuicConnectionPeer::GetSentPacketManager(&connection_);
167 RttStats* rtt_stats = QuicSentPacketManagerPeer::GetRttStats(manager);
168 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kRtt),
169 QuicTime::Delta::Zero(), QuicTime::Zero());
171 EXPECT_FALSE(flow_controller_->IsBlocked());
172 EXPECT_FALSE(flow_controller_->FlowControlViolation());
173 EXPECT_EQ(kInitialSessionFlowControlWindowForTest,
174 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
176 QuicByteCount threshold =
177 QuicFlowControllerPeer::WindowUpdateThreshold(flow_controller_.get());
179 QuicStreamOffset receive_offset = threshold + 1;
180 // Receive some bytes, updating highest received offset, but not enough to
181 // fill flow control receive window.
182 EXPECT_TRUE(flow_controller_->UpdateHighestReceivedOffset(receive_offset));
183 EXPECT_FALSE(flow_controller_->FlowControlViolation());
184 EXPECT_EQ(kInitialSessionFlowControlWindowForTest - receive_offset,
185 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
187 // Consume enough bytes to send a WINDOW_UPDATE frame.
188 flow_controller_->AddBytesConsumed(threshold + 1);
189 // Result is that once again we have a fully open receive window.
190 EXPECT_FALSE(flow_controller_->FlowControlViolation());
191 EXPECT_EQ(kInitialSessionFlowControlWindowForTest,
192 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
194 // Move time forward, but by less than two RTTs. Then receive and consume
195 // some more, forcing a second WINDOW_UPDATE with an increased max window
196 // size.
197 connection_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2 * kRtt - 1));
198 receive_offset += threshold + 1;
199 EXPECT_TRUE(flow_controller_->UpdateHighestReceivedOffset(receive_offset));
200 flow_controller_->AddBytesConsumed(threshold + 1);
201 EXPECT_FALSE(flow_controller_->FlowControlViolation());
202 QuicByteCount new_threshold =
203 QuicFlowControllerPeer::WindowUpdateThreshold(flow_controller_.get());
204 EXPECT_GT(new_threshold, threshold);
207 TEST_F(QuicFlowControllerTest, ReceivingBytesFastStatusQuo) {
208 ValueRestore<bool> old_flag(&FLAGS_quic_auto_tune_receive_window, false);
209 // This test will generate two WINDOW_UPDATE frames.
210 EXPECT_CALL(connection_, SendWindowUpdate(stream_id_, ::testing::_)).Times(2);
212 Initialize();
213 flow_controller_->set_auto_tune_receive_window(true);
215 // Make sure clock is inititialized.
216 connection_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
218 QuicSentPacketManager* manager =
219 QuicConnectionPeer::GetSentPacketManager(&connection_);
221 RttStats* rtt_stats = QuicSentPacketManagerPeer::GetRttStats(manager);
222 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kRtt),
223 QuicTime::Delta::Zero(), QuicTime::Zero());
225 EXPECT_FALSE(flow_controller_->IsBlocked());
226 EXPECT_FALSE(flow_controller_->FlowControlViolation());
227 EXPECT_EQ(kInitialSessionFlowControlWindowForTest,
228 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
230 QuicByteCount threshold =
231 QuicFlowControllerPeer::WindowUpdateThreshold(flow_controller_.get());
233 QuicStreamOffset receive_offset = threshold + 1;
234 // Receive some bytes, updating highest received offset, but not enough to
235 // fill flow control receive window.
236 EXPECT_TRUE(flow_controller_->UpdateHighestReceivedOffset(receive_offset));
237 EXPECT_FALSE(flow_controller_->FlowControlViolation());
238 EXPECT_EQ(kInitialSessionFlowControlWindowForTest - receive_offset,
239 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
241 // Consume enough bytes to send a WINDOW_UPDATE frame.
242 flow_controller_->AddBytesConsumed(threshold + 1);
243 // Result is that once again we have a fully open receive window.
244 EXPECT_FALSE(flow_controller_->FlowControlViolation());
245 EXPECT_EQ(kInitialSessionFlowControlWindowForTest,
246 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
248 // Move time forward, but by less than two RTTs. Then receive and consume
249 // some more, forcing a second WINDOW_UPDATE with an increased max window
250 // size.
251 connection_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2 * kRtt - 1));
252 receive_offset += threshold + 1;
253 EXPECT_TRUE(flow_controller_->UpdateHighestReceivedOffset(receive_offset));
254 flow_controller_->AddBytesConsumed(threshold + 1);
255 EXPECT_FALSE(flow_controller_->FlowControlViolation());
256 QuicByteCount new_threshold =
257 QuicFlowControllerPeer::WindowUpdateThreshold(flow_controller_.get());
258 EXPECT_EQ(new_threshold, threshold);
261 TEST_F(QuicFlowControllerTest, ReceivingBytesNormalStableFlowWindow) {
262 ValueRestore<bool> old_flag(&FLAGS_quic_auto_tune_receive_window, true);
263 // This test will generate two WINDOW_UPDATE frames.
264 EXPECT_CALL(connection_, SendWindowUpdate(stream_id_, ::testing::_)).Times(2);
266 Initialize();
267 flow_controller_->set_auto_tune_receive_window(true);
269 // Make sure clock is inititialized.
270 connection_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
272 QuicSentPacketManager* manager =
273 QuicConnectionPeer::GetSentPacketManager(&connection_);
274 RttStats* rtt_stats = QuicSentPacketManagerPeer::GetRttStats(manager);
275 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kRtt),
276 QuicTime::Delta::Zero(), QuicTime::Zero());
278 EXPECT_FALSE(flow_controller_->IsBlocked());
279 EXPECT_FALSE(flow_controller_->FlowControlViolation());
280 EXPECT_EQ(kInitialSessionFlowControlWindowForTest,
281 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
283 QuicByteCount threshold =
284 QuicFlowControllerPeer::WindowUpdateThreshold(flow_controller_.get());
286 QuicStreamOffset receive_offset = threshold + 1;
287 // Receive some bytes, updating highest received offset, but not enough to
288 // fill flow control receive window.
289 EXPECT_TRUE(flow_controller_->UpdateHighestReceivedOffset(receive_offset));
290 EXPECT_FALSE(flow_controller_->FlowControlViolation());
291 EXPECT_EQ(kInitialSessionFlowControlWindowForTest - receive_offset,
292 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
294 flow_controller_->AddBytesConsumed(threshold + 1);
296 // Result is that once again we have a fully open receive window.
297 EXPECT_FALSE(flow_controller_->FlowControlViolation());
298 EXPECT_EQ(kInitialSessionFlowControlWindowForTest,
299 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
301 // Move time forward, but by more than two RTTs. Then receive and consume
302 // some more, forcing a second WINDOW_UPDATE with unchanged max window size.
303 connection_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2 * kRtt + 1));
305 receive_offset += threshold + 1;
306 EXPECT_TRUE(flow_controller_->UpdateHighestReceivedOffset(receive_offset));
308 flow_controller_->AddBytesConsumed(threshold + 1);
309 EXPECT_FALSE(flow_controller_->FlowControlViolation());
311 QuicByteCount new_threshold =
312 QuicFlowControllerPeer::WindowUpdateThreshold(flow_controller_.get());
314 EXPECT_EQ(new_threshold, threshold);
317 TEST_F(QuicFlowControllerTest, ReceivingBytesNormalStatusQuo) {
318 ValueRestore<bool> old_flag(&FLAGS_quic_auto_tune_receive_window, false);
319 // This test will generate two WINDOW_UPDATE frames.
320 EXPECT_CALL(connection_, SendWindowUpdate(stream_id_, ::testing::_)).Times(2);
322 Initialize();
323 flow_controller_->set_auto_tune_receive_window(true);
325 // Make sure clock is inititialized.
326 connection_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
328 QuicSentPacketManager* manager =
329 QuicConnectionPeer::GetSentPacketManager(&connection_);
330 RttStats* rtt_stats = QuicSentPacketManagerPeer::GetRttStats(manager);
331 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kRtt),
332 QuicTime::Delta::Zero(), QuicTime::Zero());
334 EXPECT_FALSE(flow_controller_->IsBlocked());
335 EXPECT_FALSE(flow_controller_->FlowControlViolation());
336 EXPECT_EQ(kInitialSessionFlowControlWindowForTest,
337 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
339 QuicByteCount threshold =
340 QuicFlowControllerPeer::WindowUpdateThreshold(flow_controller_.get());
342 QuicStreamOffset receive_offset = threshold + 1;
343 // Receive some bytes, updating highest received offset, but not enough to
344 // fill flow control receive window.
345 EXPECT_TRUE(flow_controller_->UpdateHighestReceivedOffset(receive_offset));
346 EXPECT_FALSE(flow_controller_->FlowControlViolation());
347 EXPECT_EQ(kInitialSessionFlowControlWindowForTest - receive_offset,
348 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
350 flow_controller_->AddBytesConsumed(threshold + 1);
352 // Result is that once again we have a fully open receive window.
353 EXPECT_FALSE(flow_controller_->FlowControlViolation());
354 EXPECT_EQ(kInitialSessionFlowControlWindowForTest,
355 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
357 // Move time forward, but by more than two RTTs. Then receive and consume
358 // some more, forcing a second WINDOW_UPDATE with unchanged max window size.
359 connection_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2 * kRtt + 1));
361 receive_offset += threshold + 1;
362 EXPECT_TRUE(flow_controller_->UpdateHighestReceivedOffset(receive_offset));
364 flow_controller_->AddBytesConsumed(threshold + 1);
365 EXPECT_FALSE(flow_controller_->FlowControlViolation());
367 QuicByteCount new_threshold =
368 QuicFlowControllerPeer::WindowUpdateThreshold(flow_controller_.get());
370 EXPECT_EQ(new_threshold, threshold);
373 } // namespace test
374 } // namespace net