3 Star 0 Fork 0

Gitee 极速下载 / bytebufferpool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/valyala/bytebufferpool
克隆/下载
pool_test.go 1.68 KB
一键复制 编辑 原始数据 按行查看 历史
package bytebufferpool
import (
"math/rand"
"testing"
"time"
)
func TestIndex(t *testing.T) {
testIndex(t, 0, 0)
testIndex(t, 1, 0)
testIndex(t, minSize-1, 0)
testIndex(t, minSize, 0)
testIndex(t, minSize+1, 1)
testIndex(t, 2*minSize-1, 1)
testIndex(t, 2*minSize, 1)
testIndex(t, 2*minSize+1, 2)
testIndex(t, maxSize-1, steps-1)
testIndex(t, maxSize, steps-1)
testIndex(t, maxSize+1, steps-1)
}
func testIndex(t *testing.T, n, expectedIdx int) {
idx := index(n)
if idx != expectedIdx {
t.Fatalf("unexpected idx for n=%d: %d. Expecting %d", n, idx, expectedIdx)
}
}
func TestPoolCalibrate(t *testing.T) {
for i := 0; i < steps*calibrateCallsThreshold; i++ {
n := 1004
if i%15 == 0 {
n = rand.Intn(15234)
}
testGetPut(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))
testGetPut(t, n)
testGetPut(t, n+1)
testGetPut(t, n-1)
for j := 0; j < 10; j++ {
testGetPut(t, j+n)
}
}
}
func testGetPut(t *testing.T, n int) {
bb := Get()
if len(bb.B) > 0 {
t.Fatalf("non-empty byte buffer returned from acquire")
}
bb.B = allocNBytes(bb.B, n)
Put(bb)
}
func allocNBytes(dst []byte, n int) []byte {
diff := n - cap(dst)
if diff <= 0 {
return dst[:n]
}
return append(dst, make([]byte, diff)...)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/bytebufferpool.git
git@gitee.com:mirrors/bytebufferpool.git
mirrors
bytebufferpool
bytebufferpool
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891