fix: increase util test coverage

This commit is contained in:
Skillz4Killz
2023-03-27 14:44:00 +00:00
parent c44d32f93c
commit 7152bbe9b2
4 changed files with 59 additions and 24 deletions

View File

@@ -30,8 +30,7 @@ export class LeakyBucket implements LeakyBucketOptions {
/** Refills the bucket as needed. */
refillBucket(): void {
console.log('refilling bucket');
logger.info(`[LeakyBucket] Timeout for leaky bucket requests executed. Refilling bucket.`)
logger.debug(`[LeakyBucket] Timeout for leaky bucket requests executed. Refilling bucket.`)
// Lower the used amount by the refill amount
this.used = this.refillAmount > this.used ? 0 : this.used - this.refillAmount
// Reset the refillsAt timestamp since it just got refilled
@@ -39,7 +38,9 @@ export class LeakyBucket implements LeakyBucketOptions {
if (this.used > 0) {
if (this.timeoutId) clearTimeout(this.timeoutId)
this.timeoutId = setTimeout(() => this.refillBucket, this.refillInterval)
this.timeoutId = setTimeout(() => {
this.refillBucket()
}, this.refillInterval)
this.refillsAt = Date.now() + this.refillInterval
}
}
@@ -63,7 +64,9 @@ export class LeakyBucket implements LeakyBucketOptions {
if (!this.timeoutId) {
logger.debug(`[LeakyBucket] Creating new timeout for leaky bucket requests.`)
this.timeoutId = setTimeout(() => this.refillBucket, this.refillInterval)
this.timeoutId = setTimeout(() => {
this.refillBucket()
}, this.refillInterval)
// Set the time for when this refill will occur.
this.refillsAt = Date.now() + this.refillInterval
}

View File

@@ -5,8 +5,6 @@
// on npm.
// https://deno.land/std@0.153.0/fmt/colors.ts?source
const noColor = false
export interface Code {
open: string
close: string
@@ -20,17 +18,13 @@ export interface Rgb {
b: number
}
let enabled = !noColor
let enabled = true
/**
* Set changing text color to enabled or disabled
* @param value
*/
export function setColorEnabled(value: boolean) {
if (noColor) {
return
}
enabled = value
}

View File

@@ -55,11 +55,10 @@ describe('bucket.ts', () => {
})
it('will return bucket with default property', () => {
const bucket = new LeakyBucket({
max: 111,
refillInterval: 2002,
refillAmount: 3003,
})
const bucket = new LeakyBucket()
expect(bucket.max).equals(1)
expect(bucket.refillInterval).equals(5000)
expect(bucket.refillAmount).equals(1)
expect(bucket.queue).to.deep.equal([])
})
@@ -99,25 +98,58 @@ describe('bucket.ts', () => {
}
})
it('idk', async () => {
it('bucket refills are done properly', async () => {
const bucket = new LeakyBucket({
max: 2,
refillInterval: 500,
refillAmount: 2,
})
const now = Date.now()
await bucket.acquire()
console.log(Date.now() - now, bucket.used, bucket.remaining)
expect(bucket.remaining).equals(1)
expect(bucket.used).equals(1)
await clock.tickAsync(1000)
console.log(Date.now() - now, bucket.used, bucket.remaining)
expect(bucket.remaining).equals(2)
expect(bucket.used).equals(0)
await bucket.acquire()
console.log(Date.now() - now, bucket.used, bucket.remaining)
await clock.tickAsync(1000)
console.log(Date.now() - now, bucket.used, bucket.remaining)
})
it('bucket refills when refill amount is < max', async () => {
const bucket = new LeakyBucket({
max: 3,
refillInterval: 800,
refillAmount: 1,
})
await bucket.acquire()
await bucket.acquire()
expect(bucket.remaining).equals(1)
expect(bucket.used).equals(2)
await clock.tickAsync(1000)
expect(bucket.remaining).equals(2)
expect(bucket.used).equals(1)
await clock.tickAsync(2000)
expect(bucket.remaining).equals(3)
expect(bucket.used).equals(0)
})
it('bucket refills when refill interval is slow', async () => {
const bucket = new LeakyBucket({
max: 1,
refillInterval: 500,
refillAmount: 1,
})
await bucket.acquire()
await bucket.acquire()
expect(bucket.remaining).equals(0)
expect(bucket.used).equals(1)
// await clock.tickAsync(600)
// expect(bucket.remaining).equals(1)
// expect(bucket.used).equals(0)
})
})
})

View File

@@ -6,6 +6,12 @@ describe('Logger', () => {
it('create logger with default options', () => {
const loggy = createLogger()
loggy.setLevel(LogLevels.Debug)
loggy.debug('debugging')
loggy.error('error')
loggy.fatal('fatal')
loggy.info('info')
loggy.warn('warn')
loggy.setDepth(LogDepth.Full)
loggy.debug('debugging')