diff --git a/lib/internal/queue.js b/lib/internal/queue.js index c825e62d5..479eaf87b 100644 --- a/lib/internal/queue.js +++ b/lib/internal/queue.js @@ -119,11 +119,12 @@ export default function queue(worker, concurrency, payload) { data.push(node.data); } + numRunning += 1; + workersList.push(tasks[0]); + if (q._tasks.length === 0) { q.empty(); } - numRunning += 1; - workersList.push(tasks[0]); if (numRunning === q.concurrency) { q.saturated(); diff --git a/mocha_test/queue.js b/mocha_test/queue.js index 6b09ea8a1..d436b0a25 100644 --- a/mocha_test/queue.js +++ b/mocha_test/queue.js @@ -603,6 +603,35 @@ describe('queue', function(){ q.push([]); }); + + // #1367 + it('empty and not idle()', function(done) { + var calls = []; + var q = async.queue(function(task, cb) { + // nop + calls.push('process ' + task); + setImmediate(cb); + }, 1); + + q.empty = function () { + calls.push('empty'); + assert(q.idle() === false, + 'tasks should be running when empty is called') + expect(q.running()).to.equal(1); + } + + q.drain = function() { + calls.push('drain'); + expect(calls).to.eql([ + 'empty', + 'process 1', + 'drain' + ]); + done(); + }; + q.push(1); + }); + it('saturated', function(done) { var saturatedCalled = false; var q = async.queue(function(task, cb) {