2020-02-13 16:19:46 +00:00
|
|
|
package kv
|
2019-09-26 16:29:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
2020-01-22 05:39:21 +00:00
|
|
|
|
2020-02-13 16:19:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-03-05 05:35:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/slasher/db/iface"
|
2020-01-22 05:39:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/slasher/flags"
|
|
|
|
"github.com/urfave/cli"
|
2019-09-26 16:29:10 +00:00
|
|
|
)
|
|
|
|
|
2020-02-13 16:19:46 +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)
|
|
|
|
}
|
2020-02-13 16:19:46 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-03-08 21:11:59 +00:00
|
|
|
cfg := &Config{SpanCacheEnabled: ctx.GlobalBool(flags.UseSpanCacheFlag.Name)}
|
2020-02-13 16:19:46 +00:00
|
|
|
db, err := NewKVStore(p, cfg)
|
2020-01-22 05:39:21 +00:00
|
|
|
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 {
|
2020-01-22 05:39:21 +00:00
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not generate random file path: %v", err)
|
|
|
|
}
|
2020-02-13 16:19:46 +00:00
|
|
|
p := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
|
2020-01-22 05:39:21 +00:00
|
|
|
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}
|
2020-02-13 16:19:46 +00:00
|
|
|
newDB, err := NewKVStore(p, cfg)
|
2019-09-26 16:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to instantiate DB: %v", err)
|
|
|
|
}
|
2020-02-13 16:19:46 +00:00
|
|
|
return newDB
|
2019-09-26 16:29:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 05:35:14 +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)
|
|
|
|
}
|
|
|
|
}
|