From 969bfb603f7877eacb2d7c8140ebe001f7785264 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 5 May 2023 13:53:33 +0300 Subject: [PATCH] [#321] shard/test: Parallelize TestShard_List ``` go test -count=1 -run TestShard_List -race . Before: 2.492s After: 0.109s ``` Signed-off-by: Evgenii Stratonikov --- pkg/local_object_storage/shard/list_test.go | 57 ++++++++++++--------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/pkg/local_object_storage/shard/list_test.go b/pkg/local_object_storage/shard/list_test.go index 8fac41a0f..e0759f668 100644 --- a/pkg/local_object_storage/shard/list_test.go +++ b/pkg/local_object_storage/shard/list_test.go @@ -2,6 +2,7 @@ package shard_test import ( "context" + "sync" "testing" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object" @@ -9,22 +10,21 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard" cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test" "github.com/stretchr/testify/require" + "golang.org/x/sync/errgroup" ) func TestShard_List(t *testing.T) { - sh := newShard(t, false) - shWC := newShard(t, true) - - defer func() { - releaseShard(sh, t) - releaseShard(shWC, t) - }() - t.Run("without write cache", func(t *testing.T) { + t.Parallel() + sh := newShard(t, false) + defer releaseShard(sh, t) testShardList(t, sh) }) t.Run("with write cache", func(t *testing.T) { + t.Parallel() + shWC := newShard(t, true) + defer releaseShard(shWC, t) testShardList(t, shWC) }) } @@ -33,30 +33,41 @@ func testShardList(t *testing.T, sh *shard.Shard) { const C = 10 const N = 5 + var mtx sync.Mutex objs := make(map[string]int) - var putPrm shard.PutPrm + var errG errgroup.Group + errG.SetLimit(C * N) for i := 0; i < C; i++ { - cnr := cidtest.ID() + errG.Go(func() error { + cnr := cidtest.ID() - for j := 0; j < N; j++ { - obj := testutil.GenerateObjectWithCID(cnr) - testutil.AddPayload(obj, 1<<2) + for j := 0; j < N; j++ { + errG.Go(func() error { + obj := testutil.GenerateObjectWithCID(cnr) + testutil.AddPayload(obj, 1<<2) - // add parent as virtual object, it must be ignored in List() - parent := testutil.GenerateObjectWithCID(cnr) - idParent, _ := parent.ID() - obj.SetParentID(idParent) - obj.SetParent(parent) + // add parent as virtual object, it must be ignored in List() + parent := testutil.GenerateObjectWithCID(cnr) + idParent, _ := parent.ID() + obj.SetParentID(idParent) + obj.SetParent(parent) - objs[object.AddressOf(obj).EncodeToString()] = 0 + mtx.Lock() + objs[object.AddressOf(obj).EncodeToString()] = 0 + mtx.Unlock() - putPrm.SetObject(obj) + var putPrm shard.PutPrm + putPrm.SetObject(obj) - _, err := sh.Put(context.Background(), putPrm) - require.NoError(t, err) - } + _, err := sh.Put(context.Background(), putPrm) + return err + }) + } + return nil + }) } + require.NoError(t, errG.Wait()) res, err := sh.List() require.NoError(t, err)