mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
Support a close callback when closing the stream
This commit is contained in:
parent
68819dffda
commit
d1ac31c105
3
index.js
3
index.js
@ -32,10 +32,11 @@ for(var key in Cursor.prototype) {
|
||||
}
|
||||
}
|
||||
|
||||
QueryStream.prototype.close = function() {
|
||||
QueryStream.prototype.close = function(cb) {
|
||||
this._closing = true
|
||||
var self = this
|
||||
Cursor.prototype.close.call(this, function(err) {
|
||||
if (cb) { cb(err); }
|
||||
if(err) return self.emit('error', err)
|
||||
process.nextTick(function() {
|
||||
self.push(null)
|
||||
|
||||
@ -34,7 +34,6 @@ helper('early close', function(client) {
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
helper('should not throw errors after early close', function(client) {
|
||||
it('can be closed early without error', function(done) {
|
||||
var stream = new QueryStream('SELECT * FROM generate_series(0, 2000) num');
|
||||
@ -80,3 +79,22 @@ helper('should not throw errors after early close', function(client) {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
helper('close callback', function (client) {
|
||||
it('notifies an optional callback when the conneciton is closed', function (done) {
|
||||
var stream = new QueryStream('SELECT * FROM generate_series(0, $1) num', [10], {batchSize: 2, highWaterMark: 2});
|
||||
var query = client.query(stream);
|
||||
query.once('readable', function() { // only reading once
|
||||
query.read();
|
||||
});
|
||||
query.once('readable', function() {
|
||||
query.close(function () {
|
||||
// nothing to assert. This test will time out if the callback does not work.
|
||||
done();
|
||||
});
|
||||
});
|
||||
query.on('close', function () {
|
||||
assert(false, "close event should not fire"); // no close event because we did not read to the end of the stream.
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user