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.abort();
|
---|
39 | }
|
---|
40 | });
|
---|
41 |
|
---|
42 | q.on('jobEnd',function() {
|
---|
43 | testJobsEndFired++;
|
---|
44 | });
|
---|
45 |
|
---|
46 | q.on('end',function() {
|
---|
47 | testEndFired = true;
|
---|
48 | assert.equal(testExecutedJobs, 5);
|
---|
49 | assert.equal(testJobsStartFired,5);
|
---|
50 | assert.equal(testJobsEndFired,5);
|
---|
51 | assert.equal(testConcurrency,maxConcurrency);
|
---|
52 | assert.ok(testStartFired);
|
---|
53 | });
|
---|
54 |
|
---|
55 | var running = q.stats()._jobsRunning;
|
---|
56 |
|
---|
57 | assert.equal(testExecutedJobs,0);
|
---|
58 | assert.equal(testJobsStartFired,0);
|
---|
59 | assert.equal(testJobsEndFired,0);
|
---|
60 | assert.equal(running,0);
|
---|
61 | assert.ok(!testStartFired);
|
---|
62 | assert.ok(!testEndFired);
|
---|
63 |
|
---|
64 | q.run();
|
---|