2023-07-12 09:28:52 +00:00
|
|
|
import { tap, expect } from '@push.rocks/tapbundle';
|
2019-04-10 09:34:30 +00:00
|
|
|
|
2022-11-21 08:14:32 +00:00
|
|
|
import * as smarttime from '../ts/index.js';
|
2019-04-10 09:34:30 +00:00
|
|
|
|
|
|
|
let testCronManager: smarttime.CronManager;
|
|
|
|
|
|
|
|
tap.test('should create a valid instance of cronmanager', async () => {
|
|
|
|
testCronManager = new smarttime.CronManager();
|
2022-02-02 15:55:12 +00:00
|
|
|
expect(testCronManager).toBeInstanceOf(smarttime.CronManager);
|
2019-04-10 12:06:20 +00:00
|
|
|
});
|
|
|
|
|
2020-07-11 21:41:33 +00:00
|
|
|
tap.test('should create a valid cronJon', async (tools) => {
|
2019-04-10 12:06:20 +00:00
|
|
|
const done = tools.defer();
|
2020-09-03 20:06:02 +00:00
|
|
|
const done2 = tools.defer();
|
|
|
|
const done3 = tools.defer();
|
2019-04-10 12:06:20 +00:00
|
|
|
let counter = 0;
|
2020-09-03 20:06:02 +00:00
|
|
|
let counter2 = 0;
|
|
|
|
let counter3 = 0;
|
|
|
|
const cronJob = testCronManager.addCronjob('*/2 * * * * *', async () => {
|
2021-10-06 09:58:36 +00:00
|
|
|
if (counter === 5) {
|
2020-09-03 20:06:02 +00:00
|
|
|
testCronManager.removeCronjob(cronJob);
|
2019-04-10 12:06:20 +00:00
|
|
|
done.resolve();
|
|
|
|
}
|
2019-06-17 14:54:39 +00:00
|
|
|
counter++;
|
2020-09-03 20:06:02 +00:00
|
|
|
console.log(`${new Date().getSeconds()} hey ${counter} -> runs every 2 seconds`);
|
|
|
|
});
|
|
|
|
const cronJob2 = testCronManager.addCronjob('*/3 * * * * *', async () => {
|
2021-10-06 09:58:36 +00:00
|
|
|
if (counter2 === 5) {
|
2020-09-03 20:06:02 +00:00
|
|
|
testCronManager.removeCronjob(cronJob2);
|
|
|
|
done2.resolve();
|
|
|
|
}
|
|
|
|
counter2++;
|
|
|
|
console.log(`${new Date().getSeconds()} hey ${counter2} -> runs every 3 seconds`);
|
|
|
|
});
|
|
|
|
const cronJob3 = testCronManager.addCronjob('*/4 * * * * *', async () => {
|
2021-10-06 09:58:36 +00:00
|
|
|
if (counter3 === 5) {
|
2020-09-03 20:06:02 +00:00
|
|
|
done3.resolve();
|
|
|
|
}
|
|
|
|
counter3++;
|
|
|
|
console.log(`${new Date().getSeconds()} hey ${counter3} -> runs every 4 seconds`);
|
2019-04-10 12:06:20 +00:00
|
|
|
});
|
|
|
|
testCronManager.start();
|
|
|
|
await done.promise;
|
2020-09-03 20:06:02 +00:00
|
|
|
await done2.promise;
|
|
|
|
await done3.promise;
|
2019-04-10 12:06:20 +00:00
|
|
|
testCronManager.stop();
|
2020-09-07 17:16:11 +00:00
|
|
|
testCronManager.removeCronjob(cronJob3);
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('runs every full minute', async (tools) => {
|
|
|
|
const done = tools.defer();
|
|
|
|
const cronJob = testCronManager.addCronjob('0 * * * * *', async () => {
|
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
testCronManager.start();
|
|
|
|
await done.promise;
|
|
|
|
testCronManager.stop();
|
2019-04-10 09:34:30 +00:00
|
|
|
});
|
|
|
|
|
2024-06-23 21:27:10 +00:00
|
|
|
export default tap.start();
|