Rename Cnt() to Size() and split up tests

From PR Review https://github.com/safing/portbase/pull/57
This commit is contained in:
Daniel 2020-07-10 12:02:55 +02:00
parent 5f2c861e2d
commit eb0cbf58a8
2 changed files with 8 additions and 5 deletions

View file

@ -104,8 +104,8 @@ func (p *StablePool) Get() interface{} {
return nil return nil
} }
// Cnt returns the amount of items the pool currently holds. // Size returns the amount of items the pool currently holds.
func (p *StablePool) Cnt() int { func (p *StablePool) Size() int {
p.lock.Lock() p.lock.Lock()
defer p.lock.Unlock() defer p.lock.Unlock()

View file

@ -9,8 +9,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestStablePool(t *testing.T) { func TestStablePoolRealWorld(t *testing.T) {
// "real world" simulation // "real world" simulation
cnt := 0 cnt := 0
@ -55,7 +54,7 @@ func TestStablePool(t *testing.T) {
// wait for round to finish // wait for round to finish
testWorkerWg.Wait() testWorkerWg.Wait()
} }
t.Logf("real world simulation: cnt=%d p.cnt=%d p.max=%d\n", cnt, testPool.Cnt(), testPool.Max()) t.Logf("real world simulation: cnt=%d p.cnt=%d p.max=%d\n", cnt, testPool.Size(), testPool.Max())
assert.GreaterOrEqual(t, 200, cnt, "should not use more than 200 values") assert.GreaterOrEqual(t, 200, cnt, "should not use more than 200 values")
assert.GreaterOrEqual(t, 100, testPool.Max(), "pool should have at most this max size") assert.GreaterOrEqual(t, 100, testPool.Max(), "pool should have at most this max size")
@ -71,7 +70,9 @@ func TestStablePool(t *testing.T) {
} }
} }
assert.Equal(t, 100, optPool.Max(), "pool should have exactly this max size") assert.Equal(t, 100, optPool.Max(), "pool should have exactly this max size")
}
func TestStablePoolFuzzing(t *testing.T) {
// fuzzing test // fuzzing test
fuzzPool := &StablePool{} fuzzPool := &StablePool{}
@ -97,7 +98,9 @@ func TestStablePool(t *testing.T) {
fuzzWg.Done() fuzzWg.Done()
// wait for all to finish // wait for all to finish
fuzzWorkerWg.Wait() fuzzWorkerWg.Wait()
}
func TestStablePoolBreaking(t *testing.T) {
// try to break it // try to break it
breakPool := &StablePool{} breakPool := &StablePool{}