setConcurency method: it is used to dynamically change concurreny. + test cases

This commit is contained in:
2014-09-01 13:29:28 +05:30
parent 6ae9504a10
commit 3a3043221b
2 changed files with 30 additions and 3 deletions
+15 -3
View File
@@ -30,12 +30,24 @@ function JobManager(opts){
JobManager.prototype.updateState = function(){ JobManager.prototype.updateState = function(){
var workers = this.workers; var workers = this.workers;
var tmpPoolLen = workers.length && (workers.length * Math.ceil( this.concurrency/workers.length ) ) , i; var tmpPoolBlock = workers.length && Math.ceil( this.concurrency/workers.length ) , i;
var tmpPool = []; var tmpPool = [];
for( i=0; i < tmpPoolLen; i++){ for( i=0; i < tmpPoolBlock; i++){
tmpPool[i] = workers[ i % workers.length ]; tmpPool = tmpPool.concat( this.workers );
} }
this.tmpPool = tmpPool; this.tmpPool = tmpPool;
this.tmpPoolLen = tmpPoolBlock * this.workers.length ;
};
JobManager.prototype.setConcurrency = function( newVal ){
if( newVal > this.tmpPoolLen ){
var tmpPool = this.tmpPool;
var additionalPoolBlocks = Math.ceil( ( newVal - this.tmpPoolLen )/this.workers.length ), i;
for( i=0; i < additionalPoolBlocks; i++){
tmpPool = tmpPool.concat( this.workers );
}
this.tmpPoolLen += additionalPoolBlocks * this.workers.length ;
}
}; };
/* /*
+15
View File
@@ -72,5 +72,20 @@ describe('JobManager', function(){
jm.runningTasks.should.be.equal( 0 ); jm.runningTasks.should.be.equal( 0 );
done(); done();
}); });
it('should dynamically change concurrency', function( done ){
jm.concurrency = NO_WORKERS-1;
jm.updateState();
jm.tmpPoolLen.should.be.equal( NO_WORKERS );
jm.setConcurrency( (2*NO_WORKERS) +1 );
jm.tmpPoolLen.should.be.equal( 3*NO_WORKERS );
jm.setConcurrency( (3*NO_WORKERS) +1 );
jm.tmpPoolLen.should.be.equal( 4*NO_WORKERS );
jm.setConcurrency( (4*NO_WORKERS) +1 );
jm.tmpPoolLen.should.be.equal( 5*NO_WORKERS );
jm.setConcurrency( (5*NO_WORKERS) +1 );
jm.tmpPoolLen.should.be.equal( 6*NO_WORKERS );
done();
});
}); });
}); });