From 90de3ab4e61db4db6c1616f0fd77e31fddad5edf Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Tue, 22 Aug 2023 10:16:32 +0200 Subject: [PATCH] add evict behaviour tests --- expirable/expirable_lru_test.go | 37 +++++++++++++++++++++++++++++ lru_test.go | 37 +++++++++++++++++++++++++++++ simplelru/lru_test.go | 41 ++++++++++++++++++++++++++++++++- 3 files changed, 114 insertions(+), 1 deletion(-) diff --git a/expirable/expirable_lru_test.go b/expirable/expirable_lru_test.go index cc328e3..a7dd6bb 100644 --- a/expirable/expirable_lru_test.go +++ b/expirable/expirable_lru_test.go @@ -478,6 +478,43 @@ func TestLRURemoveOldest(t *testing.T) { } } +// https://github.com/hashicorp/golang-lru/issues/141 +// test highlighting behaviour of eviction of the key with changed value +func TestCache_EvictionSameKey(t *testing.T) { + var evictedKeys []int + + cache := NewLRU[int, struct{}]( + 2, + func(key int, _ struct{}) { + evictedKeys = append(evictedKeys, key) + }, + 0) + + cache.Add(1, struct{}{}) + if !reflect.DeepEqual(cache.Keys(), []int{1}) { + t.Fatalf("keys differs from expected: %v", cache.Keys()) + } + + cache.Add(2, struct{}{}) + if !reflect.DeepEqual(cache.Keys(), []int{1, 2}) { + t.Fatalf("keys differs from expected: %v", cache.Keys()) + } + + cache.Add(1, struct{}{}) + if !reflect.DeepEqual(cache.Keys(), []int{2, 1}) { + t.Fatalf("keys differs from expected: %v", cache.Keys()) + } + + cache.Add(3, struct{}{}) + if !reflect.DeepEqual(cache.Keys(), []int{1, 3}) { + t.Fatalf("keys differs from expected: %v", cache.Keys()) + } + + if !reflect.DeepEqual(evictedKeys, []int{2}) { + t.Fatalf("evictedKeys differs from expected: %v", evictedKeys) + } +} + func ExampleLRU() { // make cache with 10ms TTL and 5 max keys cache := NewLRU[string, string](5, nil, time.Millisecond*10) diff --git a/lru_test.go b/lru_test.go index 9da0b23..2ca8d88 100644 --- a/lru_test.go +++ b/lru_test.go @@ -4,6 +4,7 @@ package lru import ( + "reflect" "testing" ) @@ -293,3 +294,39 @@ func TestLRUResize(t *testing.T) { t.Errorf("Cache should have contained 2 elements") } } + +// https://github.com/hashicorp/golang-lru/issues/141 +// test highlighting behaviour of eviction of the key with changed value +func TestCache_EvictionSameKey(t *testing.T) { + var evictedKeys []int + + cache, _ := NewWithEvict( + 2, + func(key int, _ struct{}) { + evictedKeys = append(evictedKeys, key) + }) + + cache.Add(1, struct{}{}) + if !reflect.DeepEqual(cache.Keys(), []int{1}) { + t.Fatalf("keys differs from expected: %v", cache.Keys()) + } + + cache.Add(2, struct{}{}) + if !reflect.DeepEqual(cache.Keys(), []int{1, 2}) { + t.Fatalf("keys differs from expected: %v", cache.Keys()) + } + + cache.Add(1, struct{}{}) + if !reflect.DeepEqual(cache.Keys(), []int{2, 1}) { + t.Fatalf("keys differs from expected: %v", cache.Keys()) + } + + cache.Add(3, struct{}{}) + if !reflect.DeepEqual(cache.Keys(), []int{1, 3}) { + t.Fatalf("keys differs from expected: %v", cache.Keys()) + } + + if !reflect.DeepEqual(evictedKeys, []int{1}) { + t.Fatalf("evictedKeys differs from expected: %v", evictedKeys) + } +} diff --git a/simplelru/lru_test.go b/simplelru/lru_test.go index a6b247f..f22050c 100644 --- a/simplelru/lru_test.go +++ b/simplelru/lru_test.go @@ -3,7 +3,10 @@ package simplelru -import "testing" +import ( + "reflect" + "testing" +) func TestLRU(t *testing.T) { evictCounter := 0 @@ -207,3 +210,39 @@ func TestLRU_Resize(t *testing.T) { t.Errorf("Cache should have contained 2 elements") } } + +// https://github.com/hashicorp/golang-lru/issues/141 +// test highlighting behaviour of eviction of the key with changed value +func TestCache_EvictionSameKey(t *testing.T) { + var evictedKeys []int + + cache, _ := NewLRU( + 2, + func(key int, _ struct{}) { + evictedKeys = append(evictedKeys, key) + }) + + cache.Add(1, struct{}{}) + if !reflect.DeepEqual(cache.Keys(), []int{1}) { + t.Fatalf("keys differs from expected: %v", cache.Keys()) + } + + cache.Add(2, struct{}{}) + if !reflect.DeepEqual(cache.Keys(), []int{1, 2}) { + t.Fatalf("keys differs from expected: %v", cache.Keys()) + } + + cache.Add(1, struct{}{}) + if !reflect.DeepEqual(cache.Keys(), []int{2, 1}) { + t.Fatalf("keys differs from expected: %v", cache.Keys()) + } + + cache.Add(3, struct{}{}) + if !reflect.DeepEqual(cache.Keys(), []int{1, 3}) { + t.Fatalf("keys differs from expected: %v", cache.Keys()) + } + + if !reflect.DeepEqual(evictedKeys, []int{1, 2}) { + t.Fatalf("evictedKeys differs from expected: %v", evictedKeys) + } +}