Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
170 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); | ||
}); | ||
}; | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
); | ||
}; | ||
|