Skip to content

Commit

Permalink
Add testing for lru cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Jan 30, 2025
1 parent 5e57c25 commit ff8255e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions server/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"testing"
"time"

lru "github.com/hashicorp/golang-lru/v2"
)

func getCacheSize() int {
Expand Down Expand Up @@ -42,3 +44,22 @@ func TestCache(t *testing.T) {
t.Fatalf("expected 0 items in cache, got %d", size)
}
}

func TestLRUCache(t *testing.T) {
cacheLRU, _ = lru.New[string, any](1000)

for i := 0; i < 2000; i++ {
withLRUCache(fmt.Sprintf("item-%d", i), func() ([]byte, error) {
return []byte{byte(i % 256)}, nil
})
}
if l := cacheLRU.Len(); l != 1000 {
t.Fatalf("expected 1000 items in cache, got %d", l)
}

// the `gc` function does not remove items from the LRU cache
gc(time.Now())
if l := cacheLRU.Len(); l != 1000 {
t.Fatalf("expected 1000 items in cache, got %d", l)
}
}

0 comments on commit ff8255e

Please sign in to comment.