summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2016-03-22 15:26:46 -0700
committerAlexander Early <alexander.early@gmail.com>2016-03-22 15:26:46 -0700
commit6dd08eed9f15ed6b986a83c0f849394118f98e91 (patch)
treef7d825f4a5ae367400532b22fa0f18562711e0bc
parent0e4d0672bd55d98e11adb93b49a7275510463d47 (diff)
downloadasync-6dd08eed9f15ed6b986a83c0f849394118f98e91.tar.gz
convert priorityQueue tests to mocha
-rw-r--r--lib/internal/queue.js15
-rw-r--r--mocha_test/priorityQueue.js108
-rw-r--r--mocha_test/queue.js6
-rwxr-xr-xtest/test-async.js112
4 files changed, 119 insertions, 122 deletions
diff --git a/lib/internal/queue.js b/lib/internal/queue.js
index 3e7eb1e..9521091 100644
--- a/lib/internal/queue.js
+++ b/lib/internal/queue.js
@@ -42,12 +42,6 @@ export default function queue(worker, concurrency, payload) {
q.tasks.push(item);
}
- if (q.tasks.length === q.concurrency) {
- q.saturated();
- }
- if (q.tasks.length <= (q.concurrency - q.buffer) ) {
- q.unsaturated();
- }
});
setImmediate(q.process);
}
@@ -111,8 +105,17 @@ export default function queue(worker, concurrency, payload) {
}
workers += 1;
workersList.push(tasks[0]);
+
+ if (workers === q.concurrency) {
+ q.saturated();
+ }
+ if (workers <= (q.concurrency - q.buffer) ) {
+ q.unsaturated();
+ }
+
var cb = onlyOnce(_next(q, tasks));
worker(data, cb);
+
}
},
length: function () {
diff --git a/mocha_test/priorityQueue.js b/mocha_test/priorityQueue.js
index 9832b43..9ed74c6 100644
--- a/mocha_test/priorityQueue.js
+++ b/mocha_test/priorityQueue.js
@@ -2,6 +2,114 @@ var async = require('../lib');
var expect = require('chai').expect;
describe('priorityQueue', function() {
+
+ it('priorityQueue', function (done) {
+ var call_order = [];
+
+ // order of completion: 2,1,4,3
+
+ var q = async.priorityQueue(function (task, callback) {
+ call_order.push('process ' + task);
+ callback('error', 'arg');
+ }, 1);
+
+ q.push(1, 1.4, function (err, arg) {
+ expect(err).to.equal('error');
+ expect(arg).to.equal('arg');
+ expect(q.length()).to.equal(2);
+ call_order.push('callback ' + 1);
+ });
+ q.push(2, 0.2, function (err, arg) {
+ expect(err).to.equal('error');
+ expect(arg).to.equal('arg');
+ expect(q.length()).to.equal(3);
+ call_order.push('callback ' + 2);
+ });
+ q.push(3, 3.8, function (err, arg) {
+ expect(err).to.equal('error');
+ expect(arg).to.equal('arg');
+ expect(q.length()).to.equal(0);
+ call_order.push('callback ' + 3);
+ });
+ q.push(4, 2.9, function (err, arg) {
+ expect(err).to.equal('error');
+ expect(arg).to.equal('arg');
+ expect(q.length()).to.equal(1);
+ call_order.push('callback ' + 4);
+ });
+ expect(q.length()).to.equal(4);
+ expect(q.concurrency).to.equal(1);
+
+ q.drain = function () {
+ expect(call_order).to.eql([
+ 'process 2', 'callback 2',
+ 'process 1', 'callback 1',
+ 'process 4', 'callback 4',
+ 'process 3', 'callback 3'
+ ]);
+ expect(q.concurrency).to.equal(1);
+ expect(q.length()).to.equal(0);
+ done();
+ };
+ });
+
+ it('concurrency', function (done) {
+ var call_order = [],
+ delays = [160,80,240,80];
+
+ // worker1: --2-3
+ // worker2: -1---4
+ // order of completion: 1,2,3,4
+
+ var q = async.priorityQueue(function (task, callback) {
+ setTimeout(function () {
+ call_order.push('process ' + task);
+ callback('error', 'arg');
+ }, delays.splice(0,1)[0]);
+ }, 2);
+
+ q.push(1, 1.4, function (err, arg) {
+ expect(err).to.equal('error');
+ expect(arg).to.equal('arg');
+ expect(q.length()).to.equal(2);
+ call_order.push('callback ' + 1);
+ });
+ q.push(2, 0.2, function (err, arg) {
+ expect(err).to.equal('error');
+ expect(arg).to.equal('arg');
+ expect(q.length()).to.equal(1);
+ call_order.push('callback ' + 2);
+ });
+ q.push(3, 3.8, function (err, arg) {
+ expect(err).to.equal('error');
+ expect(arg).to.equal('arg');
+ expect(q.length()).to.equal(0);
+ call_order.push('callback ' + 3);
+ });
+ q.push(4, 2.9, function (err, arg) {
+ expect(err).to.equal('error');
+ expect(arg).to.equal('arg');
+ expect(q.length()).to.equal(0);
+ call_order.push('callback ' + 4);
+ });
+ expect(q.length()).to.equal(4);
+ expect(q.concurrency).to.equal(2);
+
+ q.drain = function () {
+ expect(call_order).to.eql([
+ 'process 1', 'callback 1',
+ 'process 2', 'callback 2',
+ 'process 3', 'callback 3',
+ 'process 4', 'callback 4'
+ ]);
+ expect(q.concurrency).to.equal(2);
+ expect(q.length()).to.equal(0);
+ done();
+ };
+ });
+
+
+
context('q.unsaturated(): ',function() {
it('should have a default buffer property that equals 25% of the concurrenct rate', function(done) {
var calls = [];
diff --git a/mocha_test/queue.js b/mocha_test/queue.js
index c511e09..654c2c0 100644
--- a/mocha_test/queue.js
+++ b/mocha_test/queue.js
@@ -504,7 +504,7 @@ describe('queue', function(){
var q = async.queue(function(task, cb) {
// nop
calls.push('process ' + task);
- async.setImmediate(cb);
+ setTimeout(cb, 10);
}, 10);
q.concurrency = 3;
@@ -582,9 +582,7 @@ describe('queue', function(){
done();
};
- setTimeout(function () {
- q.push(['foo', 'bar', 'baz', 'moo']);
- }, 10);
+ q.push(['foo', 'bar', 'baz', 'moo']);
});
it('started', function(done) {
diff --git a/test/test-async.js b/test/test-async.js
index d4ea377..c87a0aa 100755
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -2149,118 +2149,6 @@ exports['whilst optional callback'] = function (test) {
-exports['priorityQueue'] = {
-
- 'priorityQueue': function (test) {
- test.expect(17);
- var call_order = [];
-
- // order of completion: 2,1,4,3
-
- var q = async.priorityQueue(function (task, callback) {
- call_order.push('process ' + task);
- callback('error', 'arg');
- }, 1);
-
- q.push(1, 1.4, function (err, arg) {
- test.equal(err, 'error');
- test.equal(arg, 'arg');
- test.equal(q.length(), 2);
- call_order.push('callback ' + 1);
- });
- q.push(2, 0.2, function (err, arg) {
- test.equal(err, 'error');
- test.equal(arg, 'arg');
- test.equal(q.length(), 3);
- call_order.push('callback ' + 2);
- });
- q.push(3, 3.8, function (err, arg) {
- test.equal(err, 'error');
- test.equal(arg, 'arg');
- test.equal(q.length(), 0);
- call_order.push('callback ' + 3);
- });
- q.push(4, 2.9, function (err, arg) {
- test.equal(err, 'error');
- test.equal(arg, 'arg');
- test.equal(q.length(), 1);
- call_order.push('callback ' + 4);
- });
- test.equal(q.length(), 4);
- test.equal(q.concurrency, 1);
-
- q.drain = function () {
- test.same(call_order, [
- 'process 2', 'callback 2',
- 'process 1', 'callback 1',
- 'process 4', 'callback 4',
- 'process 3', 'callback 3'
- ]);
- test.equal(q.concurrency, 1);
- test.equal(q.length(), 0);
- test.done();
- };
-},
-
- 'concurrency': function (test) {
- test.expect(17);
- var call_order = [],
- delays = [160,80,240,80];
-
- // worker1: --2-3
- // worker2: -1---4
- // order of completion: 1,2,3,4
-
- var q = async.priorityQueue(function (task, callback) {
- setTimeout(function () {
- call_order.push('process ' + task);
- callback('error', 'arg');
- }, delays.splice(0,1)[0]);
- }, 2);
-
- q.push(1, 1.4, function (err, arg) {
- test.equal(err, 'error');
- test.equal(arg, 'arg');
- test.equal(q.length(), 2);
- call_order.push('callback ' + 1);
- });
- q.push(2, 0.2, function (err, arg) {
- test.equal(err, 'error');
- test.equal(arg, 'arg');
- test.equal(q.length(), 1);
- call_order.push('callback ' + 2);
- });
- q.push(3, 3.8, function (err, arg) {
- test.equal(err, 'error');
- test.equal(arg, 'arg');
- test.equal(q.length(), 0);
- call_order.push('callback ' + 3);
- });
- q.push(4, 2.9, function (err, arg) {
- test.equal(err, 'error');
- test.equal(arg, 'arg');
- test.equal(q.length(), 0);
- call_order.push('callback ' + 4);
- });
- test.equal(q.length(), 4);
- test.equal(q.concurrency, 2);
-
- q.drain = function () {
- test.same(call_order, [
- 'process 1', 'callback 1',
- 'process 2', 'callback 2',
- 'process 3', 'callback 3',
- 'process 4', 'callback 4'
- ]);
- test.equal(q.concurrency, 2);
- test.equal(q.length(), 0);
- test.done();
- };
-}
-
-};
-
-
exports['cargo'] = {
'cargo': function (test) {