From dea0120873cf8046c0e5f1630795685e2499bc7a Mon Sep 17 00:00:00 2001 From: brianc Date: Thu, 11 Aug 2016 10:07:42 -0500 Subject: [PATCH] Add callback to client#end A long standing bug was the pure JS client didn't accept or call a callback on `client.end`. This is inconsistent with both the documentation & general node patterns. This fixes the issue & adds a test. The issue did not exist in the native version of the client. --- lib/client.js | 5 ++++- lib/connection.js | 3 +++ test/integration/client/end-callback-tests.js | 6 ++++++ test/integration/test-helper.js | 4 ++-- 4 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 test/integration/client/end-callback-tests.js diff --git a/lib/client.js b/lib/client.js index c4cea8239..54ab017cb 100644 --- a/lib/client.js +++ b/lib/client.js @@ -336,8 +336,11 @@ Client.prototype.query = function(config, values, callback) { return query; }; -Client.prototype.end = function() { +Client.prototype.end = function(cb) { this.connection.end(); + if (cb) { + this.connection.once('end', cb); + } }; Client.md5 = function(string) { diff --git a/lib/connection.js b/lib/connection.js index ca56c0679..6584c871d 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -122,6 +122,9 @@ Connection.prototype.attachListeners = function(stream) { packet = self._reader.read(); } }); + stream.on('end', function() { + self.emit('end'); + }); }; Connection.prototype.requestSsl = function() { diff --git a/test/integration/client/end-callback-tests.js b/test/integration/client/end-callback-tests.js new file mode 100644 index 000000000..997cfb0cc --- /dev/null +++ b/test/integration/client/end-callback-tests.js @@ -0,0 +1,6 @@ +var helper = require('./test-helper') + +var client = helper.client(assert.success(function() { + client.end(assert.success(function() { + })) +})) diff --git a/test/integration/test-helper.js b/test/integration/test-helper.js index 7905d157b..c6a4922dc 100644 --- a/test/integration/test-helper.js +++ b/test/integration/test-helper.js @@ -7,9 +7,9 @@ if(helper.args.native) { } //creates a client from cli parameters -helper.client = function() { +helper.client = function(cb) { var client = new Client(helper.config); - client.connect(); + client.connect(cb); return client; };