Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Side by Side Diff: net/server/http_connection_unittest.cc

Issue 296053012: Replace StreamListenSocket with StreamSocket in HttpServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Don't export HttpServer which is built in a static lib Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "net/server/http_connection.h"
mmenke 2014/06/11 16:05:49 nit: Line break after main header.
byungchul 2014/08/05 07:06:19 Done.
6 #include "testing/gtest/include/gtest/gtest.h"
7
8 namespace net {
9
10 class HttpConnectionTest : public testing::Test {
11 protected:
12 // Visibility change.
13 static const int kInitialBufSize =
14 HttpConnection::ReadIOBuffer::kInitialBufSize;
15 static const int kCapacityIncreaseFactor =
16 HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor;
17 static const int kDefaultCapacityLimit =
18 HttpConnection::ReadIOBuffer::kDefaultCapacityLimit;
19 static const int kDefaultTotalSizeLimit =
20 HttpConnection::PendingWriteIOBuffer::kDefaultTotalSizeLimit;
21
22 static scoped_refptr<GrowableIOBuffer> GetBase(
23 scoped_refptr<HttpConnection::ReadIOBuffer> read_buffer) {
24 return read_buffer->base_;
25 }
26
27 static const std::queue<std::string>& GetPendingData(
28 scoped_refptr<HttpConnection::PendingWriteIOBuffer> write_buffer) {
29 return write_buffer->pending_data_;
30 }
31
32 void CheckCapacity(scoped_refptr<HttpConnection::ReadIOBuffer> buffer,
33 int expected_capacity) {
34 EXPECT_EQ(expected_capacity, buffer->GetCapacity());
35 EXPECT_EQ(expected_capacity, buffer->GetUnusedCapacity());
36 EXPECT_EQ(expected_capacity, GetBase(buffer)->capacity());
37 EXPECT_EQ(expected_capacity, GetBase(buffer)->RemainingCapacity());
38 }
39 };
40
41 TEST_F(HttpConnectionTest, ReadIOBuffer_SetCapacity) {
42 scoped_refptr<HttpConnection::ReadIOBuffer> buffer(
43 new HttpConnection::ReadIOBuffer);
44 CheckCapacity(buffer, kInitialBufSize);
45 EXPECT_EQ(0, buffer->GetUnconsumedSize());
46
47 const int kNewCapacity = kInitialBufSize + 128;
48 buffer->SetCapacity(kNewCapacity);
49 CheckCapacity(buffer, kNewCapacity);
50 EXPECT_EQ(0, buffer->GetUnconsumedSize());
51 }
52
53 TEST_F(HttpConnectionTest, ReadIOBuffer_IncreaseCapacity) {
54 scoped_refptr<HttpConnection::ReadIOBuffer> buffer(
55 new HttpConnection::ReadIOBuffer);
56 EXPECT_TRUE(buffer->IncreaseCapacity());
57 CheckCapacity(buffer, kInitialBufSize * kCapacityIncreaseFactor);
58 EXPECT_EQ(0, buffer->GetUnconsumedSize());
59
60 // Increases capacity until it fails.
61 while (buffer->IncreaseCapacity());
62 EXPECT_FALSE(buffer->IncreaseCapacity());
63 EXPECT_EQ(kDefaultCapacityLimit + 0, buffer->capacity_limit());
64 EXPECT_EQ(kDefaultCapacityLimit + 0, buffer->GetCapacity());
65
66 // Enlarge capacity limit.
67 buffer->set_capacity_limit(buffer->capacity_limit() * 2);
68 EXPECT_TRUE(buffer->IncreaseCapacity());
69 EXPECT_EQ(kDefaultCapacityLimit * kCapacityIncreaseFactor,
70 buffer->GetCapacity());
71
72 // Shrink capacity limit. It doesn't change capacity itself.
73 buffer->set_capacity_limit(kDefaultCapacityLimit / 2);
74 EXPECT_FALSE(buffer->IncreaseCapacity());
75 EXPECT_EQ(kDefaultCapacityLimit * kCapacityIncreaseFactor,
76 buffer->GetCapacity());
77 }
78
79 TEST_F(HttpConnectionTest, ReadIOBuffer_DidRead_DidConsume) {
80 scoped_refptr<HttpConnection::ReadIOBuffer> buffer(
81 new HttpConnection::ReadIOBuffer);
82 // Start pointers of all 3 parts are same.
83 const char* initial_data = buffer->data();
84 EXPECT_EQ(initial_data, GetBase(buffer)->StartOfBuffer());
85 EXPECT_EQ(initial_data, buffer->GetUnusedIOBuffer()->data());
86
87 // Reads data.
88 const int read_length = 128;
mmenke 2014/06/11 16:05:49 nit: kReadLength (Same goes for the others)
byungchul 2014/08/05 07:06:18 Done.
89 buffer->DidRead(read_length);
90 // No change in total capacity.
91 EXPECT_EQ(kInitialBufSize + 0, buffer->GetCapacity());
92 // Change in unused capacity because of read data.
93 EXPECT_EQ(kInitialBufSize - read_length, buffer->GetUnusedCapacity());
94 EXPECT_EQ(read_length, buffer->GetUnconsumedSize());
95 // No change in start pointers of consumed and unconsumed part.
96 EXPECT_EQ(initial_data, GetBase(buffer)->StartOfBuffer());
97 EXPECT_EQ(initial_data, buffer->data());
98 // Change in start pointer of unused part.
99 EXPECT_EQ(initial_data + read_length, buffer->GetUnusedIOBuffer()->data());
100
101 // Consumes data partially.
102 const int consumed_length = 32;
103 ASSERT_LT(consumed_length, read_length);
104 buffer->DidConsume(consumed_length);
105 // No change in capacity.
106 EXPECT_EQ(kInitialBufSize + 0, buffer->GetCapacity());
107 EXPECT_EQ(kInitialBufSize - read_length, buffer->GetUnusedCapacity());
108 // Change in unconsumed size.
109 EXPECT_EQ(read_length - consumed_length, buffer->GetUnconsumedSize());
110 // No change in start pointer of consumed part.
111 EXPECT_EQ(initial_data, GetBase(buffer)->StartOfBuffer());
112 // Change in start pointer of unconsumed part.
113 EXPECT_EQ(initial_data + consumed_length, buffer->data());
114 // No change in start pointer of unused part.
115 EXPECT_EQ(initial_data + read_length, buffer->GetUnusedIOBuffer()->data());
116
117 // Reads another data.
mmenke 2014/06/11 16:05:49 nit: "Reads more data."
byungchul 2014/08/05 07:06:19 Done.
118 const int read_length_2 = 64;
119 buffer->DidRead(read_length_2);
120 // No change in total capacity.
121 EXPECT_EQ(kInitialBufSize + 0, buffer->GetCapacity());
122 // Change in unused capacity because of read data.
123 EXPECT_EQ(kInitialBufSize - read_length - read_length_2,
124 buffer->GetUnusedCapacity());
125 EXPECT_EQ(read_length - consumed_length + read_length_2,
126 buffer->GetUnconsumedSize());
127 // No change in start pointers of consumed and unconsumed part.
128 EXPECT_EQ(initial_data, GetBase(buffer)->StartOfBuffer());
129 EXPECT_EQ(initial_data + consumed_length, buffer->data());
130 // Change in start pointer of unused part.
131 EXPECT_EQ(initial_data + read_length + read_length_2,
132 buffer->GetUnusedIOBuffer()->data());
133
134 // Consumes data fully.
135 buffer->DidConsume(read_length - consumed_length + read_length_2);
136 // Capacity reduced because read data was too small comparing to capacity.
137 EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor, buffer->GetCapacity());
138 EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor,
139 buffer->GetUnusedCapacity());
140 // All reverts to initial because no data is left.
141 EXPECT_EQ(0, buffer->GetUnconsumedSize());
142 // Start data could be changed even when capacity is reduced.
143 initial_data = buffer->data();
144 EXPECT_EQ(initial_data, GetBase(buffer)->StartOfBuffer());
145 EXPECT_EQ(initial_data, buffer->GetUnusedIOBuffer()->data());
146 }
mmenke 2014/06/11 16:05:49 Need a test where the buffer length is increased w
byungchul 2014/08/05 07:06:19 Do you mean content itself checking when capacity
147
148 TEST_F(HttpConnectionTest, PendingWriteIOBuffer_Append_DidConsume) {
149 scoped_refptr<HttpConnection::PendingWriteIOBuffer> buffer(
150 new HttpConnection::PendingWriteIOBuffer());
151 EXPECT_TRUE(buffer->IsEmpty());
152 EXPECT_EQ(0, buffer->GetSizeToWrite());
153 EXPECT_EQ(0, buffer->total_size());
154 EXPECT_TRUE(GetPendingData(buffer).empty());
155
156 const std::string data("data to write");
157 EXPECT_TRUE(buffer->Append(data));
158 EXPECT_FALSE(buffer->IsEmpty());
159 EXPECT_EQ(static_cast<int>(data.size()), buffer->GetSizeToWrite());
160 EXPECT_EQ(static_cast<int>(data.size()), buffer->total_size());
161 EXPECT_EQ(1U, GetPendingData(buffer).size());
162
163 const std::string data_2("another data to write");
mmenke 2014/06/11 16:05:50 nit: "more data to write"
byungchul 2014/08/05 07:06:19 Done.
164 EXPECT_TRUE(buffer->Append(data_2));
165 EXPECT_FALSE(buffer->IsEmpty());
166 // No change in size to write.
167 EXPECT_EQ(static_cast<int>(data.size()), buffer->GetSizeToWrite());
168 // Change in total size.
169 EXPECT_EQ(static_cast<int>(data.size() + data_2.size()),
170 buffer->total_size());
171 EXPECT_EQ(2U, GetPendingData(buffer).size());
172
173 // Consumes data partially.
174 const int consumed_length = data.length() - 1;
175 buffer->DidConsume(consumed_length);
176 EXPECT_FALSE(buffer->IsEmpty());
177 // Change in size to write.
178 EXPECT_EQ(static_cast<int>(data.size()) - consumed_length,
179 buffer->GetSizeToWrite());
180 // Change in total size.
181 EXPECT_EQ(static_cast<int>(data.size() + data_2.size()) - consumed_length,
182 buffer->total_size());
183 EXPECT_EQ(2U, GetPendingData(buffer).size());
184
185 // Consumes first data fully.
186 buffer->DidConsume(data.size() - consumed_length);
187 EXPECT_FALSE(buffer->IsEmpty());
188 // Now, size to write is size of data added second.
189 EXPECT_EQ(static_cast<int>(data_2.size()), buffer->GetSizeToWrite());
190 // Change in total size.
191 EXPECT_EQ(static_cast<int>(data_2.size()), buffer->total_size());
192 EXPECT_EQ(1U, GetPendingData(buffer).size());
193
194 // Consumes second data fully.
195 buffer->DidConsume(data_2.size());
196 EXPECT_TRUE(buffer->IsEmpty());
197 EXPECT_EQ(0, buffer->GetSizeToWrite());
198 EXPECT_EQ(0, buffer->total_size());
199 EXPECT_TRUE(GetPendingData(buffer).empty());
200 }
201
202 TEST_F(HttpConnectionTest, PendingWriteIOBuffer_TotalSizeLimit) {
203 scoped_refptr<HttpConnection::PendingWriteIOBuffer> buffer(
204 new HttpConnection::PendingWriteIOBuffer());
205 EXPECT_EQ(kDefaultTotalSizeLimit + 0, buffer->total_size_limit());
206
207 // Sets total size limit very small.
208 buffer->set_total_size_limit(10);
209
210 const int data_length = 4;
211 const std::string data(data_length, 'd');
212 EXPECT_TRUE(buffer->Append(data));
213 EXPECT_EQ(data_length, buffer->total_size());
214 EXPECT_TRUE(buffer->Append(data));
215 EXPECT_EQ(data_length * 2, buffer->total_size());
216
217 // Cannot append more data because it exceeds the limit.
218 EXPECT_FALSE(buffer->Append(data));
219 EXPECT_EQ(data_length * 2, buffer->total_size());
220
221 // Consumes data partially.
222 const int consumed_length = 2;
223 buffer->DidConsume(consumed_length);
224 EXPECT_EQ(data_length * 2 - consumed_length, buffer->total_size());
225
226 // Can add more data.
227 EXPECT_TRUE(buffer->Append(data));
228 EXPECT_EQ(data_length * 3 - consumed_length, buffer->total_size());
229
230 // Cannot append more data because it exceeds the limit.
231 EXPECT_FALSE(buffer->Append(data));
232 EXPECT_EQ(data_length * 3 - consumed_length, buffer->total_size());
233
234 // Enlarge limit.
235 buffer->set_total_size_limit(20);
236 // Can add more data.
237 EXPECT_TRUE(buffer->Append(data));
238 EXPECT_EQ(data_length * 4 - consumed_length, buffer->total_size());
239 }
240
241 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698