nats-jobs
Version:
Background job processor using NATS
58 lines (57 loc) • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const globals_1 = require("@jest/globals");
const util_1 = require("./util");
(0, globals_1.describe)('getNextBackoff', () => {
(0, globals_1.test)('should always the same value if number is passed', () => {
const msg = {
info: {
redeliveryCount: 2,
},
};
const result = (0, util_1.getNextBackoff)(1000, msg);
(0, globals_1.expect)(result).toBe(1000);
});
(0, globals_1.test)('should return next value if array is passed', () => {
const msg = {
info: {
redeliveryCount: 2,
},
};
const backoff = [1000, 2000, 3000];
const result = (0, util_1.getNextBackoff)(backoff, msg);
(0, globals_1.expect)(result).toBe(2000);
});
(0, globals_1.test)('should return last value if array is passed and end of array reached', () => {
const msg = {
info: {
redeliveryCount: 5,
},
};
const backoff = [1000, 2000, 3000];
const result = (0, util_1.getNextBackoff)(backoff, msg);
(0, globals_1.expect)(result).toBe(3000);
});
});
(0, globals_1.describe)('expBackoff', () => {
(0, globals_1.test)('should double values with defaults', () => {
const result = (0, util_1.expBackoff)(1000);
(0, globals_1.expect)(result).toEqual([1000, 2000, 4000, 8000, 16000]);
});
(0, globals_1.test)('should return desired number of entries without repeating', () => {
const result = (0, util_1.expBackoff)(1000, { numEntries: 7 });
(0, globals_1.expect)(result).toEqual([1000, 2000, 4000, 8000, 16000, 32000, 64000]);
});
(0, globals_1.test)('should limit entries according to numEntries', () => {
const result = (0, util_1.expBackoff)(1000, { numEntries: 3 });
(0, globals_1.expect)(result).toEqual([1000, 2000, 4000]);
});
(0, globals_1.test)('should repeat after n entries according to repeatAfter', () => {
const result = (0, util_1.expBackoff)(1000, { repeatAfter: 3 });
(0, globals_1.expect)(result).toEqual([1000, 2000, 4000, 4000, 4000]);
});
(0, globals_1.test)('should respect all options', () => {
const result = (0, util_1.expBackoff)(5000, { repeatAfter: 4, numEntries: 6 });
(0, globals_1.expect)(result).toEqual([5000, 10000, 20000, 40000, 40000, 40000]);
});
});