taskbuffer/test/test.8.bufferedrun.ts

53 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2023-07-26 12:16:33 +00:00
import { tap, expect } from '@push.rocks/tapbundle';
2021-11-11 18:59:56 +00:00
2022-03-25 11:14:49 +00:00
import * as taskbuffer from '../ts/index.js';
2021-11-11 18:59:56 +00:00
let counter1 = 0;
let counter2 = 0;
let counter3 = 0;
tap.test('should run buffered', async (tools) => {
const task = new taskbuffer.Task({
name: 'a buffered task',
taskFunction: async () => {
counter1++;
await tools.delayFor(2000);
console.log(`task 1 ran ${counter1} times`);
},
buffered: true,
bufferMax: 1,
afterTask: () => {
return task2;
2021-11-11 19:48:31 +00:00
},
2021-11-11 18:59:56 +00:00
});
const task2 = new taskbuffer.Task({
name: 'a buffered task',
taskFunction: async () => {
counter2++;
await tools.delayFor(2000);
console.log(`task2 ran ${counter2} times`);
},
buffered: true,
bufferMax: 1,
afterTask: () => {
return task3;
2021-11-11 19:48:31 +00:00
},
2021-11-11 18:59:56 +00:00
});
const task3 = new taskbuffer.Task({
name: 'a buffered task',
taskFunction: async () => {
counter3++;
await tools.delayFor(2000);
console.log(`task3 ran ${counter3} times`);
},
buffered: true,
2021-11-11 19:48:31 +00:00
bufferMax: 1,
2021-11-11 18:59:56 +00:00
});
2021-11-11 19:48:31 +00:00
while (counter1 < 10) {
2021-11-11 18:59:56 +00:00
await tools.delayFor(5000);
task.trigger();
}
2021-11-11 19:48:31 +00:00
});
2021-11-11 18:59:56 +00:00
2021-11-11 19:48:31 +00:00
tap.start();