-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferpool_test.go
More file actions
57 lines (51 loc) · 1.32 KB
/
bufferpool_test.go
File metadata and controls
57 lines (51 loc) · 1.32 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
package anet
import (
"testing"
)
// nextPow2 returns the smallest power of two >= v, with a minimum of 32.
func nextPow2(v int) int {
res := 32
for res < v {
res <<= 1
}
return res
}
func TestGetBufferBasic(t *testing.T) {
t.Parallel()
cases := []struct {
size int
expectedCap int
}{
{size: 1, expectedCap: 32},
{size: 32, expectedCap: 32},
{size: 33, expectedCap: 64},
{size: 1000, expectedCap: nextPow2(1000)},
{size: maxBufferSize, expectedCap: maxBufferSize},
}
for _, c := range cases {
buf := globalBufferPool.getBuffer(c.size)
if len(buf) < c.size {
t.Errorf("getBuffer(%d) returned len %d, want >= %d", c.size, len(buf), c.size)
}
capBuf := cap(buf)
if capBuf != c.expectedCap {
t.Errorf("getBuffer(%d) returned cap %d, want %d", c.size, capBuf, c.expectedCap)
}
// return buffer to pool
globalBufferPool.putBuffer(buf)
}
}
func TestGetBufferLarge(t *testing.T) {
t.Parallel()
// request size > maxBufferSize should allocate exact size
large := maxBufferSize*2 + 1
buf := globalBufferPool.getBuffer(large)
if len(buf) != large {
t.Errorf("getBuffer(large) returned len %d, want %d", len(buf), large)
}
if cap(buf) != large {
t.Errorf("getBuffer(large) returned cap %d, want %d", cap(buf), large)
}
// putBuffer should not panic
globalBufferPool.putBuffer(buf)
}