UNPKG

@house-agency/brewtils

Version:

The Brewery Node.js Utilities (brewtils)

109 lines (88 loc) 2.86 kB
const expect = require('chai').expect; const log = require('../log'); const q = require('q'); const retry = require('../retry'); describe('Retry', () => { before(() => { log.level('fatal'); }); it('shall success', () => { var invoked = 0; const func = function named_func() { invoked++; return q(true); }; return retry(func) .then(value => { expect(value).to.be.true; expect(invoked).to.equal(1); }); }); it('shall success after three retries with a 300 msec delay', () => { var invoked = 0; const time = (new Date()).getTime(); const func = function named_func() { invoked++; return q.try(() => { if (invoked < 3) { throw Error('Expected error'); } return true; }); }; return retry(func, null, 300) .then(value => { expect(value).to.be.true; expect(invoked).to.equal(3); expect(time + (300 * 2)).to.be.below((new Date()).getTime()); }); }); it('shall fail after four retries with a 100 msec delay', () => { var invoked = 0; const time = (new Date()).getTime(); const func = function named_func() { invoked++; return q.try(() => { throw Error('Expected error'); }); }; return retry(func, 4) .then(() => { throw new Error('Shall not happen'); }) .catch(error => { expect(error.message).to.equal('Expected error'); expect(invoked).to.equal(4); expect(time + (100 * 3)).to.be.below((new Date()).getTime()); }); }); it('shall not accept a specific error and retry otherwise', () => { var invoked = 0; const time = (new Date()).getTime(); const func = function named_func() { invoked++; return q.try(() => { if(invoked < 4) throw Error('Expected error'); else throw Error('Filtered error'); }); }; //Returns true when should be retried const filter_func = function error_func(error) { if(error.message === 'Filtered error') return false; else return true; }; return retry(func, 4, 300, filter_func) .then(() => { throw new Error('Shall not happen'); }) .catch(error => { expect(error.message).to.equal('Filtered error'); expect(invoked).to.equal(4); expect(time + (100 * 3)).to.be.below((new Date()).getTime()); }); }); });