From 45336ddb3fb274c9306d5748b9e1a7f42a1b2991 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 22 Dec 2018 21:18:27 -0600 Subject: [PATCH] Name project "Puddle" pool was inconvenient as a name because the obvious name for a variable is pool. But that caused a name collision with the package. --- README.md | 5 ++--- pool.go | 4 ++-- pool_test.go | 34 +++++++++++++++++----------------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 360c4ba..38c3272 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,9 @@ -# Pool +# Puddle -is a generic resource pool. +Puddle is a generic resource pool library for Go. ## TODO -* Permanent name * Error reporting for async errors * Min pool size * Max resource lifetime diff --git a/pool.go b/pool.go index 9333b6c..8295db2 100644 --- a/pool.go +++ b/pool.go @@ -1,4 +1,4 @@ -package pool +package puddle import ( "context" @@ -39,7 +39,7 @@ type Pool struct { closeRes CloseFunc } -func New(create CreateFunc, closeRes CloseFunc) *Pool { +func NewPool(create CreateFunc, closeRes CloseFunc) *Pool { return &Pool{ cond: sync.NewCond(new(sync.Mutex)), allResources: make(map[interface{}]*resourceWrapper), diff --git a/pool_test.go b/pool_test.go index c5df2b8..3912850 100644 --- a/pool_test.go +++ b/pool_test.go @@ -1,4 +1,4 @@ -package pool_test +package puddle_test import ( "context" @@ -7,7 +7,7 @@ import ( "testing" "time" - "github.com/jackc/pool" + "github.com/jackc/puddle" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -41,7 +41,7 @@ func TestPoolGetCreatesResourceWhenNoneAvailable(t *testing.T) { createFunc := func() (interface{}, error) { return createCalls.Next(), nil } - pool := pool.New(createFunc, stubCloseRes) + pool := puddle.NewPool(createFunc, stubCloseRes) res, err := pool.Get(context.Background()) require.NoError(t, err) @@ -55,7 +55,7 @@ func TestPoolGetDoesNotCreatesResourceWhenItWouldExceedMaxSize(t *testing.T) { createFunc := func() (interface{}, error) { return createCalls.Next(), nil } - pool := pool.New(createFunc, stubCloseRes) + pool := puddle.NewPool(createFunc, stubCloseRes) pool.SetMaxSize(1) wg := &sync.WaitGroup{} @@ -84,7 +84,7 @@ func TestPoolGetReturnsErrorFromFailedResourceCreate(t *testing.T) { createFunc := func() (interface{}, error) { return nil, errCreateFailed } - pool := pool.New(createFunc, stubCloseRes) + pool := puddle.NewPool(createFunc, stubCloseRes) res, err := pool.Get(context.Background()) assert.Equal(t, errCreateFailed, err) @@ -96,7 +96,7 @@ func TestPoolGetReusesResources(t *testing.T) { createFunc := func() (interface{}, error) { return createCalls.Next(), nil } - pool := pool.New(createFunc, stubCloseRes) + pool := puddle.NewPool(createFunc, stubCloseRes) res, err := pool.Get(context.Background()) require.NoError(t, err) @@ -117,7 +117,7 @@ func TestPoolGetContextAlreadyCanceled(t *testing.T) { createFunc := func() (interface{}, error) { panic("should never be called") } - pool := pool.New(createFunc, stubCloseRes) + pool := puddle.NewPool(createFunc, stubCloseRes) ctx, cancel := context.WithCancel(context.Background()) cancel() @@ -135,7 +135,7 @@ func TestPoolGetContextCanceledDuringCreate(t *testing.T) { time.Sleep(1 * time.Second) return createCalls.Next(), nil } - pool := pool.New(createFunc, stubCloseRes) + pool := puddle.NewPool(createFunc, stubCloseRes) res, err := pool.Get(ctx) assert.Equal(t, context.Canceled, err) @@ -147,7 +147,7 @@ func TestPoolReturnPanicsIfResourceNotPartOfPool(t *testing.T) { createFunc := func() (interface{}, error) { return createCalls.Next(), nil } - pool := pool.New(createFunc, stubCloseRes) + pool := puddle.NewPool(createFunc, stubCloseRes) assert.Panics(t, func() { pool.Return(42) }) } @@ -164,7 +164,7 @@ func TestPoolCloseClosesAllAvailableResources(t *testing.T) { return nil } - p := pool.New(createFunc, closeFunc) + p := puddle.NewPool(createFunc, closeFunc) resources := make([]interface{}, 4) for i := range resources { @@ -194,7 +194,7 @@ func TestPoolReturnClosesResourcePoolIsAlreadyClosed(t *testing.T) { return nil } - p := pool.New(createFunc, closeFunc) + p := puddle.NewPool(createFunc, closeFunc) resources := make([]interface{}, 4) for i := range resources { @@ -218,11 +218,11 @@ func TestPoolGetReturnsErrorWhenPoolIsClosed(t *testing.T) { createFunc := func() (interface{}, error) { return createCalls.Next(), nil } - p := pool.New(createFunc, stubCloseRes) - p.Close() + pool := puddle.NewPool(createFunc, stubCloseRes) + pool.Close() - res, err := p.Get(context.Background()) - assert.Equal(t, pool.ErrClosedPool, err) + res, err := pool.Get(context.Background()) + assert.Equal(t, puddle.ErrClosedPool, err) assert.Nil(t, res) } @@ -231,7 +231,7 @@ func BenchmarkPoolGetAndReturnNoContention(b *testing.B) { createFunc := func() (interface{}, error) { return createCalls.Next(), nil } - pool := pool.New(createFunc, stubCloseRes) + pool := puddle.NewPool(createFunc, stubCloseRes) for i := 0; i < b.N; i++ { res, err := pool.Get(context.Background()) @@ -250,7 +250,7 @@ func BenchmarkPoolGetAndReturnHeavyContention(b *testing.B) { createFunc := func() (interface{}, error) { return createCalls.Next(), nil } - pool := pool.New(createFunc, stubCloseRes) + pool := puddle.NewPool(createFunc, stubCloseRes) pool.SetMaxSize(poolSize) doneChan := make(chan struct{})