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; };