Skip to content

Commit

Permalink
Merge pull request #1230 from suguru03/fix-priority-queue
Browse files Browse the repository at this point in the history
fix execution order
  • Loading branch information
aearly authored Jul 12, 2016
2 parents 4794563 + 3991202 commit a46e3b7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/priorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default function(worker, concurrency) {
});
}

priority = priority || 0;
var nextNode = q._tasks.head;
while (nextNode && priority >= nextNode.priority) {
nextNode = nextNode.next;
Expand Down
29 changes: 29 additions & 0 deletions mocha_test/priorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,35 @@ describe('priorityQueue', function() {
};
});

it('pause in worker with concurrency', function(done) {
var call_order = [];
var q = async.priorityQueue(function (task, callback) {
if (task.isLongRunning) {
q.pause();
setTimeout(function () {
call_order.push(task.id);
q.resume();
callback();
}, 50);
}
else {
call_order.push(task.id);
setTimeout(callback, 10);
}
}, 10);

q.push({ id: 1, isLongRunning: true});
q.push({ id: 2 });
q.push({ id: 3 });
q.push({ id: 4 });
q.push({ id: 5 });

q.drain = function () {
expect(call_order).to.eql([1, 2, 3, 4, 5]);
done();
};
});

context('q.saturated(): ', function() {
it('should call the saturated callback if tasks length is concurrency', function(done) {
var calls = [];
Expand Down

0 comments on commit a46e3b7

Please sign in to comment.