Comparing version 0.0.3 to 0.9.1
{ | ||
"name": "rerun", | ||
"description": "A retry library for node.js", | ||
"version": "0.0.3", | ||
"version": "0.9.1", | ||
"author": "BigPanda <noam@bigpanda.io>", | ||
@@ -20,3 +20,3 @@ "repository": { | ||
}, | ||
"main": "./src/retry.js", | ||
"main": "./src/index.js", | ||
"scripts": { | ||
@@ -23,0 +23,0 @@ "test": "mocha --recursive test/*" |
module.exports = { | ||
request: require('./request'), | ||
RetryError: require('./error/retry'), | ||
RejectError: require('./error/reject'), | ||
promise: require('./promise') | ||
} |
var Q = require('q'); | ||
var RetryError = require('./error/retry'); | ||
var RejectError = require('./error/reject'); | ||
module.exports = function (toRetry, options) { | ||
var deferred = Q.defer(); | ||
var options = options || {}; | ||
var retries = options.retries || 3; | ||
var timeout = options.timeout || 50; | ||
var factor = options.factor || 1; | ||
@@ -13,6 +16,7 @@ function _succeed(returned) { | ||
function _failed(error) { | ||
if (!(error instanceof RetryError) || --retries <= 0) { | ||
return deferred.reject(error); | ||
if (error instanceof RejectError || --retries <= 0) { | ||
deferred.reject(error); | ||
} else { | ||
return toRetry().then(_succeed, _failed); | ||
Q.delay(timeout).then(function () { return toRetry() }).then(_succeed, _failed); | ||
timeout *= factor; | ||
} | ||
@@ -19,0 +23,0 @@ } |
var request = require('request'); | ||
var RetryError = require('./error/retry'); | ||
var RejectError = require('./error/reject'); | ||
var promiseRetry = require('./promise'); | ||
var Q = require('q'); | ||
module.exports = function (requestData, retries) { | ||
var retries = retries || requestData.retries; | ||
module.exports = function (requestData) { | ||
return promiseRetry(function () { | ||
@@ -13,9 +11,12 @@ return Q.nfcall(request, requestData).then(function (answer) { | ||
var body = answer[1]; | ||
if (response && response.statusCode >= 300 && response.statusCode != 400) { | ||
return Q.reject(new RetryError('Status code: ' + response.statusCode + '. ' + (body ? 'body: ' + JSON.stringify(body) : ''))); | ||
if (response && response.statusCode == 400) { | ||
return Q.reject(new RejectError('Status code: ' + response.statusCode + '. ' + (body ? 'body: ' + JSON.stringify(body) : ''))); | ||
} else if (response && response.statusCode >= 300) { | ||
return Q.reject(new Error('Status code: ' + response.statusCode + '. ' + (body ? 'body: ' + JSON.stringify(body) : ''))); | ||
} | ||
return Q(answer); | ||
}, { retries: retries }); | ||
}); | ||
}) | ||
}, requestData); | ||
} |
@@ -21,3 +21,3 @@ var chai = require('chai'); | ||
done(new Error('should fail')); | ||
}, function (error) { | ||
}, function () { | ||
scope.done(); | ||
@@ -36,6 +36,6 @@ done(); | ||
]; | ||
var scope = nock('http://localhost:3027').post('/test', JSON.stringify({ objects: array })).times(2).reply(200); | ||
nock('http://localhost:3027').post('/test', JSON.stringify({ objects: array })).reply(401); | ||
nock('http://localhost:3027').post('/test', JSON.stringify({ objects: array })).reply(200); | ||
var promise = request({ url: 'http://localhost:3027/test', json: { objects: array }, retries: 4, method: 'POST' }); | ||
promise.then(function () { | ||
scope.done(); | ||
done(); | ||
@@ -46,2 +46,39 @@ }, function (error) { | ||
}); | ||
it('should not retry on user error', function (done) { | ||
var id = { complicated: 'id' }; | ||
var data = [ | ||
{ foo: 'bar' } | ||
]; | ||
var array = [ | ||
{ id: id, data: data } | ||
]; | ||
var scope = nock('http://localhost:3027').post('/test', JSON.stringify({ objects: array })).reply(400); | ||
var promise = request({ url: 'http://localhost:3027/test', json: { objects: array }, retries: 3, method: 'POST' }); | ||
promise.then(function () { | ||
done(new Error('Should fail')); | ||
}, function () { | ||
scope.done(); | ||
done(); | ||
}); | ||
}); | ||
it('should wait exponential time', function (done) { | ||
var id = { complicated: 'id' }; | ||
var data = [ | ||
{ foo: 'bar' } | ||
]; | ||
var array = [ | ||
{ id: id, data: data } | ||
]; | ||
nock('http://localhost:3027').post('/test', JSON.stringify({ objects: array })).times(4).reply(401); | ||
var scope = nock('http://localhost:3027').post('/test', JSON.stringify({ objects: array })).reply(200); | ||
var promise = request({ url: 'http://localhost:3027/test', json: { objects: array }, retries: 5, factor: 2, timeout: 10, method: 'POST' }); | ||
var timeBefore = new Date().getTime(); | ||
promise.then(function () { | ||
expect(new Date().getTime() - timeBefore).to.lte(170).and.to.gte(130); | ||
scope.done(); | ||
done(); | ||
}, function (err) { | ||
done(err); | ||
}); | ||
}); | ||
}); |
16505
125