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
+7 -4
View File
@@ -30,8 +30,7 @@ export class LeakyBucket implements LeakyBucketOptions {
/** Refills the bucket as needed. */ /** Refills the bucket as needed. */
refillBucket(): void { refillBucket(): void {
console.log('refilling bucket'); logger.debug(`[LeakyBucket] Timeout for leaky bucket requests executed. Refilling bucket.`)
logger.info(`[LeakyBucket] Timeout for leaky bucket requests executed. Refilling bucket.`)
// Lower the used amount by the refill amount // Lower the used amount by the refill amount
this.used = this.refillAmount > this.used ? 0 : this.used - this.refillAmount this.used = this.refillAmount > this.used ? 0 : this.used - this.refillAmount
// Reset the refillsAt timestamp since it just got refilled // Reset the refillsAt timestamp since it just got refilled
@@ -39,7 +38,9 @@ export class LeakyBucket implements LeakyBucketOptions {
if (this.used > 0) { if (this.used > 0) {
if (this.timeoutId) clearTimeout(this.timeoutId) 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 this.refillsAt = Date.now() + this.refillInterval
} }
} }
@@ -63,7 +64,9 @@ export class LeakyBucket implements LeakyBucketOptions {
if (!this.timeoutId) { if (!this.timeoutId) {
logger.debug(`[LeakyBucket] Creating new timeout for leaky bucket requests.`) 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. // Set the time for when this refill will occur.
this.refillsAt = Date.now() + this.refillInterval this.refillsAt = Date.now() + this.refillInterval
} }
+1 -7
View File
@@ -5,8 +5,6 @@
// on npm. // on npm.
// https://deno.land/std@0.153.0/fmt/colors.ts?source // https://deno.land/std@0.153.0/fmt/colors.ts?source
const noColor = false
export interface Code { export interface Code {
open: string open: string
close: string close: string
@@ -20,17 +18,13 @@ export interface Rgb {
b: number b: number
} }
let enabled = !noColor let enabled = true
/** /**
* Set changing text color to enabled or disabled * Set changing text color to enabled or disabled
* @param value * @param value
*/ */
export function setColorEnabled(value: boolean) { export function setColorEnabled(value: boolean) {
if (noColor) {
return
}
enabled = value enabled = value
} }
+45 -13
View File
@@ -55,11 +55,10 @@ describe('bucket.ts', () => {
}) })
it('will return bucket with default property', () => { it('will return bucket with default property', () => {
const bucket = new LeakyBucket({ const bucket = new LeakyBucket()
max: 111, expect(bucket.max).equals(1)
refillInterval: 2002, expect(bucket.refillInterval).equals(5000)
refillAmount: 3003, expect(bucket.refillAmount).equals(1)
})
expect(bucket.queue).to.deep.equal([]) 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({ const bucket = new LeakyBucket({
max: 2, max: 2,
refillInterval: 500, refillInterval: 500,
refillAmount: 2, refillAmount: 2,
}) })
const now = Date.now()
await bucket.acquire() await bucket.acquire()
expect(bucket.remaining).equals(1)
console.log(Date.now() - now, bucket.used, bucket.remaining) expect(bucket.used).equals(1)
await clock.tickAsync(1000) 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() await bucket.acquire()
console.log(Date.now() - now, bucket.used, bucket.remaining)
await clock.tickAsync(1000) 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)
}) })
}) })
}) })
+6
View File
@@ -6,6 +6,12 @@ describe('Logger', () => {
it('create logger with default options', () => { it('create logger with default options', () => {
const loggy = createLogger() const loggy = createLogger()
loggy.setLevel(LogLevels.Debug) loggy.setLevel(LogLevels.Debug)
loggy.debug('debugging')
loggy.error('error')
loggy.fatal('fatal')
loggy.info('info')
loggy.warn('warn')
loggy.setDepth(LogDepth.Full) loggy.setDepth(LogDepth.Full)
loggy.debug('debugging') loggy.debug('debugging')