From e4ced69a3a2b73f73662b9c9baeef035137ded46 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 13 Apr 2019 18:43:25 -0500 Subject: [PATCH] Ensure maxSize > 0 --- pool.go | 6 +++++- pool_test.go | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pool.go b/pool.go index 9687abd..2bce46c 100644 --- a/pool.go +++ b/pool.go @@ -94,8 +94,12 @@ type Pool struct { closed bool } -// NewPool creates a new pool. +// NewPool creates a new pool. Panics if maxSize is less than 1. func NewPool(constructor Constructor, destructor Destructor, maxSize int32) *Pool { + if maxSize < 1 { + panic("maxSize is less than 1") + } + return &Pool{ cond: sync.NewCond(new(sync.Mutex)), destructWG: &sync.WaitGroup{}, diff --git a/pool_test.go b/pool_test.go index 2b6876c..837e546 100644 --- a/pool_test.go +++ b/pool_test.go @@ -72,6 +72,12 @@ func waitForRead(ch chan int) bool { } } +func TestNewPoolRequiresMaxSizeGreaterThan0(t *testing.T) { + constructor, _ := createConstructor() + assert.Panics(t, func() { puddle.NewPool(constructor, stubDestructor, -1) }) + assert.Panics(t, func() { puddle.NewPool(constructor, stubDestructor, 0) }) +} + func TestPoolAcquireCreatesResourceWhenNoneIdle(t *testing.T) { constructor, _ := createConstructor() pool := puddle.NewPool(constructor, stubDestructor, 10)