2021-12-16 05:09:57 -08:00
|
|
|
//go:build rocksdb_build
|
|
|
|
|
2021-10-05 08:39:55 -07:00
|
|
|
package rocksdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-12-16 05:09:57 -08:00
|
|
|
"path/filepath"
|
2021-10-05 08:39:55 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
dbm "github.com/cosmos/cosmos-sdk/db"
|
|
|
|
"github.com/cosmos/cosmos-sdk/db/dbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func load(t *testing.T, dir string) dbm.DBConnection {
|
|
|
|
db, err := NewDB(dir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSetHasDelete(t *testing.T) {
|
|
|
|
dbtest.DoTestGetSetHasDelete(t, load)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIterators(t *testing.T) {
|
|
|
|
dbtest.DoTestIterators(t, load)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransactions(t *testing.T) {
|
|
|
|
dbtest.DoTestTransactions(t, load, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersioning(t *testing.T) {
|
|
|
|
dbtest.DoTestVersioning(t, load)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRevert(t *testing.T) {
|
|
|
|
dbtest.DoTestRevert(t, load, false)
|
|
|
|
dbtest.DoTestRevert(t, load, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReloadDB(t *testing.T) {
|
|
|
|
dbtest.DoTestReloadDB(t, load)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the DB can be reloaded after a failed Revert
|
|
|
|
func TestRevertRecovery(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
db, err := NewDB(dir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
txn := db.Writer()
|
|
|
|
require.NoError(t, txn.Set([]byte{1}, []byte{1}))
|
2021-12-16 05:09:57 -08:00
|
|
|
require.NoError(t, txn.Commit())
|
|
|
|
_, err = db.SaveNextVersion()
|
|
|
|
require.NoError(t, err)
|
|
|
|
txn = db.Writer()
|
2021-10-05 08:39:55 -07:00
|
|
|
require.NoError(t, txn.Set([]byte{2}, []byte{2}))
|
|
|
|
require.NoError(t, txn.Commit())
|
|
|
|
|
2021-12-16 05:09:57 -08:00
|
|
|
// move checkpoints dir temporarily to trigger an error
|
|
|
|
hideDir := filepath.Join(dir, "hide_checkpoints")
|
|
|
|
require.NoError(t, os.Rename(db.checkpointsDir(), hideDir))
|
2021-10-05 08:39:55 -07:00
|
|
|
require.Error(t, db.Revert())
|
2021-12-16 05:09:57 -08:00
|
|
|
require.NoError(t, os.Rename(hideDir, db.checkpointsDir()))
|
2021-10-05 08:39:55 -07:00
|
|
|
|
|
|
|
db, err = NewDB(dir)
|
|
|
|
require.NoError(t, err)
|
2021-12-16 05:09:57 -08:00
|
|
|
view := db.Reader()
|
|
|
|
val, err := view.Get([]byte{1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []byte{1}, val)
|
|
|
|
val, err = view.Get([]byte{2})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, val)
|
|
|
|
view.Discard()
|
2021-10-05 08:39:55 -07:00
|
|
|
}
|