Achieve 100% test coverage
This commit is contained in:
@@ -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)) })
|
||||||
|
}
|
||||||
@@ -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) {
|
func (p *Pool) constructResourceValue(ctx context.Context) (interface{}, error) {
|
||||||
|
|||||||
@@ -455,6 +455,10 @@ func TestResourceDestroyRemovesResourceFromPool(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, 0, pool.Stat().TotalResources())
|
assert.Equal(t, 0, pool.Stat().TotalResources())
|
||||||
assert.Equal(t, 0, destructorCalls.Value())
|
assert.Equal(t, 0, destructorCalls.Value())
|
||||||
|
|
||||||
|
// Can still call Value and CreationTime
|
||||||
|
res.Value()
|
||||||
|
res.CreationTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResourceHijackRemovesResourceFromPoolButDoesNotDestroy(t *testing.T) {
|
func TestResourceHijackRemovesResourceFromPoolButDoesNotDestroy(t *testing.T) {
|
||||||
@@ -470,6 +474,21 @@ func TestResourceHijackRemovesResourceFromPoolButDoesNotDestroy(t *testing.T) {
|
|||||||
assert.Equal(t, 0, pool.Stat().TotalResources())
|
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) {
|
func TestPoolAcquireReturnsErrorWhenPoolIsClosed(t *testing.T) {
|
||||||
constructor, _ := createConstructor()
|
constructor, _ := createConstructor()
|
||||||
pool := puddle.NewPool(constructor, stubDestructor, 10)
|
pool := puddle.NewPool(constructor, stubDestructor, 10)
|
||||||
|
|||||||
Reference in New Issue
Block a user