source: trip-planner-front/node_modules/qjobs/tests/abort.js@ fa375fe

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

initial commit

  • Property mode set to 100644
File size: 1.4 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.abort();
39 }
40});
41
42q.on('jobEnd',function() {
43 testJobsEndFired++;
44});
45
46q.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
55var running = q.stats()._jobsRunning;
56
57assert.equal(testExecutedJobs,0);
58assert.equal(testJobsStartFired,0);
59assert.equal(testJobsEndFired,0);
60assert.equal(running,0);
61assert.ok(!testStartFired);
62assert.ok(!testEndFired);
63
64q.run();
Note: See TracBrowser for help on using the repository browser.