Skip to content

Commit

Permalink
test: fix flakky
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Apr 8, 2024
1 parent 0e76cd6 commit fad9fb5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
70 changes: 37 additions & 33 deletions packages/bentocache/tests/cache/one_tier_local.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,44 +527,48 @@ test.group('One tier tests', () => {
assert.isUndefined(r2?.getEarlyExpiration())
})

test('early refresh should re-increment physical/logical ttls', async ({ assert }) => {
const { cache } = new CacheFactory()
.withMemoryL1()
.merge({ earlyExpiration: 0.5, ttl: 100 })
.create()

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

// wait for early refresh threshold
await setTimeout(60)

// call factory. should returns the old value.
// Disable early expiration to test physical ttl
const r2 = await cache.getOrSet('key1', slowFactory(50, { foo: 'baz' }), {
earlyExpiration: undefined,
})
for (let i = 0; i < 300; i++) {
test('early refresh should re-increment physical/logical ttls', async ({ assert }) => {
const { cache } = new CacheFactory()
.withMemoryL1()
.merge({ earlyExpiration: 0.5, ttl: '500ms' })
.create()

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

// wait for early refresh threshold
await setTimeout(350)

// call factory. should returns the old value.
// Disable early expiration to test physical ttl
const r2 = await cache.getOrSet({
key: 'key1',
factory: slowFactory(50, { foo: 'baz' }),
earlyExpiration: undefined,
})

// wait for early refresh to be done
await setTimeout(50)
// wait for early refresh to be done
await setTimeout(60)

// get the value
const r3 = await cache.get('key1')
// get the value
const r3 = await cache.get('key1')

// wait a bit
await setTimeout(50)
const r4 = await cache.get('key1')
// wait a bit
await setTimeout(50)
const r4 = await cache.get('key1')

// wait for physical ttl to expire
await setTimeout(50)
const r5 = await cache.get('key1')
// wait for physical ttl to expire
await setTimeout(600)
const r5 = await cache.get('key1')

assert.deepEqual(r1, { foo: 'bar' })
assert.deepEqual(r2, { foo: 'bar' })
assert.deepEqual(r3, { foo: 'baz' })
assert.deepEqual(r4, { foo: 'baz' })
assert.isUndefined(r5)
})
assert.deepEqual(r1, { foo: 'bar' })
assert.deepEqual(r2, { foo: 'bar' })
assert.deepEqual(r3, { foo: 'baz' })
assert.deepEqual(r4, { foo: 'baz' })
assert.isUndefined(r5)
})
}

test('soft timeout should returns old value if factory take too long', async ({ assert }) => {
const { cache } = new CacheFactory()
Expand Down
10 changes: 6 additions & 4 deletions packages/bentocache/tests/cache/two_tier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,16 +389,18 @@ test.group('Cache', () => {
const r1 = await cache.getOrSet('key1', () => ({ foo: 'bar' }))

// wait for early refresh threshold
await setTimeout(60)
await setTimeout(350)

// call factory. should returns the old value.
// Disable early expiration to test physical ttl
const r2 = await cache.getOrSet('key1', slowFactory(50, { foo: 'baz' }), {
const r2 = await cache.getOrSet({
key: 'key1',
factory: slowFactory(50, { foo: 'baz' }),
earlyExpiration: undefined,
})

// wait for early refresh to be done
await setTimeout(50)
await setTimeout(60)

// get the value
const r3 = await cache.get('key1')
Expand All @@ -408,7 +410,7 @@ test.group('Cache', () => {
const r4 = await cache.get('key1')

// wait for physical ttl to expire
await setTimeout(50)
await setTimeout(600)
const r5 = await cache.get('key1')

assert.deepEqual(r1, { foo: 'bar' })
Expand Down

0 comments on commit fad9fb5

Please sign in to comment.