fixed fetch currencies + improved test coverage
This commit is contained in:
parent
5e9c72bdf1
commit
379e15525a
2 changed files with 245 additions and 178 deletions
|
|
@ -41,19 +41,22 @@ RateService.singleton = function(opts) {
|
||||||
RateService.prototype._fetchCurrencies = function() {
|
RateService.prototype._fetchCurrencies = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
log.info('Fetching exchange rates');
|
|
||||||
|
|
||||||
var backoffSeconds = 5;
|
var backoffSeconds = 5;
|
||||||
var updateFrequencySeconds = 3600;
|
var updateFrequencySeconds = 3600;
|
||||||
var rateServiceUrl = 'https://bitpay.com/api/rates';
|
var rateServiceUrl = 'https://bitpaya.com/api/rates';
|
||||||
|
|
||||||
|
var retrieve = function () {
|
||||||
|
log.info('Fetching exchange rates');
|
||||||
self.request.get({
|
self.request.get({
|
||||||
url: rateServiceUrl,
|
url: rateServiceUrl,
|
||||||
json: true
|
json: true
|
||||||
}, function(err, res, body) {
|
}, function(err, res, body) {
|
||||||
if (err || !body) {
|
if (err || !body) {
|
||||||
|
log.debug('Error fetching exchange rates', err);
|
||||||
|
setTimeout(function () {
|
||||||
backoffSeconds *= 1.5;
|
backoffSeconds *= 1.5;
|
||||||
setTimeout(retrieve, backoffSeconds * 1000);
|
retrieve();
|
||||||
|
}, backoffSeconds * 1000);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_.each(body, function(currency) {
|
_.each(body, function(currency) {
|
||||||
|
|
@ -68,12 +71,13 @@ RateService.prototype._fetchCurrencies = function() {
|
||||||
_.each(self._queued, function(callback) {
|
_.each(self._queued, function(callback) {
|
||||||
setTimeout(callback, 1);
|
setTimeout(callback, 1);
|
||||||
});
|
});
|
||||||
setTimeout(function() {
|
setTimeout(retrieve, updateFrequencySeconds * 1000);
|
||||||
self._fetchCurrencies()
|
|
||||||
}, updateFrequencySeconds * 1000);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
retrieve();
|
||||||
|
};
|
||||||
|
|
||||||
RateService.prototype._getRate = function(code) {
|
RateService.prototype._getRate = function(code) {
|
||||||
return this._rates[code];
|
return this._rates[code];
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,78 @@
|
||||||
var RateService = copay.RateService;
|
var RateService = copay.RateService;
|
||||||
|
|
||||||
describe('RateService model', function() {
|
describe('RateService model', function() {
|
||||||
before(function() {
|
|
||||||
sinon.stub(RateService.prototype, '_fetchCurrencies').returns();
|
|
||||||
});
|
|
||||||
after(function() {});
|
|
||||||
|
|
||||||
it('should create an instance', function() {
|
it('should create an instance', function() {
|
||||||
var rs = new RateService();
|
var rs = new RateService();
|
||||||
should.exist(rs);
|
should.exist(rs);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Fetching currencies', function() {
|
||||||
|
var clock;
|
||||||
|
before(function () {
|
||||||
|
clock = sinon.useFakeTimers();
|
||||||
|
});
|
||||||
|
after(function () {
|
||||||
|
clock.restore();
|
||||||
|
});
|
||||||
|
it('should retry fetching currencies on error', function() {
|
||||||
|
var request = sinon.stub();
|
||||||
|
request.get = sinon.stub().yields('dummy error');
|
||||||
|
|
||||||
|
var rs = new RateService({
|
||||||
|
request: request
|
||||||
|
});
|
||||||
|
should.exist(rs);
|
||||||
|
request.get.calledOnce.should.be.true;
|
||||||
|
clock.tick(1000);
|
||||||
|
request.get.calledTwice.should.be.false;
|
||||||
|
clock.tick(4000);
|
||||||
|
request.get.calledTwice.should.be.true;
|
||||||
|
|
||||||
|
request.get = sinon.stub().yields(null, null, [{
|
||||||
|
code: 'USD',
|
||||||
|
name: 'United States Dollar',
|
||||||
|
rate: 2
|
||||||
|
}]);
|
||||||
|
clock.tick(7500);
|
||||||
|
request.get.calledOnce.should.be.true;
|
||||||
|
clock.tick(15000);
|
||||||
|
request.get.callCount.should.equal(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should refresh exchange rates after 1 hour', function() {
|
||||||
|
var request = sinon.stub();
|
||||||
|
request.get = sinon.stub().yields(null, null, [{
|
||||||
|
code: 'USD',
|
||||||
|
name: 'United States Dollar',
|
||||||
|
rate: 2
|
||||||
|
}]);
|
||||||
|
|
||||||
|
var rs = new RateService({
|
||||||
|
request: request
|
||||||
|
});
|
||||||
|
should.exist(rs);
|
||||||
|
request.get.calledOnce.should.be.true;
|
||||||
|
rs.toFiat(1e8, 'USD').should.equal(2);
|
||||||
|
|
||||||
|
request.get = sinon.stub().yields(null, null, [{
|
||||||
|
code: 'USD',
|
||||||
|
name: 'United States Dollar',
|
||||||
|
rate: 3
|
||||||
|
}]);
|
||||||
|
clock.tick(3600 * 1000);
|
||||||
|
request.get.calledOnce.should.be.true;
|
||||||
|
rs.toFiat(1e8, 'USD').should.equal(3);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Conversion methods', function() {
|
||||||
|
before(function() {
|
||||||
|
sinon.stub(RateService.prototype, '_fetchCurrencies').returns();
|
||||||
|
});
|
||||||
|
after(function() {
|
||||||
|
RateService.prototype._fetchCurrencies.restore();
|
||||||
|
});
|
||||||
|
|
||||||
describe('#toFiat', function() {
|
describe('#toFiat', function() {
|
||||||
it('should throw error when unavailable', function() {
|
it('should throw error when unavailable', function() {
|
||||||
var rs = new RateService();
|
var rs = new RateService();
|
||||||
|
|
@ -177,3 +239,4 @@ describe('RateService model', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue