2
0

TryAcquire created resource in background instead of blocking

refs #14
This commit is contained in:
Jack Christensen
2021-11-27 10:17:32 -06:00
committed by Jack Christensen
parent 228d0e587b
commit 021a5cc1a6
2 changed files with 32 additions and 5 deletions
+9 -2
View File
@@ -175,14 +175,21 @@ func TestPoolAcquireReusesResources(t *testing.T) {
assert.Equal(t, 1, createCounter.Value())
}
func TestPoolTryAcquireDoesNotBlock(t *testing.T) {
func TestPoolTryAcquire(t *testing.T) {
constructor, createCounter := createConstructor()
pool := puddle.NewPool(constructor, stubDestructor, 1)
// Pool is initially empty so TryAcquire fails but starts construction of resource in the background.
res, err := pool.TryAcquire(context.Background())
require.EqualError(t, err, puddle.ErrNotAvailable.Error())
assert.Nil(t, res)
// Wait for background creation to complete.
time.Sleep(100 * time.Millisecond)
res, err = pool.TryAcquire(context.Background())
require.NoError(t, err)
assert.Equal(t, 1, res.Value())
defer res.Release()
res, err = pool.TryAcquire(context.Background())