forked from signalapp/Signal-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67892d8
commit 746e99b
Showing
8 changed files
with
313 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2021 Signal Messenger, LLC | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { assert } from 'chai'; | ||
import * as sinon from 'sinon'; | ||
|
||
import { createBatcher } from '../../util/batcher'; | ||
import { sleep } from '../../util/sleep'; | ||
|
||
describe('batcher', () => { | ||
it('should schedule a full batch', async () => { | ||
const processBatch = sinon.fake.resolves(undefined); | ||
|
||
const batcher = createBatcher<number>({ | ||
name: 'test', | ||
wait: 10, | ||
maxSize: 2, | ||
processBatch, | ||
}); | ||
|
||
batcher.add(1); | ||
batcher.add(2); | ||
|
||
assert.ok(processBatch.calledOnceWith([1, 2]), 'Full batch on first call'); | ||
}); | ||
|
||
it('should schedule a partial batch', async () => { | ||
const processBatch = sinon.fake.resolves(undefined); | ||
|
||
const batcher = createBatcher<number>({ | ||
name: 'test', | ||
wait: 5, | ||
maxSize: 2, | ||
processBatch, | ||
}); | ||
|
||
batcher.add(1); | ||
|
||
await sleep(10); | ||
|
||
assert.ok(processBatch.calledOnceWith([1]), 'Partial batch after timeout'); | ||
}); | ||
|
||
it('should flushAndWait a partial batch', async () => { | ||
const processBatch = sinon.fake.resolves(undefined); | ||
|
||
const batcher = createBatcher<number>({ | ||
name: 'test', | ||
wait: 10000, | ||
maxSize: 1000, | ||
processBatch, | ||
}); | ||
|
||
batcher.add(1); | ||
|
||
await batcher.flushAndWait(); | ||
|
||
assert.ok( | ||
processBatch.calledOnceWith([1]), | ||
'Partial batch after flushAndWait' | ||
); | ||
}); | ||
|
||
it('should flushAndWait a partial batch with new items added', async () => { | ||
let calledTimes = 0; | ||
const processBatch = async (batch: Array<number>): Promise<void> => { | ||
calledTimes += 1; | ||
if (calledTimes === 1) { | ||
assert.deepEqual(batch, [1], 'First partial batch'); | ||
batcher.add(2); | ||
} else if (calledTimes === 2) { | ||
assert.deepEqual(batch, [2], 'Second partial batch'); | ||
} else { | ||
assert.strictEqual(calledTimes, 2); | ||
} | ||
}; | ||
|
||
const batcher = createBatcher<number>({ | ||
name: 'test', | ||
wait: 10000, | ||
maxSize: 1000, | ||
processBatch, | ||
}); | ||
|
||
batcher.add(1); | ||
|
||
await batcher.flushAndWait(); | ||
|
||
assert.strictEqual(calledTimes, 2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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'; | ||
|
||
describe('waitBatcher', () => { | ||
it('should schedule a full batch', async () => { | ||
const processBatch = sinon.fake.resolves(undefined); | ||
|
||
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'); | ||
}); | ||
|
||
it('should schedule a partial batch', async () => { | ||
const processBatch = sinon.fake.resolves(undefined); | ||
|
||
const batcher = createWaitBatcher<number>({ | ||
name: 'test', | ||
wait: 10, | ||
maxSize: 2, | ||
processBatch, | ||
}); | ||
|
||
await batcher.add(1); | ||
|
||
assert.ok(processBatch.calledOnceWith([1]), 'Partial batch on timeout'); | ||
}); | ||
|
||
it('should flush a partial batch', async () => { | ||
const processBatch = sinon.fake.resolves(undefined); | ||
|
||
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' | ||
); | ||
}); | ||
|
||
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, | ||
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'); | ||
assert(processBatch.secondCall.calledWith([2]), 'Second partial batch'); | ||
assert(!processBatch.thirdCall); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.