Skip to content

Commit

Permalink
Merge pull request #754 from bao987654321/fix/_eachLimit_continues_af…
Browse files Browse the repository at this point in the history
…ter_error

Fixed issue with _eachLimit continuing to run after error.  Fixes #649
  • Loading branch information
aearly committed May 22, 2015
2 parents 3a2aed9 + 8f5dbd6 commit a6d2301
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,20 @@
var completed = 0;
var started = 0;
var running = 0;
var errored = false;

(function replenish () {
if (completed >= arr.length) {
return callback(null);
}

while (running < limit && started < arr.length) {
while (running < limit && started < arr.length && !errored) {
started += 1;
running += 1;
iterator(arr[started - 1], function (err) {
if (err) {
callback(err);
errored = true;
callback = noop;
}
else {
Expand Down
83 changes: 83 additions & 0 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,42 @@ exports['parallel call in another context'] = function(test) {
vm.runInNewContext(fn, sandbox);
};

exports['parallel does not continue replenishing after error'] = function (test) {
var started = 0;
var arr = [
funcToCall,
funcToCall,
funcToCall,
funcToCall,
funcToCall,
funcToCall,
funcToCall,
funcToCall,
funcToCall,
];
var delay = 10;
var limit = 3;
var maxTime = 10 * arr.length;
function funcToCall(callback) {
started ++;
if (started === 3) {
return callback(new Error ("Test Error"));
}
setTimeout(function(){
callback();
}, delay);
}

async.parallelLimit(arr, limit, function(x, callback) {

}, function(err){});

setTimeout(function(){
test.equal(started, 3);
test.done();
}, maxTime);
};


exports['series'] = function(test){
var call_order = [];
Expand Down Expand Up @@ -1386,6 +1422,30 @@ exports['eachLimit synchronous'] = function(test){
});
};


exports['eachLimit does not continue replenishing after error'] = function (test) {
var started = 0;
var arr = [0,1,2,3,4,5,6,7,8,9];
var delay = 10;
var limit = 3;
var maxTime = 10 * arr.length;

async.eachLimit(arr, limit, function(x, callback) {
started ++;
if (started === 3) {
return callback(new Error ("Test Error"));
}
setTimeout(function(){
callback();
}, delay);
}, function(err){});

setTimeout(function(){
test.equal(started, 3);
test.done();
}, maxTime);
};

exports['forEachSeries alias'] = function (test) {
test.strictEqual(async.eachSeries, async.forEachSeries);
test.done();
Expand Down Expand Up @@ -1668,6 +1728,29 @@ exports['mapLimit error'] = function(test){
setTimeout(test.done, 25);
};

exports['mapLimit does not continue replenishing after error'] = function (test) {
var started = 0;
var arr = [0,1,2,3,4,5,6,7,8,9];
var delay = 10;
var limit = 3;
var maxTime = 10 * arr.length;

async.mapLimit(arr, limit, function(x, callback) {
started ++;
if (started === 3) {
return callback(new Error ("Test Error"));
}
setTimeout(function(){
callback();
}, delay);
}, function(err){});

setTimeout(function(){
test.equal(started, 3);
test.done();
}, maxTime);
};


exports['reduce'] = function(test){
var call_order = [];
Expand Down

0 comments on commit a6d2301

Please sign in to comment.