Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add evict behaviour tests #152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions expirable/expirable_lru_test.go
Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions lru_test.go
Expand Up @@ -4,6 +4,7 @@
package lru

import (
"reflect"
"testing"
)

Expand Down Expand Up @@ -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)
}
}
41 changes: 40 additions & 1 deletion simplelru/lru_test.go
Expand Up @@ -3,7 +3,10 @@

package simplelru

import "testing"
import (
"reflect"
"testing"
)

func TestLRU(t *testing.T) {
evictCounter := 0
Expand Down Expand Up @@ -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)
}
}