Skip to content

Commit

Permalink
muxdb: add test cases for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
qianbin committed Feb 28, 2024
1 parent 9f72ae1 commit 631fe55
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions muxdb/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,51 @@ import (
"crypto/rand"
"testing"

"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/trie"
)

type mockedRootNode struct {
trie.Node
ver trie.Version
}

func (m *mockedRootNode) Version() trie.Version { return m.ver }

func TestCacheRootNode(t *testing.T) {
cache := newCache(0, 100)

n1 := &mockedRootNode{ver: trie.Version{Major: 1, Minor: 1}}
cache.AddRootNode("", n1)
assert.Equal(t, n1, cache.GetRootNode("", n1.ver))

// minor ver not matched
assert.Equal(t, nil, cache.GetRootNode("", trie.Version{Major: 1}))
}

func TestCacheNodeBlob(t *testing.T) {
var (
cache = newCache(1, 0)
keyBuf []byte
blob = []byte{1, 1, 1}
ver = trie.Version{Major: 1, Minor: 1}
)

// add to committing cache
cache.AddNodeBlob(&keyBuf, "", nil, ver, blob, true)
assert.Equal(t, blob, cache.GetNodeBlob(&keyBuf, "", nil, ver, false))
// minor ver not matched
assert.Nil(t, cache.GetNodeBlob(&keyBuf, "", nil, trie.Version{Major: 1}, false))

cache = newCache(1, 0)

// add to querying cache
cache.AddNodeBlob(&keyBuf, "", nil, ver, blob, false)
assert.Equal(t, blob, cache.GetNodeBlob(&keyBuf, "", nil, ver, false))
// minor ver not matched
assert.Nil(t, cache.GetNodeBlob(&keyBuf, "", nil, trie.Version{Major: 1}, false))
}

func Benchmark_cacheNoeBlob(b *testing.B) {
var (
cache = newCache(100, 0)
Expand Down

0 comments on commit 631fe55

Please sign in to comment.