fix callback called twice for client/request

This fixes the errback being called twice when both a client and
request error occur on the same command call.
This commit is contained in:
freewil 2012-04-26 20:57:00 -04:00
parent a8d6f646a7
commit ff2ea67a7f
3 changed files with 30 additions and 8 deletions

View File

@ -1,5 +1,9 @@
# node-bitcoin changelog
## v1.2.2 (2012/04/26)
Fix callback being called twice when a client and request error
occur on the same command call.
## v1.2.1 (2012/04/26)
* Add missing `getBlock` command

View File

@ -15,6 +15,7 @@ var Client = function(port, host, user, password) {
this.call = function(method, params, callback, errback, path) {
var client = http.createClient(port, host);
var clientRequestErrorCalled = false;
// First we encode the request into JSON
var requestJSON = JSON.stringify({
@ -40,14 +41,15 @@ var Client = function(port, host, user, password) {
var request = client.request('POST', path || '/', headers);
request.write(requestJSON);
client.on('error', function(e){
errback(e);
});
request.on('error', function(e){
errback(e);
});
function clientRequestErrorHandler(e) {
if (!clientRequestErrorCalled) {
clientRequestErrorCalled = true;
errback(e);
}
}
client.on('error', clientRequestErrorHandler);
request.on('error', clientRequestErrorHandler);
request.on('response', function(response) {
// We need to buffer the response chunks in a nonblocking way.

View File

@ -174,5 +174,21 @@ vows.describe('api').addBatch({
},
},
},
'creating a client on a non-listening port': {
topic: function() {
return new bitcoin.Client(config.host, 9897, 'baduser', 'badpwd');
},
'will return client object': function(client) {
assert.equal(typeof client, 'object');
},
'but when calling a command': {
topic: function(client) {
client.listSinceBlock(this.callback);
},
'should not call callback more than once': function(err, result) {
assert.instanceOf(err, Error);
}
}
}
}).export(module);