forked from valyala/bytebufferpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytebuffer_test.go
138 lines (124 loc) · 2.95 KB
/
bytebuffer_test.go
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
package bytebufferpool
import (
"bytes"
"fmt"
"io"
"testing"
"time"
)
func TestByteBufferReadFrom(t *testing.T) {
prefix := "foobar"
expectedS := "asadfsdafsadfasdfisdsdfa"
prefixLen := int64(len(prefix))
expectedN := int64(len(expectedS))
var bb ByteBuffer
bb.WriteString(prefix)
rf := (io.ReaderFrom)(&bb)
for i := 0; i < 20; i++ {
r := bytes.NewBufferString(expectedS)
n, err := rf.ReadFrom(r)
if n != expectedN {
t.Fatalf("unexpected n=%d. Expecting %d. iteration %d", n, expectedN, i)
}
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
bbLen := int64(bb.Len())
expectedLen := prefixLen + int64(i+1)*expectedN
if bbLen != expectedLen {
t.Fatalf("unexpected byteBuffer length: %d. Expecting %d", bbLen, expectedLen)
}
for j := 0; j < i; j++ {
start := prefixLen + int64(j)*expectedN
b := bb.B[start : start+expectedN]
if string(b) != expectedS {
t.Fatalf("unexpected byteBuffer contents: %q. Expecting %q", b, expectedS)
}
}
}
}
func TestByteBufferWriteTo(t *testing.T) {
expectedS := "foobarbaz"
var bb ByteBuffer
bb.WriteString(expectedS[:3])
bb.WriteString(expectedS[3:])
wt := (io.WriterTo)(&bb)
var w bytes.Buffer
for i := 0; i < 10; i++ {
n, err := wt.WriteTo(&w)
if n != int64(len(expectedS)) {
t.Fatalf("unexpected n returned from WriteTo: %d. Expecting %d", n, len(expectedS))
}
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
s := string(w.Bytes())
if s != expectedS {
t.Fatalf("unexpected string written %q. Expecting %q", s, expectedS)
}
w.Reset()
}
}
func TestByteBufferGetPutSerial(t *testing.T) {
testByteBufferGetPut(t)
}
func TestByteBufferGetPutConcurrent(t *testing.T) {
concurrency := 10
ch := make(chan struct{}, concurrency)
for i := 0; i < concurrency; i++ {
go func() {
testByteBufferGetPut(t)
ch <- struct{}{}
}()
}
for i := 0; i < concurrency; i++ {
select {
case <-ch:
case <-time.After(time.Second):
t.Fatalf("timeout!")
}
}
}
func testByteBufferGetPut(t *testing.T) {
for i := 0; i < 10; i++ {
expectedS := fmt.Sprintf("num %d", i)
b := Get()
b.B = append(b.B, "num "...)
b.B = append(b.B, fmt.Sprintf("%d", i)...)
if string(b.B) != expectedS {
t.Fatalf("unexpected result: %q. Expecting %q", b.B, expectedS)
}
Put(b)
}
}
func testByteBufferGetString(t *testing.T) {
for i := 0; i < 10; i++ {
expectedS := fmt.Sprintf("num %d", i)
b := Get()
b.SetString(expectedS)
if b.String() != expectedS {
t.Fatalf("unexpected result: %q. Expecting %q", b.B, expectedS)
}
Put(b)
}
}
func TestByteBufferGetStringSerial(t *testing.T) {
testByteBufferGetString(t)
}
func TestByteBufferGetStringConcurrent(t *testing.T) {
concurrency := 10
ch := make(chan struct{}, concurrency)
for i := 0; i < concurrency; i++ {
go func() {
testByteBufferGetString(t)
ch <- struct{}{}
}()
}
for i := 0; i < concurrency; i++ {
select {
case <-ch:
case <-time.After(time.Second):
t.Fatalf("timeout!")
}
}
}