sdk-monorepo/src/test/utils.test.ts

57 lines
1.6 KiB
TypeScript
Raw Normal View History

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 () => {
pooler = new AsyncUtils.PromisePooler([tp], [], 8)
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 = []
pooler = new AsyncUtils.PromisePooler([tp, tp, tp, tp], [], 8)
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)
})
})
})
})