2019-08-12 19:33:07 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
2019-08-12 19:33:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// setupDB instantiates and returns a Store instance.
|
|
|
|
func setupDB(t testing.TB) *Store {
|
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not generate random file path: %v", err)
|
|
|
|
}
|
|
|
|
path := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("Failed to remove directory: %v", err)
|
|
|
|
}
|
2020-03-30 22:10:45 +00:00
|
|
|
db, err := NewKVStore(path, cache.NewStateSummaryCache())
|
2019-08-12 19:33:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to instantiate DB: %v", err)
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
// teardownDB cleans up a test Store instance.
|
|
|
|
func teardownDB(t testing.TB, db *Store) {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Fatalf("Failed to close database: %v", err)
|
|
|
|
}
|
2019-08-15 21:41:51 +00:00
|
|
|
if err := os.RemoveAll(db.DatabasePath()); err != nil {
|
2019-08-12 19:33:07 +00:00
|
|
|
t.Fatalf("Failed to remove directory: %v", err)
|
|
|
|
}
|
|
|
|
}
|