2021-08-27 22:20:40 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-09-03 12:40:22 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2021-08-27 22:20:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const testingCacheLifetime = 5 * time.Second
|
2021-09-01 16:10:31 +00:00
|
|
|
const testingCacheSize = 10
|
2021-08-27 22:20:40 +00:00
|
|
|
|
|
|
|
func randID(t *testing.T) *object.ID {
|
|
|
|
id := object.NewID()
|
|
|
|
id.SetSHA256(randSHA256Checksum(t))
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func randSHA256Checksum(t *testing.T) (cs [sha256.Size]byte) {
|
|
|
|
_, err := rand.Read(cs[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestObjectsListCache(t *testing.T) {
|
|
|
|
var (
|
2021-09-03 12:40:22 +00:00
|
|
|
listSize = 10
|
|
|
|
ids []*object.ID
|
|
|
|
userKey = "key"
|
2021-08-27 22:20:40 +00:00
|
|
|
)
|
|
|
|
|
2021-09-03 12:40:22 +00:00
|
|
|
for i := 0; i < listSize; i++ {
|
2021-08-27 22:20:40 +00:00
|
|
|
id := randID(t)
|
2021-09-01 16:10:31 +00:00
|
|
|
ids = append(ids, id)
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("lifetime", func(t *testing.T) {
|
|
|
|
var (
|
2021-09-01 16:10:31 +00:00
|
|
|
cache = NewObjectsListCache(testingCacheSize, testingCacheLifetime)
|
|
|
|
cacheKey = ObjectsListKey{cid: userKey}
|
2021-08-27 22:20:40 +00:00
|
|
|
)
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
err := cache.Put(cacheKey, ids)
|
|
|
|
require.NoError(t, err)
|
2021-08-27 22:20:40 +00:00
|
|
|
|
|
|
|
condition := func() bool {
|
|
|
|
return cache.Get(cacheKey) == nil
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
require.Never(t, condition, cache.lifetime, time.Second)
|
2021-08-27 22:20:40 +00:00
|
|
|
require.Eventually(t, condition, time.Second, 10*time.Millisecond)
|
|
|
|
})
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
t.Run("get cache with empty prefix", func(t *testing.T) {
|
2021-08-27 22:20:40 +00:00
|
|
|
var (
|
2021-09-01 16:10:31 +00:00
|
|
|
cache = NewObjectsListCache(testingCacheSize, testingCacheLifetime)
|
|
|
|
cacheKey = ObjectsListKey{cid: userKey}
|
2021-08-27 22:20:40 +00:00
|
|
|
)
|
2021-09-01 16:10:31 +00:00
|
|
|
err := cache.Put(cacheKey, ids)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-27 22:20:40 +00:00
|
|
|
actual := cache.Get(cacheKey)
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
require.Equal(t, len(ids), len(actual))
|
|
|
|
for i := range ids {
|
|
|
|
require.Equal(t, ids[i], actual[i])
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
t.Run("get cache with prefix", func(t *testing.T) {
|
2021-08-27 22:20:40 +00:00
|
|
|
cacheKey := ObjectsListKey{
|
2021-09-01 16:10:31 +00:00
|
|
|
cid: userKey,
|
|
|
|
prefix: "dir",
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
cache := NewObjectsListCache(testingCacheSize, testingCacheLifetime)
|
|
|
|
err := cache.Put(cacheKey, ids)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-27 22:20:40 +00:00
|
|
|
actual := cache.Get(cacheKey)
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
require.Equal(t, len(ids), len(actual))
|
|
|
|
for i := range ids {
|
|
|
|
require.Equal(t, ids[i], actual[i])
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
t.Run("get cache with other prefix", func(t *testing.T) {
|
2021-08-27 22:20:40 +00:00
|
|
|
var (
|
|
|
|
cacheKey = ObjectsListKey{
|
2021-09-01 16:10:31 +00:00
|
|
|
cid: userKey,
|
|
|
|
prefix: "dir",
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newKey = ObjectsListKey{
|
2021-09-01 16:10:31 +00:00
|
|
|
cid: "key",
|
|
|
|
prefix: "obj",
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
cache := NewObjectsListCache(testingCacheSize, testingCacheLifetime)
|
|
|
|
err := cache.Put(cacheKey, ids)
|
|
|
|
require.NoError(t, err)
|
2021-08-27 22:20:40 +00:00
|
|
|
|
|
|
|
actual := cache.Get(newKey)
|
|
|
|
require.Nil(t, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get cache with non-existing key", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
cacheKey = ObjectsListKey{
|
2021-09-01 16:10:31 +00:00
|
|
|
cid: userKey,
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
newKey = ObjectsListKey{
|
2021-09-01 16:10:31 +00:00
|
|
|
cid: "asdf",
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
cache := NewObjectsListCache(testingCacheSize, testingCacheLifetime)
|
|
|
|
err := cache.Put(cacheKey, ids)
|
|
|
|
require.NoError(t, err)
|
2021-08-27 22:20:40 +00:00
|
|
|
|
|
|
|
actual := cache.Get(newKey)
|
|
|
|
require.Nil(t, actual)
|
|
|
|
})
|
|
|
|
}
|
2021-09-03 12:40:22 +00:00
|
|
|
|
|
|
|
func TestCleanCacheEntriesChangedWithPutObject(t *testing.T) {
|
|
|
|
var (
|
|
|
|
cid = cid.New()
|
|
|
|
oids = []*object.ID{randID(t)}
|
|
|
|
keys []ObjectsListKey
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, p := range []string{"", "dir/", "dir/lol/"} {
|
|
|
|
keys = append(keys, ObjectsListKey{cid: cid.String(), prefix: p})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("put object to the root of the bucket", func(t *testing.T) {
|
|
|
|
cache := NewObjectsListCache(testingCacheSize, time.Minute)
|
|
|
|
for _, k := range keys {
|
|
|
|
err := cache.Put(k, oids)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
cache.CleanCacheEntriesContainingObject("obj1", cid)
|
|
|
|
for _, k := range keys {
|
|
|
|
list := cache.Get(k)
|
|
|
|
if k.prefix == "" {
|
|
|
|
require.Nil(t, list)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, list)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("put object to dir/", func(t *testing.T) {
|
|
|
|
cache := NewObjectsListCache(testingCacheSize, time.Minute)
|
|
|
|
for _, k := range keys {
|
|
|
|
err := cache.Put(k, oids)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
cache.CleanCacheEntriesContainingObject("dir/obj", cid)
|
|
|
|
for _, k := range keys {
|
|
|
|
list := cache.Get(k)
|
|
|
|
if k.prefix == "" || k.prefix == "dir/" {
|
|
|
|
require.Nil(t, list)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, list)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("put object to dir/lol/", func(t *testing.T) {
|
|
|
|
cache := NewObjectsListCache(testingCacheSize, time.Minute)
|
|
|
|
for _, k := range keys {
|
|
|
|
err := cache.Put(k, oids)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
cache.CleanCacheEntriesContainingObject("dir/lol/obj", cid)
|
|
|
|
for _, k := range keys {
|
|
|
|
list := cache.Get(k)
|
|
|
|
require.Nil(t, list)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|