2021-03-26 00:00:03 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
import * as sinon from 'sinon';
|
|
|
|
|
|
|
|
import { createWaitBatcher } from '../../util/waitBatcher';
|
2024-08-28 06:05:08 +00:00
|
|
|
import { drop } from '../../util/drop';
|
|
|
|
import { sleep } from '../../util/sleep';
|
2021-03-26 00:00:03 +00:00
|
|
|
|
|
|
|
describe('waitBatcher', () => {
|
2024-08-28 06:05:08 +00:00
|
|
|
let processBatch: sinon.SinonSpy;
|
|
|
|
let processResults: Array<Array<number>>;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
processResults = [];
|
|
|
|
processBatch = sinon.fake(async (list: Array<number>) => {
|
|
|
|
await sleep(1);
|
|
|
|
processResults.push(list);
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
});
|
2021-03-26 00:00:03 +00:00
|
|
|
|
2024-08-28 06:05:08 +00:00
|
|
|
it('should schedule a full batch', async () => {
|
2021-03-26 00:00:03 +00:00
|
|
|
const batcher = createWaitBatcher<number>({
|
|
|
|
name: 'test',
|
|
|
|
wait: 10,
|
|
|
|
maxSize: 2,
|
|
|
|
processBatch,
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all([batcher.add(1), batcher.add(2)]);
|
|
|
|
|
|
|
|
assert.ok(processBatch.calledOnceWith([1, 2]), 'Full batch on first call');
|
2024-08-28 06:05:08 +00:00
|
|
|
assert.deepEqual(processResults[0], [1, 2]);
|
2021-03-26 00:00:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should schedule a partial batch', async () => {
|
|
|
|
const batcher = createWaitBatcher<number>({
|
|
|
|
name: 'test',
|
|
|
|
wait: 10,
|
|
|
|
maxSize: 2,
|
|
|
|
processBatch,
|
|
|
|
});
|
|
|
|
|
|
|
|
await batcher.add(1);
|
|
|
|
|
|
|
|
assert.ok(processBatch.calledOnceWith([1]), 'Partial batch on timeout');
|
2024-08-28 06:05:08 +00:00
|
|
|
assert.deepEqual(processResults[0], [1]);
|
2021-03-26 00:00:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should flush a partial batch', async () => {
|
|
|
|
const batcher = createWaitBatcher<number>({
|
|
|
|
name: 'test',
|
|
|
|
wait: 10000,
|
|
|
|
maxSize: 1000,
|
|
|
|
processBatch,
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all([batcher.add(1), batcher.flushAndWait()]);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
processBatch.calledOnceWith([1]),
|
|
|
|
'Partial batch on flushAndWait'
|
|
|
|
);
|
2024-08-28 06:05:08 +00:00
|
|
|
assert.deepEqual(processResults[0], [1]);
|
2021-03-26 00:00:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should flush a partial batch with new items added', async () => {
|
|
|
|
const batcher = createWaitBatcher<number>({
|
|
|
|
name: 'test',
|
|
|
|
wait: 10000,
|
|
|
|
maxSize: 1000,
|
|
|
|
processBatch,
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
(async () => {
|
|
|
|
await batcher.add(1);
|
|
|
|
await batcher.add(2);
|
|
|
|
})(),
|
|
|
|
batcher.flushAndWait(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert(processBatch.firstCall.calledWith([1]), 'First partial batch');
|
2024-08-28 06:05:08 +00:00
|
|
|
assert.deepEqual(processResults[0], [1]);
|
2021-03-26 00:00:03 +00:00
|
|
|
assert(processBatch.secondCall.calledWith([2]), 'Second partial batch');
|
2024-08-28 06:05:08 +00:00
|
|
|
assert.deepEqual(processResults[1], [2]);
|
2021-03-26 00:00:03 +00:00
|
|
|
assert(!processBatch.thirdCall);
|
|
|
|
});
|
2024-08-28 06:05:08 +00:00
|
|
|
|
|
|
|
it('#addNoopAndWait returns as soon as #2 is complete, but more have been added', async () => {
|
|
|
|
const batcher = createWaitBatcher<number>({
|
|
|
|
name: 'test',
|
|
|
|
wait: 1000,
|
|
|
|
maxSize: 1000,
|
|
|
|
processBatch,
|
|
|
|
});
|
|
|
|
|
|
|
|
drop(batcher.add(1));
|
|
|
|
drop(batcher.add(2));
|
|
|
|
const waitPromise = batcher.pushNoopAndWait();
|
|
|
|
drop(batcher.add(3));
|
|
|
|
|
|
|
|
await waitPromise;
|
|
|
|
|
|
|
|
assert(processBatch.firstCall.calledWith([1, 2]), 'First partial batch');
|
|
|
|
assert.deepEqual(processResults[0], [1, 2]);
|
|
|
|
|
|
|
|
// Typescript needs this; doesn't realize that secondCall could be set later
|
|
|
|
const { secondCall } = processBatch;
|
|
|
|
assert(!secondCall);
|
|
|
|
|
|
|
|
await batcher.flushAndWait();
|
|
|
|
|
|
|
|
assert(processBatch.secondCall.calledWith([3]), 'Second partial batch');
|
|
|
|
assert.deepEqual(processResults[1], [3]);
|
|
|
|
});
|
2021-03-26 00:00:03 +00:00
|
|
|
});
|