From a2a30b81d8481de9e62c70817e3d7bd412579e26 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 10 Jun 2016 19:05:22 -0400 Subject: [PATCH] bitcoind: start tryAllClients with the current round-robin index --- lib/services/bitcoind.js | 4 ++-- test/services/bitcoind.unit.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 04dd812c..ddcab71f 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -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); diff --git a/test/services/bitcoind.unit.js b/test/services/bitcoind.unit.js index 4eca90ae..99dc6589 100644 --- a/test/services/bitcoind.unit.js +++ b/test/services/bitcoind.unit.js @@ -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;