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"
|
2020-08-08 18:39:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2019-08-12 19:33:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// setupDB instantiates and returns a Store instance.
|
|
|
|
func setupDB(t testing.TB) *Store {
|
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, err, "Could not generate random file path")
|
2020-05-04 01:14:34 +00:00
|
|
|
p := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, os.RemoveAll(p), "Failed to remove directory")
|
2020-05-04 01:14:34 +00:00
|
|
|
db, err := NewKVStore(p, cache.NewStateSummaryCache())
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, err, "Failed to instantiate DB")
|
2020-05-04 01:14:34 +00:00
|
|
|
t.Cleanup(func() {
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, db.Close(), "Failed to close database")
|
|
|
|
require.NoError(t, os.RemoveAll(db.DatabasePath()), "Failed to remove directory")
|
2020-05-04 01:14:34 +00:00
|
|
|
})
|
2019-08-12 19:33:07 +00:00
|
|
|
return db
|
|
|
|
}
|