Merge pull request #2042 from brianc/bmc/callback-on-ready

Fire close callback when ready for next query
This commit is contained in:
Brian C 2019-12-26 21:30:06 -06:00 committed by GitHub
commit 2431a63853
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 4 deletions

View File

@ -6,7 +6,7 @@ const util = require('util')
let nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
function Cursor (text, values, config) {
function Cursor(text, values, config) {
EventEmitter.call(this)
this._conf = config || {}
@ -192,7 +192,7 @@ Cursor.prototype.close = function (cb) {
this._closePortal()
this.state = 'done'
if (cb) {
this.connection.once('closeComplete', function () {
this.connection.once('readyForQuery', function () {
cb()
})
}

View File

@ -7,7 +7,10 @@ describe('close', function () {
beforeEach(function (done) {
const client = (this.client = new pg.Client())
client.connect(done)
client.on('drain', client.end.bind(client))
})
this.afterEach(function (done) {
this.client.end(done)
})
it('can close a finished cursor without a callback', function (done) {
@ -34,8 +37,9 @@ describe('close', function () {
const cursor = new Cursor(text)
const client = this.client
client.query(cursor)
cursor.read(25, function (err) {
cursor.read(25, function (err, rows) {
assert.ifError(err)
assert.strictEqual(rows.length, 25)
cursor.close(function (err) {
assert.ifError(err)
client.query('SELECT NOW()', done)