[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 | var testPause = false;
|
---|
| 15 | var testUnpause = false;
|
---|
| 16 |
|
---|
| 17 | var myjob = function(args,next) {
|
---|
| 18 | setTimeout(function() {
|
---|
| 19 | testExecutedJobs++;
|
---|
| 20 | next();
|
---|
| 21 | },50);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | // Let's add 10 job and add them to the queue
|
---|
| 25 | for (var i = 0; i<10; i++) {
|
---|
| 26 | q.add(myjob,['test'+i]);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | q.on('start',function() {
|
---|
| 30 | testStartFired = true;
|
---|
| 31 | });
|
---|
| 32 |
|
---|
| 33 | q.on('jobStart',function() {
|
---|
| 34 | var running = q.stats()._jobsRunning;
|
---|
| 35 | if (running>testConcurrency) testConcurrency = running;
|
---|
| 36 | testJobsStartFired++;
|
---|
| 37 | if (testJobsStartFired == 5) {
|
---|
| 38 | q.pause(true);
|
---|
| 39 | }
|
---|
| 40 | });
|
---|
| 41 |
|
---|
| 42 | q.on('jobEnd',function() {
|
---|
| 43 | testJobsEndFired++;
|
---|
| 44 | });
|
---|
| 45 |
|
---|
| 46 | q.on('end',function() {
|
---|
| 47 | testEndFired = true;
|
---|
| 48 | assert.equal(testExecutedJobs, 10);
|
---|
| 49 | assert.equal(testJobsStartFired,10);
|
---|
| 50 | assert.equal(testJobsEndFired,10);
|
---|
| 51 | assert.equal(testConcurrency,maxConcurrency);
|
---|
| 52 | assert.ok(testStartFired);
|
---|
| 53 | assert.ok(testPause);
|
---|
| 54 | assert.ok(testUnpause);
|
---|
| 55 | });
|
---|
| 56 |
|
---|
| 57 | q.on('pause',function(since) {
|
---|
| 58 | testPause = true;
|
---|
| 59 | if (since>2000) {
|
---|
| 60 | q.pause(false);
|
---|
| 61 | }
|
---|
| 62 | });
|
---|
| 63 |
|
---|
| 64 | q.on('unpause',function() {
|
---|
| 65 | testUnpause = true;
|
---|
| 66 | });
|
---|
| 67 |
|
---|
| 68 | var running = q.stats()._jobsRunning;
|
---|
| 69 |
|
---|
| 70 | assert.equal(testExecutedJobs,0);
|
---|
| 71 | assert.equal(testJobsStartFired,0);
|
---|
| 72 | assert.equal(testJobsEndFired,0);
|
---|
| 73 | assert.equal(running,0);
|
---|
| 74 | assert.ok(!testStartFired);
|
---|
| 75 | assert.ok(!testEndFired);
|
---|
| 76 | assert.ok(!testPause);
|
---|
| 77 | assert.ok(!testUnpause);
|
---|
| 78 |
|
---|
| 79 | q.run();
|
---|