mirror of
https://github.com/valyala/bytebufferpool.git
synced 2026-06-15 13:36:59 +03:00
44 lines
799 B
Go
44 lines
799 B
Go
package bytebufferpool
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestByteBufferGetPutSerial(t *testing.T) {
|
|
testByteBufferGetPut(t)
|
|
}
|
|
|
|
func TestByteBufferGetPutConcurrent(t *testing.T) {
|
|
concurrency := 10
|
|
ch := make(chan struct{}, concurrency)
|
|
for i := 0; i < concurrency; i++ {
|
|
go func() {
|
|
testByteBufferGetPut(t)
|
|
ch <- struct{}{}
|
|
}()
|
|
}
|
|
|
|
for i := 0; i < concurrency; i++ {
|
|
select {
|
|
case <-ch:
|
|
case <-time.After(time.Second):
|
|
t.Fatalf("timeout!")
|
|
}
|
|
}
|
|
}
|
|
|
|
func testByteBufferGetPut(t *testing.T) {
|
|
for i := 0; i < 10; i++ {
|
|
expectedS := fmt.Sprintf("num %d", i)
|
|
b := Get()
|
|
b.B = append(b.B, "num "...)
|
|
b.B = append(b.B, fmt.Sprintf("%d", i)...)
|
|
if string(b.B) != expectedS {
|
|
t.Fatalf("unexpected result: %q. Expecting %q", b.B, expectedS)
|
|
}
|
|
Put(b)
|
|
}
|
|
}
|