diff options
author | Caolan McMahon <caolan.mcmahon@gmail.com> | 2014-03-28 12:41:10 +0000 |
---|---|---|
committer | Caolan McMahon <caolan.mcmahon@gmail.com> | 2014-03-28 12:41:10 +0000 |
commit | 191a5076bf4a9cb6ac87b1bf6e9d4eb0a13527d7 (patch) | |
tree | 5fb93d2e26634692c513ca9dd884b3a6d3c43b2f | |
parent | 287cb65116c34f5fff1b953c6b9a95bce0a3dbde (diff) | |
parent | c2b098bb46c9c17bb901a91073dae461dfad7ca8 (diff) | |
download | async-191a5076bf4a9cb6ac87b1bf6e9d4eb0a13527d7.tar.gz |
Merge pull request #433 from tikonen/master
Queue calls drain if empty task list is pushed
-rwxr-xr-x | lib/async.js | 8 | ||||
-rwxr-xr-x | test/test-async.js | 22 |
2 files changed, 30 insertions, 0 deletions
diff --git a/lib/async.js b/lib/async.js index c6ac74e..16240f8 100755 --- a/lib/async.js +++ b/lib/async.js @@ -678,6 +678,14 @@ if(data.constructor !== Array) { data = [data]; } + if(data.length == 0) { + // call drain immediately if there are no tasks + return async.setImmediate(function() { + if (q.drain) { + q.drain(); + } + }); + } _each(data, function(task) { var item = { data: task, diff --git a/test/test-async.js b/test/test-async.js index fd6fe46..d18262a 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -2483,3 +2483,25 @@ exports['queue events'] = function(test) { q.push('poo', function () {calls.push('poo cb');}); q.push('moo', function () {calls.push('moo cb');}); }; + +exports['queue empty'] = function(test) { + var calls = []; + var q = async.queue(function(task, cb) { + // nop + calls.push('process ' + task); + async.setImmediate(cb); + }, 3); + + q.drain = function() { + test.ok( + q.length() == 0 && q.running() == 0, + 'queue should be empty now and no more workers should be running' + ); + calls.push('drain'); + test.same(calls, [ + 'drain' + ]); + test.done(); + }; + q.push([]); +}; |