Skip to content

Commit

Permalink
Add additional pool test & deprecate .end
Browse files Browse the repository at this point in the history
There are no tests covering cursor.end().  It's also a weird method to have on the cursor as it terminates the connection to postgres internally.  I don't recall why I added this method in the first place but its not correct to close a connection to postgres by calling .end on a cursor...seems like a pretty big footgun.  If you have a pooled client and you call `cursor.end` it'll close the client internally and likely confuse the pool.  Plus it's just weird to be able to close a connection by calling .end on a query or cursor.  So...I'm deprecating that method.
  • Loading branch information
brianc committed Oct 28, 2019
1 parent 6d47026 commit d3aee3d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,15 @@ Cursor.prototype._getRows = function(rows, cb) {
this.connection.flush()
}

Cursor.prototype.end = function(cb) {
// users really shouldn't be calling 'end' here and terminating a connection to postgres
// via the low level connection.end api
Cursor.prototype.end = util.deprecate(function(cb) {
if (this.state !== 'initialized') {
this.connection.sync()
}
this.connection.once('end', cb)
this.connection.end()
}
}, 'Cursor.end is deprecated. Call end on the client itself to end a connection to the database.')

Cursor.prototype.close = function(cb) {
if (this.state === 'done') {
Expand Down
20 changes: 20 additions & 0 deletions test/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,24 @@ describe('pool', function() {
done()
})
})

it('can close multiple times on a pool', async function() {
const pool = new pg.Pool({ max: 1 })
const run = () =>
new Promise(async resolve => {
const cursor = new Cursor(text)
const client = await pool.connect()
client.query(cursor)
cursor.read(25, function(err) {
assert.ifError(err)
cursor.close(function(err) {
assert.ifError(err)
client.release()
resolve()
})
})
})
await Promise.all([run(), run(), run()])
await pool.end()
})
})

0 comments on commit d3aee3d

Please sign in to comment.