2023-01-30 11:13:17 +01:00
|
|
|
import { SchedulerService } from './scheduler-service';
|
2023-05-24 12:26:54 +02:00
|
|
|
import { LogProvider } from '../logger';
|
2023-01-11 16:15:53 +01:00
|
|
|
|
|
|
|
function ms(timeMs) {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, timeMs));
|
|
|
|
}
|
|
|
|
|
|
|
|
const getLogger = () => {
|
2023-04-04 14:15:45 +02:00
|
|
|
const records: any[] = [];
|
2023-05-24 12:26:54 +02:00
|
|
|
const logger: LogProvider = () => ({
|
2023-01-11 16:15:53 +01:00
|
|
|
error(...args: any[]) {
|
|
|
|
records.push(args);
|
|
|
|
},
|
|
|
|
debug() {},
|
|
|
|
info() {},
|
|
|
|
warn() {},
|
|
|
|
fatal() {},
|
|
|
|
});
|
2023-05-24 12:26:54 +02:00
|
|
|
const getRecords = () => records;
|
2023-01-11 16:15:53 +01:00
|
|
|
|
2023-05-24 12:26:54 +02:00
|
|
|
return { logger, getRecords };
|
2023-01-11 16:15:53 +01:00
|
|
|
};
|
|
|
|
|
2023-01-12 11:26:59 +01:00
|
|
|
test('Schedules job immediately', async () => {
|
2023-05-24 12:26:54 +02:00
|
|
|
const { logger } = getLogger();
|
|
|
|
const schedulerService = new SchedulerService(logger);
|
2023-01-12 11:26:59 +01:00
|
|
|
const job = jest.fn();
|
|
|
|
|
|
|
|
schedulerService.schedule(job, 10);
|
|
|
|
|
|
|
|
expect(job).toBeCalledTimes(1);
|
|
|
|
schedulerService.stop();
|
|
|
|
});
|
|
|
|
|
2023-05-24 12:26:54 +02:00
|
|
|
test('Does not schedule job immediately when paused', async () => {
|
|
|
|
const { logger } = getLogger();
|
|
|
|
const schedulerService = new SchedulerService(logger);
|
|
|
|
const job = jest.fn();
|
|
|
|
|
|
|
|
schedulerService.pause();
|
|
|
|
schedulerService.schedule(job, 10);
|
|
|
|
|
|
|
|
expect(job).toBeCalledTimes(0);
|
|
|
|
schedulerService.stop();
|
|
|
|
});
|
|
|
|
|
2023-01-11 16:15:53 +01:00
|
|
|
test('Can schedule a single regular job', async () => {
|
2023-05-24 12:26:54 +02:00
|
|
|
const { logger } = getLogger();
|
|
|
|
const schedulerService = new SchedulerService(logger);
|
2023-01-11 16:15:53 +01:00
|
|
|
const job = jest.fn();
|
|
|
|
|
2023-04-04 14:15:45 +02:00
|
|
|
schedulerService.schedule(job, 50);
|
|
|
|
await ms(75);
|
2023-01-11 16:15:53 +01:00
|
|
|
|
2023-01-12 11:26:59 +01:00
|
|
|
expect(job).toBeCalledTimes(2);
|
2023-01-11 16:15:53 +01:00
|
|
|
schedulerService.stop();
|
|
|
|
});
|
|
|
|
|
2023-05-24 12:26:54 +02:00
|
|
|
test('Scheduled job ignored in a paused mode', async () => {
|
|
|
|
const { logger } = getLogger();
|
|
|
|
const schedulerService = new SchedulerService(logger);
|
|
|
|
const job = jest.fn();
|
|
|
|
|
|
|
|
schedulerService.pause();
|
|
|
|
schedulerService.schedule(job, 50);
|
|
|
|
await ms(75);
|
|
|
|
|
|
|
|
expect(job).toBeCalledTimes(0);
|
|
|
|
schedulerService.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can resume paused job', async () => {
|
|
|
|
const { logger } = getLogger();
|
|
|
|
const schedulerService = new SchedulerService(logger);
|
|
|
|
const job = jest.fn();
|
|
|
|
|
|
|
|
schedulerService.pause();
|
|
|
|
schedulerService.schedule(job, 50);
|
|
|
|
schedulerService.resume();
|
|
|
|
await ms(75);
|
|
|
|
|
|
|
|
expect(job).toBeCalledTimes(1);
|
|
|
|
schedulerService.stop();
|
|
|
|
});
|
|
|
|
|
2023-01-11 16:15:53 +01:00
|
|
|
test('Can schedule multiple jobs at the same interval', async () => {
|
2023-05-24 12:26:54 +02:00
|
|
|
const { logger } = getLogger();
|
|
|
|
const schedulerService = new SchedulerService(logger);
|
2023-01-11 16:15:53 +01:00
|
|
|
const job = jest.fn();
|
|
|
|
const anotherJob = jest.fn();
|
|
|
|
|
2023-04-04 14:15:45 +02:00
|
|
|
schedulerService.schedule(job, 50);
|
|
|
|
schedulerService.schedule(anotherJob, 50);
|
|
|
|
await ms(75);
|
2023-01-11 16:15:53 +01:00
|
|
|
|
2023-01-12 11:26:59 +01:00
|
|
|
expect(job).toBeCalledTimes(2);
|
|
|
|
expect(anotherJob).toBeCalledTimes(2);
|
2023-01-11 16:15:53 +01:00
|
|
|
schedulerService.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can schedule multiple jobs at the different intervals', async () => {
|
2023-05-24 12:26:54 +02:00
|
|
|
const { logger } = getLogger();
|
|
|
|
const schedulerService = new SchedulerService(logger);
|
2023-01-11 16:15:53 +01:00
|
|
|
const job = jest.fn();
|
|
|
|
const anotherJob = jest.fn();
|
|
|
|
|
2023-02-15 10:26:19 +01:00
|
|
|
schedulerService.schedule(job, 100);
|
|
|
|
schedulerService.schedule(anotherJob, 200);
|
|
|
|
await ms(250);
|
2023-01-11 16:15:53 +01:00
|
|
|
|
2023-01-12 11:26:59 +01:00
|
|
|
expect(job).toBeCalledTimes(3);
|
|
|
|
expect(anotherJob).toBeCalledTimes(2);
|
2023-01-11 16:15:53 +01:00
|
|
|
schedulerService.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can handle crash of a async job', async () => {
|
2023-05-24 12:26:54 +02:00
|
|
|
const { logger, getRecords } = getLogger();
|
2023-01-11 16:15:53 +01:00
|
|
|
const schedulerService = new SchedulerService(logger);
|
|
|
|
const job = async () => {
|
|
|
|
await Promise.reject('async reason');
|
|
|
|
};
|
|
|
|
|
2023-04-04 14:15:45 +02:00
|
|
|
schedulerService.schedule(job, 50);
|
|
|
|
await ms(75);
|
2023-01-11 16:15:53 +01:00
|
|
|
|
|
|
|
schedulerService.stop();
|
2023-05-24 12:26:54 +02:00
|
|
|
expect(getRecords()).toEqual([
|
2023-01-11 16:15:53 +01:00
|
|
|
['scheduled job failed', 'async reason'],
|
2023-01-12 11:26:59 +01:00
|
|
|
['scheduled job failed', 'async reason'],
|
2023-01-11 16:15:53 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can handle crash of a sync job', async () => {
|
2023-05-24 12:26:54 +02:00
|
|
|
const { logger, getRecords } = getLogger();
|
2023-01-11 16:15:53 +01:00
|
|
|
const schedulerService = new SchedulerService(logger);
|
|
|
|
const job = () => {
|
|
|
|
throw new Error('sync reason');
|
|
|
|
};
|
|
|
|
|
2023-04-04 14:15:45 +02:00
|
|
|
schedulerService.schedule(job, 50);
|
|
|
|
await ms(75);
|
2023-01-11 16:15:53 +01:00
|
|
|
|
|
|
|
schedulerService.stop();
|
2023-05-24 12:26:54 +02:00
|
|
|
expect(getRecords()).toEqual([
|
2023-01-11 16:15:53 +01:00
|
|
|
['scheduled job failed', new Error('sync reason')],
|
2023-01-12 11:26:59 +01:00
|
|
|
['scheduled job failed', new Error('sync reason')],
|
2023-01-11 16:15:53 +01:00
|
|
|
]);
|
|
|
|
});
|