2020-02-13 16:19:46 +00:00
|
|
|
package kv
|
2019-09-26 16:29:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
2020-08-18 12:41:25 +00:00
|
|
|
"io/ioutil"
|
2019-09-26 16:29:10 +00:00
|
|
|
"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-08-18 12:41:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
|
|
"github.com/sirupsen/logrus"
|
2019-09-26 16:29:10 +00:00
|
|
|
)
|
|
|
|
|
2020-08-18 12:41:25 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
logrus.SetOutput(ioutil.Discard)
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2020-10-12 08:11:05 +00:00
|
|
|
func setupDB(t testing.TB) *Store {
|
2019-09-26 16:29:10 +00:00
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, err, "Could not generate random file path")
|
2020-02-13 16:19:46 +00:00
|
|
|
p := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, os.RemoveAll(p), "Failed to remove directory")
|
2020-04-22 15:53:09 +00:00
|
|
|
cfg := &Config{}
|
2020-02-13 16:19:46 +00:00
|
|
|
db, err := NewKVStore(p, cfg)
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, err, "Failed to instantiate DB")
|
2020-05-04 01:14:34 +00:00
|
|
|
t.Cleanup(func() {
|
2020-08-18 12:41:25 +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
|
|
|
})
|
2020-01-22 05:39:21 +00:00
|
|
|
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))
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, err, "Could not generate random file path")
|
2020-02-13 16:19:46 +00:00
|
|
|
p := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, os.RemoveAll(p), "Failed to remove directory")
|
2020-04-22 15:53:09 +00:00
|
|
|
cfg := &Config{SpanCacheSize: cacheSize}
|
2020-05-04 01:14:34 +00:00
|
|
|
db, err := NewKVStore(p, cfg)
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, err, "Failed to instantiate DB")
|
2020-05-04 01:14:34 +00:00
|
|
|
t.Cleanup(func() {
|
2020-08-18 12:41:25 +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
|
|
|
})
|
|
|
|
return db
|
2019-09-26 16:29:10 +00:00
|
|
|
}
|