From 30896744c8b41bc297528580fa612aa6bc45afe2 Mon Sep 17 00:00:00 2001 From: Mike Graf Date: Mon, 28 Aug 2017 22:30:42 -0700 Subject: [PATCH] Return ErrClosedPool when Acquire() with closed pool --- conn_pool.go | 5 ++++- conn_pool_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/conn_pool.go b/conn_pool.go index 5fa923b7..ea6b4bbc 100644 --- a/conn_pool.go +++ b/conn_pool.go @@ -43,6 +43,9 @@ type ConnPoolStat struct { // ErrAcquireTimeout occurs when an attempt to acquire a connection times out. var ErrAcquireTimeout = errors.New("timeout acquiring connection from pool") +// ErrClosedPool occurs when an attempt to acquire a connection times out. +var ErrClosedPool = errors.New("cannot acquire from closed pool") + // NewConnPool creates a new ConnPool. config.ConnConfig is passed through to // Connect directly. func NewConnPool(config ConnPoolConfig) (p *ConnPool, err error) { @@ -108,7 +111,7 @@ func (p *ConnPool) deadlinePassed(deadline *time.Time) bool { // acquire performs acquision assuming pool is already locked func (p *ConnPool) acquire(deadline *time.Time) (*Conn, error) { if p.closed { - return nil, errors.New("cannot acquire from closed pool") + return nil, ErrClosedPool } // A connection is available diff --git a/conn_pool_test.go b/conn_pool_test.go index ccc38ba9..174b8379 100644 --- a/conn_pool_test.go +++ b/conn_pool_test.go @@ -315,6 +315,23 @@ func TestPoolWithoutAcquireTimeoutSet(t *testing.T) { } } +func TestPoolErrClosedPool(t *testing.T) { + t.Parallel() + + pool := createConnPool(t, 1) + // Intentionaly close the pool now so we can test ErrClosedPool + pool.Close() + + c, err := pool.Acquire() + if c != nil { + t.Fatalf("Expected acquired connection to be nil, instead it was '%v'", c) + } + + if err == nil || err != pgx.ErrClosedPool { + t.Fatalf("Expected error to be pgx.ErrClosedPool, instead it was '%v'", err) + } +} + func TestPoolReleaseWithTransactions(t *testing.T) { t.Parallel()