Re-land: C++ readability review
[chromium-blink-merge.git] / net / quic / quic_flow_controller_test.cc
blob4562a142d3b98e989045e8f973742afa13367612
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"
16 namespace net {
17 namespace test {
19 class QuicFlowControllerTest : public ::testing::Test {
20 public:
21 QuicFlowControllerTest()
22 : stream_id_(1234),
23 send_window_(kInitialSessionFlowControlWindowForTest),
24 receive_window_(kInitialSessionFlowControlWindowForTest),
25 max_receive_window_(kInitialSessionFlowControlWindowForTest),
26 connection_(Perspective::IS_CLIENT) {}
28 void Initialize() {
29 flow_controller_.reset(new QuicFlowController(
30 &connection_, stream_id_, Perspective::IS_CLIENT, send_window_,
31 receive_window_, max_receive_window_));
34 protected:
35 QuicStreamId stream_id_;
36 QuicByteCount send_window_;
37 QuicByteCount receive_window_;
38 QuicByteCount max_receive_window_;
39 scoped_ptr<QuicFlowController> flow_controller_;
40 MockConnection connection_;
43 TEST_F(QuicFlowControllerTest, SendingBytes) {
44 Initialize();
46 EXPECT_FALSE(flow_controller_->IsBlocked());
47 EXPECT_FALSE(flow_controller_->FlowControlViolation());
48 EXPECT_EQ(send_window_, flow_controller_->SendWindowSize());
50 // Send some bytes, but not enough to block.
51 flow_controller_->AddBytesSent(send_window_ / 2);
52 EXPECT_FALSE(flow_controller_->IsBlocked());
53 EXPECT_EQ(send_window_ / 2, flow_controller_->SendWindowSize());
55 // Send enough bytes to block.
56 flow_controller_->AddBytesSent(send_window_ / 2);
57 EXPECT_TRUE(flow_controller_->IsBlocked());
58 EXPECT_EQ(0u, flow_controller_->SendWindowSize());
60 // BLOCKED frame should get sent.
61 EXPECT_CALL(connection_, SendBlocked(stream_id_)).Times(1);
62 flow_controller_->MaybeSendBlocked();
64 // Update the send window, and verify this has unblocked.
65 EXPECT_TRUE(flow_controller_->UpdateSendWindowOffset(2 * send_window_));
66 EXPECT_FALSE(flow_controller_->IsBlocked());
67 EXPECT_EQ(send_window_, flow_controller_->SendWindowSize());
69 // Updating with a smaller offset doesn't change anything.
70 EXPECT_FALSE(flow_controller_->UpdateSendWindowOffset(send_window_ / 10));
71 EXPECT_EQ(send_window_, flow_controller_->SendWindowSize());
73 // Try to send more bytes, violating flow control.
74 EXPECT_CALL(connection_,
75 SendConnectionClose(QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA));
76 EXPECT_DFATAL(
77 flow_controller_->AddBytesSent(send_window_ * 10),
78 base::StringPrintf("Trying to send an extra %" PRIu64 " bytes",
79 send_window_ * 10));
80 EXPECT_TRUE(flow_controller_->IsBlocked());
81 EXPECT_EQ(0u, flow_controller_->SendWindowSize());
84 TEST_F(QuicFlowControllerTest, ReceivingBytes) {
85 Initialize();
87 EXPECT_FALSE(flow_controller_->IsBlocked());
88 EXPECT_FALSE(flow_controller_->FlowControlViolation());
89 EXPECT_EQ(kInitialSessionFlowControlWindowForTest,
90 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
92 // Receive some bytes, updating highest received offset, but not enough to
93 // fill flow control receive window.
94 EXPECT_TRUE(
95 flow_controller_->UpdateHighestReceivedOffset(1 + receive_window_ / 2));
96 EXPECT_FALSE(flow_controller_->FlowControlViolation());
97 EXPECT_EQ((receive_window_ / 2) - 1,
98 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
100 // Consume enough bytes to send a WINDOW_UPDATE frame.
101 EXPECT_CALL(connection_, SendWindowUpdate(stream_id_, ::testing::_)).Times(1);
103 flow_controller_->AddBytesConsumed(1 + receive_window_ / 2);
105 // Result is that once again we have a fully open receive window.
106 EXPECT_FALSE(flow_controller_->FlowControlViolation());
107 EXPECT_EQ(kInitialSessionFlowControlWindowForTest,
108 QuicFlowControllerPeer::ReceiveWindowSize(flow_controller_.get()));
111 TEST_F(QuicFlowControllerTest, OnlySendBlockedFrameOncePerOffset) {
112 Initialize();
114 // Test that we don't send duplicate BLOCKED frames. We should only send one
115 // BLOCKED frame at a given send window offset.
116 EXPECT_FALSE(flow_controller_->IsBlocked());
117 EXPECT_FALSE(flow_controller_->FlowControlViolation());
118 EXPECT_EQ(send_window_, flow_controller_->SendWindowSize());
120 // Send enough bytes to block.
121 flow_controller_->AddBytesSent(send_window_);
122 EXPECT_TRUE(flow_controller_->IsBlocked());
123 EXPECT_EQ(0u, flow_controller_->SendWindowSize());
125 // Expect that 2 BLOCKED frames should get sent in total.
126 EXPECT_CALL(connection_, SendBlocked(stream_id_)).Times(2);
128 // BLOCKED frame should get sent.
129 flow_controller_->MaybeSendBlocked();
131 // BLOCKED frame should not get sent again until our send offset changes.
132 flow_controller_->MaybeSendBlocked();
133 flow_controller_->MaybeSendBlocked();
134 flow_controller_->MaybeSendBlocked();
135 flow_controller_->MaybeSendBlocked();
136 flow_controller_->MaybeSendBlocked();
138 // Update the send window, then send enough bytes to block again.
139 EXPECT_TRUE(flow_controller_->UpdateSendWindowOffset(2 * send_window_));
140 EXPECT_FALSE(flow_controller_->IsBlocked());
141 EXPECT_EQ(send_window_, flow_controller_->SendWindowSize());
142 flow_controller_->AddBytesSent(send_window_);
143 EXPECT_TRUE(flow_controller_->IsBlocked());
144 EXPECT_EQ(0u, flow_controller_->SendWindowSize());
146 // BLOCKED frame should get sent as send offset has changed.
147 flow_controller_->MaybeSendBlocked();
150 } // namespace test
151 } // namespace net