taskbuffer/test/test.1.task.ts

117 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

2018-08-04 15:53:22 +00:00
import { expect, tap } from '@pushrocks/tapbundle';
2022-03-25 11:14:49 +00:00
import * as taskbuffer from '../ts/index.js';
2017-06-17 14:56:33 +00:00
2018-08-04 15:53:22 +00:00
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartdelay from '@pushrocks/smartdelay';
2017-06-17 14:56:33 +00:00
// setup some testData to work with
2018-08-04 15:53:22 +00:00
let testTask: taskbuffer.Task;
2017-06-17 14:56:33 +00:00
let testPreTask = new taskbuffer.Task({
2019-01-14 23:17:44 +00:00
taskFunction: async () => {
2018-08-04 15:53:22 +00:00
console.log('preTask executed');
2017-06-17 14:56:33 +00:00
},
2020-07-12 00:48:51 +00:00
preTask: testTask,
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
// some more tasks to test with
2018-08-04 15:53:22 +00:00
let task1Counter = 0; // how often task 1 is being executed
2017-06-17 14:56:33 +00:00
let task1 = new taskbuffer.Task({
name: 'Task 1',
taskFunction: () => {
2018-08-04 15:53:22 +00:00
let done = smartpromise.defer();
console.log('Task1 started');
2017-06-17 14:56:33 +00:00
setTimeout(() => {
2018-08-04 15:53:22 +00:00
task1Counter++;
console.log('Task1 executed');
done.resolve();
}, 5000);
return done.promise;
2020-07-12 00:48:51 +00:00
},
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
let task2 = new taskbuffer.Task({
name: 'Task 1',
2019-01-14 23:17:44 +00:00
taskFunction: async () => {
const done = smartpromise.defer();
2018-08-04 15:53:22 +00:00
console.log('Task2 started');
2017-06-17 14:56:33 +00:00
setTimeout(() => {
2018-08-04 15:53:22 +00:00
console.log('Task2 executed');
done.resolve();
}, 5000);
2019-01-14 23:17:44 +00:00
await done.promise;
2020-07-12 00:48:51 +00:00
},
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
let task3 = new taskbuffer.Task({
name: 'Task 3',
taskFunction: () => {
2018-08-04 15:53:22 +00:00
let done = smartpromise.defer();
console.log('Task3 started');
2017-06-17 14:56:33 +00:00
setTimeout(() => {
2018-08-04 15:53:22 +00:00
console.log('Task3 executed');
done.resolve();
}, 5000);
return done.promise;
2020-07-12 00:48:51 +00:00
},
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
tap.test('new Task() should return a new task', async () => {
2018-08-04 15:53:22 +00:00
testTask = new taskbuffer.Task({
taskFunction: async () => {
console.log('executed twice');
},
2020-07-12 00:48:51 +00:00
preTask: testPreTask,
2018-08-04 15:53:22 +00:00
});
});
2017-06-17 14:56:33 +00:00
2017-06-17 15:29:26 +00:00
tap.test('expect testTask to be an instance of Task', async () => {
2022-03-25 11:14:49 +00:00
expect(testTask).toBeInstanceOf(taskbuffer.Task);
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2017-06-17 15:29:26 +00:00
tap.test('expect testTask.idle is true', async () => {
2017-06-17 14:56:33 +00:00
if (!testTask.idle) {
2018-08-04 15:53:22 +00:00
throw new Error('testTask.idle is not true');
2017-06-17 14:56:33 +00:00
}
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2017-06-17 15:29:26 +00:00
tap.test('testTask.running should be of type boolean and initially false', async () => {
2022-03-25 11:14:49 +00:00
expect(testTask.running).toBeTypeofBoolean();
2017-06-17 14:56:33 +00:00
// tslint:disable-next-line:no-unused-expression
2022-03-25 11:14:49 +00:00
expect(testTask.running).toBeFalse();
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2017-06-17 15:29:26 +00:00
tap.test('testTask.trigger() should return Promise', async () => {
2022-03-25 11:14:49 +00:00
expect(testTask.trigger()).toBeInstanceOf(Promise);
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2017-06-17 15:29:26 +00:00
tap.test('testTask.trigger() returned Promise should be fullfilled', async () => {
2018-08-04 15:53:22 +00:00
await testTask.trigger();
});
2017-06-17 14:56:33 +00:00
2017-06-17 15:29:26 +00:00
tap.test('expect to run a task without pre and afterTask errorless', async () => {
2018-08-04 15:53:22 +00:00
let localTestTask = new taskbuffer.Task({
taskFunction: async () => {
console.log('only once');
2020-07-12 00:48:51 +00:00
},
2018-08-04 15:53:22 +00:00
});
await localTestTask.trigger();
});
2017-06-17 14:56:33 +00:00
2017-06-17 15:29:26 +00:00
tap.test('expect task to run in buffered mode', async () => {
2017-06-17 14:56:33 +00:00
let localTestTask = new taskbuffer.Task({
2018-08-04 15:53:22 +00:00
taskFunction: async () => {
await smartdelay.delayFor(3000);
},
2017-06-17 14:56:33 +00:00
buffered: true,
2020-07-12 00:48:51 +00:00
bufferMax: 2,
2018-08-04 15:53:22 +00:00
});
localTestTask.trigger();
localTestTask.trigger();
localTestTask.trigger();
await localTestTask.trigger();
});
tap.start();