Skip to content

Commit a15d08e

Browse files
cdeevfrrDanny Rivers
and
Danny Rivers
authoredDec 16, 2020
Handle errors despite timeouts (#71)
* 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>
1 parent bf0ae23 commit a15d08e

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
 

‎lib/retry_operation.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ RetryOperation.prototype.retry = function(err) {
5050
}
5151
var currentTime = new Date().getTime();
5252
if (err && currentTime - this._operationStart >= this._maxRetryTime) {
53+
this._errors.push(err);
5354
this._errors.unshift(new Error('RetryOperation timeout occurred'));
5455
return false;
5556
}

‎test/integration/test-retry-operation.js

+35
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,38 @@ var retry = require(common.dir.lib + '/retry');
256256

257257
fn();
258258
})();
259+
260+
(function testErrorsPreservedWhenMaxRetryTimeExceeded() {
261+
var error = new Error('some error');
262+
var maxRetryTime = 30;
263+
var operation = retry.operation({
264+
minTimeout: 1,
265+
maxRetryTime: maxRetryTime
266+
});
267+
268+
var finalCallback = fake.callback('finalCallback');
269+
fake.expectAnytime(finalCallback);
270+
271+
var longAsyncFunction = function (wait, callback){
272+
setTimeout(callback, wait);
273+
};
274+
275+
var fn = function() {
276+
var startTime = new Date().getTime();
277+
operation.attempt(function() {
278+
279+
var curTime = new Date().getTime();
280+
longAsyncFunction(maxRetryTime - (curTime - startTime - 1), function(){
281+
if (operation.retry(error)) {
282+
assert.fail('timeout should be occurred');
283+
return;
284+
}
285+
286+
assert.strictEqual(operation.mainError(), error);
287+
finalCallback();
288+
});
289+
});
290+
};
291+
292+
fn();
293+
})();

0 commit comments

Comments
 (0)
Please sign in to comment.