2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/io/IOBufQueue.h>
18 #include <folly/Range.h>
20 #include <gflags/gflags.h>
21 #include <gtest/gtest.h>
28 using folly::IOBufQueue;
29 using folly::StringPiece;
32 using std::unique_ptr;
34 // String Comma Length macro for string literals
35 #define SCL(x) (x), sizeof(x) - 1
39 IOBufQueue::Options clOptions;
42 clOptions.cacheChainLength = true;
45 Initializer initializer;
48 stringToIOBuf(const char* s, uint32_t len) {
49 unique_ptr<IOBuf> buf = IOBuf::create(len);
50 memcpy(buf->writableTail(), s, len);
52 return std::move(buf);
55 void checkConsistency(const IOBufQueue& queue) {
56 if (queue.options().cacheChainLength) {
57 size_t len = queue.front() ? queue.front()->computeChainDataLength() : 0;
58 EXPECT_EQ(len, queue.chainLength());
64 TEST(IOBufQueue, Simple) {
65 IOBufQueue queue(clOptions);
66 EXPECT_EQ(nullptr, queue.front());
67 queue.append(SCL(""));
68 EXPECT_EQ(nullptr, queue.front());
69 queue.append(unique_ptr<IOBuf>());
70 EXPECT_EQ(nullptr, queue.front());
72 queue.append(emptyString);
73 EXPECT_EQ(nullptr, queue.front());
76 TEST(IOBufQueue, Append) {
77 IOBufQueue queue(clOptions);
78 queue.append(SCL("Hello"));
79 IOBufQueue queue2(clOptions);
80 queue2.append(SCL(", "));
81 queue2.append(SCL("World"));
82 checkConsistency(queue);
83 checkConsistency(queue2);
84 queue.append(queue2.move());
85 checkConsistency(queue);
86 checkConsistency(queue2);
87 const IOBuf* chain = queue.front();
88 EXPECT_NE((IOBuf*)nullptr, chain);
89 EXPECT_EQ(12, chain->computeChainDataLength());
90 EXPECT_EQ(nullptr, queue2.front());
93 TEST(IOBufQueue, Append2) {
94 IOBufQueue queue(clOptions);
95 queue.append(SCL("Hello"));
96 IOBufQueue queue2(clOptions);
97 queue2.append(SCL(", "));
98 queue2.append(SCL("World"));
99 checkConsistency(queue);
100 checkConsistency(queue2);
101 queue.append(queue2);
102 checkConsistency(queue);
103 checkConsistency(queue2);
104 const IOBuf* chain = queue.front();
105 EXPECT_NE((IOBuf*)nullptr, chain);
106 EXPECT_EQ(12, chain->computeChainDataLength());
107 EXPECT_EQ(nullptr, queue2.front());
110 TEST(IOBufQueue, AppendStringPiece) {
111 std::string s("Hello, World");
112 IOBufQueue queue(clOptions);
113 IOBufQueue queue2(clOptions);
114 queue.append(s.data(), s.length());
116 checkConsistency(queue);
117 checkConsistency(queue2);
118 const IOBuf* chain = queue.front();
119 const IOBuf* chain2 = queue2.front();
120 EXPECT_EQ(s.length(), chain->computeChainDataLength());
121 EXPECT_EQ(s.length(), chain2->computeChainDataLength());
122 EXPECT_EQ(0, memcmp(chain->data(), chain2->data(), s.length()));
125 TEST(IOBufQueue, Split) {
126 IOBufQueue queue(clOptions);
127 queue.append(stringToIOBuf(SCL("Hello")));
128 queue.append(stringToIOBuf(SCL(",")));
129 queue.append(stringToIOBuf(SCL(" ")));
130 queue.append(stringToIOBuf(SCL("")));
131 queue.append(stringToIOBuf(SCL("World")));
132 checkConsistency(queue);
133 EXPECT_EQ(12, queue.front()->computeChainDataLength());
135 unique_ptr<IOBuf> prefix(queue.split(1));
136 checkConsistency(queue);
137 EXPECT_EQ(1, prefix->computeChainDataLength());
138 EXPECT_EQ(11, queue.front()->computeChainDataLength());
139 prefix = queue.split(2);
140 checkConsistency(queue);
141 EXPECT_EQ(2, prefix->computeChainDataLength());
142 EXPECT_EQ(9, queue.front()->computeChainDataLength());
143 prefix = queue.split(3);
144 checkConsistency(queue);
145 EXPECT_EQ(3, prefix->computeChainDataLength());
146 EXPECT_EQ(6, queue.front()->computeChainDataLength());
147 prefix = queue.split(1);
148 checkConsistency(queue);
149 EXPECT_EQ(1, prefix->computeChainDataLength());
150 EXPECT_EQ(5, queue.front()->computeChainDataLength());
151 prefix = queue.split(5);
152 checkConsistency(queue);
153 EXPECT_EQ(5, prefix->computeChainDataLength());
154 EXPECT_EQ((IOBuf*)nullptr, queue.front());
156 queue.append(stringToIOBuf(SCL("Hello,")));
157 queue.append(stringToIOBuf(SCL(" World")));
158 checkConsistency(queue);
159 bool exceptionFired = false;
160 EXPECT_THROW({prefix = queue.split(13);}, std::underflow_error);
161 checkConsistency(queue);
164 TEST(IOBufQueue, Preallocate) {
165 IOBufQueue queue(clOptions);
166 queue.append(string("Hello"));
167 pair<void*,uint32_t> writable = queue.preallocate(2, 64, 64);
168 checkConsistency(queue);
169 EXPECT_NE((void*)nullptr, writable.first);
170 EXPECT_LE(2, writable.second);
171 EXPECT_GE(64, writable.second);
172 memcpy(writable.first, SCL(", "));
173 queue.postallocate(2);
174 checkConsistency(queue);
175 EXPECT_EQ(7, queue.front()->computeChainDataLength());
176 queue.append(SCL("World"));
177 checkConsistency(queue);
178 EXPECT_EQ(12, queue.front()->computeChainDataLength());
179 // There are not 2048 bytes available, this will alloc a new buf
180 writable = queue.preallocate(2048, 4096);
181 checkConsistency(queue);
182 EXPECT_LE(2048, writable.second);
183 // IOBuf allocates more than newAllocationSize, and we didn't cap it
184 EXPECT_GE(writable.second, 4096);
185 queue.postallocate(writable.second);
186 // queue has no empty space, make sure we allocate at least min, even if
187 // newAllocationSize < min
188 writable = queue.preallocate(1024, 1, 1024);
189 checkConsistency(queue);
190 EXPECT_EQ(1024, writable.second);
193 TEST(IOBufQueue, Wrap) {
194 IOBufQueue queue(clOptions);
195 const char* buf = "hello world goodbye";
196 size_t len = strlen(buf);
197 queue.wrapBuffer(buf, len, 6);
198 auto iob = queue.move();
199 EXPECT_EQ((len - 1) / 6 + 1, iob->countChainElements());
202 EXPECT_EQ(StringPiece(buf),
203 StringPiece(reinterpret_cast<const char*>(iob->data()),
207 TEST(IOBufQueue, Trim) {
208 IOBufQueue queue(clOptions);
209 unique_ptr<IOBuf> a = IOBuf::create(4);
211 queue.append(std::move(a));
212 checkConsistency(queue);
213 a = IOBuf::create(6);
215 queue.append(std::move(a));
216 checkConsistency(queue);
217 a = IOBuf::create(8);
219 queue.append(std::move(a));
220 checkConsistency(queue);
221 a = IOBuf::create(10);
223 queue.append(std::move(a));
224 checkConsistency(queue);
226 EXPECT_EQ(4, queue.front()->countChainElements());
227 EXPECT_EQ(28, queue.front()->computeChainDataLength());
228 EXPECT_EQ(4, queue.front()->length());
231 checkConsistency(queue);
232 EXPECT_EQ(4, queue.front()->countChainElements());
233 EXPECT_EQ(27, queue.front()->computeChainDataLength());
234 EXPECT_EQ(3, queue.front()->length());
237 checkConsistency(queue);
238 EXPECT_EQ(3, queue.front()->countChainElements());
239 EXPECT_EQ(22, queue.front()->computeChainDataLength());
240 EXPECT_EQ(4, queue.front()->length());
243 checkConsistency(queue);
244 EXPECT_EQ(3, queue.front()->countChainElements());
245 EXPECT_EQ(21, queue.front()->computeChainDataLength());
246 EXPECT_EQ(9, queue.front()->prev()->length());
249 checkConsistency(queue);
250 EXPECT_EQ(1, queue.front()->countChainElements());
251 EXPECT_EQ(1, queue.front()->computeChainDataLength());
252 EXPECT_EQ(1, queue.front()->prev()->length());
255 checkConsistency(queue);
256 EXPECT_EQ(nullptr, queue.front());
258 EXPECT_THROW(queue.trimStart(2), std::underflow_error);
259 checkConsistency(queue);
261 EXPECT_THROW(queue.trimEnd(30), std::underflow_error);
262 checkConsistency(queue);
265 TEST(IOBufQueue, TrimPack) {
266 IOBufQueue queue(clOptions);
267 unique_ptr<IOBuf> a = IOBuf::create(64);
269 queue.append(std::move(a), true);
270 checkConsistency(queue);
271 a = IOBuf::create(6);
273 queue.append(std::move(a), true);
274 checkConsistency(queue);
275 a = IOBuf::create(8);
277 queue.append(std::move(a), true);
278 checkConsistency(queue);
279 a = IOBuf::create(10);
281 queue.append(std::move(a), true);
282 checkConsistency(queue);
284 EXPECT_EQ(1, queue.front()->countChainElements());
285 EXPECT_EQ(28, queue.front()->computeChainDataLength());
286 EXPECT_EQ(28, queue.front()->length());
289 checkConsistency(queue);
290 EXPECT_EQ(1, queue.front()->countChainElements());
291 EXPECT_EQ(27, queue.front()->computeChainDataLength());
292 EXPECT_EQ(27, queue.front()->length());
295 checkConsistency(queue);
296 EXPECT_EQ(1, queue.front()->countChainElements());
297 EXPECT_EQ(22, queue.front()->computeChainDataLength());
298 EXPECT_EQ(22, queue.front()->length());
301 checkConsistency(queue);
302 EXPECT_EQ(1, queue.front()->countChainElements());
303 EXPECT_EQ(21, queue.front()->computeChainDataLength());
304 EXPECT_EQ(21, queue.front()->prev()->length());
307 checkConsistency(queue);
308 EXPECT_EQ(1, queue.front()->countChainElements());
309 EXPECT_EQ(1, queue.front()->computeChainDataLength());
310 EXPECT_EQ(1, queue.front()->prev()->length());
313 checkConsistency(queue);
314 EXPECT_EQ(nullptr, queue.front());
316 EXPECT_THROW(queue.trimStart(2), std::underflow_error);
317 checkConsistency(queue);
319 EXPECT_THROW(queue.trimEnd(30), std::underflow_error);
320 checkConsistency(queue);
323 TEST(IOBufQueue, Prepend) {
324 folly::IOBufQueue queue;
326 auto buf = folly::IOBuf::create(10);
328 queue.append(std::move(buf));
330 queue.append(SCL(" World"));
331 queue.prepend(SCL("Hello"));
333 EXPECT_THROW(queue.prepend(SCL("x")), std::overflow_error);
335 auto out = queue.move();
337 EXPECT_EQ("Hello World",
338 StringPiece(reinterpret_cast<const char*>(out->data()),
342 TEST(IOBufQueue, PopFirst) {
343 IOBufQueue queue(IOBufQueue::cacheChainLength());
344 const char * strings[] = {
352 const size_t numStrings=sizeof(strings)/sizeof(*strings);
353 size_t chainLength = 0;
354 for(ssize_t i=0; i<numStrings; ++i) {
355 queue.append(stringToIOBuf(strings[i], strlen(strings[i])));
356 checkConsistency(queue);
357 chainLength += strlen(strings[i]);
360 unique_ptr<IOBuf> first;
361 for(ssize_t i=0; i<numStrings; ++i) {
362 checkConsistency(queue);
363 EXPECT_EQ(chainLength, queue.front()->computeChainDataLength());
364 EXPECT_EQ(chainLength, queue.chainLength());
365 first = queue.pop_front();
366 chainLength-=strlen(strings[i]);
367 EXPECT_EQ(strlen(strings[i]), first->computeChainDataLength());
369 checkConsistency(queue);
370 EXPECT_EQ(chainLength, queue.chainLength());
372 EXPECT_EQ((IOBuf*)nullptr, queue.front());
373 first = queue.pop_front();
374 EXPECT_EQ((IOBuf*)nullptr, first.get());
376 checkConsistency(queue);
377 EXPECT_EQ((IOBuf*)nullptr, queue.front());
378 EXPECT_EQ(0, queue.chainLength());
381 int main(int argc, char** argv) {
382 testing::InitGoogleTest(&argc, argv);
383 gflags::ParseCommandLineFlags(&argc, &argv, true);
385 return RUN_ALL_TESTS();