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 := 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)...) }