cache/cache_test.go

157 lines
3.1 KiB
Go
Raw Normal View History

2025-01-18 00:55:00 +01:00
package cache_test
2021-11-19 13:21:46 +01:00
import (
"testing"
"time"
2025-01-18 00:55:00 +01:00
"github.com/stretchr/testify/assert"
"code.bcarlin.net/go/cache"
2021-11-19 13:21:46 +01:00
)
func TestCache(t *testing.T) {
2025-01-18 00:55:00 +01:00
t.Parallel()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
const key = "test"
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
t.Run("simple put and get", func(t *testing.T) {
t.Parallel()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c := cache.New[string, string]()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
value := "foo"
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c.Put(key, value)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
n := c.Count()
assert.Equal(t, 1, n, "Expected %d entries in the cache, got %d", 1, n)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
got, ok := c.Get(key)
assert.True(t, ok, "Expected cache to have an entry for key %q, but it did not", key)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
assert.Equal(t, value, got)
})
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
t.Run("Using the same key overwrites the value", func(t *testing.T) {
t.Parallel()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c := cache.New[string, string]()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c.Put(key, "foo")
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
value := "bar"
c.Put(key, value)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
n := c.Count()
assert.Equal(t, 1, n, "Expected %d entries in the cache, got %d", 1, n)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
got, ok := c.Get(key)
assert.True(t, ok, "Expected cache to have an entry for key %q, but it did not", key)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
assert.Equal(t, value, got)
})
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
t.Run("cache is missed", func(t *testing.T) {
t.Parallel()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c := cache.New[string, string]()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
val, ok := c.Get("does-not-exist")
assert.False(t, ok, "Expected cache to not have an entry for key 'does-not-exist', but it did")
assert.Zero(t, val)
})
2021-11-19 13:21:46 +01:00
}
func TestCacheTTL(t *testing.T) {
2025-01-18 00:55:00 +01:00
t.Parallel()
const (
key = "test"
value = "foo"
)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
t.Run("simple put and get a non-expired entry", func(t *testing.T) {
t.Parallel()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c := cache.New[string, string]()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c.PutTTL(key, value, 1*time.Hour)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
n := c.Count()
assert.Equal(t, 1, n, "Expected %d entries in the cache, got %d", 1, n)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
_, ok := c.Get(key)
assert.True(t, ok, "Expected cache to have an entry for key %q, but it did not", key)
})
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
t.Run("simple put and get a 0 TTL", func(t *testing.T) {
t.Parallel()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c := cache.New[string, string]()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c.PutTTL(key, value, 0)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
n := c.Count()
assert.Equal(t, 1, n, "Expected %d entries in the cache, got %d", 1, n)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
_, ok := c.Get(key)
assert.True(t, ok, "Expected cache to have an entry for key %q, but it did not", key)
})
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
t.Run("simple put and get an expired entry", func(t *testing.T) {
t.Parallel()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c := cache.New[string, string]()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c.PutTTL(key, value, -1*time.Hour)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
n := c.Count()
assert.Equal(t, 1, n, "Expected %d entries in the cache, got %d", 1, n)
_, ok := c.Get(key)
assert.False(t, ok, "Expected cache to not have an entry for key %q, but it did", key)
n = c.Count()
assert.Equal(t, 0, n, "Expected %d entries in the cache, got %d", 0, n)
})
2021-11-19 13:21:46 +01:00
}
func TestCacheDel(t *testing.T) {
2025-01-18 00:55:00 +01:00
t.Parallel()
const (
key = "test"
value = "foo"
)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
t.Run("delete an existing key", func(t *testing.T) {
t.Parallel()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c := cache.New[string, string]()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c.Put(key, value)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
n := c.Count()
assert.Equal(t, 1, n, "Expected %d entries in the cache, got %d", 1, n)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c.Del(key)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
n = c.Count()
assert.Equal(t, 0, n, "Expected %d entries in the cache, got %d", 0, n)
})
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
t.Run("delete a non-existing key", func(t *testing.T) {
t.Parallel()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c := cache.New[string, string]()
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c.Put(key, value)
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
c.Del("does-not-exist")
2021-11-19 13:21:46 +01:00
2025-01-18 00:55:00 +01:00
n := c.Count()
assert.Equal(t, 1, n, "Expected %d entries in the cache, got %d", 1, n)
})
2021-11-19 13:21:46 +01:00
}