Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jun 12, 2020
1 parent 64776ec commit 69829b1
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions mutable_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package iavl
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"os"
"runtime"
"testing"

Expand Down Expand Up @@ -263,3 +266,65 @@ func BenchmarkMutableTree_Set(b *testing.B) {
t.Set(randBytes(10), []byte{})
}
}

func TestDeleteVersion_issue261(t *testing.T) {
makeVersions := func(t *testing.T, mainDB db.DB, versions int, keepEvery int64) {
// We set the same RNG for every run, which of course means we'll be regenerating the
// same keys and values.
r := rand.New(rand.NewSource(49872768940))

// This follows SDK behavior.
keepRecent := int64(1)
if keepEvery == 1 {
keepRecent = 0
}

tree, err := NewMutableTreeWithOpts(mainDB, db.NewMemDB(), 0, &Options{
KeepEvery: keepEvery,
KeepRecent: keepRecent,
})
require.NoError(t, err)

version, err := tree.LoadVersion(0)
require.NoError(t, err)
t.Logf("Loaded version %v\n", version)

// Create new versions
for v := 0; v < versions; v++ {
for i := 0; i < 4096; i++ {
key := []byte(fmt.Sprintf("%v", r.Intn(65536)))
value := []byte(fmt.Sprintf("%v", r.Intn(1<<20)))
tree.Set(key, value)
}

_, version, err = tree.SaveVersion()
require.NoError(t, err)
t.Logf("Saved version %v\n", version)

// Delete the previous keepEvery version if it's a multiple of KeepEvery. This follows
// SDK pruning behavior.
if version%keepEvery == 0 && version > keepEvery {
err = tree.DeleteVersion(version - keepEvery)
require.NoError(t, err)
t.Logf("Deleted version %v\n", version-keepEvery)
}
}
}

// Use the same on-disk database for all runs.
tempdir, err := ioutil.TempDir("", "iavl")
require.NoError(t, err)
defer os.RemoveAll(tempdir)

levelDB, err := db.NewGoLevelDB("pruning", "")
require.NoError(t, err)

// First, create 12 versions and persist every 5 versions to disk, deleting the previous
// persisted version.
makeVersions(t, levelDB, 12, 5)

// Now, create another 12 versions and persist every 5 versions to disk, deleting the previous
// persisted one. This will load version 10 (the last persisted one) and generate new versions
// from that. In the original issue, this panics after version 10 has been deleted.
makeVersions(t, levelDB, 12, 5)
}

0 comments on commit 69829b1

Please sign in to comment.