micro/util/xpool/pool_test.go

46 lines
770 B
Go
Raw Permalink Normal View History

package pool
import (
"bytes"
"testing"
)
func TestByte(t *testing.T) {
p := NewBytePool(1024)
b := p.Get()
copy(*b, []byte(`test`))
if bytes.Equal(*b, []byte("test")) {
t.Fatal("pool not works")
}
p.Put(b)
b = p.Get()
for i := 0; i < 1500; i++ {
*b = append(*b, []byte(`test`)...)
}
p.Put(b)
st := p.Stats()
if st.Get != 2 && st.Put != 2 && st.Mis != 1 && st.Ret != 1 {
t.Fatalf("pool stats error %#+v", st)
}
}
func TestBytes(t *testing.T) {
p := NewBytesPool(1024)
b := p.Get()
b.Write([]byte(`test`))
if b.String() != "test" {
t.Fatal("pool not works")
}
p.Put(b)
}
func TestStrings(t *testing.T) {
p := NewStringsPool(20)
b := p.Get()
b.Write([]byte(`test`))
if b.String() != "test" {
t.Fatal("pool not works")
}
p.Put(b)
}