diff options
author | Alex Early <alexander.early@gmail.com> | 2016-04-26 21:48:06 -0700 |
---|---|---|
committer | Alex Early <alexander.early@gmail.com> | 2016-04-26 21:48:06 -0700 |
commit | 4eeff85a0cfec6463aeb1d0dbb90fd945c0be5a2 (patch) | |
tree | 3df3609ee8e16f1484a66e0fbe286c407fe74c68 | |
parent | 1114e65d1e58abcf420d2f4cbb8ea413ded49859 (diff) | |
parent | 8578287ef84ccad8dd0e5eea47b53c434b5bd261 (diff) | |
download | async-4eeff85a0cfec6463aeb1d0dbb90fd945c0be5a2.tar.gz |
Merge pull request #1132 from suguru03/fix-priority-queue
fix `priorityQueue`
-rw-r--r-- | lib/priorityQueue.js | 6 | ||||
-rw-r--r-- | mocha_test/priorityQueue.js | 41 | ||||
-rw-r--r-- | mocha_test/queue.js | 38 |
3 files changed, 74 insertions, 11 deletions
diff --git a/lib/priorityQueue.js b/lib/priorityQueue.js index a33f2f5..4c29596 100644 --- a/lib/priorityQueue.js +++ b/lib/priorityQueue.js @@ -73,12 +73,6 @@ export default function(worker, concurrency) { q.tasks.splice(_binarySearch(q.tasks, item, _compareTasks) + 1, 0, item); - if (q.tasks.length === q.concurrency) { - q.saturated(); - } - if (q.tasks.length <= (q.concurrency - q.buffer) ) { - q.unsaturated(); - } setImmediate(q.process); }); } diff --git a/mocha_test/priorityQueue.js b/mocha_test/priorityQueue.js index fa5cfce..b777815 100644 --- a/mocha_test/priorityQueue.js +++ b/mocha_test/priorityQueue.js @@ -108,7 +108,43 @@ describe('priorityQueue', function() { }; }); - + context('q.saturated(): ', function() { + it('should call the saturated callback if tasks length is concurrency', function(done) { + var calls = []; + var q = async.priorityQueue(function(task, cb) { + calls.push('process ' + task); + async.setImmediate(cb); + }, 4); + q.saturated = function() { + calls.push('saturated'); + }; + q.empty = function() { + expect(calls.indexOf('saturated')).to.be.above(-1); + setTimeout(function() { + expect(calls).eql([ + 'process foo4', + 'process foo3', + 'process foo2', + "saturated", + 'process foo1', + 'foo4 cb', + "saturated", + 'process foo0', + 'foo3 cb', + 'foo2 cb', + 'foo1 cb', + 'foo0 cb' + ]); + done(); + }, 50); + }; + q.push('foo0', 5, function () {calls.push('foo0 cb');}); + q.push('foo1', 4, function () {calls.push('foo1 cb');}); + q.push('foo2', 3, function () {calls.push('foo2 cb');}); + q.push('foo3', 2, function () {calls.push('foo3 cb');}); + q.push('foo4', 1, function () {calls.push('foo4 cb');}); + }); + }); context('q.unsaturated(): ',function() { it('should have a default buffer property that equals 25% of the concurrenct rate', function(done) { @@ -148,9 +184,6 @@ describe('priorityQueue', function() { expect(calls.indexOf('unsaturated')).to.be.above(-1); setTimeout(function() { expect(calls).eql([ - 'unsaturated', - 'unsaturated', - 'unsaturated', 'process foo4', 'process foo3', 'process foo2', diff --git a/mocha_test/queue.js b/mocha_test/queue.js index 2baf124..a51efb3 100644 --- a/mocha_test/queue.js +++ b/mocha_test/queue.js @@ -599,7 +599,43 @@ describe('queue', function(){ done(); }); - + context('q.saturated(): ', function() { + it('should call the saturated callback if tasks length is concurrency', function(done) { + var calls = []; + var q = async.queue(function(task, cb) { + calls.push('process ' + task); + async.setImmediate(cb); + }, 4); + q.saturated = function() { + calls.push('saturated'); + }; + q.empty = function() { + expect(calls.indexOf('saturated')).to.be.above(-1); + setTimeout(function() { + expect(calls).eql([ + 'process foo0', + 'process foo1', + 'process foo2', + "saturated", + 'process foo3', + 'foo0 cb', + "saturated", + 'process foo4', + 'foo1 cb', + 'foo2 cb', + 'foo3 cb', + 'foo4 cb' + ]); + done(); + }, 50); + }; + q.push('foo0', function () {calls.push('foo0 cb');}); + q.push('foo1', function () {calls.push('foo1 cb');}); + q.push('foo2', function () {calls.push('foo2 cb');}); + q.push('foo3', function () {calls.push('foo3 cb');}); + q.push('foo4', function () {calls.push('foo4 cb');}); + }); + }); context('q.unsaturated(): ',function() { it('should have a default buffer property that equals 25% of the concurrenct rate', function(done){ |