Skip to content

Commit 66c6776

Browse files
committed
Make client.end return promise with active query
1 parent d4bb51f commit 66c6776

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ Client.prototype.end = function (cb) {
400400
// if we have an active query we need to force a disconnect
401401
// on the socket - otherwise a hung query could block end forever
402402
this.connection.stream.destroy(new Error('Connection terminated by user'))
403-
return
403+
return cb ? cb() : Promise.resolve()
404404
}
405405
if (cb) {
406406
this.connection.end()
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict"
2+
var helper = require('./../test-helper')
3+
4+
const suite = new helper.Suite()
5+
6+
suite.test('calling end during active query should return a promise', (done) => {
7+
const client = new helper.pg.Client()
8+
let callCount = 0
9+
// ensure both the query rejects and the end promise resolves
10+
const after = () => {
11+
if (++callCount > 1) {
12+
done()
13+
}
14+
}
15+
client.connect().then(() => {
16+
client.query('SELECT NOW()').catch(after)
17+
client.end().then(after)
18+
})
19+
})
20+
21+
suite.test('calling end during an active query should call end callback', (done) => {
22+
const client = new helper.pg.Client()
23+
let callCount = 0
24+
// ensure both the query rejects and the end callback fires
25+
const after = () => {
26+
if (++callCount > 1) {
27+
done()
28+
}
29+
}
30+
client.connect().then(() => {
31+
client.query('SELECT NOW()').catch(after)
32+
client.end(after)
33+
})
34+
})

0 commit comments

Comments
 (0)