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_utils.h"
10 #include "net/quic/test_tools/quic_connection_peer.h"
11 #include "net/quic/test_tools/quic_flow_controller_peer.h"
12 #include "net/quic/test_tools/quic_test_utils.h"
13 #include "net/test/gtest_util.h"
14 #include "testing/gmock/include/gmock/gmock.h"
19 class QuicFlowControllerTest
: public ::testing::Test
{
21 QuicFlowControllerTest()
23 send_window_(kInitialSessionFlowControlWindowForTest
),
24 receive_window_(kInitialSessionFlowControlWindowForTest
),
25 max_receive_window_(kInitialSessionFlowControlWindowForTest
),
30 flow_controller_
.reset(new QuicFlowController(
31 &connection_
, stream_id_
, false, send_window_
,
32 receive_window_
, max_receive_window_
));
36 QuicStreamId stream_id_
;
37 QuicByteCount send_window_
;
38 QuicByteCount receive_window_
;
39 QuicByteCount max_receive_window_
;
40 scoped_ptr
<QuicFlowController
> flow_controller_
;
41 MockConnection connection_
;
44 TEST_F(QuicFlowControllerTest
, SendingBytes
) {
47 EXPECT_TRUE(flow_controller_
->IsEnabled());
48 EXPECT_FALSE(flow_controller_
->IsBlocked());
49 EXPECT_FALSE(flow_controller_
->FlowControlViolation());
50 EXPECT_EQ(send_window_
, flow_controller_
->SendWindowSize());
52 // Send some bytes, but not enough to block.
53 flow_controller_
->AddBytesSent(send_window_
/ 2);
54 EXPECT_FALSE(flow_controller_
->IsBlocked());
55 EXPECT_EQ(send_window_
/ 2, flow_controller_
->SendWindowSize());
57 // Send enough bytes to block.
58 flow_controller_
->AddBytesSent(send_window_
/ 2);
59 EXPECT_TRUE(flow_controller_
->IsBlocked());
60 EXPECT_EQ(0u, flow_controller_
->SendWindowSize());
62 // BLOCKED frame should get sent.
63 EXPECT_CALL(connection_
, SendBlocked(stream_id_
)).Times(1);
64 flow_controller_
->MaybeSendBlocked();
66 // Update the send window, and verify this has unblocked.
67 EXPECT_TRUE(flow_controller_
->UpdateSendWindowOffset(2 * send_window_
));
68 EXPECT_FALSE(flow_controller_
->IsBlocked());
69 EXPECT_EQ(send_window_
, flow_controller_
->SendWindowSize());
71 // Updating with a smaller offset doesn't change anything.
72 EXPECT_FALSE(flow_controller_
->UpdateSendWindowOffset(send_window_
/ 10));
73 EXPECT_EQ(send_window_
, flow_controller_
->SendWindowSize());
75 // Try to send more bytes, violating flow control.
76 EXPECT_CALL(connection_
,
77 SendConnectionClose(QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA
));
79 flow_controller_
->AddBytesSent(send_window_
* 10),
80 base::StringPrintf("Trying to send an extra %" PRIu64
" bytes",
82 EXPECT_TRUE(flow_controller_
->IsBlocked());
83 EXPECT_EQ(0u, flow_controller_
->SendWindowSize());
86 TEST_F(QuicFlowControllerTest
, ReceivingBytes
) {
89 EXPECT_TRUE(flow_controller_
->IsEnabled());
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.
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
) {
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_TRUE(flow_controller_
->IsEnabled());
120 EXPECT_FALSE(flow_controller_
->IsBlocked());
121 EXPECT_FALSE(flow_controller_
->FlowControlViolation());
122 EXPECT_EQ(send_window_
, flow_controller_
->SendWindowSize());
124 // Send enough bytes to block.
125 flow_controller_
->AddBytesSent(send_window_
);
126 EXPECT_TRUE(flow_controller_
->IsBlocked());
127 EXPECT_EQ(0u, flow_controller_
->SendWindowSize());
129 // Expect that 2 BLOCKED frames should get sent in total.
130 EXPECT_CALL(connection_
, SendBlocked(stream_id_
)).Times(2);
132 // BLOCKED frame should get sent.
133 flow_controller_
->MaybeSendBlocked();
135 // BLOCKED frame should not get sent again until our send offset changes.
136 flow_controller_
->MaybeSendBlocked();
137 flow_controller_
->MaybeSendBlocked();
138 flow_controller_
->MaybeSendBlocked();
139 flow_controller_
->MaybeSendBlocked();
140 flow_controller_
->MaybeSendBlocked();
142 // Update the send window, then send enough bytes to block again.
143 EXPECT_TRUE(flow_controller_
->UpdateSendWindowOffset(2 * send_window_
));
144 EXPECT_FALSE(flow_controller_
->IsBlocked());
145 EXPECT_EQ(send_window_
, flow_controller_
->SendWindowSize());
146 flow_controller_
->AddBytesSent(send_window_
);
147 EXPECT_TRUE(flow_controller_
->IsBlocked());
148 EXPECT_EQ(0u, flow_controller_
->SendWindowSize());
150 // BLOCKED frame should get sent as send offset has changed.
151 flow_controller_
->MaybeSendBlocked();