Files
lik/test/test.timedaggregator.both.ts

82 lines
2.2 KiB
TypeScript

// import test framework
import { expect, tap } from '@push.rocks/tapbundle';
// import the module
import * as lik from '../ts/index.js';
tap.test('should create a timed aggregator and aggregate items', async (tools) => {
const batches: string[][] = [];
const aggregator = new lik.TimedAggregtor<string>({
aggregationIntervalInMillis: 200,
functionForAggregation: (aggregation) => {
batches.push(aggregation);
},
});
aggregator.add('first');
aggregator.add('second');
await tools.delayFor(300);
expect(batches.length).toEqual(1);
expect(batches[0]).toContain('first');
expect(batches[0]).toContain('second');
aggregator.add('third');
await tools.delayFor(300);
expect(batches.length).toEqual(2);
expect(batches[1]).toContain('third');
aggregator.stop();
});
tap.test('stop() prevents further aggregation', async (tools) => {
const batches: number[][] = [];
const aggregator = new lik.TimedAggregtor<number>({
aggregationIntervalInMillis: 100,
functionForAggregation: (items) => {
batches.push(items);
},
});
aggregator.add(1);
aggregator.stop();
aggregator.add(2);
await tools.delayFor(200);
expect(batches.length).toEqual(0);
});
tap.test('stop(true) flushes remaining items', async () => {
const batches: number[][] = [];
const aggregator = new lik.TimedAggregtor<number>({
aggregationIntervalInMillis: 5000,
functionForAggregation: (items) => {
batches.push(items);
},
});
aggregator.add(10);
aggregator.add(20);
aggregator.stop(true);
expect(batches.length).toEqual(1);
expect(batches[0]).toEqual([10, 20]);
});
tap.test('restart allows adding again after stop', async (tools) => {
const batches: string[][] = [];
const aggregator = new lik.TimedAggregtor<string>({
aggregationIntervalInMillis: 100,
functionForAggregation: (items) => {
batches.push(items);
},
});
aggregator.add('a');
aggregator.stop();
aggregator.restart();
aggregator.add('b');
await tools.delayFor(200);
expect(batches.length).toEqual(1);
expect(batches[0]).toContain('b');
aggregator.stop();
});
tap.test('TimedAggregator alias exists', async () => {
expect(lik.TimedAggregator).toEqual(lik.TimedAggregtor);
});
export default tap.start();