Skip to content

Commit

Permalink
Include previous error even if there's a timeout
Browse files Browse the repository at this point in the history
tim-kos/node-retry#70

Signed-off-by: Olli Vanhoja <olli.vanhoja@gmail.com>
  • Loading branch information
Danny Rivers authored and OlliV committed Dec 15, 2019
1 parent f6157a8 commit b67400c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions __tests__/test-retry-operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,33 @@ test("testMaxRetryTime", () => {
});
});
});

test('testErrorsPreservedWhenMaxRetryTimeExceeded', () => {
const error = new Error('some error');
const maxRetryTime = 30;
const operation = retry.operation({
minTimeout: 1,
maxRetryTime: maxRetryTime
});

const longAsyncFunction = function (wait, callback){
setTimeout(callback, wait);
};

return new Promise((resolve, reject) => {
const startTime = new Date().getTime();
operation.attempt(function() {

const curTime = new Date().getTime();
longAsyncFunction(maxRetryTime - (curTime - startTime - 1), function(){
if (operation.retry(error)) {
reject(new Error('timeout should be occurred'));
return;
}

expect(operation.mainError()).toBe(error);
resolve();
});
});
});
});
2 changes: 2 additions & 0 deletions lib/retry_operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ RetryOperation.prototype.retry = function(err) {
}
const currentTime = new Date().getTime();
if (err && currentTime - this._operationStart >= this._maxRetryTime) {
this._errors.push(err);
this._errors.unshift(new Error("RetryOperation timeout occurred"));

return false;
}

Expand Down

0 comments on commit b67400c

Please sign in to comment.