Exemple #1
0
func TestRx(t *testing.T) {
	p := pool.NewByteSlice(10, nil)
	data := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	doTestRx(t, data, 0, p)
	doTestRx(t, data, 2, nil)
	doTestRx(t, data, 4, p)
}
Exemple #2
0
func BenchmarkAllocByteSlicePool(b *testing.B) {
	s := make([]byte, 10)
	p := pool.NewByteSlice(1, nil)
	for i := 0; i < b.N; i++ {
		p.Put(s)
		s = p.Get()
	}
}
Exemple #3
0
func TestBSPoolEmpty(t *testing.T) {
	p := pool.NewByteSlice(10, nil)
	for i := 0; i < 12; i++ {
		b := make([]byte, 64)
		b[63] = byte(i)
		p.Put(b)
	}
	p.Empty()
	if b := p.Get(); b != nil {
		t.Fatal("Pool not empty")
	}
}
Exemple #4
0
func TestBSPoolNil(t *testing.T) {
	p := pool.NewByteSlice(10, nil)
	for i := 0; i < 12; i++ {
		b := make([]byte, 64)
		b[63] = byte(i)
		p.Put(b)
	}
	for i := 0; i < 10; i++ {
		if b := p.Get(); b == nil {
			t.Fatalf("Item %d is nil", i)
		} else if b[63] != byte(i) {
			t.Fatalf("Item %d[0] != %d", b[63], i)
		}
	}
	if b := p.Get(); b != nil {
		t.Fatal("Too many items in pool")
	}
}
Exemple #5
0
func TestBSPoolAlloc(t *testing.T) {
	p := pool.NewByteSlice(10, func() []byte { return make([]byte, 64) })
	for i := 0; i < 12; i++ {
		b := make([]byte, 64)
		b[63] = byte(i)
		p.Put(b)
	}
	for i := 0; i < 10; i++ {
		if b := p.Get(); b == nil {
			t.Fatalf("Item %d is nil", i)
		} else if b[63] != byte(i) {
			t.Fatalf("Item %d[0] != %d", b[63], i)
		}
	}
	if b := p.Get(); b == nil {
		t.Fatal("No new allocation")
	} else if b[63] != 0 {
		t.Fatal("Unitialized new allocation")
	}
}