123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- 'use strict'
- const expect = require('chai').expect
- const lru = require('@joystream/storage-utils/lru')
- const DEFAULT_SLEEP = 1
- function sleep(ms = DEFAULT_SLEEP) {
- return new Promise((resolve) => {
- setTimeout(resolve, ms)
- })
- }
- describe('util/lru', function () {
- describe('simple usage', function () {
- it('does not contain keys that were not added', function () {
- const cache = new lru.LRUCache()
- expect(cache.size()).to.equal(0)
- const val = cache.get('something')
- expect(val).to.be.undefined
- expect(cache.has('something')).to.be.false
- })
- it('contains keys that were added', function () {
- const cache = new lru.LRUCache()
- cache.put('something', 'yay!')
- expect(cache.size()).to.equal(1)
- const val = cache.get('something')
- expect(val).to.be.equal('yay!')
- expect(cache.has('something')).to.be.true
- })
- it('does not contain keys that were deleted', function () {
- const cache = new lru.LRUCache()
- cache.put('something', 'yay!')
- expect(cache.size()).to.equal(1)
- let val = cache.get('something')
- expect(val).to.be.equal('yay!')
- expect(cache.has('something')).to.be.true
- cache.del('something')
- expect(cache.size()).to.equal(0)
- val = cache.get('something')
- expect(val).to.be.undefined
- expect(cache.has('something')).to.be.false
- })
- it('can be cleared', function () {
- const cache = new lru.LRUCache()
- cache.put('something', 'yay!')
- expect(cache.size()).to.equal(1)
- cache.clear()
- expect(cache.size()).to.equal(0)
- })
- })
- describe('capacity management', function () {
- it('does not grow beyond capacity', async function () {
- const cache = new lru.LRUCache(2)
- expect(cache.size()).to.equal(0)
- cache.put('foo', '42')
- expect(cache.size()).to.equal(1)
- await sleep()
- cache.put('bar', '42')
- expect(cache.size()).to.equal(2)
- await sleep()
- cache.put('baz', '42')
- expect(cache.size()).to.equal(2)
- })
- it('removes the oldest key when pruning', async function () {
- const cache = new lru.LRUCache(2)
- expect(cache.size()).to.equal(0)
- cache.put('foo', '42')
- expect(cache.size()).to.equal(1)
- expect(cache.has('foo')).to.be.true
- await sleep()
- cache.put('bar', '42')
- expect(cache.size()).to.equal(2)
- expect(cache.has('foo')).to.be.true
- expect(cache.has('bar')).to.be.true
- await sleep()
- cache.put('baz', '42')
- expect(cache.size()).to.equal(2)
- expect(cache.has('bar')).to.be.true
- expect(cache.has('baz')).to.be.true
- })
- it('updates LRU timestamp when reading', async function () {
- const cache = new lru.LRUCache(2)
- expect(cache.size()).to.equal(0)
- cache.put('foo', '42')
- expect(cache.size()).to.equal(1)
- expect(cache.has('foo')).to.be.true
- await sleep()
- cache.put('bar', '42')
- expect(cache.size()).to.equal(2)
- expect(cache.has('foo')).to.be.true
- expect(cache.has('bar')).to.be.true
- await sleep()
-
-
- cache.get('foo')
-
- await sleep()
- cache.put('baz', '42')
- expect(cache.size()).to.equal(2)
- expect(cache.has('foo')).to.be.true
- expect(cache.has('baz')).to.be.true
- })
- })
- })
|