Skip to content

Commit

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

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,
})
test('early refresh should re-increment physical/logical ttls', async ({ assert }) => {
const { cache } = new CacheFactory()
.withMemoryL1()
.merge({ earlyExpiration: 0.5, ttl: '500ms' })
.create()

// wait for early refresh to be done
await setTimeout(60)
// init cache
const r1 = await cache.getOrSet('key1', () => ({ foo: 'bar' }))

// get the value
const r3 = await cache.get('key1')
// wait for early refresh threshold
await setTimeout(350)

// wait a bit
await setTimeout(50)
const r4 = await cache.get('key1')
// 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 physical ttl to expire
await setTimeout(600)
const r5 = await cache.get('key1')
// wait for early refresh to be done
await setTimeout(60)

assert.deepEqual(r1, { foo: 'bar' })
assert.deepEqual(r2, { foo: 'bar' })
assert.deepEqual(r3, { foo: 'baz' })
assert.deepEqual(r4, { foo: 'baz' })
assert.isUndefined(r5)
})
}
// get the value
const r3 = await cache.get('key1')

// wait a bit
await setTimeout(50)
const r4 = 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)
})

test('soft timeout should returns old value if factory take too long', async ({ assert }) => {
const { cache } = new CacheFactory()
Expand Down

0 comments on commit 323bd96

Please sign in to comment.