summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2017-04-02 17:33:08 -0700
committerAlexander Early <alexander.early@gmail.com>2017-04-02 17:33:17 -0700
commit47f5d1f0dcf96593c81e2adb3479a8005ebcfb24 (patch)
tree6df64c62b1e16e029e4ec257998ca586e2b15389
parente14542aeb5b47da0616603bf8936d484b12f8488 (diff)
downloadasync-47f5d1f0dcf96593c81e2adb3479a8005ebcfb24.tar.gz
change timing of q.empty() so that q.idle() will be false. Fixes #1367
-rw-r--r--lib/internal/queue.js5
-rw-r--r--mocha_test/queue.js29
2 files changed, 32 insertions, 2 deletions
diff --git a/lib/internal/queue.js b/lib/internal/queue.js
index c825e62..479eaf8 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 6b09ea8..d436b0a 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) {