Esempio n. 1
0
func TestArrayPool(t *testing.T) {
	pool, err := gopool.NewArrayPool(gopool.Factory(makeBuffer), gopool.Reset(resetBuffer), gopool.Close(closeBuffer))
	if err != nil {
		t.Fatalf("Actual: %#v; Expected: %#v", err, nil)
	}
	test(t, pool)
}
Esempio n. 2
0
func TestArrayPoolErrorWithNonPositiveSize(t *testing.T) {
	pool, err := gopool.NewArrayPool(gopool.Size(0))
	if pool != nil {
		t.Errorf("Actual: %#v; Expected: %#v", pool, nil)
	}
	if err == nil {
		t.Errorf("Actual: %#v; Expected: %#v", err, "not nil")
	}

	pool, err = gopool.NewArrayPool(gopool.Size(-1))
	if pool != nil {
		t.Errorf("Actual: %#v; Expected: %#v", pool, nil)
	}
	if err == nil {
		t.Errorf("Actual: %#v; Expected: %#v", err, "not nil")
	}
}
Esempio n. 3
0
func TestArrayPoolErrorWithoutFactory(t *testing.T) {
	pool, err := gopool.NewArrayPool()
	if pool != nil {
		t.Errorf("Actual: %#v; Expected: %#v", pool, nil)
	}
	if err == nil {
		t.Errorf("Actual: %#v; Expected: %#v", err, "not nil")
	}
}
Esempio n. 4
0
func TestArrayPoolCreatesSizeItems(t *testing.T) {
	var size = 42
	var factoryInvoked int
	_, err := gopool.NewArrayPool(gopool.Size(size),
		gopool.Factory(func() (interface{}, error) {
			factoryInvoked++
			return nil, nil
		}))
	if err != nil {
		t.Fatal(err)
	}

	if actual, expected := factoryInvoked, size; actual != expected {
		t.Errorf("Actual: %#v; Expected: %#v", actual, expected)
	}
}
Esempio n. 5
0
func TestArrayPoolInvokesReset(t *testing.T) {
	var resetInvoked int
	pool, err := gopool.NewArrayPool(
		gopool.Factory(func() (interface{}, error) {
			return nil, nil
		}),
		gopool.Reset(func(item interface{}) {
			resetInvoked++
		}))
	if err != nil {
		t.Fatal(err)
	}
	pool.Put(pool.Get())
	if actual, expected := resetInvoked, 1; actual != expected {
		t.Errorf("Actual: %#v; Expected: %#v", actual, expected)
	}
}
Esempio n. 6
0
func TestArrayPoolInvokesClose(t *testing.T) {
	var closeInvoked int
	pool, err := gopool.NewArrayPool(gopool.Size(1),
		gopool.Factory(func() (interface{}, error) {
			return nil, nil
		}),
		gopool.Close(func(_ interface{}) error {
			closeInvoked++
			return errors.New("foo")
		}))
	if err != nil {
		t.Fatal(err)
	}
	if err := pool.Close(); err == nil || err.Error() != "foo" {
		t.Errorf("Actual: %#v; Expected: %#v", err, "foo")
	}
	if actual, expected := closeInvoked, 1; actual != expected {
		t.Errorf("Actual: %#v; Expected: %#v", actual, expected)
	}
}
Esempio n. 7
0
func BenchmarkArrayHighConcurrency(b *testing.B) {
	pool, _ := gopool.NewArrayPool(gopool.Factory(makeBuffer), gopool.Reset(resetBuffer), gopool.Close(closeBuffer), gopool.Size(10000))
	bench(b, pool, highConcurrency)
}