From bde1d35f419dad8ba5ad9284195bed73840d6931 Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Tue, 2 Jul 2024 20:33:37 +0200 Subject: [PATCH] Bugfix delete + add test (#4) --- index.js | 3 ++- test.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 529274d..f93ccac 100644 --- a/index.js +++ b/index.js @@ -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 } diff --git a/test.js b/test.js index 413f6c3..d83230b 100644 --- a/test.js +++ b/test.js @@ -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 })