source: trip-planner-front/node_modules/qjobs/tests/pause.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.7 KB
Line 
1#!/usr/bin/env node
2var assert = require('assert');
3var qjob = require('../qjobs');
4
5var maxConcurrency = 2;
6var q = new qjob({maxConcurrency:maxConcurrency});
7
8var testExecutedJobs = 0;
9var testStartFired = false;
10var testEndFired = false;
11var testJobsStartFired = 0;
12var testJobsEndFired = 0;
13var testConcurrency = 0;
14var testPause = false;
15var testUnpause = false;
16
17var 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
25for (var i = 0; i<10; i++) {
26 q.add(myjob,['test'+i]);
27}
28
29q.on('start',function() {
30 testStartFired = true;
31});
32
33q.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
42q.on('jobEnd',function() {
43 testJobsEndFired++;
44});
45
46q.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
57q.on('pause',function(since) {
58 testPause = true;
59 if (since>2000) {
60 q.pause(false);
61 }
62});
63
64q.on('unpause',function() {
65 testUnpause = true;
66});
67
68var running = q.stats()._jobsRunning;
69
70assert.equal(testExecutedJobs,0);
71assert.equal(testJobsStartFired,0);
72assert.equal(testJobsEndFired,0);
73assert.equal(running,0);
74assert.ok(!testStartFired);
75assert.ok(!testEndFired);
76assert.ok(!testPause);
77assert.ok(!testUnpause);
78
79q.run();
Note: See TracBrowser for help on using the repository browser.