2021-08-27 22:20:40 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-12-14 16:58:00 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
cidtest "github.com/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
oidtest "github.com/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2021-08-27 22:20:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-23 13:54:02 +00:00
|
|
|
"go.uber.org/zap"
|
2021-08-27 22:20:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const testingCacheLifetime = 5 * time.Second
|
2021-09-01 16:10:31 +00:00
|
|
|
const testingCacheSize = 10
|
2021-08-27 22:20:40 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
func getTestObjectsListConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
Size: testingCacheSize,
|
|
|
|
Lifetime: testingCacheLifetime,
|
2022-06-23 13:54:02 +00:00
|
|
|
Logger: zap.NewExample(),
|
2021-09-10 06:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 22:20:40 +00:00
|
|
|
func TestObjectsListCache(t *testing.T) {
|
|
|
|
var (
|
2022-06-28 12:56:41 +00:00
|
|
|
listSize = 10
|
2022-10-24 14:44:11 +00:00
|
|
|
versions []*data.NodeVersion
|
2022-06-28 12:56:41 +00:00
|
|
|
cidKey, cidKey2 = cidtest.ID(), cidtest.ID()
|
2021-08-27 22:20:40 +00:00
|
|
|
)
|
|
|
|
|
2021-09-03 12:40:22 +00:00
|
|
|
for i := 0; i < listSize; i++ {
|
2022-10-24 14:44:11 +00:00
|
|
|
versions = append(versions, &data.NodeVersion{BaseNodeVersion: data.BaseNodeVersion{OID: oidtest.ID()}})
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("lifetime", func(t *testing.T) {
|
|
|
|
var (
|
2022-10-24 14:44:11 +00:00
|
|
|
config = getTestObjectsListConfig()
|
|
|
|
cache = NewObjectsListCache(config)
|
|
|
|
listKey = ObjectsListKey{cid: cidKey}
|
2021-08-27 22:20:40 +00:00
|
|
|
)
|
|
|
|
|
2022-10-24 14:44:11 +00:00
|
|
|
err := cache.PutVersions(listKey, versions)
|
2021-09-01 16:10:31 +00:00
|
|
|
require.NoError(t, err)
|
2021-08-27 22:20:40 +00:00
|
|
|
|
|
|
|
condition := func() bool {
|
2022-10-24 14:44:11 +00:00
|
|
|
return cache.GetVersions(listKey) == nil
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
require.Never(t, condition, config.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 (
|
2022-10-24 14:44:11 +00:00
|
|
|
cache = NewObjectsListCache(getTestObjectsListConfig())
|
|
|
|
listKey = ObjectsListKey{cid: cidKey}
|
2021-08-27 22:20:40 +00:00
|
|
|
)
|
2022-10-24 14:44:11 +00:00
|
|
|
err := cache.PutVersions(listKey, versions)
|
2021-09-01 16:10:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-10-24 14:44:11 +00:00
|
|
|
actual := cache.GetVersions(listKey)
|
2021-08-27 22:20:40 +00:00
|
|
|
|
2022-10-24 14:44:11 +00:00
|
|
|
require.Equal(t, len(versions), len(actual))
|
|
|
|
for i := range versions {
|
|
|
|
require.Equal(t, versions[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) {
|
2022-10-24 14:44:11 +00:00
|
|
|
listKey := ObjectsListKey{
|
2022-06-28 12:56:41 +00:00
|
|
|
cid: cidKey,
|
2021-09-01 16:10:31 +00:00
|
|
|
prefix: "dir",
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
cache := NewObjectsListCache(getTestObjectsListConfig())
|
2022-10-24 14:44:11 +00:00
|
|
|
err := cache.PutVersions(listKey, versions)
|
2021-09-01 16:10:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-10-24 14:44:11 +00:00
|
|
|
actual := cache.GetVersions(listKey)
|
2021-08-27 22:20:40 +00:00
|
|
|
|
2022-10-24 14:44:11 +00:00
|
|
|
require.Equal(t, len(versions), len(actual))
|
|
|
|
for i := range versions {
|
|
|
|
require.Equal(t, versions[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 (
|
2022-10-24 14:44:11 +00:00
|
|
|
listKey = ObjectsListKey{
|
2022-06-28 12:56:41 +00:00
|
|
|
cid: cidKey,
|
2021-09-01 16:10:31 +00:00
|
|
|
prefix: "dir",
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newKey = ObjectsListKey{
|
2022-06-28 12:56:41 +00:00
|
|
|
cid: cidKey,
|
2021-09-01 16:10:31 +00:00
|
|
|
prefix: "obj",
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
cache := NewObjectsListCache(getTestObjectsListConfig())
|
2022-10-24 14:44:11 +00:00
|
|
|
err := cache.PutVersions(listKey, versions)
|
2021-09-01 16:10:31 +00:00
|
|
|
require.NoError(t, err)
|
2021-08-27 22:20:40 +00:00
|
|
|
|
2022-10-24 14:44:11 +00:00
|
|
|
actual := cache.GetVersions(newKey)
|
2021-08-27 22:20:40 +00:00
|
|
|
require.Nil(t, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get cache with non-existing key", func(t *testing.T) {
|
|
|
|
var (
|
2022-10-24 14:44:11 +00:00
|
|
|
listKey = ObjectsListKey{
|
2022-06-28 12:56:41 +00:00
|
|
|
cid: cidKey,
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
newKey = ObjectsListKey{
|
2022-06-28 12:56:41 +00:00
|
|
|
cid: cidKey2,
|
2021-08-27 22:20:40 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
cache := NewObjectsListCache(getTestObjectsListConfig())
|
2022-10-24 14:44:11 +00:00
|
|
|
err := cache.PutVersions(listKey, versions)
|
2021-09-01 16:10:31 +00:00
|
|
|
require.NoError(t, err)
|
2021-08-27 22:20:40 +00:00
|
|
|
|
2022-10-24 14:44:11 +00:00
|
|
|
actual := cache.GetVersions(newKey)
|
2021-08-27 22:20:40 +00:00
|
|
|
require.Nil(t, actual)
|
|
|
|
})
|
|
|
|
}
|
2021-09-03 12:40:22 +00:00
|
|
|
|
|
|
|
func TestCleanCacheEntriesChangedWithPutObject(t *testing.T) {
|
|
|
|
var (
|
2022-10-24 14:44:11 +00:00
|
|
|
id = cidtest.ID()
|
|
|
|
versions = []*data.NodeVersion{{BaseNodeVersion: data.BaseNodeVersion{OID: oidtest.ID()}}}
|
|
|
|
keys []ObjectsListKey
|
2021-09-03 12:40:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, p := range []string{"", "dir/", "dir/lol/"} {
|
2022-06-28 12:56:41 +00:00
|
|
|
keys = append(keys, ObjectsListKey{cid: id, prefix: p})
|
2021-09-03 12:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("put object to the root of the bucket", func(t *testing.T) {
|
2021-09-10 06:56:56 +00:00
|
|
|
config := getTestObjectsListConfig()
|
|
|
|
config.Lifetime = time.Minute
|
|
|
|
cache := NewObjectsListCache(config)
|
2021-09-03 12:40:22 +00:00
|
|
|
for _, k := range keys {
|
2022-10-24 14:44:11 +00:00
|
|
|
err := cache.PutVersions(k, versions)
|
2021-09-03 12:40:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2022-05-25 17:25:43 +00:00
|
|
|
cache.CleanCacheEntriesContainingObject("obj1", id)
|
2021-09-03 12:40:22 +00:00
|
|
|
for _, k := range keys {
|
2022-10-24 14:44:11 +00:00
|
|
|
list := cache.GetVersions(k)
|
2021-09-03 12:40:22 +00:00
|
|
|
if k.prefix == "" {
|
|
|
|
require.Nil(t, list)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, list)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("put object to dir/", func(t *testing.T) {
|
2021-09-10 06:56:56 +00:00
|
|
|
config := getTestObjectsListConfig()
|
|
|
|
config.Lifetime = time.Minute
|
|
|
|
cache := NewObjectsListCache(config)
|
2021-09-03 12:40:22 +00:00
|
|
|
for _, k := range keys {
|
2022-10-24 14:44:11 +00:00
|
|
|
err := cache.PutVersions(k, versions)
|
2021-09-03 12:40:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2022-05-25 17:25:43 +00:00
|
|
|
cache.CleanCacheEntriesContainingObject("dir/obj", id)
|
2021-09-03 12:40:22 +00:00
|
|
|
for _, k := range keys {
|
2022-10-24 14:44:11 +00:00
|
|
|
list := cache.GetVersions(k)
|
2021-09-03 12:40:22 +00:00
|
|
|
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) {
|
2021-09-10 06:56:56 +00:00
|
|
|
config := getTestObjectsListConfig()
|
|
|
|
config.Lifetime = time.Minute
|
|
|
|
cache := NewObjectsListCache(config)
|
2021-09-03 12:40:22 +00:00
|
|
|
for _, k := range keys {
|
2022-10-24 14:44:11 +00:00
|
|
|
err := cache.PutVersions(k, versions)
|
2021-09-03 12:40:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2022-05-25 17:25:43 +00:00
|
|
|
cache.CleanCacheEntriesContainingObject("dir/lol/obj", id)
|
2021-09-03 12:40:22 +00:00
|
|
|
for _, k := range keys {
|
2022-10-24 14:44:11 +00:00
|
|
|
list := cache.GetVersions(k)
|
2021-09-03 12:40:22 +00:00
|
|
|
require.Nil(t, list)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|