prysm-pulse/slasher/db/kv/kv_test.go

59 lines
1.6 KiB
Go
Raw Normal View History

package kv
2019-09-26 16:29:10 +00:00
import (
"crypto/rand"
"fmt"
"math/big"
"os"
"path"
"testing"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/slasher/db/iface"
"github.com/prysmaticlabs/prysm/slasher/flags"
"github.com/urfave/cli"
2019-09-26 16:29:10 +00:00
)
func setupDB(t testing.TB, ctx *cli.Context) *Store {
2019-09-26 16:29:10 +00:00
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
if err != nil {
t.Fatalf("Could not generate random file path: %v", err)
}
p := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
2019-09-26 16:29:10 +00:00
if err := os.RemoveAll(p); err != nil {
t.Fatalf("Failed to remove directory: %v", err)
}
cfg := &Config{SpanCacheEnabled: ctx.GlobalBool(flags.UseSpanCacheFlag.Name)}
db, err := NewKVStore(p, cfg)
if err != nil {
t.Fatalf("Failed to instantiate DB: %v", err)
}
return db
}
2019-09-26 16:29:10 +00:00
2020-03-09 05:35:39 +00:00
func setupDBDiffCacheSize(t testing.TB, cacheSize int) *Store {
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
if err != nil {
t.Fatalf("Could not generate random file path: %v", err)
}
p := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
if err := os.RemoveAll(p); err != nil {
t.Fatalf("Failed to remove directory: %v", err)
}
2020-03-09 05:35:39 +00:00
cfg := &Config{SpanCacheEnabled: true, SpanCacheSize: cacheSize}
newDB, err := NewKVStore(p, cfg)
2019-09-26 16:29:10 +00:00
if err != nil {
t.Fatalf("Failed to instantiate DB: %v", err)
}
return newDB
2019-09-26 16:29:10 +00:00
}
func teardownDB(t testing.TB, db iface.Database) {
2019-09-26 16:29:10 +00:00
if err := db.Close(); err != nil {
t.Fatalf("Failed to close database: %v", err)
}
if err := os.RemoveAll(db.DatabasePath()); err != nil {
t.Fatalf("Failed to remove directory: %v", err)
}
}