mirror of
https://github.com/valyala/bytebufferpool.git
synced 2026-06-14 13:26:35 +03:00
Initial implementation
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package bytebufferpool
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
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 := AcquireByteBuffer()
|
||||
if len(bb.B) > 0 {
|
||||
t.Fatalf("non-empty byte buffer returned from acquire")
|
||||
}
|
||||
bb.B = allocNBytes(bb.B, n)
|
||||
ReleaseByteBuffer(bb)
|
||||
}
|
||||
|
||||
func allocNBytes(dst []byte, n int) []byte {
|
||||
diff := n - cap(dst)
|
||||
if diff <= 0 {
|
||||
return dst[:n]
|
||||
}
|
||||
return append(dst, make([]byte, diff)...)
|
||||
}
|
||||
Reference in New Issue
Block a user