示例#1
0
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)
	}
}
示例#2
0
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")
	}
}
示例#3
0
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")
	}
}
示例#4
0
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")
	}
}