Conversation: savePromises for off-queue post-handleDataMessage work
This commit is contained in:
parent
059c971ee2
commit
768ab26a20
5 changed files with 145 additions and 62 deletions
|
@ -5,11 +5,23 @@ import { assert } from 'chai';
|
|||
import * as sinon from 'sinon';
|
||||
|
||||
import { createWaitBatcher } from '../../util/waitBatcher';
|
||||
import { drop } from '../../util/drop';
|
||||
import { sleep } from '../../util/sleep';
|
||||
|
||||
describe('waitBatcher', () => {
|
||||
it('should schedule a full batch', async () => {
|
||||
const processBatch = sinon.fake.resolves(undefined);
|
||||
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;
|
||||
});
|
||||
});
|
||||
|
||||
it('should schedule a full batch', async () => {
|
||||
const batcher = createWaitBatcher<number>({
|
||||
name: 'test',
|
||||
wait: 10,
|
||||
|
@ -20,11 +32,10 @@ describe('waitBatcher', () => {
|
|||
await Promise.all([batcher.add(1), batcher.add(2)]);
|
||||
|
||||
assert.ok(processBatch.calledOnceWith([1, 2]), 'Full batch on first call');
|
||||
assert.deepEqual(processResults[0], [1, 2]);
|
||||
});
|
||||
|
||||
it('should schedule a partial batch', async () => {
|
||||
const processBatch = sinon.fake.resolves(undefined);
|
||||
|
||||
const batcher = createWaitBatcher<number>({
|
||||
name: 'test',
|
||||
wait: 10,
|
||||
|
@ -35,11 +46,10 @@ describe('waitBatcher', () => {
|
|||
await batcher.add(1);
|
||||
|
||||
assert.ok(processBatch.calledOnceWith([1]), 'Partial batch on timeout');
|
||||
assert.deepEqual(processResults[0], [1]);
|
||||
});
|
||||
|
||||
it('should flush a partial batch', async () => {
|
||||
const processBatch = sinon.fake.resolves(undefined);
|
||||
|
||||
const batcher = createWaitBatcher<number>({
|
||||
name: 'test',
|
||||
wait: 10000,
|
||||
|
@ -53,11 +63,10 @@ describe('waitBatcher', () => {
|
|||
processBatch.calledOnceWith([1]),
|
||||
'Partial batch on flushAndWait'
|
||||
);
|
||||
assert.deepEqual(processResults[0], [1]);
|
||||
});
|
||||
|
||||
it('should flush a partial batch with new items added', async () => {
|
||||
const processBatch = sinon.fake.resolves(undefined);
|
||||
|
||||
const batcher = createWaitBatcher<number>({
|
||||
name: 'test',
|
||||
wait: 10000,
|
||||
|
@ -74,7 +83,37 @@ describe('waitBatcher', () => {
|
|||
]);
|
||||
|
||||
assert(processBatch.firstCall.calledWith([1]), 'First partial batch');
|
||||
assert.deepEqual(processResults[0], [1]);
|
||||
assert(processBatch.secondCall.calledWith([2]), 'Second partial batch');
|
||||
assert.deepEqual(processResults[1], [2]);
|
||||
assert(!processBatch.thirdCall);
|
||||
});
|
||||
|
||||
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]);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue