mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-03 17:30:07 +00:00
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:
@@ -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)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user