taskbuffer/test/test.4.taskmanager.both.ts

51 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

2023-07-26 12:16:33 +00:00
import { expect, tap } from '@push.rocks/tapbundle';
2022-03-25 11:14:49 +00:00
import * as taskbuffer from '../ts/index.js';
2020-07-12 00:48:51 +00:00
2023-07-26 12:16:33 +00:00
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartdelay from '@push.rocks/smartdelay';
2020-07-12 00:48:51 +00:00
let myTaskManager: taskbuffer.TaskManager;
let taskRunCounter = 0;
const taskDone = smartpromise.defer();
tap.test('should create an instance of TaskManager', async () => {
myTaskManager = new taskbuffer.TaskManager();
2022-03-25 11:14:49 +00:00
expect(myTaskManager).toBeInstanceOf(taskbuffer.TaskManager);
2020-07-12 00:48:51 +00:00
});
tap.test('should run the task as expected', async () => {
let referenceBoolean = false;
myTaskManager.addTask(
new taskbuffer.Task({
name: 'myTask',
2021-11-11 19:37:27 +00:00
bufferMax: 1,
buffered: true,
2020-07-12 00:48:51 +00:00
taskFunction: async () => {
2021-11-11 19:37:27 +00:00
console.log('Task "myTask" executed!');
2020-07-12 00:48:51 +00:00
referenceBoolean = true;
taskRunCounter++;
if (taskRunCounter === 10) {
taskDone.resolve();
}
},
})
);
2020-09-02 13:04:12 +00:00
myTaskManager.start();
2020-07-12 00:48:51 +00:00
await myTaskManager.triggerTaskByName('myTask');
// tslint:disable-next-line:no-unused-expression
2022-03-25 11:14:49 +00:00
expect(referenceBoolean).toBeTrue();
2020-07-12 00:48:51 +00:00
});
tap.test('should schedule task', async () => {
2021-11-11 19:37:27 +00:00
myTaskManager.scheduleTaskByName('myTask', '*/10 * * * * *');
2020-07-12 00:48:51 +00:00
await taskDone.promise;
myTaskManager.descheduleTaskByName('myTask');
});
2020-09-02 13:04:12 +00:00
tap.test('should stop the taskmanager', async () => {
myTaskManager.stop();
});
2020-07-12 00:48:51 +00:00
tap.start();