diff options
author | Alexander Early <alexander.early@gmail.com> | 2016-03-22 15:26:46 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2016-03-22 15:26:46 -0700 |
commit | 6dd08eed9f15ed6b986a83c0f849394118f98e91 (patch) | |
tree | f7d825f4a5ae367400532b22fa0f18562711e0bc /mocha_test | |
parent | 0e4d0672bd55d98e11adb93b49a7275510463d47 (diff) | |
download | async-6dd08eed9f15ed6b986a83c0f849394118f98e91.tar.gz |
convert priorityQueue tests to mocha
Diffstat (limited to 'mocha_test')
-rw-r--r-- | mocha_test/priorityQueue.js | 108 | ||||
-rw-r--r-- | mocha_test/queue.js | 6 |
2 files changed, 110 insertions, 4 deletions
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) { |