Skip to content

Commit

Permalink
refactor: replace setTimeout by sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Feb 1, 2025
1 parent be987ca commit 497336c
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 104 deletions.
50 changes: 25 additions & 25 deletions packages/bentocache/tests/bus/bus.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from '@japa/runner'
import { setTimeout } from 'node:timers/promises'
import { sleep } from '@julr/utils/misc'
import { MemoryTransport } from '@boringnode/bus/transports/memory'

import { ChaosBus } from '../helpers/chaos/chaos_bus.js'
Expand All @@ -18,15 +18,15 @@ test.group('Bus synchronization', () => {
const [cache3] = new CacheFactory().withL1L2Config().create()

await cache1.set({ key, value: 24 })
await setTimeout(100)
await sleep(100)

assert.equal(await cache1.get({ key }), 24)
assert.equal(await cache2.get({ key }), 24)
assert.equal(await cache3.get({ key }), 24)

await cache1.delete({ key })

await setTimeout(100)
await sleep(100)

assert.isUndefined(await cache1.get({ key }))
assert.isUndefined(await cache2.get({ key }))
Expand All @@ -41,15 +41,15 @@ test.group('Bus synchronization', () => {
const [cache3] = new CacheFactory().withL1L2Config().create()

await cache1.namespace('users').set({ key, value: 24 })
await setTimeout(100)
await sleep(100)

assert.equal(await cache1.namespace('users').get({ key }), 24)
assert.equal(await cache2.namespace('users').get({ key }), 24)
assert.equal(await cache3.namespace('users').get({ key }), 24)

await cache1.namespace('users').delete({ key })

await setTimeout(100)
await sleep(100)

assert.isUndefined(await cache1.namespace('users').get({ key }))
assert.isUndefined(await cache2.namespace('users').get({ key }))
Expand All @@ -70,27 +70,27 @@ test.group('Bus synchronization', () => {
await cache1NSUsersMe.set({ key, value: 24 })
await cache3NSAdmin.set({ key, value: 42 })
await cache1.set({ key, value: 33 })
await setTimeout(100)
await sleep(100)

assert.equal(await cache1NSUsersMe.get({ key }), 24)
assert.equal(await cache2NSUsersMe.get({ key }), 24)
assert.equal(await cache3NSAdmin.get({ key }), 42)

await cache1NSUsersMe.clear()

await setTimeout(100)
await sleep(100)

assert.isUndefined(await cache1NSUsersMe.get({ key }))
assert.isUndefined(await cache2.namespace('users').namespace('me').get({ key }))
assert.equal(await cache3NSAdmin.get({ key }), 42)

await cache2.namespace('admin').clear()
await setTimeout(100)
await sleep(100)

assert.isUndefined(await cache3NSAdmin.get({ key }))
assert.equal(await cache2.get({ key }), 33)
await cache2.delete({ key })
await setTimeout(100)
await sleep(100)

assert.isUndefined(await cache1.get({ key }))
}).disableTimeout()
Expand All @@ -105,17 +105,17 @@ test.group('Bus synchronization', () => {
const cache2NSAdmins = cache2.namespace('admins')
await cache1NSUsers.set({ key, value: 24 })
await cache2NSAdmins.set({ key, value: 42 })
await setTimeout(100)
await sleep(100)

await cache1NSUsers.clear()

await setTimeout(100)
await sleep(100)

assert.isUndefined(await cache1NSUsers.get({ key }))
assert.equal(await cache2NSAdmins.get({ key }), 42)

await cache2NSAdmins.clear()
await setTimeout(100)
await sleep(100)

assert.isUndefined(await cache1.namespace('admins').get({ key }))
}).disableTimeout()
Expand Down Expand Up @@ -143,16 +143,16 @@ test.group('Bus synchronization', () => {
bus2.alwaysThrow()
bus3.alwaysThrow()

await setTimeout(100)
await sleep(100)

await cache1.set({ key: 'foo', value: 1 })
await setTimeout(200)
await sleep(200)

await cache2.set({ key: 'foo', value: 2 })
await setTimeout(200)
await sleep(200)

await cache3.set({ key: 'foo', value: 3 })
await setTimeout(200)
await sleep(200)

assert.deepEqual(await cache1.get({ key: 'foo' }), 1)
assert.deepEqual(await cache2.get({ key: 'foo' }), 2)
Expand All @@ -163,7 +163,7 @@ test.group('Bus synchronization', () => {
bus2.neverThrow()
bus3.neverThrow()

await setTimeout(200)
await sleep(200)

assert.deepEqual(await cache1.get({ key: 'foo' }), 3)
assert.deepEqual(await cache2.get({ key: 'foo' }), 3)
Expand All @@ -190,14 +190,14 @@ test.group('Bus synchronization', () => {
await cache.set({ key: 'foo', value: 1 })
await cache2.set({ key: 'foo', value: 2 })

await setTimeout(200)
await sleep(200)

bus1.neverThrow()
bus2.neverThrow()

await cache.set({ key: 'foo2', value: 1 })

await setTimeout(200)
await sleep(200)

assert.deepEqual(await cache.get({ key: 'foo' }), 1)
assert.deepEqual(await cache2.get({ key: 'foo' }), 2)
Expand Down Expand Up @@ -226,7 +226,7 @@ test.group('Bus synchronization', () => {
bus1.neverThrow()
assert.deepEqual(bus2.receivedMessages.length, 0)

await setTimeout(1000)
await sleep(1000)

assert.deepEqual(bus2.receivedMessages.length, 20)
}).disableTimeout()
Expand Down Expand Up @@ -254,7 +254,7 @@ test.group('Bus synchronization', () => {
await cache1.set({ key: 'foo', value: 'bar' })
await cache2.set({ key: 'foo', value: 'baz' })

await setTimeout(110)
await sleep(110)

remoteDriver.neverThrow()
const result = await cache1.getOrSet({ key: 'foo', factory: throwingFactory('fail') })
Expand Down Expand Up @@ -294,7 +294,7 @@ test.group('Bus synchronization', () => {
done()
})

await setTimeout(200)
await sleep(200)

await bus2.publish('foo', data)
})
Expand Down Expand Up @@ -326,7 +326,7 @@ test.group('Bus synchronization', () => {
done()
})

await setTimeout(200)
await sleep(200)

await bus2.publish('foo', data)
})
Expand All @@ -352,14 +352,14 @@ test.group('Bus synchronization', () => {
done()
})

await setTimeout(200)
await sleep(200)

await bus2.publish('foo', data)

await bus1.disconnect()

await bus2.disconnect()

await setTimeout(200)
await sleep(200)
}).waitForDone()
})
40 changes: 20 additions & 20 deletions packages/bentocache/tests/cache/one_tier_local.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dayjs from 'dayjs'
import { test } from '@japa/runner'
import { setTimeout } from 'node:timers/promises'
import { sleep } from '@julr/utils/misc'

import { CacheFactory } from '../../factories/cache_factory.js'
import { throwingFactory, slowFactory } from '../helpers/index.js'
Expand Down Expand Up @@ -52,7 +52,7 @@ test.group('One tier tests', () => {
const { cache } = new CacheFactory().withMemoryL1().merge({ grace: '4h' }).create()

await cache.set({ key: 'key', value: 'value', ttl: '100ms' })
await setTimeout(100)
await sleep(100)

const r1 = await cache.get({ key: 'key' })
const r2 = await cache.get({ key: 'key', defaultValue: undefined, grace: false })
Expand Down Expand Up @@ -86,7 +86,7 @@ test.group('One tier tests', () => {
const r1 = await cache.get({ key: 'key' })

// wait til key expires
await setTimeout(100)
await sleep(100)

// we should not get value since grace period is disabled globally
const r2 = await cache.get({ key: 'key' })
Expand Down Expand Up @@ -121,10 +121,10 @@ test.group('One tier tests', () => {

await cache.set({ key: 'key1', value: 'value1', ttl: '100ms' })

await setTimeout(100)
await sleep(100)
const r1 = await cache.missing({ key: 'key1' })

await setTimeout(500)
await sleep(500)
const r2 = await cache.missing({ key: 'key1' })

assert.isFalse(r1)
Expand All @@ -147,11 +147,11 @@ test.group('One tier tests', () => {
const { cache } = new CacheFactory().withMemoryL1().merge({ grace: '500ms' }).create()

await cache.set({ key: 'key1', value: 'value1', ttl: '100ms' })
await setTimeout(100)
await sleep(100)

const r1 = await cache.has({ key: 'key1' })

await setTimeout(500)
await sleep(500)
const r2 = await cache.has({ key: 'key1' })

assert.isTrue(r1)
Expand All @@ -178,7 +178,7 @@ test.group('One tier tests', () => {
await cache.set({ key: 'key1', value: 'value1', ttl: '100ms' })
await cache.set({ key: 'key2', value: 'bar' })

await setTimeout(100)
await sleep(100)

await cache.delete({ key: 'key1' })
await cache.delete({ key: 'key2' })
Expand All @@ -193,7 +193,7 @@ test.group('One tier tests', () => {
await cache.set({ key: 'key1', value: 'value1', ttl: '100ms' })
await cache.set({ key: 'key2', value: 'bar' })

await setTimeout(100)
await sleep(100)

await cache.deleteMany({ keys: ['key1', 'key2'] })

Expand All @@ -219,7 +219,7 @@ test.group('One tier tests', () => {
await cache.getOrSetForever({ key: 'key', factory: () => 'value' })
assert.deepEqual(await cache.get({ key: 'key' }), 'value')

await setTimeout(100)
await sleep(100)

assert.deepEqual(await cache.get({ key: 'key' }), 'value')
})
Expand Down Expand Up @@ -248,7 +248,7 @@ test.group('One tier tests', () => {
const { cache } = new CacheFactory().withMemoryL1().merge({ ttl: 10 }).create()

await cache.setForever({ key: 'key', value: 'value' })
await setTimeout(30)
await sleep(30)
assert.deepEqual(await cache.get({ key: 'key' }), 'value')
})

Expand Down Expand Up @@ -290,7 +290,7 @@ test.group('One tier tests', () => {
const { cache } = new CacheFactory().withMemoryL1().create()

await cache.getOrSet({ key: 'key1', ttl: '10ms', factory: () => ({ foo: 'bar' }) })
await setTimeout(20)
await sleep(20)

assert.isUndefined(await cache.get({ key: 'key1' }))
})
Expand All @@ -302,7 +302,7 @@ test.group('One tier tests', () => {

const result = await cache.getOrSet({ key: 'key1', factory: () => ({ foo: 'bar' }) })

await setTimeout(100)
await sleep(100)
const result2 = await cache.getOrSet({
key: 'key1',
factory: () => {
Expand All @@ -326,7 +326,7 @@ test.group('One tier tests', () => {

const r1 = await cache.getOrSet({ key: 'key1', factory: () => ({ foo: 'bar' }) })

await setTimeout(100)
await sleep(100)

const r2 = await cache.getOrSet({ key: 'key1', factory: () => ({ foo: 'baz' }) })

Expand All @@ -344,12 +344,12 @@ test.group('One tier tests', () => {
const r1 = await cache.getOrSet({ key: 'key1', factory: () => ({ foo: 'bar' }) })

// wait til key is expired
await setTimeout(50)
await sleep(50)

// should returns graced value
const r2 = await cache.getOrSet({ key: 'key1', factory: throwingFactory() })

await setTimeout(300)
await sleep(300)

// Graced value is now expired. Factory should be called
const r3 = cache.getOrSet({ key: 'key1', factory: throwingFactory('Error in cb') })
Expand All @@ -370,7 +370,7 @@ test.group('One tier tests', () => {
const r1 = await cache.getOrSet({ key: 'key1', factory: () => ({ foo: 'bar' }) })

// wait til key is expired
await setTimeout(50)
await sleep(50)

// should returns graced value
const r2 = await cache.getOrSet({ key: 'key1', factory: throwingFactory('Error in cb') })
Expand All @@ -385,7 +385,7 @@ test.group('One tier tests', () => {
},
})

await setTimeout(800)
await sleep(800)

// wait til graceBackoff is expired. Factory should be called
const r4 = await cache.getOrSet({ key: 'key1', factory: async () => ({ foo: 'baz' }) })
Expand All @@ -407,13 +407,13 @@ test.group('One tier tests', () => {
const r1 = await cache.getOrSet({ key: 'key1', factory: () => ({ foo: 'bar' }) })

// wait for expiration
await setTimeout(100)
await sleep(100)

// factory that will exceed soft timeout
const r2 = await cache.getOrSet({ key: 'key1', factory: slowFactory(550, { foo: 'baz' }) })

// wait til factory is done
await setTimeout(50)
await sleep(50)

// get the value
const r3 = await cache.getOrSet({ key: 'key1', factory: () => ({ foo: 'bazzz' }) })
Expand Down
Loading

0 comments on commit 497336c

Please sign in to comment.