[6a3a178] | 1 | #!/usr/bin/env node
|
---|
| 2 | var assert = require('assert');
|
---|
| 3 | var qjob = require('../qjobs');
|
---|
| 4 |
|
---|
| 5 | var maxConcurrency = 2;
|
---|
| 6 | var q = new qjob({maxConcurrency:maxConcurrency});
|
---|
| 7 |
|
---|
| 8 | var testExecutedJobs = 0;
|
---|
| 9 | var testStartFired = false;
|
---|
| 10 | var testEndFired = false;
|
---|
| 11 | var testJobsStartFired = 0;
|
---|
| 12 | var testJobsEndFired = 0;
|
---|
| 13 | var testConcurrency = 0;
|
---|
| 14 |
|
---|
| 15 | var myjob = function(args,next) {
|
---|
| 16 | setTimeout(function() {
|
---|
| 17 | testExecutedJobs++;
|
---|
| 18 | next();
|
---|
| 19 | },50);
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | // Let's add 10 job and add them to the queue
|
---|
| 23 | for (var i = 0; i<10; i++) {
|
---|
| 24 | q.add(myjob,['test'+i]);
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | q.on('start',function() {
|
---|
| 28 | testStartFired = true;
|
---|
| 29 | });
|
---|
| 30 |
|
---|
| 31 | q.on('jobStart',function() {
|
---|
| 32 | var running = q.stats()._jobsRunning;
|
---|
| 33 | if (running>testConcurrency) testConcurrency = running;
|
---|
| 34 | testJobsStartFired++;
|
---|
| 35 | });
|
---|
| 36 |
|
---|
| 37 | q.on('jobEnd',function() {
|
---|
| 38 | testJobsEndFired++;
|
---|
| 39 | });
|
---|
| 40 |
|
---|
| 41 | q.on('end',function() {
|
---|
| 42 | testEndFired = true;
|
---|
| 43 | assert.equal(testExecutedJobs, 10);
|
---|
| 44 | assert.equal(testJobsStartFired,10);
|
---|
| 45 | assert.equal(testJobsEndFired,10);
|
---|
| 46 | assert.equal(testConcurrency,maxConcurrency);
|
---|
| 47 | assert.ok(testStartFired);
|
---|
| 48 | });
|
---|
| 49 |
|
---|
| 50 | var running = q.stats()._jobsRunning;
|
---|
| 51 |
|
---|
| 52 | assert.equal(testExecutedJobs,0);
|
---|
| 53 | assert.equal(testJobsStartFired,0);
|
---|
| 54 | assert.equal(testJobsEndFired,0);
|
---|
| 55 | assert.equal(running,0);
|
---|
| 56 | assert.ok(!testStartFired);
|
---|
| 57 | assert.ok(!testEndFired);
|
---|
| 58 |
|
---|
| 59 | q.run();
|
---|