Skip to content

Commit

Permalink
test: add a unit test to demonstrate a un-intuitive behavior (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Jan 18, 2023
1 parent 98e2b0d commit e9ee47e
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions mutable_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1533,3 +1533,39 @@ func TestNoFastStorageUpgrade_Integration_SaveVersion_Load_Iterate_Success(t *te
})
})
}

// TestMutableTree_InitialVersion_FirstVersion demonstrate the un-intuitive behavior,
// when InitialVersion is set the nodes created in the first version are not assigned with expected version number.
func TestMutableTree_InitialVersion_FirstVersion(t *testing.T) {
db := db.NewMemDB()

initialVersion := int64(1000)
tree, err := NewMutableTreeWithOpts(db, 0, &Options{
InitialVersion: uint64(initialVersion),
}, true)
require.NoError(t, err)

_, err = tree.Set([]byte("hello"), []byte("world"))
require.NoError(t, err)

rootHash, version, err := tree.SaveVersion()
require.NoError(t, err)
require.Equal(t, initialVersion, version)

// the nodes created at the first version are not assigned with the `InitialVersion`
node, err := tree.ndb.GetNode(rootHash)
require.NoError(t, err)
require.Equal(t, int64(1), node.version)

_, err = tree.Set([]byte("hello"), []byte("world1"))
require.NoError(t, err)

rootHash, version, err = tree.SaveVersion()
require.NoError(t, err)
require.Equal(t, initialVersion+1, version)

// the following versions behaves normally
node, err = tree.ndb.GetNode(rootHash)
require.NoError(t, err)
require.Equal(t, initialVersion+1, node.version)
}

0 comments on commit e9ee47e

Please sign in to comment.