From 3a3043221b672662350f58ae22a71ff00ee7896b Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Mon, 1 Sep 2014 13:29:28 +0530 Subject: [PATCH] setConcurency method: it is used to dynamically change concurreny. + test cases --- src/JobManager.js | 18 +++++++++++++++--- test/JobManager.spec.js | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/JobManager.js b/src/JobManager.js index 634b347..04f3e43 100644 --- a/src/JobManager.js +++ b/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 ; + } }; /* diff --git a/test/JobManager.spec.js b/test/JobManager.spec.js index b259022..c47fa95 100644 --- a/test/JobManager.spec.js +++ b/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(); + }); }); });