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_FALSE(flow_controller_
->IsBlocked());
48 EXPECT_FALSE(flow_controller_
->FlowControlViolation());
49 EXPECT_EQ(send_window_
, flow_controller_
->SendWindowSize());
51 // Send some bytes, but not enough to block.
52 flow_controller_
->AddBytesSent(send_window_
/ 2);
53 EXPECT_FALSE(flow_controller_
->IsBlocked());
54 EXPECT_EQ(send_window_
/ 2, flow_controller_
->SendWindowSize());
56 // Send enough bytes to block.
57 flow_controller_
->AddBytesSent(send_window_
/ 2);
58 EXPECT_TRUE(flow_controller_
->IsBlocked());
59 EXPECT_EQ(0u, flow_controller_
->SendWindowSize());
61 // BLOCKED frame should get sent.
62 EXPECT_CALL(connection_
, SendBlocked(stream_id_
)).Times(1);
63 flow_controller_
->MaybeSendBlocked();
65 // Update the send window, and verify this has unblocked.
66 EXPECT_TRUE(flow_controller_
->UpdateSendWindowOffset(2 * send_window_
));
67 EXPECT_FALSE(flow_controller_
->IsBlocked());
68 EXPECT_EQ(send_window_
, flow_controller_
->SendWindowSize());
70 // Updating with a smaller offset doesn't change anything.
71 EXPECT_FALSE(flow_controller_
->UpdateSendWindowOffset(send_window_
/ 10));
72 EXPECT_EQ(send_window_
, flow_controller_
->SendWindowSize());
74 // Try to send more bytes, violating flow control.
75 EXPECT_CALL(connection_
,
76 SendConnectionClose(QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA
));
78 flow_controller_
->AddBytesSent(send_window_
* 10),
79 base::StringPrintf("Trying to send an extra %" PRIu64
" bytes",
81 EXPECT_TRUE(flow_controller_
->IsBlocked());
82 EXPECT_EQ(0u, flow_controller_
->SendWindowSize());
85 TEST_F(QuicFlowControllerTest
, ReceivingBytes
) {
88 EXPECT_FALSE(flow_controller_
->IsBlocked());
89 EXPECT_FALSE(flow_controller_
->FlowControlViolation());
90 EXPECT_EQ(kInitialSessionFlowControlWindowForTest
,
91 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_
.get()));
93 // Receive some bytes, updating highest received offset, but not enough to
94 // fill flow control receive window.
96 flow_controller_
->UpdateHighestReceivedOffset(1 + receive_window_
/ 2));
97 EXPECT_FALSE(flow_controller_
->FlowControlViolation());
98 EXPECT_EQ((receive_window_
/ 2) - 1,
99 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_
.get()));
101 // Consume enough bytes to send a WINDOW_UPDATE frame.
102 EXPECT_CALL(connection_
, SendWindowUpdate(stream_id_
, ::testing::_
)).Times(1);
104 flow_controller_
->AddBytesConsumed(1 + receive_window_
/ 2);
106 // Result is that once again we have a fully open receive window.
107 EXPECT_FALSE(flow_controller_
->FlowControlViolation());
108 EXPECT_EQ(kInitialSessionFlowControlWindowForTest
,
109 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_
.get()));
112 TEST_F(QuicFlowControllerTest
, OnlySendBlockedFrameOncePerOffset
) {
115 // Test that we don't send duplicate BLOCKED frames. We should only send one
116 // BLOCKED frame at a given send window offset.
117 EXPECT_FALSE(flow_controller_
->IsBlocked());
118 EXPECT_FALSE(flow_controller_
->FlowControlViolation());
119 EXPECT_EQ(send_window_
, flow_controller_
->SendWindowSize());
121 // Send enough bytes to block.
122 flow_controller_
->AddBytesSent(send_window_
);
123 EXPECT_TRUE(flow_controller_
->IsBlocked());
124 EXPECT_EQ(0u, flow_controller_
->SendWindowSize());
126 // Expect that 2 BLOCKED frames should get sent in total.
127 EXPECT_CALL(connection_
, SendBlocked(stream_id_
)).Times(2);
129 // BLOCKED frame should get sent.
130 flow_controller_
->MaybeSendBlocked();
132 // BLOCKED frame should not get sent again until our send offset changes.
133 flow_controller_
->MaybeSendBlocked();
134 flow_controller_
->MaybeSendBlocked();
135 flow_controller_
->MaybeSendBlocked();
136 flow_controller_
->MaybeSendBlocked();
137 flow_controller_
->MaybeSendBlocked();
139 // Update the send window, then send enough bytes to block again.
140 EXPECT_TRUE(flow_controller_
->UpdateSendWindowOffset(2 * send_window_
));
141 EXPECT_FALSE(flow_controller_
->IsBlocked());
142 EXPECT_EQ(send_window_
, flow_controller_
->SendWindowSize());
143 flow_controller_
->AddBytesSent(send_window_
);
144 EXPECT_TRUE(flow_controller_
->IsBlocked());
145 EXPECT_EQ(0u, flow_controller_
->SendWindowSize());
147 // BLOCKED frame should get sent as send offset has changed.
148 flow_controller_
->MaybeSendBlocked();