Skip to content

Commit

Permalink
Add callback to client#end (#1106)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brianc committed Aug 11, 2016
1 parent a95d9ac commit a536afb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ Connection.prototype.attachListeners = function(stream) {
packet = self._reader.read();
}
});
stream.on('end', function() {
self.emit('end');
});
};

Connection.prototype.requestSsl = function() {
Expand Down
6 changes: 6 additions & 0 deletions test/integration/client/end-callback-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var helper = require('./test-helper')

var client = helper.client(assert.success(function() {
client.end(assert.success(function() {
}))
}))
4 changes: 2 additions & 2 deletions test/integration/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit a536afb

Please sign in to comment.