Skip to content

Commit

Permalink
Bugfix delete + add test (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote authored Jul 2, 2024
1 parent f543a90 commit bde1d35
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class GlobalCache {
delete (key) {
const existing = this._map.get(key)
if (existing === undefined) return false
this._delete(existing.index)

this._delete(existing.entry.index)
return true
}

Expand Down
37 changes: 37 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,43 @@ test('subs share same cache, but no key conflicts', t => {
t.is(nrMisses, 1, '1 entry got cleared from the global cache')
})

test('delete', t => {
const cache = new GlobalCache()
const sub = cache.sub()

cache.set('key', 'value')
cache.set('what', 'ever')
sub.set('key', 'value')
sub.set('what2', 'ever 2')

t.is(cache.globalSize, 4, 'sanity check')
t.is(cache.size, 2, 'sanity check')

{
const deleted = cache.delete('key')
t.is(deleted, true, 'true when deleted')
}

t.is(cache.globalSize, 3, 'removed globally')
t.is(cache.size, 1, 'removed locally')
t.is(cache.get('key'), undefined, 'no entry')

{
const deleted = sub.delete('key')
t.is(deleted, true, 'true when deleted')
}

t.is(sub.globalSize, 2, 'removed globally')
t.is(sub.size, 1, 'removed locally')
t.is(sub.get('key'), undefined, 'no entry')

t.is(
sub.delete('nothing here'),
false,
'false when nothing to delete'
)
})

test('internal structure remains consistent', t => {
const cache = new GlobalCache({ maxSize: 3 })

Expand Down

0 comments on commit bde1d35

Please sign in to comment.