2023-04-08 15:17:52 -04:00
|
|
|
import { expect } from 'chai'
|
2023-05-02 18:19:28 -04:00
|
|
|
import { AsyncUtils, HexUtils } from '@tornado/sdk-utils'
|
2023-04-08 15:17:52 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
2023-05-02 18:19:28 -04:00
|
|
|
it('Should print a hex num', async () => {
|
|
|
|
console.log(HexUtils.numberToHex(5))
|
|
|
|
})
|
|
|
|
|
2023-04-08 15:17:52 -04:00
|
|
|
it('pool(Many) & pending & totalAdded', async () => {
|
2023-04-27 13:34:22 -04:00
|
|
|
pooler = new AsyncUtils.PromisePooler([tp], [], 8)
|
2023-04-08 15:17:52 -04: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 () => {
|
2023-05-02 18:19:28 -04:00
|
|
|
let res: any[] = []
|
2023-04-27 13:34:22 -04:00
|
|
|
pooler = new AsyncUtils.PromisePooler([tp, tp, tp, tp], [], 8)
|
2023-04-08 15:17:52 -04: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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|