From a536afb1a8baa6d584bd460e7c1286d75bb36fe3 Mon Sep 17 00:00:00 2001 From: Brian C Date: Thu, 11 Aug 2016 10:17:03 -0500 Subject: [PATCH] Add callback to client#end (#1106) 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 c4cea823..54ab017c 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 ca56c067..6584c871 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 00000000..997cfb0c --- /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 7905d157..c6a4922d 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; };