fix(utils): leaky bucket slow refill test (#2907)

* fix(utils): leaky bucket slow refill test

* fix the fix

* remove left comments

* Update packages/utils/tests/bucket.spec.ts

Co-authored-by: Jonathan Ho <heiheiho000@gmail.com>

---------

Co-authored-by: Jonathan Ho <heiheiho000@gmail.com>
This commit is contained in:
ITOH
2023-03-30 16:54:37 +02:00
committed by GitHub
parent fdf0d53f9e
commit 4fe621ef76

View File

@@ -3,6 +3,14 @@ import { afterEach, beforeEach, describe, it } from 'mocha'
import sinon from 'sinon'
import { LeakyBucket } from '../src/bucket.js'
async function promiseState(p: Promise<any>): Promise<string> {
const t = {}
return await Promise.race([p, t]).then(
(v) => (v === t ? 'pending' : 'fulfilled'),
() => 'rejected',
)
}
describe('bucket.ts', () => {
let clock: sinon.SinonFakeTimers
@@ -143,8 +151,21 @@ describe('bucket.ts', () => {
refillAmount: 1,
})
await bucket.acquire()
await bucket.acquire()
const acquired1 = bucket.acquire()
const acquired2 = bucket.acquire()
// js event loop
await (async () => {})()
expect(await promiseState(acquired1)).to.equal('fulfilled')
expect(await promiseState(acquired2)).to.equal('pending')
await clock.tickAsync(499)
expect(await promiseState(acquired2)).to.equal('pending')
await clock.tickAsync(1)
expect(await promiseState(acquired2)).to.equal('fulfilled')
expect(bucket.remaining).equals(0)
expect(bucket.used).equals(1)
})