From 382b4016c3db56c50cd7283f99e3999c98a55a7f Mon Sep 17 00:00:00 2001 From: irenarindos Date: Thu, 31 Aug 2023 11:30:15 -0400 Subject: [PATCH 1/2] expirable LRU: fix so that Get/Peek cannot return an ok and empty value --- expirable/expirable_lru.go | 4 ++-- expirable/expirable_lru_test.go | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/expirable/expirable_lru.go b/expirable/expirable_lru.go index 005f1a0..89978d6 100644 --- a/expirable/expirable_lru.go +++ b/expirable/expirable_lru.go @@ -153,7 +153,7 @@ func (c *LRU[K, V]) Get(key K) (value V, ok bool) { if ent, ok = c.items[key]; ok { // Expired item check if time.Now().After(ent.ExpiresAt) { - return + return value, false } c.evictList.MoveToFront(ent) return ent.Value, true @@ -179,7 +179,7 @@ func (c *LRU[K, V]) Peek(key K) (value V, ok bool) { if ent, ok = c.items[key]; ok { // Expired item check if time.Now().After(ent.ExpiresAt) { - return + return value, false } return ent.Value, true } diff --git a/expirable/expirable_lru_test.go b/expirable/expirable_lru_test.go index 11fb7e3..fa6a345 100644 --- a/expirable/expirable_lru_test.go +++ b/expirable/expirable_lru_test.go @@ -390,7 +390,17 @@ func TestLoadingExpired(t *testing.T) { t.Fatalf("should be true") } - time.Sleep(time.Millisecond * 100) // wait for entry to expire + for { + result, ok := lc.Get("key1") + if ok && result == "" { + t.Fatalf("ok should return a result") + } + if !ok { + break + } + } + + time.Sleep(time.Millisecond * 2) // wait for expiration reaper if lc.Len() != 0 { t.Fatalf("length differs from expected") } From fcb456157e9b1f3801dded651996ca3d14acbe77 Mon Sep 17 00:00:00 2001 From: irenarindos Date: Thu, 31 Aug 2023 11:33:24 -0400 Subject: [PATCH 2/2] fixup! expirable LRU: fix so that Get/Peek cannot return an ok and empty value --- expirable/expirable_lru_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expirable/expirable_lru_test.go b/expirable/expirable_lru_test.go index fa6a345..fd3b255 100644 --- a/expirable/expirable_lru_test.go +++ b/expirable/expirable_lru_test.go @@ -400,7 +400,7 @@ func TestLoadingExpired(t *testing.T) { } } - time.Sleep(time.Millisecond * 2) // wait for expiration reaper + time.Sleep(time.Millisecond * 100) // wait for expiration reaper if lc.Len() != 0 { t.Fatalf("length differs from expected") }