From ade7ab95a0c3cf41b35350b94b21ef8aad4dbe2e Mon Sep 17 00:00:00 2001 From: jeromew Date: Fri, 29 Jul 2016 23:45:01 +0000 Subject: [PATCH 1/3] Test: `end` event should not be triggered 2 times on copy-from --- test/copy-from.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/copy-from.js b/test/copy-from.js index b8e6e0a..1765ca5 100644 --- a/test/copy-from.js +++ b/test/copy-from.js @@ -39,6 +39,7 @@ var testRange = function(top) { fromClient.query('SELECT COUNT(*) FROM numbers', function(err, res) { assert.ifError(err) 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') countDone() var firstRowDone = gonna('have correct result') @@ -54,3 +55,19 @@ var testRange = function(top) { } 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() From 7003f6070fd8a4997ec7fe52ca32a8bfd155838a Mon Sep 17 00:00:00 2001 From: jeromew Date: Fri, 29 Jul 2016 23:50:09 +0000 Subject: [PATCH 2/3] Fix issue #54: `end` is being triggered 2 times --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d6f0399..17b7237 100644 --- a/index.js +++ b/index.js @@ -62,8 +62,9 @@ CopyStreamQuery.prototype.handleCommandComplete = function(msg) { this.rowCount = parseInt(match[1], 10) } - this.unpipe() - this.emit('end') + // unpipe from connection + this.unpipe(this.connection) + this.connection = null } CopyStreamQuery.prototype.handleReadyForQuery = function() { From b2e108571e40413889efdc03e05cc6fffe4676b0 Mon Sep 17 00:00:00 2001 From: jeromew Date: Sat, 30 Jul 2016 00:08:54 +0000 Subject: [PATCH 3/3] Issue #54: We should probably delay the _flush cb() to CommandComplete --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 17b7237..ccac224 100644 --- a/index.js +++ b/index.js @@ -43,7 +43,7 @@ CopyStreamQuery.prototype._flush = function(cb) { var Int32Len = 4; var finBuffer = Buffer([code.CopyDone, 0, 0, 0, Int32Len]) this.push(finBuffer) - cb() + this.cb_flush = cb } CopyStreamQuery.prototype.handleError = function(e) { @@ -62,6 +62,10 @@ CopyStreamQuery.prototype.handleCommandComplete = function(msg) { this.rowCount = parseInt(match[1], 10) } + // we delay the _flush cb so that the 'end' event is + // triggered after CommandComplete + this.cb_flush() + // unpipe from connection this.unpipe(this.connection) this.connection = null