2
0

Achieve 100% test coverage

This commit is contained in:
Jack Christensen
2018-12-26 17:17:08 -06:00
parent 75889e9497
commit 383709a0b4
3 changed files with 32 additions and 1 deletions
+12
View File
@@ -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)) })
}
+1 -1
View File
@@ -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) {
+19
View File
@@ -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)