Skip to content

Commit d3aee3d

Browse files
committed
Add additional pool test & deprecate .end
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.
1 parent 6d47026 commit d3aee3d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,15 @@ Cursor.prototype._getRows = function(rows, cb) {
154154
this.connection.flush()
155155
}
156156

157-
Cursor.prototype.end = function(cb) {
157+
// users really shouldn't be calling 'end' here and terminating a connection to postgres
158+
// via the low level connection.end api
159+
Cursor.prototype.end = util.deprecate(function(cb) {
158160
if (this.state !== 'initialized') {
159161
this.connection.sync()
160162
}
161163
this.connection.once('end', cb)
162164
this.connection.end()
163-
}
165+
}, 'Cursor.end is deprecated. Call end on the client itself to end a connection to the database.')
164166

165167
Cursor.prototype.close = function(cb) {
166168
if (this.state === 'done') {

test/pool.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,24 @@ describe('pool', function() {
8383
done()
8484
})
8585
})
86+
87+
it('can close multiple times on a pool', async function() {
88+
const pool = new pg.Pool({ max: 1 })
89+
const run = () =>
90+
new Promise(async resolve => {
91+
const cursor = new Cursor(text)
92+
const client = await pool.connect()
93+
client.query(cursor)
94+
cursor.read(25, function(err) {
95+
assert.ifError(err)
96+
cursor.close(function(err) {
97+
assert.ifError(err)
98+
client.release()
99+
resolve()
100+
})
101+
})
102+
})
103+
await Promise.all([run(), run(), run()])
104+
await pool.end()
105+
})
86106
})

0 commit comments

Comments
 (0)