Merge pull request #54 from jeromew/upstream-end

Test: `end` event should not be triggered 2 times on copy-from
This commit is contained in:
jeromew 2016-08-22 17:47:29 +02:00 committed by GitHub
commit 8174e10fb5
2 changed files with 25 additions and 3 deletions

View File

@ -43,7 +43,7 @@ CopyStreamQuery.prototype._flush = function(cb) {
var Int32Len = 4; var Int32Len = 4;
var finBuffer = Buffer([code.CopyDone, 0, 0, 0, Int32Len]) var finBuffer = Buffer([code.CopyDone, 0, 0, 0, Int32Len])
this.push(finBuffer) this.push(finBuffer)
cb() this.cb_flush = cb
} }
CopyStreamQuery.prototype.handleError = function(e) { CopyStreamQuery.prototype.handleError = function(e) {
@ -62,8 +62,13 @@ CopyStreamQuery.prototype.handleCommandComplete = function(msg) {
this.rowCount = parseInt(match[1], 10) this.rowCount = parseInt(match[1], 10)
} }
this.unpipe() // we delay the _flush cb so that the 'end' event is
this.emit('end') // triggered after CommandComplete
this.cb_flush()
// unpipe from connection
this.unpipe(this.connection)
this.connection = null
} }
CopyStreamQuery.prototype.handleReadyForQuery = function() { CopyStreamQuery.prototype.handleReadyForQuery = function() {

View File

@ -39,6 +39,7 @@ var testRange = function(top) {
fromClient.query('SELECT COUNT(*) FROM numbers', function(err, res) { fromClient.query('SELECT COUNT(*) FROM numbers', function(err, res) {
assert.ifError(err) assert.ifError(err)
assert.equal(res.rows[0].count, top, 'expected ' + top + ' rows but got ' + res.rows[0].count) assert.equal(res.rows[0].count, top, 'expected ' + top + ' rows but got ' + res.rows[0].count)
assert.equal(stream.rowCount, top, 'expected ' + top + ' rows but db count is ' + stream.rowCount)
//console.log('found ', res.rows.length, 'rows') //console.log('found ', res.rows.length, 'rows')
countDone() countDone()
var firstRowDone = gonna('have correct result') var firstRowDone = gonna('have correct result')
@ -54,3 +55,19 @@ var testRange = function(top) {
} }
testRange(1000) testRange(1000)
var testSingleEnd = function() {
var fromClient = client()
fromClient.query('CREATE TEMP TABLE numbers(num int)')
var txt = 'COPY numbers FROM STDIN';
var stream = fromClient.query(copy(txt))
var count = 0;
stream.on('end', function() {
count++;
assert(count==1, '`end` Event was triggered ' + count + ' times');
if (count == 1) fromClient.end();
})
stream.end(Buffer('1\n'))
}
testSingleEnd()