Brian C 8eca181d20
Fix pg-query-stream implementation (#2051)
* Fix pg-query-stream

There were some subtle behaviors with the stream being implemented incorrectly & not working as expected with async iteration.  I've modified the code based on #2050 and comments in #2035 to have better test coverage of async iterables and update the internals significantly to more closely match the readable stream interface.

Note: this is a __breaking__ (semver major) change to this package as the close event behavior is changed slightly, and `highWaterMark` is no longer supported.  It shouldn't impact most usage, but breaking regardless.

* Remove a bunch of additional code

* Add test for destroy + error propagation

* Add failing test for destroying unsubmitted stream

* Do not throw an uncatchable error when closing an unused cursor
2019-12-29 23:08:09 -06:00

55 lines
1.4 KiB
JavaScript

const assert = require('assert')
const Cursor = require('../')
const pg = require('pg')
const text = 'SELECT generate_series as num FROM generate_series(0, 50)'
describe('close', function () {
beforeEach(function (done) {
const client = (this.client = new pg.Client())
client.connect(done)
})
this.afterEach(function (done) {
this.client.end(done)
})
it('can close a finished cursor without a callback', function (done) {
const cursor = new Cursor(text)
this.client.query(cursor)
this.client.query('SELECT NOW()', done)
cursor.read(100, function (err) {
assert.ifError(err)
cursor.close()
})
})
it('closes cursor early', function (done) {
const cursor = new Cursor(text)
this.client.query(cursor)
this.client.query('SELECT NOW()', done)
cursor.read(25, function (err) {
assert.ifError(err)
cursor.close()
})
})
it('works with callback style', function (done) {
const cursor = new Cursor(text)
const client = this.client
client.query(cursor)
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)
})
})
})
it('is a no-op to "close" the cursor before submitting it', function (done) {
const cursor = new Cursor(text)
cursor.close(done)
})
})