[#668] shard/test: Simplify shard construction

newCustomShard() has many parameters but only the first is obligatory.
`enableWriteCache` is left as-is, because it directly affects the
functionality.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-09-06 17:32:14 +03:00 committed by Evgenii Stratonikov
parent 429f941cda
commit 268adb79cb
4 changed files with 55 additions and 44 deletions

View file

@ -29,7 +29,7 @@ func Test_GCDropsLockedExpiredSimpleObject(t *testing.T) {
wcOpts := writecacheconfig.Options{ wcOpts := writecacheconfig.Options{
Type: writecacheconfig.TypeBBolt, Type: writecacheconfig.TypeBBolt,
} }
sh := newCustomShard(t, t.TempDir(), false, wcOpts, nil, []meta.Option{meta.WithEpochState(epoch)}) sh := newCustomShard(t, false, shardOptions{rootPath: t.TempDir(), wcOpts: wcOpts, metaOptions: []meta.Option{meta.WithEpochState(epoch)}})
t.Cleanup(func() { t.Cleanup(func() {
releaseShard(sh, t) releaseShard(sh, t)
@ -129,7 +129,7 @@ func Test_GCDropsLockedExpiredComplexObject(t *testing.T) {
wcOpts := writecacheconfig.Options{ wcOpts := writecacheconfig.Options{
Type: writecacheconfig.TypeBBolt, Type: writecacheconfig.TypeBBolt,
} }
sh := newCustomShard(t, t.TempDir(), false, wcOpts, nil, []meta.Option{meta.WithEpochState(epoch)}) sh := newCustomShard(t, false, shardOptions{rootPath: t.TempDir(), wcOpts: wcOpts, metaOptions: []meta.Option{meta.WithEpochState(epoch)}})
t.Cleanup(func() { t.Cleanup(func() {
releaseShard(sh, t) releaseShard(sh, t)

View file

@ -76,8 +76,11 @@ func testShardGetRange(t *testing.T, hasWriteCache bool) {
}, },
} }
sh := newCustomShard(t, t.TempDir(), hasWriteCache, wcOpts, sh := newCustomShard(t, hasWriteCache, shardOptions{
[]blobstor.Option{blobstor.WithStorages([]blobstor.SubStorage{ rootPath: t.TempDir(),
wcOpts: wcOpts,
bsOpts: []blobstor.Option{
blobstor.WithStorages([]blobstor.SubStorage{
{ {
Storage: blobovniczatree.NewBlobovniczaTree( Storage: blobovniczatree.NewBlobovniczaTree(
blobovniczatree.WithLogger(test.NewLogger(t, true)), blobovniczatree.WithLogger(test.NewLogger(t, true)),
@ -92,8 +95,9 @@ func testShardGetRange(t *testing.T, hasWriteCache bool) {
Storage: fstree.New( Storage: fstree.New(
fstree.WithPath(filepath.Join(t.TempDir(), "blob"))), fstree.WithPath(filepath.Join(t.TempDir(), "blob"))),
}, },
})}, }),
nil) },
})
defer releaseShard(sh, t) defer releaseShard(sh, t)
for _, tc := range testCases { for _, tc := range testCases {

View file

@ -30,41 +30,48 @@ func (s epochState) CurrentEpoch() uint64 {
return s.Value return s.Value
} }
type shardOptions struct {
rootPath string
wcOpts writecacheconfig.Options
bsOpts []blobstor.Option
metaOptions []meta.Option
}
func newShard(t testing.TB, enableWriteCache bool) *Shard { func newShard(t testing.TB, enableWriteCache bool) *Shard {
sh := newCustomShard(t, t.TempDir(), enableWriteCache, sh := newCustomShard(t, enableWriteCache, shardOptions{
writecacheconfig.Options{Type: writecacheconfig.TypeBBolt}, rootPath: t.TempDir(),
nil, wcOpts: writecacheconfig.Options{Type: writecacheconfig.TypeBBolt},
nil) })
t.Cleanup(func() { releaseShard(sh, t) }) t.Cleanup(func() { releaseShard(sh, t) })
return sh return sh
} }
func newCustomShard(t testing.TB, rootPath string, enableWriteCache bool, wcOpts writecacheconfig.Options, bsOpts []blobstor.Option, metaOptions []meta.Option) *Shard { func newCustomShard(t testing.TB, enableWriteCache bool, o shardOptions) *Shard {
var sh *Shard var sh *Shard
if enableWriteCache { if enableWriteCache {
rootPath = filepath.Join(rootPath, "wc") o.rootPath = filepath.Join(o.rootPath, "wc")
switch wcOpts.Type { switch o.wcOpts.Type {
case writecacheconfig.TypeBBolt: case writecacheconfig.TypeBBolt:
wcOpts.BBoltOptions = append( o.wcOpts.BBoltOptions = append(
[]writecachebbolt.Option{writecachebbolt.WithPath(filepath.Join(rootPath, "wcache"))}, []writecachebbolt.Option{writecachebbolt.WithPath(filepath.Join(o.rootPath, "wcache"))},
wcOpts.BBoltOptions...) o.wcOpts.BBoltOptions...)
case writecacheconfig.TypeBadger: case writecacheconfig.TypeBadger:
wcOpts.BadgerOptions = append( o.wcOpts.BadgerOptions = append(
[]writecachebadger.Option{writecachebadger.WithPath(filepath.Join(rootPath, "wcache"))}, []writecachebadger.Option{writecachebadger.WithPath(filepath.Join(o.rootPath, "wcache"))},
wcOpts.BadgerOptions...) o.wcOpts.BadgerOptions...)
} }
} else { } else {
rootPath = filepath.Join(rootPath, "nowc") o.rootPath = filepath.Join(o.rootPath, "nowc")
} }
if bsOpts == nil { if o.bsOpts == nil {
bsOpts = []blobstor.Option{ o.bsOpts = []blobstor.Option{
blobstor.WithLogger(test.NewLogger(t, true)), blobstor.WithLogger(test.NewLogger(t, true)),
blobstor.WithStorages([]blobstor.SubStorage{ blobstor.WithStorages([]blobstor.SubStorage{
{ {
Storage: blobovniczatree.NewBlobovniczaTree( Storage: blobovniczatree.NewBlobovniczaTree(
blobovniczatree.WithLogger(test.NewLogger(t, true)), blobovniczatree.WithLogger(test.NewLogger(t, true)),
blobovniczatree.WithRootPath(filepath.Join(rootPath, "blob", "blobovnicza")), blobovniczatree.WithRootPath(filepath.Join(o.rootPath, "blob", "blobovnicza")),
blobovniczatree.WithBlobovniczaShallowDepth(1), blobovniczatree.WithBlobovniczaShallowDepth(1),
blobovniczatree.WithBlobovniczaShallowWidth(1)), blobovniczatree.WithBlobovniczaShallowWidth(1)),
Policy: func(_ *objectSDK.Object, data []byte) bool { Policy: func(_ *objectSDK.Object, data []byte) bool {
@ -73,7 +80,7 @@ func newCustomShard(t testing.TB, rootPath string, enableWriteCache bool, wcOpts
}, },
{ {
Storage: fstree.New( Storage: fstree.New(
fstree.WithPath(filepath.Join(rootPath, "blob"))), fstree.WithPath(filepath.Join(o.rootPath, "blob"))),
}, },
}), }),
} }
@ -82,15 +89,15 @@ func newCustomShard(t testing.TB, rootPath string, enableWriteCache bool, wcOpts
opts := []Option{ opts := []Option{
WithID(NewIDFromBytes([]byte{})), WithID(NewIDFromBytes([]byte{})),
WithLogger(test.NewLogger(t, true)), WithLogger(test.NewLogger(t, true)),
WithBlobStorOptions(bsOpts...), WithBlobStorOptions(o.bsOpts...),
WithMetaBaseOptions( WithMetaBaseOptions(
append([]meta.Option{ append([]meta.Option{
meta.WithPath(filepath.Join(rootPath, "meta")), meta.WithEpochState(epochState{})}, meta.WithPath(filepath.Join(o.rootPath, "meta")), meta.WithEpochState(epochState{})},
metaOptions...)..., o.metaOptions...)...,
), ),
WithPiloramaOptions(pilorama.WithPath(filepath.Join(rootPath, "pilorama"))), WithPiloramaOptions(pilorama.WithPath(filepath.Join(o.rootPath, "pilorama"))),
WithWriteCache(enableWriteCache), WithWriteCache(enableWriteCache),
WithWriteCacheOptions(wcOpts), WithWriteCacheOptions(o.wcOpts),
WithDeletedLockCallback(func(_ context.Context, addresses []oid.Address) { WithDeletedLockCallback(func(_ context.Context, addresses []oid.Address) {
sh.HandleDeletedLocks(addresses) sh.HandleDeletedLocks(addresses)
}), }),

View file

@ -43,7 +43,7 @@ func TestWriteCacheObjectLoss(t *testing.T) {
}, },
} }
sh := newCustomShard(t, dir, true, wcOpts, nil, nil) sh := newCustomShard(t, true, shardOptions{rootPath: dir, wcOpts: wcOpts})
var errG errgroup.Group var errG errgroup.Group
for i := range objects { for i := range objects {
@ -58,7 +58,7 @@ func TestWriteCacheObjectLoss(t *testing.T) {
require.NoError(t, errG.Wait()) require.NoError(t, errG.Wait())
require.NoError(t, sh.Close()) require.NoError(t, sh.Close())
sh = newCustomShard(t, dir, true, wcOpts, nil, nil) sh = newCustomShard(t, true, shardOptions{rootPath: dir, wcOpts: wcOpts})
defer releaseShard(sh, t) defer releaseShard(sh, t)
var getPrm GetPrm var getPrm GetPrm