Skip to content

Commit 5a6166d

Browse files
committed
Fire close callback when ready for next query
Working on fixing some timing issues in pg-query-stream it uncovered an issue where the cursor is firing its 'close' callback before it's actually entirely ready to dispatch another query. This change makes the close callback trigger when the connection re-enters `readyForQuery` state.
1 parent d28d826 commit 5a6166d

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

packages/pg-cursor/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const util = require('util')
66

77
let nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
88

9-
function Cursor (text, values, config) {
9+
function Cursor(text, values, config) {
1010
EventEmitter.call(this)
1111

1212
this._conf = config || {}
@@ -192,7 +192,7 @@ Cursor.prototype.close = function (cb) {
192192
this._closePortal()
193193
this.state = 'done'
194194
if (cb) {
195-
this.connection.once('closeComplete', function () {
195+
this.connection.once('readyForQuery', function () {
196196
cb()
197197
})
198198
}

packages/pg-cursor/test/close.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ describe('close', function () {
77
beforeEach(function (done) {
88
const client = (this.client = new pg.Client())
99
client.connect(done)
10-
client.on('drain', client.end.bind(client))
10+
})
11+
12+
this.afterEach(function (done) {
13+
this.client.end(done)
1114
})
1215

1316
it('can close a finished cursor without a callback', function (done) {
@@ -34,8 +37,9 @@ describe('close', function () {
3437
const cursor = new Cursor(text)
3538
const client = this.client
3639
client.query(cursor)
37-
cursor.read(25, function (err) {
40+
cursor.read(25, function (err, rows) {
3841
assert.ifError(err)
42+
assert.strictEqual(rows.length, 25)
3943
cursor.close(function (err) {
4044
assert.ifError(err)
4145
client.query('SELECT NOW()', done)

0 commit comments

Comments
 (0)