2021-05-24 16:46:44 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2021-06-03 10:39:33 +00:00
|
|
|
"github.com/cloudskiff/driftctl/test/resource"
|
2021-05-24 16:46:44 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkCache(b *testing.B) {
|
2021-06-03 10:39:33 +00:00
|
|
|
cache := New(500)
|
2021-05-24 16:46:44 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
key := fmt.Sprintf("test-key-%d", i)
|
2021-06-03 10:39:33 +00:00
|
|
|
data := make([]*resource.FakeResource, 1024)
|
2021-05-25 10:36:48 +00:00
|
|
|
assert.Equal(b, false, cache.Put(key, data))
|
2021-05-24 16:46:44 +00:00
|
|
|
assert.Equal(b, data, cache.Get(key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCache(t *testing.T) {
|
|
|
|
t.Run("should return nil on non-existing key", func(t *testing.T) {
|
2021-05-25 10:36:48 +00:00
|
|
|
cache := New(5)
|
2021-05-24 16:46:44 +00:00
|
|
|
assert.Equal(t, nil, cache.Get("test"))
|
2021-05-25 10:36:48 +00:00
|
|
|
assert.Equal(t, 0, cache.Len())
|
2021-05-24 16:46:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should retrieve newly added key", func(t *testing.T) {
|
2021-05-25 10:36:48 +00:00
|
|
|
cache := New(5)
|
|
|
|
assert.Equal(t, false, cache.Put("s3", []string{}))
|
2021-05-24 16:46:44 +00:00
|
|
|
assert.Equal(t, []string{}, cache.Get("s3"))
|
2021-05-25 10:36:48 +00:00
|
|
|
assert.Equal(t, 1, cache.Len())
|
2021-05-24 16:46:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should override existing key", func(t *testing.T) {
|
2021-05-25 10:36:48 +00:00
|
|
|
cache := New(5)
|
|
|
|
assert.Equal(t, false, cache.Put("s3", []string{}))
|
2021-05-24 16:46:44 +00:00
|
|
|
assert.Equal(t, []string{}, cache.Get("s3"))
|
|
|
|
|
2021-05-25 10:36:48 +00:00
|
|
|
assert.Equal(t, true, cache.Put("s3", []string{"test"}))
|
2021-05-24 16:46:44 +00:00
|
|
|
assert.Equal(t, []string{"test"}, cache.Get("s3"))
|
2021-05-25 10:36:48 +00:00
|
|
|
assert.Equal(t, 1, cache.Len())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should delete the least used keys", func(t *testing.T) {
|
2021-05-28 09:26:02 +00:00
|
|
|
keys := []struct {
|
|
|
|
key string
|
|
|
|
value interface{}
|
|
|
|
}{
|
|
|
|
{key: "test-0", value: nil},
|
|
|
|
{key: "test-1", value: nil},
|
|
|
|
{key: "test-2", value: nil},
|
|
|
|
{key: "test-3", value: nil},
|
|
|
|
{key: "test-4", value: nil},
|
|
|
|
{key: "test-5", value: nil},
|
|
|
|
{key: "test-6", value: "value"},
|
|
|
|
{key: "test-7", value: "value"},
|
|
|
|
{key: "test-8", value: "value"},
|
|
|
|
{key: "test-9", value: "value"},
|
|
|
|
{key: "test-10", value: "value"},
|
|
|
|
}
|
2021-05-25 10:36:48 +00:00
|
|
|
|
2021-05-28 09:26:02 +00:00
|
|
|
cache := New(5)
|
2021-05-25 10:36:48 +00:00
|
|
|
for i := 0; i <= 10; i++ {
|
|
|
|
cache.Put(fmt.Sprintf("test-%d", i), "value")
|
|
|
|
}
|
2021-05-28 09:26:02 +00:00
|
|
|
for _, k := range keys {
|
|
|
|
assert.Equal(t, k.value, cache.Get(k.key))
|
|
|
|
}
|
2021-05-25 10:36:48 +00:00
|
|
|
assert.Equal(t, 5, cache.Len())
|
2021-05-24 16:46:44 +00:00
|
|
|
})
|
2021-06-04 13:26:13 +00:00
|
|
|
|
|
|
|
t.Run("should ignore keys when capacity is 0", func(t *testing.T) {
|
|
|
|
keys := []struct {
|
|
|
|
key string
|
|
|
|
value interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"test",
|
|
|
|
[]string{"slice"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test",
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test2",
|
|
|
|
[]resource.FakeResource{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cache := New(0)
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
assert.Equal(t, false, cache.Put(k.key, k.value))
|
|
|
|
assert.Equal(t, nil, cache.Get(k.key))
|
|
|
|
}
|
|
|
|
assert.Equal(t, 0, cache.Len())
|
|
|
|
})
|
2021-05-24 16:46:44 +00:00
|
|
|
}
|