mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-26 05:17:22 +00:00
eb192049b8
* create a testing db method that can be used with the new database interface * lint * lint * PR feedback * Update setup_db.go
40 lines
930 B
Go
40 lines
930 B
Go
package kv
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"math/big"
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
|
|
"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)
|
|
}
|
|
db, err := NewKVStore(path)
|
|
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)
|
|
}
|
|
if err := os.RemoveAll(db.DatabasePath()); err != nil {
|
|
t.Fatalf("Failed to remove directory: %v", err)
|
|
}
|
|
}
|