-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathbuffer_pool_test.go
60 lines (52 loc) · 1.15 KB
/
buffer_pool_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
package falcore
import (
/* "io"*/
"bytes"
"testing"
)
func TestBufferPool(t *testing.T) {
pool := newBufferPool(10, 1024)
text := []byte("Hello World")
// get one
bpe := pool.take(bytes.NewBuffer(text))
// read all
out := make([]byte, 1024)
l, _ := bpe.br.Read(out)
if bytes.Compare(out[0:l], text) != 0 {
t.Errorf("Read invalid data: %v", out)
}
if l != len(text) {
t.Errorf("Expected length %v got %v", len(text), l)
}
pool.give(bpe)
// get one
bpe = pool.take(bytes.NewBuffer(text))
// read all
out = make([]byte, 1024)
l, _ = bpe.br.Read(out)
if bytes.Compare(out[0:l], text) != 0 {
t.Errorf("Read invalid data: %v", out)
}
if l != len(text) {
t.Errorf("Expected length %v got %v", len(text), l)
}
pool.give(bpe)
// get one
bpe = pool.take(bytes.NewBuffer(text))
// read 1 byte
out = make([]byte, 1)
bpe.br.Read(out)
pool.give(bpe)
// get one
bpe = pool.take(bytes.NewBuffer(text))
// read all
out = make([]byte, 1024)
l, _ = bpe.br.Read(out)
if bytes.Compare(out[0:l], text) != 0 {
t.Errorf("Read invalid data: %v", out)
}
if l != len(text) {
t.Errorf("Expected length %v got %v", len(text), l)
}
pool.give(bpe)
}