Skip to content

Commit

Permalink
fix(queue): fix an idle bug
Browse files Browse the repository at this point in the history
  • Loading branch information
suguru03 committed Jun 12, 2017
1 parent 0a440ba commit a0e1f3b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -7853,11 +7853,11 @@
function runQueue() {
while (!q.paused && workers < q.concurrency && q._tasks.length) {
var task = q._tasks.shift();
workers++;
workersList.push(task);
if (q._tasks.length === 0) {
q.empty();
}
workers++;
workersList.push(task);
if (workers === q.concurrency) {
q.saturated();
}
Expand All @@ -7875,11 +7875,11 @@
while (++index < size) {
data[index] = tasks[index].data;
}
workers++;
Array.prototype.push.apply(workersList, tasks);
if (q._tasks.length === 0) {
q.empty();
}
workers++;
Array.prototype.push.apply(workersList, tasks);
if (workers === q.concurrency) {
q.saturated();
}
Expand Down
24 changes: 24 additions & 0 deletions test/controlFlow/test.queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -1016,4 +1016,28 @@ parallel('#priorityQueue', function() {
}, 50);
});

it('should not be idle when empty is called', function(done) {
var calls = [];
var worker = function(task, cb) {
calls.push('process ' + task);
async.setImmediate(cb);
};
var q = async.queue(worker, 1);

q.empty = function () {
calls.push('empty');
assert(q.idle() === false, 'tasks should be running when empty is called')
assert.strictEqual(q.running(), 1);
};

q.drain = function() {
assert.deepEqual(calls, [
'empty',
'process 1',
]);
done();
};
q.push(1);
});

});

0 comments on commit a0e1f3b

Please sign in to comment.