2023-04-08 22:17:52 +03:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import { AsyncUtils } from 'lib/utils'
|
|
|
|
|
|
|
|
describe('utils', () => {
|
|
|
|
describe('namespace AsyncUtils', () => {
|
|
|
|
describe('class PromisePooler', () => {
|
|
|
|
let pooler: AsyncUtils.PromisePooler | null
|
|
|
|
|
|
|
|
function tp(delay: number): Promise<number> {
|
|
|
|
return new Promise((resolve) =>
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve(delay)
|
|
|
|
}, delay)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
pooler = null
|
|
|
|
})
|
|
|
|
|
|
|
|
it('pool(Many) & pending & totalAdded', async () => {
|
2023-04-27 20:34:22 +03:00
|
|
|
pooler = new AsyncUtils.PromisePooler([tp], [], 8)
|
2023-04-08 22:17:52 +03:00
|
|
|
pooler.pool(30)
|
|
|
|
expect(pooler.pending).to.equal(1)
|
|
|
|
|
|
|
|
await pooler.poolMany([20], [30], [32], [40])
|
|
|
|
expect(pooler.pending).to.equal(5)
|
|
|
|
|
|
|
|
await pooler.pool(50)
|
|
|
|
await pooler.pool(50)
|
|
|
|
expect(pooler.pending).to.equal(7)
|
|
|
|
expect(await pooler.all()).to.eql([30, 20, 30, 32, 40, 50, 50])
|
|
|
|
expect(pooler.pending).to.equal(0)
|
|
|
|
expect(pooler.totalAdded).to.equal(7)
|
|
|
|
expect(pooler._results.length).to.equal(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('race: promises should be raced in proper order', async () => {
|
|
|
|
let res = []
|
2023-04-27 20:34:22 +03:00
|
|
|
pooler = new AsyncUtils.PromisePooler([tp, tp, tp, tp], [], 8)
|
2023-04-08 22:17:52 +03:00
|
|
|
|
|
|
|
await pooler.pool(20)
|
|
|
|
await pooler.poolMany([10], [20], [22], [30])
|
|
|
|
await pooler.pool(40)
|
|
|
|
await pooler.pool(40)
|
|
|
|
expect(pooler.pending).to.equal(7)
|
|
|
|
|
|
|
|
for (let i = 0; i < 7; i++) {
|
|
|
|
res[i] = await pooler.race()
|
|
|
|
}
|
|
|
|
expect(res).to.eql([10, 20, 20, 22, 30, 40, 40])
|
|
|
|
expect(pooler._results.length).to.equal(0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|