taskbuffer/test/test.1.task.ts

90 lines
2.5 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';
2023-07-26 12:16:33 +00:00
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartdelay from '@push.rocks/smartdelay';
2017-06-17 14:56:33 +00:00
tap.test('new Task() should return a new task', async () => {
2023-08-01 22:51:43 +00:00
const testTask = new taskbuffer.Task({
2018-08-04 15:53:22 +00:00
taskFunction: async () => {
console.log('executed twice');
},
});
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
2023-08-01 22:51:43 +00:00
tap.test('should have bufferMax set to the provided value', async () => {
const task2 = new taskbuffer.Task({
taskFunction: async () => {},
});
expect(task2.bufferMax).toBeUndefined(); // test for a task without bufferMax set
const bufferedTask = new taskbuffer.Task({
taskFunction: async () => {},
buffered: true,
bufferMax: 3,
});
expect(bufferedTask.bufferMax).toEqual(3);
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2023-08-01 22:51:43 +00:00
tap.test('should be able to trigger tasks multiple times', async () => {
let task1Counter = 0;
const task1 = new taskbuffer.Task({
name: 'Task 1',
taskFunction: () => {
let done = smartpromise.defer();
console.log('Task1 started');
setTimeout(() => {
task1Counter++;
console.log('Task1 executed');
done.resolve();
}, 5000);
return done.promise;
},
});
await task1.trigger();
await task1.trigger();
expect(task1Counter).toEqual(2);
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2023-08-01 22:51:43 +00:00
tap.test('should execute setup function before the task function', async () => {
const task2 = new taskbuffer.Task({
name: 'Task 2',
taskSetup: async () => {
console.log('this is the setup function for task 2. It should only run once.')
return {
nice: 'yes',
}
},
taskFunction: async (before, setupArg) => {
expect(setupArg).toEqual({ nice: 'yes' });
const done = smartpromise.defer();
console.log('Task2 started');
setTimeout(() => {
console.log('Task2 executed');
done.resolve();
}, 5000);
await done.promise;
2020-07-12 00:48:51 +00:00
},
2018-08-04 15:53:22 +00:00
});
2023-08-01 22:51:43 +00:00
await task2.trigger();
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2023-08-01 22:51:43 +00:00
tap.test('should not exceed bufferMax when task is buffered', async () => {
let counter = 0;
const bufferedTask = new taskbuffer.Task({
2018-08-04 15:53:22 +00:00
taskFunction: async () => {
2023-08-01 22:51:43 +00:00
counter++;
await smartdelay.delayFor(2000);
counter--;
2018-08-04 15:53:22 +00:00
},
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
});
2023-08-01 22:51:43 +00:00
bufferedTask.trigger();
bufferedTask.trigger();
bufferedTask.trigger();
await smartdelay.delayFor(100);
expect(counter <= bufferedTask.bufferMax).toBeTrue();
2018-08-04 15:53:22 +00:00
});
tap.start();