2020-04-29 21:32:39 +00:00
|
|
|
// Package testing defines useful helper functions for unit tests with
|
|
|
|
// the slasher database.
|
2020-02-13 16:19:46 +00:00
|
|
|
package testing
|
2019-09-26 16:29:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
2020-01-22 05:39:21 +00:00
|
|
|
|
2020-10-02 16:32:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/rand"
|
2020-02-13 16:19:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
slasherDB "github.com/prysmaticlabs/prysm/slasher/db"
|
|
|
|
"github.com/prysmaticlabs/prysm/slasher/db/kv"
|
2019-09-26 16:29:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetupSlasherDB instantiates and returns a SlasherDB instance.
|
2020-02-27 17:22:39 +00:00
|
|
|
func SetupSlasherDB(t testing.TB, spanCacheEnabled bool) *kv.Store {
|
2020-10-02 16:32:21 +00:00
|
|
|
randPath := rand.NewDeterministicGenerator().Int()
|
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-04-22 15:53:09 +00:00
|
|
|
cfg := &kv.Config{}
|
2020-02-13 16:19:46 +00:00
|
|
|
db, err := slasherDB.NewDB(p, cfg)
|
2020-01-22 05:39:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to instantiate DB: %v", err)
|
|
|
|
}
|
2020-05-29 12:09:04 +00:00
|
|
|
db.EnableSpanCache(spanCacheEnabled)
|
2020-05-04 01:14:34 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2020-01-22 05:39:21 +00:00
|
|
|
return db
|
|
|
|
}
|