go-socks5/bufferpool/pool_test.go

40 lines
572 B
Go
Raw Normal View History

package bufferpool
2020-04-20 01:50:49 +00:00
import (
"sync"
"testing"
2020-08-05 05:17:05 +00:00
"github.com/stretchr/testify/require"
2020-04-20 01:50:49 +00:00
)
func TestPool(t *testing.T) {
2020-08-06 02:12:46 +00:00
p := NewPool(2048)
2020-04-20 01:50:49 +00:00
b := p.Get()
bs := b[0:cap(b)]
2020-08-05 05:17:05 +00:00
require.Equal(t, cap(b), len(bs))
2020-08-05 06:40:07 +00:00
2020-08-05 05:17:05 +00:00
p.Put(b)
p.Get()
2020-04-20 01:50:49 +00:00
p.Put(b)
2020-08-05 05:17:05 +00:00
p.Put(make([]byte, 2048))
require.Panics(t, func() { p.Put([]byte{}) })
2020-04-20 01:50:49 +00:00
}
func BenchmarkSyncPool(b *testing.B) {
2020-08-06 02:12:46 +00:00
p := NewPool(32 * 1024)
2020-04-20 01:50:49 +00:00
wg := new(sync.WaitGroup)
b.ResetTimer()
2020-08-05 06:40:07 +00:00
b.StartTimer()
2020-04-20 01:50:49 +00:00
for n := 0; n < b.N; n++ {
wg.Add(1)
go func() {
s := p.Get()
p.Put(s)
wg.Done()
}()
}
wg.Wait()
2020-08-05 06:40:07 +00:00
b.StopTimer()
2020-04-20 01:50:49 +00:00
}