2020-08-13 20:12:22 +00:00
|
|
|
package leveledbytebufferpool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2024-05-12 09:24:48 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/slicesutil"
|
2020-08-13 20:12:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetPutConcurrent(t *testing.T) {
|
|
|
|
const concurrency = 10
|
|
|
|
doneCh := make(chan struct{}, concurrency)
|
|
|
|
for i := 0; i < concurrency; i++ {
|
|
|
|
go func() {
|
|
|
|
for capacity := -1; capacity < 100; capacity++ {
|
|
|
|
bb := Get(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
|
|
|
|
}
|
2024-05-12 09:24:48 +00:00
|
|
|
bb.B = slicesutil.SetLength(bb.B, len(bb.B)+capacity)
|
2020-08-13 20:12:22 +00:00
|
|
|
Put(bb)
|
|
|
|
}
|
|
|
|
doneCh <- struct{}{}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
tc := time.After(10 * time.Second)
|
|
|
|
for i := 0; i < concurrency; i++ {
|
|
|
|
select {
|
|
|
|
case <-tc:
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
case <-doneCh:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|