mirror of
https://github.com/valyala/bytebufferpool.git
synced 2026-06-13 13:16:35 +03:00
71 lines
1.2 KiB
Go
71 lines
1.2 KiB
Go
package bytebufferpool
|
|
|
|
import (
|
|
"math/rand"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestPoolCalibrate(t *testing.T) {
|
|
for i := 0; i < steps*calibrateCallsThreshold; i++ {
|
|
n := 1004
|
|
if i%15 == 0 {
|
|
n = rand.Intn(15234)
|
|
}
|
|
testAcquireRelease(t, n)
|
|
}
|
|
}
|
|
|
|
func TestPoolVariousSizesSerial(t *testing.T) {
|
|
testPoolVariousSizes(t)
|
|
}
|
|
|
|
func TestPoolVariousSizesConcurrent(t *testing.T) {
|
|
concurrency := 5
|
|
ch := make(chan struct{})
|
|
for i := 0; i < concurrency; i++ {
|
|
go func() {
|
|
testPoolVariousSizes(t)
|
|
ch <- struct{}{}
|
|
}()
|
|
}
|
|
for i := 0; i < concurrency; i++ {
|
|
select {
|
|
case <-ch:
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatalf("timeout")
|
|
}
|
|
}
|
|
}
|
|
|
|
func testPoolVariousSizes(t *testing.T) {
|
|
for i := 0; i < steps+1; i++ {
|
|
n := (1 << uint32(i))
|
|
|
|
testAcquireRelease(t, n)
|
|
testAcquireRelease(t, n+1)
|
|
testAcquireRelease(t, n-1)
|
|
|
|
for j := 0; j < 10; j++ {
|
|
testAcquireRelease(t, j+n)
|
|
}
|
|
}
|
|
}
|
|
|
|
func testAcquireRelease(t *testing.T, n int) {
|
|
bb := Acquire()
|
|
if len(bb.B) > 0 {
|
|
t.Fatalf("non-empty byte buffer returned from acquire")
|
|
}
|
|
bb.B = allocNBytes(bb.B, n)
|
|
Release(bb)
|
|
}
|
|
|
|
func allocNBytes(dst []byte, n int) []byte {
|
|
diff := n - cap(dst)
|
|
if diff <= 0 {
|
|
return dst[:n]
|
|
}
|
|
return append(dst, make([]byte, diff)...)
|
|
}
|