fixed fetch currencies + improved test coverage

This commit is contained in:
Ivan Socolsky 2014-11-21 12:11:35 -03:00
parent 5e9c72bdf1
commit 379e15525a
2 changed files with 245 additions and 178 deletions

View File

@ -41,19 +41,22 @@ RateService.singleton = function(opts) {
RateService.prototype._fetchCurrencies = function() {
var self = this;
log.info('Fetching exchange rates');
var backoffSeconds = 5;
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({
url: rateServiceUrl,
json: true
}, function(err, res, body) {
if (err || !body) {
log.debug('Error fetching exchange rates', err);
setTimeout(function () {
backoffSeconds *= 1.5;
setTimeout(retrieve, backoffSeconds * 1000);
retrieve();
}, backoffSeconds * 1000);
return;
}
_.each(body, function(currency) {
@ -68,12 +71,13 @@ RateService.prototype._fetchCurrencies = function() {
_.each(self._queued, function(callback) {
setTimeout(callback, 1);
});
setTimeout(function() {
self._fetchCurrencies()
}, updateFrequencySeconds * 1000);
setTimeout(retrieve, updateFrequencySeconds * 1000);
});
};
retrieve();
};
RateService.prototype._getRate = function(code) {
return this._rates[code];
};

View File

@ -3,16 +3,78 @@
var RateService = copay.RateService;
describe('RateService model', function() {
before(function() {
sinon.stub(RateService.prototype, '_fetchCurrencies').returns();
});
after(function() {});
it('should create an instance', function() {
var rs = new RateService();
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() {
it('should throw error when unavailable', function() {
var rs = new RateService();
@ -177,3 +239,4 @@ describe('RateService model', function() {
});
});
});
});