From 47f5d1f0dcf96593c81e2adb3479a8005ebcfb24 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Sun, 2 Apr 2017 17:33:08 -0700 Subject: [PATCH] change timing of q.empty() so that q.idle() will be false. Fixes #1367 --- lib/internal/queue.js | 5 +++-- mocha_test/queue.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) 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) {