-
Notifications
You must be signed in to change notification settings - Fork 9
/
bpool_test.go
47 lines (43 loc) · 924 Bytes
/
bpool_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
package protocol
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestGetPutConcurrent(t *testing.T) {
const concurrency = 10
doneCh := make(chan struct{}, concurrency)
for i := 0; i < concurrency; i++ {
go func() {
for capacity := 0; capacity < 100; capacity++ {
bb := getByteBuffer(capacity)
if len(bb.B) > 0 {
panic(fmt.Errorf("len(bb.B) must be zero; got %d", len(bb.B)))
}
if capacity < 0 {
capacity = 0
}
bb.B = append(bb.B, make([]byte, capacity)...)
putByteBuffer(bb)
}
doneCh <- struct{}{}
}()
}
tc := time.After(10 * time.Second)
for i := 0; i < concurrency; i++ {
select {
case <-tc:
t.Fatalf("timeout")
case <-doneCh:
}
}
}
func TestGetCapacity(t *testing.T) {
for i := 1; i < 130; i++ {
idx := nextLogBase2(uint32(i))
b := getByteBuffer(i)
require.Equal(t, 1<<idx, cap(b.B))
putByteBuffer(b)
}
}