Browse Source

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

master
Harish.K 11 years ago
parent
commit
3a3043221b
  1. 18
      src/JobManager.js
  2. 15
      test/JobManager.spec.js

18
src/JobManager.js

@ -30,12 +30,24 @@ function JobManager(opts){
JobManager.prototype.updateState = function(){
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 = [];
for( i=0; i < tmpPoolLen; i++){
tmpPool[i] = workers[ i % workers.length ];
for( i=0; i < tmpPoolBlock; i++){
tmpPool = tmpPool.concat( this.workers );
}
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
test/JobManager.spec.js

@ -72,5 +72,20 @@ describe('JobManager', function(){
jm.runningTasks.should.be.equal( 0 );
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();
});
});
});

Loading…
Cancel
Save