Skip to content

Commit

Permalink
Handle errors despite timeouts (#71)
Browse files Browse the repository at this point in the history
* Ensures that timeouts don't prevent errors from getting into the _errors array.

* Replaces a test that used internal knowledge with a test targeted towards actual use cases.

Co-authored-by: Danny Rivers <drivers@roirevolution.com>
  • Loading branch information
cdeevfrr and Danny Rivers committed Dec 16, 2020
1 parent bf0ae23 commit a15d08e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/retry_operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ RetryOperation.prototype.retry = function(err) {
}
var 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
35 changes: 35 additions & 0 deletions test/integration/test-retry-operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,38 @@ var retry = require(common.dir.lib + '/retry');

fn();
})();

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

var finalCallback = fake.callback('finalCallback');
fake.expectAnytime(finalCallback);

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

var fn = function() {
var startTime = new Date().getTime();
operation.attempt(function() {

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

assert.strictEqual(operation.mainError(), error);
finalCallback();
});
});
};

fn();
})();

0 comments on commit a15d08e

Please sign in to comment.