2020-11-05 06:39:51 +03:00
|
|
|
// Switch these lines once there are useful utils
|
|
|
|
// const testUtils = require('./utils');
|
|
|
|
require('./utils');
|
2020-11-19 07:58:41 +03:00
|
|
|
const path = require('path');
|
2020-11-10 06:35:04 +03:00
|
|
|
const sinon = require('sinon');
|
|
|
|
const delay = require('delay');
|
2020-11-05 06:39:51 +03:00
|
|
|
|
2020-11-10 06:35:04 +03:00
|
|
|
const JobManager = require('../index');
|
2020-11-05 06:39:51 +03:00
|
|
|
|
|
|
|
describe('Job Manager', function () {
|
2020-11-10 07:11:24 +03:00
|
|
|
let logging;
|
|
|
|
|
2020-11-10 07:15:08 +03:00
|
|
|
beforeEach(function () {
|
2020-11-10 07:11:24 +03:00
|
|
|
logging = {
|
|
|
|
info: sinon.stub(),
|
2020-11-17 08:14:37 +03:00
|
|
|
warn: sinon.stub(),
|
2020-11-10 07:11:24 +03:00
|
|
|
error: sinon.stub()
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-11-05 06:39:51 +03:00
|
|
|
it('public interface', function () {
|
2020-11-10 06:35:04 +03:00
|
|
|
const jobManager = new JobManager(logging);
|
2020-11-05 06:39:51 +03:00
|
|
|
|
|
|
|
should.exist(jobManager.addJob);
|
2020-11-05 07:36:29 +03:00
|
|
|
should.exist(jobManager.scheduleJob);
|
|
|
|
});
|
|
|
|
|
2020-11-10 06:35:04 +03:00
|
|
|
describe('Add a Job', function () {
|
|
|
|
it('adds a job to a queue', async function () {
|
|
|
|
const spy = sinon.spy();
|
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
|
|
|
|
jobManager.addJob(spy, 'test data');
|
|
|
|
should(jobManager.queue.idle()).be.false();
|
|
|
|
|
|
|
|
// give time to execute the job
|
|
|
|
await delay(1);
|
|
|
|
|
|
|
|
should(jobManager.queue.idle()).be.true();
|
|
|
|
should(spy.called).be.true();
|
|
|
|
should(spy.args[0][0]).equal('test data');
|
|
|
|
});
|
2020-11-10 07:11:24 +03:00
|
|
|
|
|
|
|
it('handles failed job gracefully', async function () {
|
|
|
|
const spy = sinon.stub().throws();
|
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
|
|
|
|
jobManager.addJob(spy, 'test data');
|
|
|
|
should(jobManager.queue.idle()).be.false();
|
|
|
|
|
|
|
|
// give time to execute the job
|
|
|
|
await delay(1);
|
|
|
|
|
|
|
|
should(jobManager.queue.idle()).be.true();
|
|
|
|
should(spy.called).be.true();
|
|
|
|
should(spy.args[0][0]).equal('test data');
|
|
|
|
should(logging.error.called).be.true();
|
|
|
|
});
|
2020-11-10 06:35:04 +03:00
|
|
|
});
|
|
|
|
|
2020-11-05 07:36:29 +03:00
|
|
|
describe('Schedule Job', function () {
|
2020-11-19 07:58:41 +03:00
|
|
|
it('fails to schedule for invalid scheduling expression', function () {
|
2020-11-10 06:35:04 +03:00
|
|
|
const jobManager = new JobManager(logging);
|
2020-11-05 07:36:29 +03:00
|
|
|
|
|
|
|
try {
|
2020-11-19 07:58:41 +03:00
|
|
|
jobManager.scheduleJob('invalid expression', 'jobName', {});
|
2020-11-05 07:36:29 +03:00
|
|
|
} catch (err) {
|
|
|
|
err.message.should.equal('Invalid schedule format');
|
|
|
|
}
|
|
|
|
});
|
2020-11-19 07:58:41 +03:00
|
|
|
|
|
|
|
it('fails to schedule for no job name', function () {
|
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
|
|
|
|
try {
|
|
|
|
jobManager.scheduleJob('invalid expression', () => {}, {});
|
|
|
|
} catch (err) {
|
|
|
|
err.message.should.equal('Name parameter should be present if job is a function');
|
|
|
|
}
|
|
|
|
});
|
2020-11-05 06:39:51 +03:00
|
|
|
});
|
2020-11-17 08:14:37 +03:00
|
|
|
|
|
|
|
describe('Shutdown', function () {
|
2020-11-19 07:58:41 +03:00
|
|
|
it('gracefully shuts down a synchronous jobs', async function () {
|
2020-11-17 08:14:37 +03:00
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
|
|
|
|
jobManager.addJob(require('./jobs/timed-job'), 200);
|
|
|
|
|
|
|
|
should(jobManager.queue.idle()).be.false();
|
|
|
|
|
|
|
|
await jobManager.shutdown();
|
|
|
|
|
|
|
|
should(jobManager.queue.idle()).be.true();
|
|
|
|
});
|
2020-11-19 07:58:41 +03:00
|
|
|
|
|
|
|
it('gracefully shuts down an interval job', async function () {
|
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
|
|
|
|
jobManager.scheduleJob('every 5 seconds', path.resolve(__dirname, './jobs/graceful.js'));
|
|
|
|
|
|
|
|
await delay(1); // let the job execution kick in
|
|
|
|
|
2020-11-23 04:24:24 +03:00
|
|
|
should(Object.keys(jobManager.bree.workers).length).equal(0);
|
|
|
|
should(Object.keys(jobManager.bree.timeouts).length).equal(0);
|
|
|
|
should(Object.keys(jobManager.bree.intervals).length).equal(1);
|
2020-11-19 07:58:41 +03:00
|
|
|
|
|
|
|
await jobManager.shutdown();
|
|
|
|
|
2020-11-23 04:24:24 +03:00
|
|
|
should(Object.keys(jobManager.bree.intervals).length).equal(0);
|
2020-11-19 07:58:41 +03:00
|
|
|
});
|
2020-11-17 08:14:37 +03:00
|
|
|
});
|
2020-11-05 06:39:51 +03:00
|
|
|
});
|