Skip to content

Commit dea0120

Browse files
committed
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.
1 parent a95d9ac commit dea0120

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

lib/client.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,11 @@ Client.prototype.query = function(config, values, callback) {
336336
return query;
337337
};
338338

339-
Client.prototype.end = function() {
339+
Client.prototype.end = function(cb) {
340340
this.connection.end();
341+
if (cb) {
342+
this.connection.once('end', cb);
343+
}
341344
};
342345

343346
Client.md5 = function(string) {

lib/connection.js

+3
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ Connection.prototype.attachListeners = function(stream) {
122122
packet = self._reader.read();
123123
}
124124
});
125+
stream.on('end', function() {
126+
self.emit('end');
127+
});
125128
};
126129

127130
Connection.prototype.requestSsl = function() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var helper = require('./test-helper')
2+
3+
var client = helper.client(assert.success(function() {
4+
client.end(assert.success(function() {
5+
}))
6+
}))

test/integration/test-helper.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ if(helper.args.native) {
77
}
88

99
//creates a client from cli parameters
10-
helper.client = function() {
10+
helper.client = function(cb) {
1111
var client = new Client(helper.config);
12-
client.connect();
12+
client.connect(cb);
1313
return client;
1414
};
1515

0 commit comments

Comments
 (0)