@@ -276,10 +276,10 @@ func (p *Pool) TryAcquire(ctx context.Context) (*Resource, error) {
|
|||||||
// will return ErrNotAvailable if no resource is available.
|
// will return ErrNotAvailable if no resource is available.
|
||||||
func (p *Pool) doAcquire(ctx context.Context, block bool) (*Resource, error) {
|
func (p *Pool) doAcquire(ctx context.Context, block bool) (*Resource, error) {
|
||||||
startNano := nanotime()
|
startNano := nanotime()
|
||||||
p.cond.L.Lock()
|
|
||||||
if doneChan := ctx.Done(); doneChan != nil {
|
if doneChan := ctx.Done(); doneChan != nil {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
p.cond.L.Lock()
|
||||||
p.canceledAcquireCount += 1
|
p.canceledAcquireCount += 1
|
||||||
p.cond.L.Unlock()
|
p.cond.L.Unlock()
|
||||||
return nil, ctx.Err()
|
return nil, ctx.Err()
|
||||||
@@ -287,6 +287,8 @@ func (p *Pool) doAcquire(ctx context.Context, block bool) (*Resource, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p.cond.L.Lock()
|
||||||
|
|
||||||
emptyAcquire := false
|
emptyAcquire := false
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|||||||
@@ -192,6 +192,20 @@ func TestPoolTryAcquireDoesNotBlock(t *testing.T) {
|
|||||||
assert.Equal(t, 1, createCounter.Value())
|
assert.Equal(t, 1, createCounter.Value())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPoolAcquireNilContextDoesNotLeavePoolLocked(t *testing.T) {
|
||||||
|
constructor, createCounter := createConstructor()
|
||||||
|
pool := puddle.NewPool(constructor, stubDestructor, 10)
|
||||||
|
|
||||||
|
assert.Panics(t, func() { pool.Acquire(nil) })
|
||||||
|
|
||||||
|
res, err := pool.Acquire(context.Background())
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, res.Value())
|
||||||
|
res.Release()
|
||||||
|
|
||||||
|
assert.Equal(t, 1, createCounter.Value())
|
||||||
|
}
|
||||||
|
|
||||||
func TestPoolAcquireContextAlreadyCanceled(t *testing.T) {
|
func TestPoolAcquireContextAlreadyCanceled(t *testing.T) {
|
||||||
constructor := func(ctx context.Context) (interface{}, error) {
|
constructor := func(ctx context.Context) (interface{}, error) {
|
||||||
panic("should never be called")
|
panic("should never be called")
|
||||||
|
|||||||
Reference in New Issue
Block a user