summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaolan McMahon <caolan.mcmahon@gmail.com>2014-03-28 12:41:10 +0000
committerCaolan McMahon <caolan.mcmahon@gmail.com>2014-03-28 12:41:10 +0000
commit191a5076bf4a9cb6ac87b1bf6e9d4eb0a13527d7 (patch)
tree5fb93d2e26634692c513ca9dd884b3a6d3c43b2f
parent287cb65116c34f5fff1b953c6b9a95bce0a3dbde (diff)
parentc2b098bb46c9c17bb901a91073dae461dfad7ca8 (diff)
downloadasync-191a5076bf4a9cb6ac87b1bf6e9d4eb0a13527d7.tar.gz
Merge pull request #433 from tikonen/master
Queue calls drain if empty task list is pushed
-rwxr-xr-xlib/async.js8
-rwxr-xr-xtest/test-async.js22
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([]);
+};