-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1058 from caolan/retryable
added retryable wrapper for async tasks
- Loading branch information
Showing
5 changed files
with
113 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import retry from './retry'; | ||
import rest from 'lodash/rest'; | ||
|
||
export default function (opts, task) { | ||
if (!task) { | ||
task = opts; | ||
opts = null; | ||
} | ||
return rest(function (args) { | ||
var callback = args.pop(); | ||
|
||
function taskFn(cb) { | ||
task.apply(null, args.concat([cb])); | ||
} | ||
|
||
if (opts) retry(opts, taskFn, callback); | ||
else retry(taskFn, callback); | ||
|
||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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); | ||
}); | ||
|
||
it('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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters