taskbuffer/test/test.4.taskmanager.ts

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2018-08-04 15:53:22 +00:00
import { expect, tap } from '@pushrocks/tapbundle';
import taskbuffer = require('../ts/index');
2017-06-18 11:25:09 +00:00
2018-08-04 15:53:22 +00:00
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartdelay from '@pushrocks/smartdelay';
2017-06-18 11:25:09 +00:00
2018-08-04 15:53:22 +00:00
let myTaskManager: taskbuffer.TaskManager;
let taskRunCounter = 0;
2019-09-05 09:35:26 +00:00
const taskDone = smartpromise.defer();
2017-06-18 11:25:09 +00:00
tap.test('should create an instance of TaskManager', async () => {
2018-08-04 15:53:22 +00:00
myTaskManager = new taskbuffer.TaskManager();
expect(myTaskManager).to.be.instanceof(taskbuffer.TaskManager);
});
2017-06-18 11:25:09 +00:00
tap.test('should run the task as expected', async () => {
2018-08-04 15:53:22 +00:00
let referenceBoolean = false;
myTaskManager.addTask(
new taskbuffer.Task({
name: 'myTask',
taskFunction: async () => {
console.log('Task executed!');
referenceBoolean = true;
taskRunCounter++;
if (taskRunCounter === 10) {
taskDone.resolve();
}
2020-07-12 00:48:51 +00:00
},
2018-08-04 15:53:22 +00:00
})
);
await myTaskManager.triggerTaskByName('myTask');
2017-06-18 11:25:09 +00:00
// tslint:disable-next-line:no-unused-expression
2018-08-04 15:53:22 +00:00
expect(referenceBoolean).to.be.true;
});
2017-06-18 11:25:09 +00:00
tap.test('should schedule task', async () => {
2018-08-04 15:53:22 +00:00
myTaskManager.scheduleTaskByName('myTask', '* * * * * *');
await taskDone.promise;
myTaskManager.descheduleTaskByName('myTask');
});
2017-06-18 11:25:09 +00:00
2018-08-04 15:53:22 +00:00
tap.start();