Skip to content

Commit

Permalink
Implement operation.reset(), closes tim-kos#40.
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-kos committed Mar 28, 2018
1 parent 39c9e6f commit 8499582
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ the current attempt number.

Allows you to stop the operation being retried. Useful for aborting the operation on a fatal error etc.

#### retryOperation.reset()

Resets the internal state of the operation object, so that you can call `attempt()` again as if this was a new operation object.

#### retryOperation.attempts()

Returns an int representing the number of attempts it took to call `fn` before it was successful.
Expand Down
6 changes: 6 additions & 0 deletions lib/retry_operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function RetryOperation(timeouts, options) {
options = { forever: options };
}

this._originalTimeouts = JSON.parse(JSON.stringify(timeouts));
this._timeouts = timeouts;
this._options = options || {};
this._maxRetryTime = options && options.maxRetryTime || Infinity;
Expand All @@ -21,6 +22,11 @@ function RetryOperation(timeouts, options) {
}
module.exports = RetryOperation;

RetryOperation.prototype.reset = function() {
this._attempts = 1;
this._timeouts = this._originalTimeouts;
}

RetryOperation.prototype.stop = function() {
if (this._timeout) {
clearTimeout(this._timeout);
Expand Down
42 changes: 40 additions & 2 deletions test/integration/test-retry-operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,45 @@ var assert = common.assert;
var fake = common.fake.create();
var retry = require(common.dir.lib + '/retry');

(function testReset() {
var error = new Error('some error');
var operation = retry.operation([1, 2, 3]);
var attempts = 0;

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

var expectedFinishes = 1;
var finishes = 0;

var fn = function() {
operation.attempt(function(currentAttempt) {
attempts++;
assert.equal(currentAttempt, attempts);
if (operation.retry(error)) {
return;
}

finishes++
assert.equal(expectedFinishes, finishes);
assert.strictEqual(attempts, 4);
assert.strictEqual(operation.attempts(), attempts);
assert.strictEqual(operation.mainError(), error);

if (finishes < 2) {
attempts = 0;
expectedFinishes++;
operation.reset();
fn()
} else {
finalCallback();
}
});
};

fn();
})();

(function testErrors() {
var operation = retry.operation();

Expand Down Expand Up @@ -53,7 +92,6 @@ var retry = require(common.dir.lib + '/retry');
})();

(function testRetry() {
var times = 3;
var error = new Error('some error');
var operation = retry.operation([1, 2, 3]);
var attempts = 0;
Expand Down Expand Up @@ -217,4 +255,4 @@ var retry = require(common.dir.lib + '/retry');
};

fn();
})();
})();

0 comments on commit 8499582

Please sign in to comment.