Skip to content

Commit

Permalink
fix tests and move to BDD
Browse files Browse the repository at this point in the history
  • Loading branch information
bkw committed Dec 4, 2011
1 parent 4fbe44e commit ad1a712
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 130 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
default: test

test:
@./node_modules/.bin/mocha --ui exports --reporter list test/*
@./node_modules/.bin/mocha --ui bdd --reporter list test/*

.PHONY: test
45 changes: 25 additions & 20 deletions test/test-addtask.js
Expand Up @@ -8,31 +8,36 @@ var randomSleep = function(payload, callback) {
called ++;
running ++;
running.should.be.below(3);
// console.log('Called with arg: ' + payload);
var args = Array.prototype.slice.call(arguments);

var delay = Math.ceil(Math.random() * 1000);
// console.log(payload + ' (Sleeping ' + delay/1000 + ' seconds)')
var delay = Math.ceil(Math.random() * 500);
setTimeout(function(){ running--; return callback(null, payload); }, delay);
}

exports['test limit is kept'] = function(){

var timeout = setTimeout(function () { throw 'Timeout'; }, 11000);

for (var i=0; i<10; i++) {
(function(i){
running.should.be.below(3);
return delayPool.addTask(randomSleep, i, function(err, res) {
running.should.be.below(3);
// console.log('Result from ' + i + ' : ' + res);
describe('poolr', function() {
describe('with limit two', function() {
it('should not run more than two tasks', function(done) {
var outstanding = 0;
for (var i=0; i<10; i++) {
outstanding++;
(function(i){
running.should.be.below(3);
return delayPool.addTask(randomSleep, i, function(err, res){
running.should.be.below(3);
if (--outstanding === 0) {
done();
}
});
})(i);
}
});

it('should have been called 10 times', function(done) {
delayPool._addTask(function(cb){return cb(null);},function(dummy) {
called.should.eql(10);
done();
});
})(i);
}

delayPool._addTask(function(cb){return cb(null);},function(dummy) {
called.should.eql(10);
clearTimeout(timeout);
});
});
}
});

50 changes: 25 additions & 25 deletions test/test-args.js
@@ -1,37 +1,37 @@
var should = require('should'),
delayPool = require('../lib/poolr.js').createPool(5),
poolr = require('../lib/poolr.js'),
called = 0,
running = 0;


var randomSleep = function(payload, callback) {
var checkArg = function(payload, callback) {
payload.should.eql(called++);
running ++;

var delay = Math.ceil(Math.random() * 1000);
// console.log(payload + ' (Sleeping ' + delay/1000 + ' seconds)')
setTimeout(function(){ running--; return callback(null, payload); }, delay);
process.nextTick(function(){return callback(null, payload)});
}

exports['argument is dispatched'] = function(){

var timeout = setTimeout(function () { throw 'Timeout'; }, 11000);

for (var i=0; i<10; i++) {
(function(i){
delayPool._addTask(
function(callback) { return randomSleep(i, callback); },
function(err, res) {
res.should.eql(i);
// console.log('Task ' + i + ' returned: ' + res);
}
);
})(i);
}

delayPool._addTask(function(cb){return cb(null);},function(dummy) {
clearTimeout(timeout);
describe('poolr', function() {
var testPool = poolr.createPool(5);
describe('taskArguments', function() {
it('should be dispatched', function(done) {
var outstanding = 0;

for (var i=0; i<10; i++) {
outstanding++;
(function(i){
testPool._addTask(
function(callback) { return checkArg(i, callback); },
function(err, res) {
res.should.eql(i);
if (--outstanding === 0) {
done();
}
}
);
})(i);
}
});
});
};
});


35 changes: 17 additions & 18 deletions test/test-basic.js
Expand Up @@ -4,28 +4,27 @@ var should = require('should'),

var randomSleep = function(callback) {
called ++;
var delay = Math.ceil(Math.random() * 1000);
var delay = Math.ceil(Math.random() * 200);
setTimeout(function(){
callback(null, 'returning after ' + delay/1000 + ' secs.');
}, delay);
}

exports['all tasks are called'] = function(){

var timeout = setTimeout(function () { throw 'Timeout'; }, 11000);

for (var i=0; i<10; i++) {
(function(i){
// console.log('Launching task ' + i);
delayPool._addTask(randomSleep, function(err, res) {
// console.log('Task ' + i + ' returned: ' + res);
});
})(i);
}

delayPool._addTask(function(cb){return cb(null);},function(dummy) {
called.should.eql(10);
clearTimeout(timeout);
describe('poolr with limit 5', function() {
it('should call all tasks', function(done) {
var outstanding = 0;
for (var i=0; i<10; i++) {
outstanding++;
(function(i){
delayPool._addTask(randomSleep, function(err, res) {
if (--outstanding === 0) {
called.should.eql(10);
done();
}
});
})(i);
}
});
}
});


21 changes: 13 additions & 8 deletions test/test-bind.js
Expand Up @@ -10,14 +10,19 @@ someClass.prototype.someFunc = function (someArg, callback) {
return callback(this.state, someArg);
}


exports['test method context remains'] = function(){
describe('poolr', function() {
var myPool = poolr(1);
var obj = new someClass('foo');
myPool.addTask(obj.someFunc.bind(obj), 'bar', function(err, res) {
err.should.eql('foo');
res.should.eql('bar');
});
}

describe('method context', function() {
var obj = new someClass('foo');

it('should be preserved when using bind()', function(done) {
myPool.addTask(obj.someFunc.bind(obj), 'bar', function(err, res) {
err.should.eql('foo');
res.should.eql('bar');
done();
});
});
});
});

14 changes: 8 additions & 6 deletions test/test-defaultobject.js
Expand Up @@ -10,14 +10,16 @@ someClass.prototype.someFunc = function (someArg, callback) {
return callback(this.state, someArg);
}


exports['test method context remains'] = function(){
describe('poolr default object', function() {
var obj = new someClass('foo');
var myPool = poolr(1, obj);
myPool.addTask(obj.someFunc, 'bar', function(err, res) {
err.should.eql('foo');
res.should.eql('bar');
it('should preserve method context', function(done) {
myPool.addTask(obj.someFunc, 'bar', function(err, res) {
err.should.eql('foo');
res.should.eql('bar');
done();
});
});
}
});


68 changes: 41 additions & 27 deletions test/test-events.js
Expand Up @@ -21,32 +21,46 @@ delayPool.on('throttle', function() {
idled++;
});

exports['emits throttle on limit'] = function(){
delayPool.addTask(sleep05);
throttled.should.eql(0);
drained.should.eql(0);

delayPool.addTask(sleep05);
throttled.should.eql(0);
drained.should.eql(0);

delayPool.addTask(sleep05);
throttled.should.eql(1);

delayPool.addTask(sleep05);
throttled.should.eql(1);


delayPool.addTask(
clearTimeout, setTimeout(function () { throw 'Timeout'; }, 2800)
);
};

setTimeout(function(){
throttled.should.eql(1);
drained.should.eql(1);
lasted.should.be.above(0);
idled.should.be.above(0);
}, 2500);
describe('poolr', function() {
describe('of two', function() {
it('should not emit throttle on first job', function() {
delayPool.addTask(sleep05);
throttled.should.eql(0);
});
it('should not emit drain after first job', function() {
drained.should.eql(0);
});
it('should not emit throttle on second job', function() {
delayPool.addTask(sleep05);
throttled.should.eql(0);
});
it('should not emit drain after second job', function() {
drained.should.eql(0);
});
it('should emit throttle on third job', function() {
delayPool.addTask(sleep05);
throttled.should.eql(1);
});
it('should have been throttled once on forth job', function() {
delayPool.addTask(sleep05);
throttled.should.eql(1);
});
it('should have emitted "throttle" excactly once', function(done) {
setTimeout(function(){
throttled.should.eql(1);
done();
}, 1000);
});
it('should have emitted "drain" excactly once', function() {
drained.should.eql(1);
});
it('should have emitted "last" at least once', function() {
lasted.should.be.above(0);
});
it('should have emitted "idle" excactly once', function() {
idled.should.eql(1);
});
});
});


26 changes: 16 additions & 10 deletions test/test-exports.js
@@ -1,13 +1,19 @@
var poolr = require('../lib/poolr.js'),
should = require('should');

module.exports = {
'version string' : function() {
poolr.version.should.match(/^\d+\.\d+\.\d+$/);
},
'constructor' : function() {
poolr.should.respondTo('createPool');
var testPool = poolr.createPool(1);
testPool.should.be.a('object');
}
};
describe('library', function() {
describe('version string', function() {
it('should be valid', function() {
poolr.version.should.match(/^\d+\.\d+\.\d+$/);
});
});
describe('constructor', function() {
it('should respond to "createPool"', function() {
poolr.should.respondTo('createPool');
});
it('should create an object', function() {
var testPool = poolr.createPool(1);
testPool.should.be.a('object');
});
});
});
14 changes: 8 additions & 6 deletions test/test-packagejson.js
Expand Up @@ -2,10 +2,12 @@ var should = require('should'),
poolr = require('../lib/poolr'),
fs = require('fs');

var pkg = JSON.parse(fs.readFileSync(__dirname + '/../package.json'));

module.exports = {
'version of module and package.json must match' : function() {
poolr.version.should.eql(pkg.version);
}
}
describe('package.json', function() {
var pkg = JSON.parse(fs.readFileSync(__dirname + '/../package.json'));
describe('version', function() {
it('should match library version', function() {
poolr.version.should.eql(pkg.version);
});
});
});
25 changes: 16 additions & 9 deletions test/test-return.js
@@ -1,18 +1,25 @@
var should = require('should'),
delayPool = require('../lib/poolr.js').createPool(2);
poolr = require('../lib/poolr.js');


var sleep05 = function(callback) {
setTimeout(function(){return callback(null)}, 500);
}

exports['addTask return false on limit'] = function(){
delayPool.addTask(sleep05).should.be.ok;
delayPool.addTask(sleep05).should.be.ok;
delayPool.addTask(sleep05).should.not.be.ok;
describe('poolr with limit two', function() {
var delayPool = poolr.createPool(2);

describe('addTask', function() {
it('should return true on first invocation', function() {
delayPool.addTask(sleep05).should.be.ok;
});
it('should return true on second invocation', function() {
delayPool.addTask(sleep05).should.be.ok;
});
it('should return false on third invocation', function() {
delayPool.addTask(sleep05).should.not.be.ok;
});
});
});

delayPool.addTask(
clearTimeout, setTimeout(function () { throw 'Timeout'; }, 2000)
);
};

0 comments on commit ad1a712

Please sign in to comment.