Make client.end return promise with active query

This commit is contained in:
Brian Carlson 2017-07-21 14:51:34 -07:00 committed by Brian C
parent d4bb51f08e
commit 66c6776f6e
2 changed files with 35 additions and 1 deletions

View File

@ -400,7 +400,7 @@ Client.prototype.end = function (cb) {
// if we have an active query we need to force a disconnect
// on the socket - otherwise a hung query could block end forever
this.connection.stream.destroy(new Error('Connection terminated by user'))
return
return cb ? cb() : Promise.resolve()
}
if (cb) {
this.connection.end()

View File

@ -0,0 +1,34 @@
"use strict"
var helper = require('./../test-helper')
const suite = new helper.Suite()
suite.test('calling end during active query should return a promise', (done) => {
const client = new helper.pg.Client()
let callCount = 0
// ensure both the query rejects and the end promise resolves
const after = () => {
if (++callCount > 1) {
done()
}
}
client.connect().then(() => {
client.query('SELECT NOW()').catch(after)
client.end().then(after)
})
})
suite.test('calling end during an active query should call end callback', (done) => {
const client = new helper.pg.Client()
let callCount = 0
// ensure both the query rejects and the end callback fires
const after = () => {
if (++callCount > 1) {
done()
}
}
client.connect().then(() => {
client.query('SELECT NOW()').catch(after)
client.end(after)
})
})