forked from TrueCloudLab/frostfs-node
[#116] node: Improve shard/engine construction in tests
* Introduce testEngineWrapper that can be constructed with different options Signed-off-by: Airat Arifullin a.arifullin@yadro.com
This commit is contained in:
parent
d6486d172e
commit
6f7b6a8813
10 changed files with 173 additions and 144 deletions
|
@ -24,6 +24,7 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/atomic"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zaptest"
|
||||
)
|
||||
|
||||
type epochState struct{}
|
||||
|
@ -50,7 +51,7 @@ func benchmarkExists(b *testing.B, shardNum int) {
|
|||
shards[i] = testNewShard(b, i)
|
||||
}
|
||||
|
||||
e := testNewEngineWithShards(b, shards...)
|
||||
e := testNewEngine(b).setInitializedShards(b, shards...).engine
|
||||
b.Cleanup(func() {
|
||||
_ = e.Close()
|
||||
_ = os.RemoveAll(b.Name())
|
||||
|
@ -75,24 +76,68 @@ func benchmarkExists(b *testing.B, shardNum int) {
|
|||
}
|
||||
}
|
||||
|
||||
func testNewEngineWithShards(t testing.TB, shards ...*shard.Shard) *StorageEngine {
|
||||
engine := New()
|
||||
type testEngineWrapper struct {
|
||||
engine *StorageEngine
|
||||
shardIDs []*shard.ID
|
||||
}
|
||||
|
||||
func testNewEngine(t testing.TB, opts ...Option) *testEngineWrapper {
|
||||
engine := New(WithLogger(&logger.Logger{Logger: zaptest.NewLogger(t)}))
|
||||
for _, opt := range opts {
|
||||
opt(engine.cfg)
|
||||
}
|
||||
return &testEngineWrapper{
|
||||
engine: engine,
|
||||
}
|
||||
}
|
||||
|
||||
func (te *testEngineWrapper) setInitializedShards(t testing.TB, shards ...*shard.Shard) *testEngineWrapper {
|
||||
for _, s := range shards {
|
||||
pool, err := ants.NewPool(10, ants.WithNonblocking(true))
|
||||
require.NoError(t, err)
|
||||
|
||||
engine.shards[s.ID().String()] = hashedShard{
|
||||
te.engine.shards[s.ID().String()] = hashedShard{
|
||||
shardWrapper: shardWrapper{
|
||||
errorCount: atomic.NewUint32(0),
|
||||
Shard: s,
|
||||
},
|
||||
hash: hrw.Hash([]byte(s.ID().String())),
|
||||
}
|
||||
engine.shardPools[s.ID().String()] = pool
|
||||
te.engine.shardPools[s.ID().String()] = pool
|
||||
te.shardIDs = append(te.shardIDs, s.ID())
|
||||
}
|
||||
return te
|
||||
}
|
||||
|
||||
func (te *testEngineWrapper) setShardsNum(t testing.TB, num int) *testEngineWrapper {
|
||||
shards := make([]*shard.Shard, 0, num)
|
||||
|
||||
for i := 0; i < num; i++ {
|
||||
shards = append(shards, testNewShard(t, i))
|
||||
}
|
||||
|
||||
return engine
|
||||
return te.setInitializedShards(t, shards...)
|
||||
}
|
||||
|
||||
func (te *testEngineWrapper) setShardsNumOpts(t testing.TB, num int, shardOpts func(id int) []shard.Option) *testEngineWrapper {
|
||||
for i := 0; i < num; i++ {
|
||||
opts := shardOpts(i)
|
||||
id, err := te.engine.AddShard(opts...)
|
||||
require.NoError(t, err)
|
||||
te.shardIDs = append(te.shardIDs, id)
|
||||
}
|
||||
return te
|
||||
}
|
||||
|
||||
func (te *testEngineWrapper) setShardsNumAdditionalOpts(t testing.TB, num int, shardOpts func(id int) []shard.Option) *testEngineWrapper {
|
||||
for i := 0; i < num; i++ {
|
||||
defaultOpts := testDefaultShardOptions(t, i)
|
||||
opts := append(defaultOpts, shardOpts(i)...)
|
||||
id, err := te.engine.AddShard(opts...)
|
||||
require.NoError(t, err)
|
||||
te.shardIDs = append(te.shardIDs, id)
|
||||
}
|
||||
return te
|
||||
}
|
||||
|
||||
func newStorages(root string, smallSize uint64) []blobstor.SubStorage {
|
||||
|
@ -145,8 +190,17 @@ func testNewShard(t testing.TB, id int) *shard.Shard {
|
|||
sid, err := generateShardID()
|
||||
require.NoError(t, err)
|
||||
|
||||
s := shard.New(
|
||||
shard.WithID(sid),
|
||||
shardOpts := append([]shard.Option{shard.WithID(sid)}, testDefaultShardOptions(t, id)...)
|
||||
s := shard.New(shardOpts...)
|
||||
|
||||
require.NoError(t, s.Open())
|
||||
require.NoError(t, s.Init(context.Background()))
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func testDefaultShardOptions(t testing.TB, id int) []shard.Option {
|
||||
return []shard.Option{
|
||||
shard.WithLogger(&logger.Logger{Logger: zap.L()}),
|
||||
shard.WithBlobStorOptions(
|
||||
blobstor.WithStorages(
|
||||
|
@ -157,46 +211,5 @@ func testNewShard(t testing.TB, id int) *shard.Shard {
|
|||
meta.WithPath(filepath.Join(t.Name(), fmt.Sprintf("%d.metabase", id))),
|
||||
meta.WithPermissions(0700),
|
||||
meta.WithEpochState(epochState{}),
|
||||
))
|
||||
|
||||
require.NoError(t, s.Open())
|
||||
require.NoError(t, s.Init(context.Background()))
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func testEngineFromShardOpts(t *testing.T, num int, extraOpts []shard.Option) *StorageEngine {
|
||||
engine := New()
|
||||
for i := 0; i < num; i++ {
|
||||
_, err := engine.AddShard(append([]shard.Option{
|
||||
shard.WithBlobStorOptions(
|
||||
blobstor.WithStorages(
|
||||
newStorages(filepath.Join(t.Name(), fmt.Sprintf("blobstor%d", i)),
|
||||
1<<20)),
|
||||
),
|
||||
shard.WithMetaBaseOptions(
|
||||
meta.WithPath(filepath.Join(t.Name(), fmt.Sprintf("metabase%d", i))),
|
||||
meta.WithPermissions(0700),
|
||||
meta.WithEpochState(epochState{}),
|
||||
),
|
||||
shard.WithPiloramaOptions(
|
||||
pilorama.WithPath(filepath.Join(t.Name(), fmt.Sprintf("pilorama%d", i)))),
|
||||
}, extraOpts...)...)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
require.NoError(t, engine.Open())
|
||||
require.NoError(t, engine.Init(context.Background()))
|
||||
|
||||
return engine
|
||||
}
|
||||
|
||||
func testNewEngineWithShardNum(t *testing.T, num int) *StorageEngine {
|
||||
shards := make([]*shard.Shard, 0, num)
|
||||
|
||||
for i := 0; i < num; i++ {
|
||||
shards = append(shards, testNewShard(t, i))
|
||||
}
|
||||
|
||||
return testNewEngineWithShards(t, shards...)
|
||||
)}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue