bitcoind: start tryAllClients with the current round-robin index

This commit is contained in:
Braydon Fuller 2016-06-10 19:05:22 -04:00
parent 6ac912545b
commit a2a30b81d8
2 changed files with 33 additions and 2 deletions

View File

@ -430,10 +430,10 @@ Bitcoin.prototype._resetCaches = function() {
Bitcoin.prototype._tryAllClients = function(func, callback) {
var self = this;
var nodesIndex = 0;
var nodesIndex = this.nodesIndex;
var retry = function(done) {
var client = self.nodes[nodesIndex].client;
nodesIndex++;
nodesIndex = (nodesIndex + 1) % self.nodes.length;
func(client, done);
};
async.retry({times: this.nodes.length, interval: this.tryAllInterval || 1000}, retry, callback);

View File

@ -556,6 +556,37 @@ describe('Bitcoin Service', function() {
done();
});
});
it('will start using the current node index (round-robin)', function(done) {
var bitcoind = new BitcoinService(baseConfig);
bitcoind.tryAllInterval = 1;
bitcoind.nodes.push({
client: {
getInfo: sinon.stub().callsArgWith(0, new Error('2'))
}
});
bitcoind.nodes.push({
client: {
getInfo: sinon.stub().callsArgWith(0, new Error('3'))
}
});
bitcoind.nodes.push({
client: {
getInfo: sinon.stub().callsArgWith(0, new Error('1'))
}
});
bitcoind.nodesIndex = 2;
bitcoind._tryAllClients(function(client, next) {
client.getInfo(next);
}, function(err) {
err.should.be.instanceOf(Error);
err.message.should.equal('3');
bitcoind.nodes[0].client.getInfo.callCount.should.equal(1);
bitcoind.nodes[1].client.getInfo.callCount.should.equal(1);
bitcoind.nodes[2].client.getInfo.callCount.should.equal(1);
bitcoind.nodesIndex.should.equal(2);
done();
});
});
it('will get error if all clients fail', function(done) {
var bitcoind = new BitcoinService(baseConfig);
bitcoind.tryAllInterval = 1;