diff options
author | Caolan McMahon <caolan@caolanmcmahon.com> | 2013-03-02 20:44:29 +0000 |
---|---|---|
committer | Caolan McMahon <caolan@caolanmcmahon.com> | 2013-03-02 20:44:29 +0000 |
commit | b63b80abb77603e8b17bc3213c498d49fcb35e1f (patch) | |
tree | ecd748b6c795ac2a6ceda6b6d1bc0cbdc1fd19ef | |
parent | 4c8cfec8ca57fe694202b5625e660fed22f7aa97 (diff) | |
download | async-b63b80abb77603e8b17bc3213c498d49fcb35e1f.tar.gz |
added default concurrency for queue, closes #249
-rwxr-xr-x | lib/async.js | 3 | ||||
-rwxr-xr-x | test/test-async.js | 53 |
2 files changed, 56 insertions, 0 deletions
diff --git a/lib/async.js b/lib/async.js index 3ebad60..5aac1b1 100755 --- a/lib/async.js +++ b/lib/async.js @@ -693,6 +693,9 @@ }; async.queue = function (worker, concurrency) { + if (concurrency === undefined) { + concurrency = 1; + } function _insert(q, data, pos, callback) { if(data.constructor !== Array) { data = [data]; diff --git a/test/test-async.js b/test/test-async.js index e9efdd3..ddc86fe 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -1689,6 +1689,59 @@ exports['queue'] = function (test) { }; }; +exports['queue default concurrency'] = function (test) { + var call_order = [], + delays = [160,80,240,80]; + + // order of completion: 1,2,3,4 + + var q = async.queue(function (task, callback) { + setTimeout(function () { + call_order.push('process ' + task); + callback('error', 'arg'); + }, delays.splice(0,1)[0]); + }); + + q.push(1, function (err, arg) { + test.equal(err, 'error'); + test.equal(arg, 'arg'); + test.equal(q.length(), 3); + call_order.push('callback ' + 1); + }); + q.push(2, function (err, arg) { + test.equal(err, 'error'); + test.equal(arg, 'arg'); + test.equal(q.length(), 2); + call_order.push('callback ' + 2); + }); + q.push(3, function (err, arg) { + test.equal(err, 'error'); + test.equal(arg, 'arg'); + test.equal(q.length(), 1); + call_order.push('callback ' + 3); + }); + q.push(4, function (err, arg) { + test.equal(err, 'error'); + test.equal(arg, 'arg'); + test.equal(q.length(), 0); + call_order.push('callback ' + 4); + }); + test.equal(q.length(), 4); + test.equal(q.concurrency, 1); + + q.drain = function () { + test.same(call_order, [ + 'process 1', 'callback 1', + 'process 2', 'callback 2', + 'process 3', 'callback 3', + 'process 4', 'callback 4' + ]); + test.equal(q.concurrency, 1); + test.equal(q.length(), 0); + test.done(); + }; +}; + exports['queue error propagation'] = function(test){ var results = []; |