-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathiobuf_unittest.cpp
More file actions
2253 lines (1996 loc) · 70.5 KB
/
iobuf_unittest.cpp
File metadata and controls
2253 lines (1996 loc) · 70.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <gtest/gtest.h>
#include <sys/types.h>
#include <sys/socket.h> // socketpair
#include <errno.h> // errno
#include <fcntl.h> // O_RDONLY
#include <stdlib.h>
#include <memory>
#include <cstring>
#include <butil/files/temp_file.h> // TempFile
#include <butil/containers/flat_map.h>
#include <butil/macros.h>
#include <butil/time.h> // Timer
#include <butil/fd_utility.h> // make_non_blocking
#include <butil/iobuf.h>
#include <butil/single_iobuf.h>
#include <butil/logging.h>
#include <butil/fd_guard.h>
#include <butil/errno.h>
#include <butil/fast_rand.h>
#include "iobuf.pb.h"
namespace butil {
namespace iobuf {
extern void* (*blockmem_allocate)(size_t);
extern void (*blockmem_deallocate)(void*);
extern void reset_blockmem_allocate_and_deallocate();
extern int32_t block_shared_count(butil::IOBuf::Block const* b);
extern uint32_t block_cap(butil::IOBuf::Block const* b);
extern IOBuf::Block* get_tls_block_head();
extern int get_tls_block_count();
extern void remove_tls_block_chain();
extern IOBuf::Block* acquire_tls_block();
extern IOBuf::Block* share_tls_block();
extern void release_tls_block_chain(IOBuf::Block* b);
extern uint32_t block_cap(IOBuf::Block const* b);
extern uint32_t block_size(IOBuf::Block const* b);
extern IOBuf::Block* get_portal_next(IOBuf::Block const* b);
}
}
namespace {
const size_t BLOCK_OVERHEAD = 32; //impl dependent
const size_t DEFAULT_PAYLOAD = butil::IOBuf::DEFAULT_BLOCK_SIZE - BLOCK_OVERHEAD;
void check_tls_block() {
ASSERT_EQ((butil::IOBuf::Block*)NULL, butil::iobuf::get_tls_block_head());
printf("tls_block of butil::IOBuf was deleted\n");
}
const int ALLOW_UNUSED check_dummy = butil::thread_atexit(check_tls_block);
static butil::FlatSet<void*> s_set;
void* debug_block_allocate(size_t block_size) {
void* b = operator new (block_size, std::nothrow);
s_set.insert(b);
return b;
}
void debug_block_deallocate(void* b) {
if (1ul != s_set.erase(b)) {
ASSERT_TRUE(false) << "Bad block=" << b;
} else {
operator delete(b);
}
}
inline bool is_debug_allocator_enabled() {
return (butil::iobuf::blockmem_allocate == debug_block_allocate);
}
void install_debug_allocator() {
if (!is_debug_allocator_enabled()) {
butil::iobuf::remove_tls_block_chain();
s_set.init(1024);
butil::iobuf::blockmem_allocate = debug_block_allocate;
butil::iobuf::blockmem_deallocate = debug_block_deallocate;
LOG(INFO) << "<Installed debug create/destroy>";
}
}
void show_prof_and_rm(const char* bin_name, const char* filename, size_t topn) {
char cmd[1024];
if (topn != 0) {
snprintf(cmd, sizeof(cmd), "if [ -e %s ] ; then CPUPROFILE_FREQUENCY=1000 ./pprof --text %s %s | head -%lu; rm -f %s; fi", filename, bin_name, filename, topn+1, filename);
} else {
snprintf(cmd, sizeof(cmd), "if [ -e %s ] ; then CPUPROFILE_FREQUENCY=1000 ./pprof --text %s %s; rm -f %s; fi", filename, bin_name, filename, filename);
}
ASSERT_EQ(0, system(cmd));
}
static void check_memory_leak() {
if (is_debug_allocator_enabled()) {
butil::IOBuf::Block* p = butil::iobuf::get_tls_block_head();
size_t n = 0;
while (p) {
ASSERT_TRUE(s_set.seek(p)) << "Memory leak: " << p;
p = butil::iobuf::get_portal_next(p);
++n;
}
ASSERT_EQ(n, s_set.size());
ASSERT_EQ(n, (size_t)butil::iobuf::get_tls_block_count());
}
}
class IOBufTest : public ::testing::Test{
protected:
IOBufTest(){};
virtual ~IOBufTest(){};
virtual void SetUp() {
};
virtual void TearDown() {
check_memory_leak();
};
};
std::string to_str(const butil::IOBuf& p) {
return p.to_string();
}
TEST_F(IOBufTest, append_zero) {
int fds[2];
ASSERT_EQ(0, pipe(fds));
butil::IOPortal p;
ASSERT_EQ(0, p.append_from_file_descriptor(fds[0], 0));
ASSERT_EQ(0, close(fds[0]));
ASSERT_EQ(0, close(fds[1]));
}
TEST_F(IOBufTest, pop_front) {
install_debug_allocator();
butil::IOBuf buf;
ASSERT_EQ(0UL, buf.pop_front(1)); // nothing happened
std::string s = "hello";
buf.append(s);
ASSERT_EQ(s, to_str(buf));
ASSERT_EQ(0UL, buf.pop_front(0)); // nothing happened
ASSERT_EQ(s, to_str(buf));
ASSERT_EQ(1UL, buf.pop_front(1));
s.erase(0, 1);
ASSERT_EQ(s, to_str(buf));
ASSERT_EQ(s.length(), buf.length());
ASSERT_FALSE(buf.empty());
ASSERT_EQ(s.length(), buf.pop_front(INT_MAX));
s.clear();
ASSERT_EQ(s, to_str(buf));
ASSERT_EQ(0UL, buf.length());
ASSERT_TRUE(buf.empty());
for (size_t i = 0; i < DEFAULT_PAYLOAD * 3/2; ++i) {
s.push_back(i);
}
buf.append(s);
ASSERT_EQ(1UL, buf.pop_front(1));
s.erase(0, 1);
ASSERT_EQ(s, to_str(buf));
ASSERT_EQ(s.length(), buf.length());
ASSERT_FALSE(buf.empty());
ASSERT_EQ(s.length(), buf.pop_front(INT_MAX));
s.clear();
ASSERT_EQ(s, to_str(buf));
ASSERT_EQ(0UL, buf.length());
ASSERT_TRUE(buf.empty());
}
TEST_F(IOBufTest, pop_back) {
install_debug_allocator();
butil::IOBuf buf;
ASSERT_EQ(0UL, buf.pop_back(1)); // nothing happened
std::string s = "hello";
buf.append(s);
ASSERT_EQ(s, to_str(buf));
ASSERT_EQ(0UL, buf.pop_back(0)); // nothing happened
ASSERT_EQ(s, to_str(buf));
ASSERT_EQ(1UL, buf.pop_back(1));
s.resize(s.size() - 1);
ASSERT_EQ(s, to_str(buf));
ASSERT_EQ(s.length(), buf.length());
ASSERT_FALSE(buf.empty());
ASSERT_EQ(s.length(), buf.pop_back(INT_MAX));
s.clear();
ASSERT_EQ(s, to_str(buf));
ASSERT_EQ(0UL, buf.length());
ASSERT_TRUE(buf.empty());
for (size_t i = 0; i < DEFAULT_PAYLOAD * 3/2; ++i) {
s.push_back(i);
}
buf.append(s);
ASSERT_EQ(1UL, buf.pop_back(1));
s.resize(s.size() - 1);
ASSERT_EQ(s, to_str(buf));
ASSERT_EQ(s.length(), buf.length());
ASSERT_FALSE(buf.empty());
ASSERT_EQ(s.length(), buf.pop_back(INT_MAX));
s.clear();
ASSERT_EQ(s, to_str(buf));
ASSERT_EQ(0UL, buf.length());
ASSERT_TRUE(buf.empty());
}
TEST_F(IOBufTest, append) {
install_debug_allocator();
butil::IOBuf b;
ASSERT_EQ(0UL, b.length());
ASSERT_TRUE(b.empty());
ASSERT_EQ(-1, b.append(NULL));
ASSERT_EQ(0, b.append(""));
ASSERT_EQ(0, b.append(std::string()));
ASSERT_EQ(-1, b.append(NULL, 1));
ASSERT_EQ(0, b.append("dummy", 0));
ASSERT_EQ(0UL, b.length());
ASSERT_TRUE(b.empty());
ASSERT_EQ(0, b.append("1"));
ASSERT_EQ(1UL, b.length());
ASSERT_FALSE(b.empty());
ASSERT_EQ("1", to_str(b));
const std::string s = "22";
ASSERT_EQ(0, b.append(s));
ASSERT_EQ(3UL, b.length());
ASSERT_FALSE(b.empty());
ASSERT_EQ("122", to_str(b));
}
TEST_F(IOBufTest, appendv) {
install_debug_allocator();
butil::IOBuf b;
const_iovec vec[] = { {"hello1", 6}, {" world1", 7},
{"hello2", 6}, {" world2", 7},
{"hello3", 6}, {" world3", 7},
{"hello4", 6}, {" world4", 7},
{"hello5", 6}, {" world5", 7} };
ASSERT_EQ(0, b.appendv(vec, arraysize(vec)));
ASSERT_EQ("hello1 world1hello2 world2hello3 world3hello4 world4hello5 world5",
b.to_string());
// Make some iov_len shorter to test if iov_len works.
vec[2].iov_len = 4; // "hello2"
vec[5].iov_len = 3; // " world3"
b.clear();
ASSERT_EQ(0, b.appendv(vec, arraysize(vec)));
ASSERT_EQ("hello1 world1hell world2hello3 wohello4 world4hello5 world5",
b.to_string());
// Append some long stuff.
const size_t full_len = DEFAULT_PAYLOAD * 9;
char* str = (char*)malloc(full_len);
ASSERT_TRUE(str);
const size_t len1 = full_len / 6;
const size_t len2 = full_len / 3;
const size_t len3 = full_len - len1 - len2;
ASSERT_GT(len1, (size_t)DEFAULT_PAYLOAD);
ASSERT_GT(len2, (size_t)DEFAULT_PAYLOAD);
ASSERT_GT(len3, (size_t)DEFAULT_PAYLOAD);
ASSERT_EQ(full_len, len1 + len2 + len3);
for (size_t i = 0; i < full_len; ++i) {
str[i] = i * 7;
}
const_iovec vec2[] = {{str, len1},
{str + len1, len2},
{str + len1 + len2, len3}};
b.clear();
ASSERT_EQ(0, b.appendv(vec2, arraysize(vec2)));
ASSERT_EQ(full_len, b.size());
ASSERT_EQ(0, memcmp(str, b.to_string().data(), full_len));
free(str);
}
TEST_F(IOBufTest, reserve) {
butil::IOBuf b;
ASSERT_EQ(butil::IOBuf::INVALID_AREA, b.reserve(0));
const size_t NRESERVED1 = 5;
const butil::IOBuf::Area a1 = b.reserve(NRESERVED1);
ASSERT_TRUE(a1 != butil::IOBuf::INVALID_AREA);
ASSERT_EQ(NRESERVED1, b.size());
b.append("hello world");
ASSERT_EQ(0, b.unsafe_assign(a1, "prefix")); // `x' will not be copied
ASSERT_EQ("prefihello world", b.to_string());
ASSERT_EQ((size_t)16, b.size());
// pop/append sth. from back-side and assign again.
ASSERT_EQ((size_t)5, b.pop_back(5));
ASSERT_EQ("prefihello ", b.to_string());
b.append("blahblahfoobar");
ASSERT_EQ(0, b.unsafe_assign(a1, "goodorbad")); // `x' will not be copied
ASSERT_EQ("goodohello blahblahfoobar", b.to_string());
// append a long string and assign again.
std::string s1(DEFAULT_PAYLOAD * 3, '\0');
for (size_t i = 0; i < s1.size(); ++i) {
s1[i] = i * 7;
}
ASSERT_EQ(DEFAULT_PAYLOAD * 3, s1.size());
// remove everything after reserved area
ASSERT_GE(b.size(), NRESERVED1);
b.pop_back(b.size() - NRESERVED1);
ASSERT_EQ(NRESERVED1, b.size());
b.append(s1);
ASSERT_EQ(0, b.unsafe_assign(a1, "appleblahblah"));
ASSERT_EQ("apple" + s1, b.to_string());
// Reserve long
b.pop_back(b.size() - NRESERVED1);
ASSERT_EQ(NRESERVED1, b.size());
const size_t NRESERVED2 = DEFAULT_PAYLOAD * 3;
const butil::IOBuf::Area a2 = b.reserve(NRESERVED2);
ASSERT_EQ(NRESERVED1 + NRESERVED2, b.size());
b.append(s1);
ASSERT_EQ(NRESERVED1 + NRESERVED2 + s1.size(), b.size());
std::string s2(NRESERVED2, 0);
for (size_t i = 0; i < s2.size(); ++i) {
s2[i] = i * 13;
}
ASSERT_EQ(NRESERVED2, s2.size());
ASSERT_EQ(0, b.unsafe_assign(a2, s2.data()));
ASSERT_EQ("apple" + s2 + s1, b.to_string());
ASSERT_EQ(0, b.unsafe_assign(a1, "orangeblahblah"));
ASSERT_EQ("orang" + s2 + s1, b.to_string());
}
#ifndef BUTIL_USE_ASAN
// ASan will detect heap-buffer-overflow error casued by FakeBlock.
struct FakeBlock {
int nshared;
FakeBlock() : nshared(1) {}
};
TEST_F(IOBufTest, iobuf_as_queue) {
install_debug_allocator();
// If INITIAL_CAP gets bigger, creating butil::IOBuf::Block are very
// small. Since We don't access butil::IOBuf::Block::data in this case.
// We replace butil::IOBuf::Block with FakeBlock with only nshared (in
// the same offset)
FakeBlock* blocks[butil::IOBuf::INITIAL_CAP+16];
const size_t NBLOCKS = ARRAY_SIZE(blocks);
butil::IOBuf::BlockRef r[NBLOCKS];
const size_t LENGTH = 7UL;
for (size_t i = 0; i < NBLOCKS; ++i) {
ASSERT_TRUE((blocks[i] = new FakeBlock));
r[i].offset = 1;
r[i].length = LENGTH;
r[i].block = (butil::IOBuf::Block*)blocks[i];
}
butil::IOBuf p;
// Empty
ASSERT_EQ(0UL, p._ref_num());
ASSERT_EQ(-1, p._pop_front_ref());
ASSERT_EQ(0UL, p.length());
// Add one ref
p._push_back_ref(r[0]);
ASSERT_EQ(1UL, p._ref_num());
ASSERT_EQ(LENGTH, p.length());
ASSERT_EQ(r[0], p._front_ref());
ASSERT_EQ(r[0], p._back_ref());
ASSERT_EQ(r[0], p._ref_at(0));
ASSERT_EQ(2, butil::iobuf::block_shared_count(r[0].block));
// Add second ref
p._push_back_ref(r[1]);
ASSERT_EQ(2UL, p._ref_num());
ASSERT_EQ(LENGTH*2, p.length());
ASSERT_EQ(r[0], p._front_ref());
ASSERT_EQ(r[1], p._back_ref());
ASSERT_EQ(r[0], p._ref_at(0));
ASSERT_EQ(r[1], p._ref_at(1));
ASSERT_EQ(2, butil::iobuf::block_shared_count(r[1].block));
// Pop a ref
ASSERT_EQ(0, p._pop_front_ref());
ASSERT_EQ(1UL, p._ref_num());
ASSERT_EQ(LENGTH, p.length());
ASSERT_EQ(r[1], p._front_ref());
ASSERT_EQ(r[1], p._back_ref());
ASSERT_EQ(r[1], p._ref_at(0));
//ASSERT_EQ(1, butil::iobuf::block_shared_count(r[0].block));
// Pop second
ASSERT_EQ(0, p._pop_front_ref());
ASSERT_EQ(0UL, p._ref_num());
ASSERT_EQ(0UL, p.length());
//ASSERT_EQ(1, r[1].block->nshared);
// Add INITIAL_CAP+2 refs, r[0] and r[1] are used, don't use again
for (size_t i = 0; i < butil::IOBuf::INITIAL_CAP+2; ++i) {
p._push_back_ref(r[i+2]);
ASSERT_EQ(i+1, p._ref_num());
ASSERT_EQ(p._ref_num()*LENGTH, p.length());
ASSERT_EQ(r[2], p._front_ref()) << i;
ASSERT_EQ(r[i+2], p._back_ref());
for (size_t j = 0; j <= i; j+=std::max(1UL, i/20) /*not check all*/) {
ASSERT_EQ(r[j+2], p._ref_at(j));
}
ASSERT_EQ(2, butil::iobuf::block_shared_count(r[i+2].block));
}
// Pop them all
const size_t saved_ref_num = p._ref_num();
while (p._ref_num() >= 2UL) {
const size_t last_ref_num = p._ref_num();
ASSERT_EQ(0, p._pop_front_ref());
ASSERT_EQ(last_ref_num, p._ref_num()+1);
ASSERT_EQ(p._ref_num()*LENGTH, p.length());
const size_t f = saved_ref_num - p._ref_num() + 2;
ASSERT_EQ(r[f], p._front_ref());
ASSERT_EQ(r[saved_ref_num+1], p._back_ref());
for (size_t j = 0; j < p._ref_num(); j += std::max(1UL, p._ref_num()/20)) {
ASSERT_EQ(r[j+f], p._ref_at(j));
}
//ASSERT_EQ(1, r[f-1].block->nshared);
}
ASSERT_EQ(1ul, p._ref_num());
// Pop last one
ASSERT_EQ(0, p._pop_front_ref());
ASSERT_EQ(0UL, p._ref_num());
ASSERT_EQ(0UL, p.length());
//ASSERT_EQ(1, r[saved_ref_num+1].block->nshared);
// Delete blocks
for (size_t i = 0; i < NBLOCKS; ++i) {
delete blocks[i];
}
}
#endif // BUTIL_USE_ASAN
TEST_F(IOBufTest, iobuf_sanity) {
install_debug_allocator();
LOG(INFO) << "sizeof(butil::IOBuf)=" << sizeof(butil::IOBuf)
<< " sizeof(IOPortal)=" << sizeof(butil::IOPortal);
butil::IOBuf b1;
std::string s1 = "hello world";
const char c1 = 'A';
const std::string s2 = "too simple";
std::string s1c = s1;
s1c.erase(0, 1);
// Append a c-std::string
ASSERT_EQ(0, b1.append(s1.c_str()));
ASSERT_EQ(s1.length(), b1.length());
ASSERT_EQ(s1, to_str(b1));
ASSERT_EQ(1UL, b1._ref_num());
// Append a char
ASSERT_EQ(0, b1.push_back(c1));
ASSERT_EQ(s1.length() + 1, b1.length());
ASSERT_EQ(s1+c1, to_str(b1));
ASSERT_EQ(1UL, b1._ref_num());
// Append a std::string
ASSERT_EQ(0, b1.append(s2));
ASSERT_EQ(s1.length() + 1 + s2.length(), b1.length());
ASSERT_EQ(s1+c1+s2, to_str(b1));
ASSERT_EQ(1UL, b1._ref_num());
// Pop first char
ASSERT_EQ(1UL, b1.pop_front(1));
ASSERT_EQ(1UL, b1._ref_num());
ASSERT_EQ(s1.length() + s2.length(), b1.length());
ASSERT_EQ(s1c+c1+s2, to_str(b1));
// Pop all
ASSERT_EQ(0UL, b1.pop_front(0));
ASSERT_EQ(s1.length() + s2.length(), b1.pop_front(b1.length()+1));
ASSERT_TRUE(b1.empty());
ASSERT_EQ(0UL, b1.length());
ASSERT_EQ(0UL, b1._ref_num());
ASSERT_EQ("", to_str(b1));
// Restore
ASSERT_EQ(0, b1.append(s1.c_str()));
ASSERT_EQ(0, b1.push_back(c1));
ASSERT_EQ(0, b1.append(s2));
// Cut first char
butil::IOBuf p;
b1.cutn(&p, 0);
b1.cutn(&p, 1);
ASSERT_EQ(s1.substr(0, 1), to_str(p));
ASSERT_EQ(s1c+c1+s2, to_str(b1));
// Cut another two and append to p
b1.cutn(&p, 2);
ASSERT_EQ(s1.substr(0, 3), to_str(p));
std::string s1d = s1;
s1d.erase(0, 3);
ASSERT_EQ(s1d+c1+s2, to_str(b1));
// Clear
b1.clear();
ASSERT_TRUE(b1.empty());
ASSERT_EQ(0UL, b1.length());
ASSERT_EQ(0UL, b1._ref_num());
ASSERT_EQ("", to_str(b1));
ASSERT_EQ(s1.substr(0, 3), to_str(p));
}
TEST_F(IOBufTest, copy_and_assign) {
install_debug_allocator();
const size_t TARGET_SIZE = butil::IOBuf::DEFAULT_BLOCK_SIZE * 2;
butil::IOBuf buf0;
buf0.append("hello");
ASSERT_EQ(1u, buf0._ref_num());
// Copy-construct from SmallView
butil::IOBuf buf1 = buf0;
ASSERT_EQ(1u, buf1._ref_num());
ASSERT_EQ(buf0, buf1);
buf1.resize(TARGET_SIZE, 'z');
ASSERT_LT(2u, buf1._ref_num());
ASSERT_EQ(TARGET_SIZE, buf1.size());
// Copy-construct from BigView
butil::IOBuf buf2 = buf1;
ASSERT_EQ(buf1, buf2);
// assign BigView to SmallView
butil::IOBuf buf3;
buf3 = buf1;
ASSERT_EQ(buf1, buf3);
// assign BigView to BigView
butil::IOBuf buf4;
buf4.resize(TARGET_SIZE, 'w');
ASSERT_NE(buf1, buf4);
buf4 = buf1;
ASSERT_EQ(buf1, buf4);
}
TEST_F(IOBufTest, compare) {
install_debug_allocator();
const char* SEED = "abcdefghijklmnqopqrstuvwxyz";
butil::IOBuf seedbuf;
seedbuf.append(SEED);
const int REP = 100;
butil::IOBuf b1;
for (int i = 0; i < REP; ++i) {
b1.append(seedbuf);
b1.append(SEED);
}
butil::IOBuf b2;
for (int i = 0; i < REP * 2; ++i) {
b2.append(SEED);
}
ASSERT_EQ(b1, b2);
butil::IOBuf b3 = b2;
b2.push_back('0');
ASSERT_NE(b1, b2);
ASSERT_EQ(b1, b3);
b1.clear();
b2.clear();
ASSERT_EQ(b1, b2);
}
TEST_F(IOBufTest, append_and_cut_it_all) {
butil::IOBuf b;
const size_t N = 32768UL;
for (size_t i = 0; i < N; ++i) {
ASSERT_EQ(0, b.push_back(i));
}
ASSERT_EQ(N, b.length());
butil::IOBuf p;
b.cutn(&p, N);
ASSERT_TRUE(b.empty());
ASSERT_EQ(N, p.length());
}
TEST_F(IOBufTest, copy_to) {
butil::IOBuf b;
const std::string seed = "abcdefghijklmnopqrstuvwxyz";
std::string src;
for (size_t i = 0; i < 1000; ++i) {
src.append(seed);
}
b.append(src);
ASSERT_GT(b.size(), DEFAULT_PAYLOAD);
std::string s1;
ASSERT_EQ(src.size(), b.copy_to(&s1));
ASSERT_EQ(src, s1);
std::string s2;
ASSERT_EQ(32u, b.copy_to(&s2, 32));
ASSERT_EQ(src.substr(0, 32), s2);
std::string s3;
const std::string expected = src.substr(DEFAULT_PAYLOAD - 1, 33);
ASSERT_EQ(33u, b.copy_to(&s3, 33, DEFAULT_PAYLOAD - 1));
ASSERT_EQ(expected, s3);
ASSERT_EQ(33u, b.append_to(&s3, 33, DEFAULT_PAYLOAD - 1));
ASSERT_EQ(expected + expected, s3);
butil::IOBuf b1;
ASSERT_EQ(src.size(), b.append_to(&b1));
ASSERT_EQ(src, b1.to_string());
butil::IOBuf b2;
ASSERT_EQ(32u, b.append_to(&b2, 32));
ASSERT_EQ(src.substr(0, 32), b2.to_string());
butil::IOBuf b3;
ASSERT_EQ(33u, b.append_to(&b3, 33, DEFAULT_PAYLOAD - 1));
ASSERT_EQ(expected, b3.to_string());
ASSERT_EQ(33u, b.append_to(&b3, 33, DEFAULT_PAYLOAD - 1));
ASSERT_EQ(expected + expected, b3.to_string());
}
TEST_F(IOBufTest, cut_by_single_text_delim) {
install_debug_allocator();
butil::IOBuf b;
butil::IOBuf p;
std::vector<butil::IOBuf> ps;
std::string s1 = "1234567\n12\n\n2567";
ASSERT_EQ(0, b.append(s1));
ASSERT_EQ(s1.length(), b.length());
for (; b.cut_until(&p, "\n") == 0; ) {
ps.push_back(p);
p.clear();
}
ASSERT_EQ(3UL, ps.size());
ASSERT_EQ("1234567", to_str(ps[0]));
ASSERT_EQ("12", to_str(ps[1]));
ASSERT_EQ("", to_str(ps[2]));
ASSERT_EQ("2567", to_str(b));
b.append("\n");
ASSERT_EQ(0, b.cut_until(&p, "\n"));
ASSERT_EQ("2567", to_str(p));
ASSERT_EQ("", to_str(b));
}
TEST_F(IOBufTest, cut_by_multiple_text_delim) {
install_debug_allocator();
butil::IOBuf b;
butil::IOBuf p;
std::vector<butil::IOBuf> ps;
std::string s1 = "\r\n1234567\r\n12\r\n\n\r2567";
ASSERT_EQ(0, b.append(s1));
ASSERT_EQ(s1.length(), b.length());
for (; b.cut_until(&p, "\r\n") == 0; ) {
ps.push_back(p);
p.clear();
}
ASSERT_EQ(3UL, ps.size());
ASSERT_EQ("", to_str(ps[0]));
ASSERT_EQ("1234567", to_str(ps[1]));
ASSERT_EQ("12", to_str(ps[2]));
ASSERT_EQ("\n\r2567", to_str(b));
b.append("\r\n");
ASSERT_EQ(0, b.cut_until(&p, "\r\n"));
ASSERT_EQ("\n\r2567", to_str(p));
ASSERT_EQ("", to_str(b));
}
TEST_F(IOBufTest, append_a_lot_and_cut_them_all) {
install_debug_allocator();
butil::IOBuf b;
butil::IOBuf p;
std::string s1 = "12345678901234567";
const size_t N = 10000;
for (size_t i= 0; i < N; ++i) {
b.append(s1);
}
ASSERT_EQ(N*s1.length(), b.length());
while (b.length() >= 7) {
b.cutn(&p, 7);
}
size_t remain = s1.length()*N % 7;
ASSERT_EQ(remain, b.length());
ASSERT_EQ(s1.substr(s1.length() - remain, remain), to_str(b));
ASSERT_EQ(s1.length()*N/7*7, p.length());
}
TEST_F(IOBufTest, cut_into_fd_tiny) {
install_debug_allocator();
butil::IOPortal b1, b2;
std::string ref;
int fds[2];
for (int j = 10; j > 0; --j) {
ref.push_back(j);
}
b1.append(ref);
ASSERT_EQ(1UL, b1.pop_front(1));
ref.erase(0, 1);
ASSERT_EQ(ref, to_str(b1));
LOG(INFO) << "ref.size=" << ref.size();
//ASSERT_EQ(0, pipe(fds));
ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
butil::make_non_blocking(fds[0]);
butil::make_non_blocking(fds[1]);
while (!b1.empty() || b2.length() != ref.length()) {
size_t b1len = b1.length(), b2len = b2.length();
errno = 0;
printf("b1.length=%lu - %ld (%m), ",
b1len, b1.cut_into_file_descriptor(fds[1]));
printf("b2.length=%lu + %ld (%m)\n",
b2len, b2.append_from_file_descriptor(fds[0], LONG_MAX));
}
printf("b1.length=%lu, b2.length=%lu\n", b1.length(), b2.length());
ASSERT_EQ(ref, to_str(b2));
close(fds[0]);
close(fds[1]);
}
TEST_F(IOBufTest, cut_multiple_into_fd_tiny) {
install_debug_allocator();
butil::IOBuf* b1[10];
butil::IOPortal b2;
std::string ref;
int fds[2];
for (size_t j = 0; j < ARRAY_SIZE(b1); ++j) {
std::string s;
for (int i = 10; i > 0; --i) {
s.push_back(j * 10 + i);
}
ref.append(s);
butil::IOPortal* b = new butil::IOPortal();
b->append(s);
b1[j] = b;
}
ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
butil::make_non_blocking(fds[0]);
butil::make_non_blocking(fds[1]);
ASSERT_EQ((ssize_t)ref.length(),
butil::IOBuf::cut_multiple_into_file_descriptor(
fds[1], b1, ARRAY_SIZE(b1)));
for (size_t j = 0; j < ARRAY_SIZE(b1); ++j) {
ASSERT_TRUE(b1[j]->empty());
delete (butil::IOPortal*)b1[j];
b1[j] = NULL;
}
ASSERT_EQ((ssize_t)ref.length(),
b2.append_from_file_descriptor(fds[0], LONG_MAX));
ASSERT_EQ(ref, to_str(b2));
close(fds[0]);
close(fds[1]);
}
TEST_F(IOBufTest, cut_into_fd_a_lot_of_data) {
install_debug_allocator();
butil::IOPortal b0, b1, b2;
std::string s, ref;
int fds[2];
for (int j = rand()%7777+300000; j > 0; --j) {
ref.push_back(j);
s.push_back(j);
if (s.length() == 1024UL || j == 1) {
b0.append(s);
ref.append("tailing");
b0.cutn(&b1, b0.length());
ASSERT_EQ(0, b1.append("tailing"));
s.clear();
}
}
ASSERT_EQ(ref.length(), b1.length());
ASSERT_EQ(ref, to_str(b1));
ASSERT_TRUE(b0.empty());
LOG(INFO) << "ref.size=" << ref.size();
//ASSERT_EQ(0, pipe(fds));
ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
butil::make_non_blocking(fds[0]);
butil::make_non_blocking(fds[1]);
const int sockbufsize = 16 * 1024 - 17;
ASSERT_EQ(0, setsockopt(fds[1], SOL_SOCKET, SO_SNDBUF,
(const char*)&sockbufsize, sizeof(int)));
ASSERT_EQ(0, setsockopt(fds[0], SOL_SOCKET, SO_RCVBUF,
(const char*)&sockbufsize, sizeof(int)));
while (!b1.empty() || b2.length() != ref.length()) {
size_t b1len = b1.length(), b2len = b2.length();
errno = 0;
printf("b1.length=%lu - %ld (%m), ", b1len, b1.cut_into_file_descriptor(fds[1]));
printf("b2.length=%lu + %ld (%m)\n", b2len, b2.append_from_file_descriptor(fds[0], LONG_MAX));
}
printf("b1.length=%lu, b2.length=%lu\n", b1.length(), b2.length());
ASSERT_EQ(ref, to_str(b2));
close(fds[0]);
close(fds[1]);
}
TEST_F(IOBufTest, cut_by_delim_perf) {
butil::iobuf::reset_blockmem_allocate_and_deallocate();
butil::IOBuf b;
butil::IOBuf p;
std::vector<butil::IOBuf> ps;
std::string s1 = "123456789012345678901234567890\n";
const size_t N = 100000;
for (size_t i = 0; i < N; ++i) {
ASSERT_EQ(0, b.append(s1));
}
ASSERT_EQ(N * s1.length(), b.length());
butil::Timer t;
//ProfilerStart("cutd.prof");
t.start();
for (; b.cut_until(&p, "\n") == 0; ) { }
t.stop();
//ProfilerStop();
LOG(INFO) << "IOPortal::cut_by_delim takes "
<< t.n_elapsed()/N << "ns, tp="
<< s1.length() * N * 1000.0 / t.n_elapsed () << "MB/s";
show_prof_and_rm("test_iobuf", "cutd.prof", 10);
}
TEST_F(IOBufTest, cut_perf) {
butil::iobuf::reset_blockmem_allocate_and_deallocate();
butil::IOBuf b;
butil::IOBuf p;
const size_t length = 60000000UL;
const size_t REP = 10;
butil::Timer t;
std::string s = "1234567890";
const bool push_char = false;
if (!push_char) {
//ProfilerStart("iobuf_append.prof");
t.start();
for (size_t j = 0; j < REP; ++j) {
b.clear();
for (size_t i = 0; i < length/s.length(); ++i) {
b.append(s);
}
}
t.stop();
//ProfilerStop();
LOG(INFO) << "IOPortal::append(std::string_" << s.length() << ") takes "
<< t.n_elapsed() * s.length() / length / REP << "ns, tp="
<< REP * length * 1000.0 / t.n_elapsed () << "MB/s";
} else {
//ProfilerStart("iobuf_pushback.prof");
t.start();
for (size_t i = 0; i < length; ++i) {
b.push_back(i);
}
t.stop();
//ProfilerStop();
LOG(INFO) << "IOPortal::push_back(char) takes "
<< t.n_elapsed() / length << "ns, tp="
<< length * 1000.0 / t.n_elapsed () << "MB/s";
}
ASSERT_EQ(length, b.length());
size_t w[3] = { 16, 128, 1024 };
//char name[32];
for (size_t i = 0; i < ARRAY_SIZE(w); ++i) {
// snprintf(name, sizeof(name), "iobuf_cut%lu.prof", w[i]);
// ProfilerStart(name);
t.start ();
while (b.length() >= w[i]+12) {
b.cutn(&p, 12);
b.cutn(&p, w[i]);
}
t.stop ();
//ProfilerStop();
LOG(INFO) << "IOPortal::cutn(12+" << w[i] << ") takes "
<< t.n_elapsed()*(w[i]+12)/length << "ns, tp="
<< length * 1000.0 / t.n_elapsed () << "MB/s";
ASSERT_LT(b.length(), w[i]+12);
t.start();
b.append(p);
t.stop();
LOG(INFO) << "IOPortal::append(butil::IOBuf) takes "
<< t.n_elapsed ()/p._ref_num() << "ns, tp="
<< length * 1000.0 / t.n_elapsed () << "MB/s";
p.clear();
ASSERT_EQ(length, b.length());
}
show_prof_and_rm("test_iobuf", "./iobuf_append.prof", 10);
show_prof_and_rm("test_iobuf", "./iobuf_pushback.prof", 10);
}
TEST_F(IOBufTest, append_store_append_cut) {
butil::iobuf::reset_blockmem_allocate_and_deallocate();
std::string ref;
ref.resize(rand()%376813+19777777);
for (size_t j = 0; j < ref.size(); ++j) {
ref[j] = j;
}
butil::IOPortal b1, b2;
std::vector<butil::IOBuf> ps;
ssize_t nr;
size_t HINT = 16*1024UL;
butil::Timer t;
size_t w[3] = { 16, 128, 1024 };
char name[64];
char profname[64];
char cmd[512];
bool write_to_dev_null = true;
size_t nappend, ncut;
butil::TempFile f;
ASSERT_EQ(0, f.save_bin(ref.data(), ref.length()));
for (size_t i = 0; i < ARRAY_SIZE(w); ++i) {
ps.reserve(ref.size()/(w[i]+12) + 1);
// LOG(INFO) << "ps.cap=" << ps.capacity();
const int ifd = open(f.fname(), O_RDONLY);
ASSERT_TRUE(ifd > 0);
if (write_to_dev_null) {
snprintf(name, sizeof(name), "/dev/null");
} else {
snprintf(name, sizeof(name), "iobuf_asac%lu.output", w[i]);
snprintf(cmd, sizeof(cmd), "cmp %s %s", f.fname(), name);
}
const int ofd = open(name, O_CREAT | O_WRONLY, 0666);
ASSERT_TRUE(ofd > 0) << strerror(errno);
snprintf(profname, sizeof(profname), "iobuf_asac%lu.prof", w[i]);
//ProfilerStart(profname);
t.start();
nappend = ncut = 0;
while ((nr = b1.append_from_file_descriptor(ifd, HINT)) > 0) {