Skip to content

Commit

Permalink
add unit test that reproduce the issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Sep 1, 2023
1 parent f40ba07 commit 0a756f0
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions memiavl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,23 +280,36 @@ func TestLoadVersion(t *testing.T) {
}

func TestZeroCopy(t *testing.T) {
db, err := Load(t.TempDir(), Options{InitialStores: []string{"test"}, CreateIfMissing: true, ZeroCopy: true})
db, err := Load(t.TempDir(), Options{InitialStores: []string{"test", "test2"}, CreateIfMissing: true, ZeroCopy: true})
require.NoError(t, err)
db.Commit([]*NamedChangeSet{
_, _, err = db.Commit([]*NamedChangeSet{
{Name: "test", Changeset: ChangeSets[0]},
{Name: "test2"},
})
require.NoError(t, err)
require.NoError(t, errors.Join(
db.RewriteSnapshot(),
db.Reload(),
))

// the test tree's root hash will reference the zero-copy value
_, _, err = db.Commit([]*NamedChangeSet{
{Name: "test"},
{Name: "test2", Changeset: ChangeSets[0]},
})
require.NoError(t, err)

commitInfo := *db.LastCommitInfo()

value := db.TreeByName("test").Get([]byte("hello"))
require.Equal(t, []byte("world"), value)

db.SetZeroCopy(false)
valueCloned := db.TreeByName("test").Get([]byte("hello"))
require.Equal(t, []byte("world"), valueCloned)

_ = commitInfo.CommitID()

require.NoError(t, db.Close())

require.Equal(t, []byte("world"), valueCloned)
Expand All @@ -307,6 +320,9 @@ func TestZeroCopy(t *testing.T) {
require.Panics(t, func() {
require.Equal(t, []byte("world"), value)
})

// it's ok to access after db closed
_ = commitInfo.CommitID()
}

func TestWalIndexConversion(t *testing.T) {
Expand Down

0 comments on commit 0a756f0

Please sign in to comment.