From b2bf81cec723a31c2a45f02d2d533b53838ef8c7 Mon Sep 17 00:00:00 2001 From: Angira Kekteeva Date: Fri, 3 Sep 2021 15:40:22 +0300 Subject: [PATCH] [#231] cache: Add unittests for ListObjectsCache Signed-off-by: Angira Kekteeva --- api/cache/objectslist_test.go | 68 ++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/api/cache/objectslist_test.go b/api/cache/objectslist_test.go index 43183cd6..d305ec4c 100644 --- a/api/cache/objectslist_test.go +++ b/api/cache/objectslist_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id" "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/stretchr/testify/require" ) @@ -29,12 +30,12 @@ func randSHA256Checksum(t *testing.T) (cs [sha256.Size]byte) { func TestObjectsListCache(t *testing.T) { var ( - cacheSize = 10 - ids []*object.ID - userKey = "key" + listSize = 10 + ids []*object.ID + userKey = "key" ) - for i := 0; i < cacheSize; i++ { + for i := 0; i < listSize; i++ { id := randID(t) ids = append(ids, id) } @@ -129,3 +130,62 @@ func TestObjectsListCache(t *testing.T) { require.Nil(t, actual) }) } + +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) + } + }) +}