diff --git a/internal_test.go b/internal_test.go new file mode 100644 index 0000000..cd8bc78 --- /dev/null +++ b/internal_test.go @@ -0,0 +1,12 @@ +package puddle + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRemoveResourcePanicsWithBugReportIfResourceDoesNotExist(t *testing.T) { + s := []*Resource{new(Resource), new(Resource), new(Resource)} + assert.PanicsWithValue(t, "BUG: removeResource could not find res in slice", func() { removeResource(s, new(Resource)) }) +} diff --git a/pool.go b/pool.go index 39c57df..ac72656 100644 --- a/pool.go +++ b/pool.go @@ -368,7 +368,7 @@ func removeResource(slice []*Resource, res *Resource) []*Resource { } } - return slice + panic("BUG: removeResource could not find res in slice") } func (p *Pool) constructResourceValue(ctx context.Context) (interface{}, error) { diff --git a/pool_test.go b/pool_test.go index 55b9f00..b8523ac 100644 --- a/pool_test.go +++ b/pool_test.go @@ -455,6 +455,10 @@ func TestResourceDestroyRemovesResourceFromPool(t *testing.T) { assert.Equal(t, 0, pool.Stat().TotalResources()) assert.Equal(t, 0, destructorCalls.Value()) + + // Can still call Value and CreationTime + res.Value() + res.CreationTime() } func TestResourceHijackRemovesResourceFromPoolButDoesNotDestroy(t *testing.T) { @@ -470,6 +474,21 @@ func TestResourceHijackRemovesResourceFromPoolButDoesNotDestroy(t *testing.T) { assert.Equal(t, 0, pool.Stat().TotalResources()) } +func TestResourcePanicsOnUsageWhenNotAcquired(t *testing.T) { + constructor, _ := createConstructor() + pool := puddle.NewPool(constructor, stubDestructor, 10) + + res, err := pool.Acquire(context.Background()) + require.NoError(t, err) + res.Release() + + assert.PanicsWithValue(t, "tried to release resource that is not acquired", res.Release) + assert.PanicsWithValue(t, "tried to destroy resource that is not acquired", res.Destroy) + assert.PanicsWithValue(t, "tried to hijack resource that is not acquired", res.Hijack) + assert.PanicsWithValue(t, "tried to access resource that is not acquired or hijacked", func() { res.Value() }) + assert.PanicsWithValue(t, "tried to access resource that is not acquired or hijacked", func() { res.CreationTime() }) +} + func TestPoolAcquireReturnsErrorWhenPoolIsClosed(t *testing.T) { constructor, _ := createConstructor() pool := puddle.NewPool(constructor, stubDestructor, 10)