Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added retryable wrapper for async tasks #1058

Merged
merged 5 commits into from
Mar 18, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import reject from './reject';
import rejectLimit from './rejectLimit';
import rejectSeries from './rejectSeries';
import retry from './retry';
import retryable from './retryable';
import seq from './seq';
import series from './series';
import setImmediate from './setImmediate';
Expand Down Expand Up @@ -115,6 +116,7 @@ export default {
rejectLimit: rejectLimit,
rejectSeries: rejectSeries,
retry: retry,
retryable: retryable,
seq: seq,
series: series,
setImmediate: setImmediate,
Expand Down Expand Up @@ -199,6 +201,7 @@ export {
rejectLimit as rejectLimit,
rejectSeries as rejectSeries,
retry as retry,
retryable as retryable,
seq as seq,
series as series,
setImmediate as setImmediate,
Expand Down
11 changes: 11 additions & 0 deletions lib/retryable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import retry from './retry';
import rest from 'lodash/rest';

export default function (opts, task) {
return rest(function (args) {
var callback = args.pop();
retry(opts, function (cb) {
task(...args.concat([cb]));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove splat?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

}, callback);
});
}
65 changes: 65 additions & 0 deletions mocha_test/retryable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var async = require('../lib');
var expect = require('chai').expect;
var assert = require('assert');

describe('retryable', function () {
it('basics', function (done) {
var calls = 0;
var retryableTask = async.retryable(3, function (arg, cb) {
calls++;
expect(arg).to.equal(42);
cb('fail');
});

retryableTask(42, function (err) {
expect(err).to.equal('fail');
expect(calls).to.equal(3);
done();
});

setTimeout(function () {
}, 15);
});

// TODO: re-enable when auto's args are swapped
it.skip('should work as an embedded task', function(done) {
var retryResult = 'RETRY';
var fooResults;
var retryResults;

async.auto({
dep: async.constant('dep'),
foo: ['dep', function(results, callback){
fooResults = results;
callback(null, 'FOO');
}],
retry: ['dep', async.retryable(function(results, callback) {
retryResults = results;
callback(null, retryResult);
})]
}, function(err, results){
assert.equal(results.retry, retryResult, "Incorrect result was returned from retry function");
assert.equal(fooResults, retryResults, "Incorrect results were passed to retry function");
done();
});
});

it('should work as an embedded task with interval', function(done) {
var start = new Date().getTime();
var opts = {times: 5, interval: 100};

async.auto({
foo: function(callback){
callback(null, 'FOO');
},
retry: async.retryable(opts, function(callback) {
callback('err');
})
}, function(){
var duration = new Date().getTime() - start;
var expectedMinimumDuration = (opts.times -1) * opts.interval;
assert(duration >= expectedMinimumDuration, "The duration should have been greater than " + expectedMinimumDuration + ", but was " + duration);
done();
});
});
});
42 changes: 0 additions & 42 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,48 +278,6 @@ exports['seq without callback'] = function (test) {
add2mul3.call(testcontext, 3);
};

// need to fix retry, this isn't working
/*
exports['retry as an embedded task'] = function(test) {
var retryResult = 'RETRY';
var fooResults;
var retryResults;

async.auto({
dep: async.constant('dep'),
foo: ['dep', function(results, callback){
fooResults = results;
callback(null, 'FOO');
}],
retry: ['dep', async.retry(function(results, callback) {
retryResults = results;
callback(null, retryResult);
})]
}, function(err, results){
test.equal(results.retry, retryResult, "Incorrect result was returned from retry function");
test.equal(fooResults, retryResults, "Incorrect results were passed to retry function");
test.done();
});
};

exports['retry as an embedded task with interval'] = function(test) {
var start = new Date().getTime();
var opts = {times: 5, interval: 100};

async.auto({
foo: function(callback){
callback(null, 'FOO');
},
retry: async.retry(opts, function(callback) {
callback('err');
})
}, function(){
var duration = new Date().getTime() - start;
var expectedMinimumDuration = (opts.times -1) * opts.interval;
test.ok(duration >= expectedMinimumDuration, "The duration should have been greater than " + expectedMinimumDuration + ", but was " + duration);
test.done();
});
};*/

exports['waterfall'] = {

Expand Down