Return ErrClosedPool when Acquire() with closed pool

pull/312/head
Mike Graf 2017-08-28 22:30:42 -07:00
parent f65776f084
commit 30896744c8
2 changed files with 21 additions and 1 deletions

View File

@ -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

View File

@ -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()