[6a3a178] | 1 | #!/usr/bin/env node
|
---|
| 2 | var assert = require('assert');
|
---|
| 3 | var qjob = require('../qjobs');
|
---|
| 4 |
|
---|
| 5 | // maximum number of jobs executed in parallels
|
---|
| 6 | var maxConcurrency = 5;
|
---|
| 7 |
|
---|
| 8 | // delay between each group of maxConcurrency jobs done
|
---|
| 9 | var interval = 1000;
|
---|
| 10 |
|
---|
| 11 | var q = new qjob({
|
---|
| 12 | maxConcurrency:maxConcurrency,
|
---|
| 13 | interval:interval
|
---|
| 14 | });
|
---|
| 15 |
|
---|
| 16 | // number of total jobs
|
---|
| 17 | var maxJobs = 20;
|
---|
| 18 |
|
---|
| 19 | // tests dedicated variables
|
---|
| 20 | var testExecutedJobs = 0;
|
---|
| 21 | var testNbSleep = 0;
|
---|
| 22 |
|
---|
| 23 | // warning, if you change maxConcurrency, maxJobs
|
---|
| 24 | // or interval variable, you will have to review
|
---|
| 25 | // the testMaxNbSleep value
|
---|
| 26 | var testMaxNbSleep = 4;
|
---|
| 27 |
|
---|
| 28 | var myjob = function(args,next) {
|
---|
| 29 | setTimeout(function() {
|
---|
| 30 | testExecutedJobs++;
|
---|
| 31 | next();
|
---|
| 32 | },args[1]);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | // Let's add 10 job and add them to the queue
|
---|
| 36 | for (var i = 0; i<maxJobs; i++) {
|
---|
| 37 | q.add(myjob,['test'+i,Math.random()*1000]);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | q.on('end',function() {
|
---|
| 41 | assert.equal(testExecutedJobs, maxJobs);
|
---|
| 42 | assert.equal(testNbSleep, testMaxNbSleep);
|
---|
| 43 | //console.log('Done');
|
---|
| 44 | });
|
---|
| 45 |
|
---|
| 46 | q.on('jobStart',function(args) {
|
---|
| 47 | //console.log(args[0]+' wait for '+args[1]+' ms');
|
---|
| 48 | });
|
---|
| 49 |
|
---|
| 50 | q.on('sleep',function() {
|
---|
| 51 | testNbSleep++;
|
---|
| 52 | //console.log('zzZZzzzz for '+interval+'ms',testNbSleep);
|
---|
| 53 | });
|
---|
| 54 |
|
---|
| 55 | q.on('continu',function() {
|
---|
| 56 | //console.log('WAKE !');
|
---|
| 57 | });
|
---|
| 58 |
|
---|
| 59 | q.run();
|
---|
| 60 |
|
---|
| 61 |
|
---|