2 * Copyright 2016 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>
23 #include <folly/Range.h>
24 #include <folly/portability/GTest.h>
27 using folly::IOBufQueue;
28 using folly::StringPiece;
31 using std::unique_ptr;
33 // String Comma Length macro for string literals
34 #define SCL(x) (x), sizeof(x) - 1
38 IOBufQueue::Options clOptions;
41 clOptions.cacheChainLength = true;
44 Initializer initializer;
47 stringToIOBuf(const char* s, uint32_t len) {
48 unique_ptr<IOBuf> buf = IOBuf::create(len);
49 memcpy(buf->writableTail(), s, len);
54 void checkConsistency(const IOBufQueue& queue) {
55 if (queue.options().cacheChainLength) {
56 size_t len = queue.front() ? queue.front()->computeChainDataLength() : 0;
57 EXPECT_EQ(len, queue.chainLength());
63 TEST(IOBufQueue, Simple) {
64 IOBufQueue queue(clOptions);
65 EXPECT_EQ(nullptr, queue.front());
66 queue.append(SCL(""));
67 EXPECT_EQ(nullptr, queue.front());
68 queue.append(unique_ptr<IOBuf>());
69 EXPECT_EQ(nullptr, queue.front());
71 queue.append(emptyString);
72 EXPECT_EQ(nullptr, queue.front());
75 TEST(IOBufQueue, Append) {
76 IOBufQueue queue(clOptions);
77 queue.append(SCL("Hello"));
78 IOBufQueue queue2(clOptions);
79 queue2.append(SCL(", "));
80 queue2.append(SCL("World"));
81 checkConsistency(queue);
82 checkConsistency(queue2);
83 queue.append(queue2.move());
84 checkConsistency(queue);
85 checkConsistency(queue2);
86 const IOBuf* chain = queue.front();
87 EXPECT_NE((IOBuf*)nullptr, chain);
88 EXPECT_EQ(12, chain->computeChainDataLength());
89 EXPECT_EQ(nullptr, queue2.front());
92 TEST(IOBufQueue, Append2) {
93 IOBufQueue queue(clOptions);
94 queue.append(SCL("Hello"));
95 IOBufQueue queue2(clOptions);
96 queue2.append(SCL(", "));
97 queue2.append(SCL("World"));
98 checkConsistency(queue);
99 checkConsistency(queue2);
100 queue.append(queue2);
101 checkConsistency(queue);
102 checkConsistency(queue2);
103 const IOBuf* chain = queue.front();
104 EXPECT_NE((IOBuf*)nullptr, chain);
105 EXPECT_EQ(12, chain->computeChainDataLength());
106 EXPECT_EQ(nullptr, queue2.front());
109 TEST(IOBufQueue, AppendStringPiece) {
110 std::string s("Hello, World");
111 IOBufQueue queue(clOptions);
112 IOBufQueue queue2(clOptions);
113 queue.append(s.data(), s.length());
115 checkConsistency(queue);
116 checkConsistency(queue2);
117 const IOBuf* chain = queue.front();
118 const IOBuf* chain2 = queue2.front();
119 EXPECT_EQ(s.length(), chain->computeChainDataLength());
120 EXPECT_EQ(s.length(), chain2->computeChainDataLength());
121 EXPECT_EQ(0, memcmp(chain->data(), chain2->data(), s.length()));
124 TEST(IOBufQueue, Split) {
125 IOBufQueue queue(clOptions);
126 queue.append(stringToIOBuf(SCL("Hello")));
127 queue.append(stringToIOBuf(SCL(",")));
128 queue.append(stringToIOBuf(SCL(" ")));
129 queue.append(stringToIOBuf(SCL("")));
130 queue.append(stringToIOBuf(SCL("World")));
131 checkConsistency(queue);
132 EXPECT_EQ(12, queue.front()->computeChainDataLength());
134 unique_ptr<IOBuf> prefix(queue.split(1));
135 checkConsistency(queue);
136 EXPECT_EQ(1, prefix->computeChainDataLength());
137 EXPECT_EQ(11, queue.front()->computeChainDataLength());
138 prefix = queue.split(2);
139 checkConsistency(queue);
140 EXPECT_EQ(2, prefix->computeChainDataLength());
141 EXPECT_EQ(9, queue.front()->computeChainDataLength());
142 prefix = queue.split(3);
143 checkConsistency(queue);
144 EXPECT_EQ(3, prefix->computeChainDataLength());
145 EXPECT_EQ(6, queue.front()->computeChainDataLength());
146 prefix = queue.split(1);
147 checkConsistency(queue);
148 EXPECT_EQ(1, prefix->computeChainDataLength());
149 EXPECT_EQ(5, queue.front()->computeChainDataLength());
150 prefix = queue.split(5);
151 checkConsistency(queue);
152 EXPECT_EQ(5, prefix->computeChainDataLength());
153 EXPECT_EQ((IOBuf*)nullptr, queue.front());
155 queue.append(stringToIOBuf(SCL("Hello,")));
156 queue.append(stringToIOBuf(SCL(" World")));
157 checkConsistency(queue);
158 EXPECT_THROW({prefix = queue.split(13);}, std::underflow_error);
159 checkConsistency(queue);
162 TEST(IOBufQueue, Preallocate) {
163 IOBufQueue queue(clOptions);
164 queue.append(string("Hello"));
165 pair<void*,uint32_t> writable = queue.preallocate(2, 64, 64);
166 checkConsistency(queue);
167 EXPECT_NE((void*)nullptr, writable.first);
168 EXPECT_LE(2, writable.second);
169 EXPECT_GE(64, writable.second);
170 memcpy(writable.first, SCL(", "));
171 queue.postallocate(2);
172 checkConsistency(queue);
173 EXPECT_EQ(7, queue.front()->computeChainDataLength());
174 queue.append(SCL("World"));
175 checkConsistency(queue);
176 EXPECT_EQ(12, queue.front()->computeChainDataLength());
177 // There are not 2048 bytes available, this will alloc a new buf
178 writable = queue.preallocate(2048, 4096);
179 checkConsistency(queue);
180 EXPECT_LE(2048, writable.second);
181 // IOBuf allocates more than newAllocationSize, and we didn't cap it
182 EXPECT_GE(writable.second, 4096);
183 queue.postallocate(writable.second);
184 // queue has no empty space, make sure we allocate at least min, even if
185 // newAllocationSize < min
186 writable = queue.preallocate(1024, 1, 1024);
187 checkConsistency(queue);
188 EXPECT_EQ(1024, writable.second);
191 TEST(IOBufQueue, Wrap) {
192 IOBufQueue queue(clOptions);
193 const char* buf = "hello world goodbye";
194 size_t len = strlen(buf);
195 queue.wrapBuffer(buf, len, 6);
196 auto iob = queue.move();
197 EXPECT_EQ((len - 1) / 6 + 1, iob->countChainElements());
200 EXPECT_EQ(StringPiece(buf),
201 StringPiece(reinterpret_cast<const char*>(iob->data()),
205 TEST(IOBufQueue, Trim) {
206 IOBufQueue queue(clOptions);
207 unique_ptr<IOBuf> a = IOBuf::create(4);
209 queue.append(std::move(a));
210 checkConsistency(queue);
211 a = IOBuf::create(6);
213 queue.append(std::move(a));
214 checkConsistency(queue);
215 a = IOBuf::create(8);
217 queue.append(std::move(a));
218 checkConsistency(queue);
219 a = IOBuf::create(10);
221 queue.append(std::move(a));
222 checkConsistency(queue);
224 EXPECT_EQ(4, queue.front()->countChainElements());
225 EXPECT_EQ(28, queue.front()->computeChainDataLength());
226 EXPECT_EQ(4, queue.front()->length());
229 checkConsistency(queue);
230 EXPECT_EQ(4, queue.front()->countChainElements());
231 EXPECT_EQ(27, queue.front()->computeChainDataLength());
232 EXPECT_EQ(3, queue.front()->length());
235 checkConsistency(queue);
236 EXPECT_EQ(3, queue.front()->countChainElements());
237 EXPECT_EQ(22, queue.front()->computeChainDataLength());
238 EXPECT_EQ(4, queue.front()->length());
241 checkConsistency(queue);
242 EXPECT_EQ(3, queue.front()->countChainElements());
243 EXPECT_EQ(21, queue.front()->computeChainDataLength());
244 EXPECT_EQ(9, queue.front()->prev()->length());
247 checkConsistency(queue);
248 EXPECT_EQ(1, queue.front()->countChainElements());
249 EXPECT_EQ(1, queue.front()->computeChainDataLength());
250 EXPECT_EQ(1, queue.front()->prev()->length());
253 checkConsistency(queue);
254 EXPECT_EQ(nullptr, queue.front());
256 EXPECT_THROW(queue.trimStart(2), std::underflow_error);
257 checkConsistency(queue);
259 EXPECT_THROW(queue.trimEnd(30), std::underflow_error);
260 checkConsistency(queue);
263 TEST(IOBufQueue, TrimPack) {
264 IOBufQueue queue(clOptions);
265 unique_ptr<IOBuf> a = IOBuf::create(64);
267 queue.append(std::move(a), true);
268 checkConsistency(queue);
269 a = IOBuf::create(6);
271 queue.append(std::move(a), true);
272 checkConsistency(queue);
273 a = IOBuf::create(8);
275 queue.append(std::move(a), true);
276 checkConsistency(queue);
277 a = IOBuf::create(10);
279 queue.append(std::move(a), true);
280 checkConsistency(queue);
282 EXPECT_EQ(1, queue.front()->countChainElements());
283 EXPECT_EQ(28, queue.front()->computeChainDataLength());
284 EXPECT_EQ(28, queue.front()->length());
287 checkConsistency(queue);
288 EXPECT_EQ(1, queue.front()->countChainElements());
289 EXPECT_EQ(27, queue.front()->computeChainDataLength());
290 EXPECT_EQ(27, queue.front()->length());
293 checkConsistency(queue);
294 EXPECT_EQ(1, queue.front()->countChainElements());
295 EXPECT_EQ(22, queue.front()->computeChainDataLength());
296 EXPECT_EQ(22, queue.front()->length());
299 checkConsistency(queue);
300 EXPECT_EQ(1, queue.front()->countChainElements());
301 EXPECT_EQ(21, queue.front()->computeChainDataLength());
302 EXPECT_EQ(21, queue.front()->prev()->length());
305 checkConsistency(queue);
306 EXPECT_EQ(1, queue.front()->countChainElements());
307 EXPECT_EQ(1, queue.front()->computeChainDataLength());
308 EXPECT_EQ(1, queue.front()->prev()->length());
311 checkConsistency(queue);
312 EXPECT_EQ(nullptr, queue.front());
314 EXPECT_THROW(queue.trimStart(2), std::underflow_error);
315 checkConsistency(queue);
317 EXPECT_THROW(queue.trimEnd(30), std::underflow_error);
318 checkConsistency(queue);
321 TEST(IOBufQueue, Prepend) {
322 folly::IOBufQueue queue;
324 auto buf = folly::IOBuf::create(10);
326 queue.append(std::move(buf));
328 queue.append(SCL(" World"));
329 queue.prepend(SCL("Hello"));
331 EXPECT_THROW(queue.prepend(SCL("x")), std::overflow_error);
333 auto out = queue.move();
335 EXPECT_EQ("Hello World",
336 StringPiece(reinterpret_cast<const char*>(out->data()),
340 TEST(IOBufQueue, PopFirst) {
341 IOBufQueue queue(IOBufQueue::cacheChainLength());
342 const char * strings[] = {
350 const size_t numStrings=sizeof(strings)/sizeof(*strings);
351 size_t chainLength = 0;
352 for(size_t i = 0; i < numStrings; ++i) {
353 queue.append(stringToIOBuf(strings[i], strlen(strings[i])));
354 checkConsistency(queue);
355 chainLength += strlen(strings[i]);
358 unique_ptr<IOBuf> first;
359 for(size_t i = 0; i < numStrings; ++i) {
360 checkConsistency(queue);
361 EXPECT_EQ(chainLength, queue.front()->computeChainDataLength());
362 EXPECT_EQ(chainLength, queue.chainLength());
363 first = queue.pop_front();
364 chainLength-=strlen(strings[i]);
365 EXPECT_EQ(strlen(strings[i]), first->computeChainDataLength());
367 checkConsistency(queue);
368 EXPECT_EQ(chainLength, queue.chainLength());
370 EXPECT_EQ((IOBuf*)nullptr, queue.front());
371 first = queue.pop_front();
372 EXPECT_EQ((IOBuf*)nullptr, first.get());
374 checkConsistency(queue);
375 EXPECT_EQ((IOBuf*)nullptr, queue.front());
376 EXPECT_EQ(0, queue.chainLength());
379 TEST(IOBufQueue, AppendToString) {
381 queue.append("hello ", 6);
382 queue.append("world", 5);
384 queue.appendToString(s);
385 EXPECT_EQ("hello world", s);
388 TEST(IOBufQueue, Gather) {
391 queue.append(stringToIOBuf(SCL("hello ")));
392 queue.append(stringToIOBuf(SCL("world")));
394 EXPECT_EQ(queue.front()->length(), 6);
396 EXPECT_EQ(queue.front()->length(), 11);
399 reinterpret_cast<const char*>(queue.front()->data()),
400 queue.front()->length());
401 EXPECT_EQ("hello world", s);