2019-08-18 23:15:13 +01:00
|
|
|
/* global describe, it */
|
|
|
|
|
2021-07-05 04:19:04 +01:00
|
|
|
import { QuickLRU } from '../../src/routes/_thirdparty/quick-lru/quick-lru.js'
|
2019-08-18 23:15:13 +01:00
|
|
|
import assert from 'assert'
|
|
|
|
|
|
|
|
describe('test-quick-lru.js', () => {
|
|
|
|
it('fires evict events correctly', () => {
|
|
|
|
const cache = new QuickLRU({ maxSize: 3 })
|
|
|
|
const evictions = []
|
|
|
|
cache.on('evict', (value, key) => {
|
|
|
|
evictions.push({ key, value })
|
|
|
|
})
|
|
|
|
cache.set('a', 1)
|
|
|
|
cache.set('b', 2)
|
|
|
|
cache.set('c', 3)
|
|
|
|
cache.set('d', 4)
|
|
|
|
cache.set('e', 5)
|
|
|
|
cache.set('f', 6)
|
|
|
|
cache.set('a', 1)
|
|
|
|
cache.set('d', 4)
|
|
|
|
cache.set('g', 7)
|
|
|
|
assert.deepStrictEqual(evictions, [
|
|
|
|
{ key: 'a', value: 1 },
|
|
|
|
{ key: 'b', value: 2 },
|
|
|
|
{ key: 'c', value: 3 },
|
|
|
|
{ key: 'e', value: 5 },
|
|
|
|
{ key: 'f', value: 6 }
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('fires evict events correctly, using get()', () => {
|
|
|
|
const cache = new QuickLRU({ maxSize: 3 })
|
|
|
|
const evictions = []
|
|
|
|
cache.on('evict', (value, key) => {
|
|
|
|
evictions.push({ key, value })
|
|
|
|
})
|
|
|
|
cache.set('a', 1)
|
|
|
|
cache.set('b', 2)
|
|
|
|
cache.set('c', 3)
|
|
|
|
cache.set('d', 4)
|
|
|
|
cache.set('e', 5)
|
|
|
|
cache.set('f', 6)
|
|
|
|
cache.set('a', 1)
|
|
|
|
cache.get('e')
|
|
|
|
cache.set('g', 7)
|
|
|
|
assert.deepStrictEqual(evictions, [
|
|
|
|
{ key: 'a', value: 1 },
|
|
|
|
{ key: 'b', value: 2 },
|
|
|
|
{ key: 'c', value: 3 },
|
|
|
|
{ key: 'd', value: 4 },
|
|
|
|
{ key: 'f', value: 6 }
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|