func BenchmarkAllocPool(b *testing.B) { s := make([]byte, 10) p := pool.New(1, nil) for i := 0; i < b.N; i++ { p.Put(s) s = p.Get().([]byte) } }
func TestPoolEmpty(t *testing.T) { p := pool.New(10, nil) for i := 0; i < 12; i++ { p.Put(&S{id: i}) } p.Empty() if s := p.Get(); s != nil { t.Fatal("Pool not empty") } }
func TestPoolNil(t *testing.T) { p := pool.New(10, nil) for i := 0; i < 12; i++ { p.Put(&S{id: i}) } for i := 0; i < 10; i++ { if s := p.Get(); s == nil { t.Fatalf("Item %d is nil", i) } else { ss := s.(*S) if ss.id != i { t.Fatalf("Item id %d != %d", ss.id, i) } } } if s := p.Get(); s != nil { t.Fatal("Too many items in pool") } }
func TestPoolAlloc(t *testing.T) { p := pool.New(10, func() interface{} { return &S{} }) for i := 0; i < 12; i++ { p.Put(&S{id: i}) } for i := 0; i < 10; i++ { if s := p.Get(); s == nil { t.Fatalf("Item %d is nil", i) } else { ss := s.(*S) if ss.id != i { t.Fatalf("Item id %d != %d", ss.id, i) } } } if s := p.Get(); s == nil { t.Fatal("No new allocation") } else if ss := s.(*S); ss.id != 0 { t.Fatal("Unitialized new allocation") } }