Skip to content

Commit

Permalink
add mock counters for tries test
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 committed Mar 4, 2022
1 parent 3699650 commit d14e80e
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions dot/state/tries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ChainSafe/gossamer/internal/trie/node"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -34,7 +35,7 @@ func Test_NewTries(t *testing.T) {
}

//go:generate mockgen -destination=mock_gauge_test.go -package $GOPACKAGE github.com/prometheus/client_golang/prometheus Gauge

//go:generate mockgen -destination=mock_counter_test.go -package $GOPACKAGE github.com/prometheus/client_golang/prometheus Counter
func Test_Tries_softSet(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -70,8 +71,23 @@ func Test_Tries_softSet(t *testing.T) {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
tries := newTriesEmpty()
tries.rootToTrie = testCase.rootToTrie
ctrl := gomock.NewController(t)

triesGauge := NewMockGauge(ctrl)
if testCase.triesGaugeInc {
triesGauge.EXPECT().Inc()
}

setCounter := NewMockCounter(ctrl)
if testCase.triesGaugeInc {
setCounter.EXPECT().Inc()
}

tries := &Tries{
rootToTrie: testCase.rootToTrie,
triesGauge: triesGauge,
setCounter: setCounter,
}

tries.softSet(testCase.root, testCase.trie)

Expand All @@ -86,37 +102,49 @@ func Test_Tries_delete(t *testing.T) {
testCases := map[string]struct {
rootToTrie map[common.Hash]*trie.Trie
root common.Hash
triesGaugeSet float64
deleteCounterInc bool
expectedRootToTrie map[common.Hash]*trie.Trie
}{
"not found": {
rootToTrie: map[common.Hash]*trie.Trie{
{3, 4, 5}: {},
},
root: common.Hash{1, 2, 3},
triesGaugeSet: 1,
root: common.Hash{1, 2, 3},
expectedRootToTrie: map[common.Hash]*trie.Trie{
{3, 4, 5}: {},
},
deleteCounterInc: true,
},
"deleted": {
rootToTrie: map[common.Hash]*trie.Trie{
{1, 2, 3}: {},
{3, 4, 5}: {},
},
root: common.Hash{1, 2, 3},
triesGaugeSet: 1,
root: common.Hash{1, 2, 3},
expectedRootToTrie: map[common.Hash]*trie.Trie{
{3, 4, 5}: {},
},
deleteCounterInc: true,
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
tries := newTriesEmpty()
ctrl := gomock.NewController(t)
deleteCounter := NewMockCounter(ctrl)
if testCase.deleteCounterInc {
deleteCounter.EXPECT().Inc()
}

tries := &Tries{
rootToTrie: testCase.rootToTrie,
triesGauge: triesGauge,
setCounter: setCounter,
deleteCounter: deleteCounter,
}

tries.rootToTrie = testCase.rootToTrie

tries.delete(testCase.root)
Expand Down

0 comments on commit d14e80e

Please sign in to comment.