2020-11-05 06:39:51 +03:00
|
|
|
// Switch these lines once there are useful utils
|
|
|
|
// const testUtils = require('./utils');
|
|
|
|
require('./utils');
|
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(),
|
|
|
|
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-10 06:35:04 +03:00
|
|
|
it('fails to run for invalid scheduling expression', function () {
|
|
|
|
const jobManager = new JobManager(logging);
|
2020-11-05 07:36:29 +03:00
|
|
|
|
|
|
|
try {
|
2020-11-10 03:15:10 +03:00
|
|
|
jobManager.scheduleJob('invalid expression', () => {}, {});
|
2020-11-05 07:36:29 +03:00
|
|
|
} catch (err) {
|
|
|
|
err.message.should.equal('Invalid schedule format');
|
|
|
|
}
|
|
|
|
});
|
2020-11-05 06:39:51 +03:00
|
|
|
});
|
|
|
|
});
|