taskbuffer/test/test.1.task.ts

116 lines
2.8 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-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
},
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;
2017-06-17 14:56:33 +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;
2017-06-17 14:56:33 +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;
2017-06-17 14:56:33 +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');
},
preTask: testPreTask
});
});
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 () => {
2018-08-04 15:53:22 +00:00
expect(testTask).to.be.instanceof(taskbuffer.Task);
});
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 () => {
2018-08-04 15:53:22 +00:00
expect(testTask.running).to.be.a('boolean');
2017-06-17 14:56:33 +00:00
// tslint:disable-next-line:no-unused-expression
2018-08-04 15:53:22 +00:00
expect(testTask.running).to.be.false;
});
2017-06-17 14:56:33 +00:00
2017-06-17 15:29:26 +00:00
tap.test('testTask.trigger() should return Promise', async () => {
2018-08-04 15:53:22 +00:00
expect(testTask.trigger()).to.be.instanceof(Promise);
});
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');
}
});
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,
bufferMax: 2
2018-08-04 15:53:22 +00:00
});
localTestTask.trigger();
localTestTask.trigger();
localTestTask.trigger();
await localTestTask.trigger();
});
tap.start();