mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-29 12:41:44 +00:00
Reformat tests to extract common setup
This commit is contained in:
@ -8,7 +8,9 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
abci "github.com/tendermint/abci/types"
|
abci "github.com/tendermint/abci/types"
|
||||||
|
|
||||||
crypto "github.com/tendermint/go-crypto"
|
crypto "github.com/tendermint/go-crypto"
|
||||||
|
|
||||||
cmn "github.com/tendermint/tmlibs/common"
|
cmn "github.com/tendermint/tmlibs/common"
|
||||||
dbm "github.com/tendermint/tmlibs/db"
|
dbm "github.com/tendermint/tmlibs/db"
|
||||||
"github.com/tendermint/tmlibs/log"
|
"github.com/tendermint/tmlibs/log"
|
||||||
@ -17,31 +19,35 @@ import (
|
|||||||
"github.com/tendermint/tendermint/types"
|
"github.com/tendermint/tendermint/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStateCopyEquals(t *testing.T) {
|
func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, *State) {
|
||||||
assert := assert.New(t)
|
|
||||||
config := cfg.ResetTestRoot("state_")
|
|
||||||
|
|
||||||
// Get State db
|
config := cfg.ResetTestRoot("state_")
|
||||||
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
|
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
|
||||||
state := GetState(stateDB, config.GenesisFile())
|
state := GetState(stateDB, config.GenesisFile())
|
||||||
state.SetLogger(log.TestingLogger())
|
state.SetLogger(log.TestingLogger())
|
||||||
|
|
||||||
|
tearDown := func(t *testing.T) {}
|
||||||
|
|
||||||
|
return tearDown, stateDB, state
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStateCopy(t *testing.T) {
|
||||||
|
tearDown, _, state := setupTestCase(t)
|
||||||
|
defer tearDown(t)
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
stateCopy := state.Copy()
|
stateCopy := state.Copy()
|
||||||
|
|
||||||
assert.True(state.Equals(stateCopy), cmn.Fmt("expected state and its copy to be identical. got %v\n expected %v\n", stateCopy, state))
|
assert.True(state.Equals(stateCopy), cmn.Fmt("expected state and its copy to be identical. got %v\n expected %v\n", stateCopy, state))
|
||||||
stateCopy.LastBlockHeight += 1
|
stateCopy.LastBlockHeight++
|
||||||
assert.False(state.Equals(stateCopy), cmn.Fmt("expected states to be different. got same %v", state))
|
assert.False(state.Equals(stateCopy), cmn.Fmt("expected states to be different. got same %v", state))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStateSaveLoad(t *testing.T) {
|
func TestStateSaveLoad(t *testing.T) {
|
||||||
assert := assert.New(t)
|
tearDown, stateDB, state := setupTestCase(t)
|
||||||
config := cfg.ResetTestRoot("state_")
|
defer tearDown(t)
|
||||||
// Get State db
|
|
||||||
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
|
|
||||||
state := GetState(stateDB, config.GenesisFile())
|
|
||||||
state.SetLogger(log.TestingLogger())
|
|
||||||
|
|
||||||
state.LastBlockHeight += 1
|
state.LastBlockHeight++
|
||||||
state.Save()
|
state.Save()
|
||||||
|
|
||||||
loadedState := LoadState(stateDB)
|
loadedState := LoadState(stateDB)
|
||||||
@ -49,14 +55,11 @@ func TestStateSaveLoad(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestABCIResponsesSaveLoad(t *testing.T) {
|
func TestABCIResponsesSaveLoad(t *testing.T) {
|
||||||
|
tearDown, _, state := setupTestCase(t)
|
||||||
|
defer tearDown(t)
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
config := cfg.ResetTestRoot("state_")
|
state.LastBlockHeight++
|
||||||
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
|
|
||||||
state := GetState(stateDB, config.GenesisFile())
|
|
||||||
state.SetLogger(log.TestingLogger())
|
|
||||||
|
|
||||||
state.LastBlockHeight += 1
|
|
||||||
|
|
||||||
// build mock responses
|
// build mock responses
|
||||||
block := makeBlock(2, state)
|
block := makeBlock(2, state)
|
||||||
@ -77,12 +80,9 @@ func TestABCIResponsesSaveLoad(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestValidatorSimpleSaveLoad(t *testing.T) {
|
func TestValidatorSimpleSaveLoad(t *testing.T) {
|
||||||
|
tearDown, _, state := setupTestCase(t)
|
||||||
|
defer tearDown(t)
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
config := cfg.ResetTestRoot("state_")
|
|
||||||
// Get State db
|
|
||||||
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
|
|
||||||
state := GetState(stateDB, config.GenesisFile())
|
|
||||||
state.SetLogger(log.TestingLogger())
|
|
||||||
|
|
||||||
// cant load anything for height 0
|
// cant load anything for height 0
|
||||||
v, err := state.LoadValidators(0)
|
v, err := state.LoadValidators(0)
|
||||||
@ -94,7 +94,7 @@ func TestValidatorSimpleSaveLoad(t *testing.T) {
|
|||||||
assert.Equal(v.Hash(), state.Validators.Hash(), "expected validator hashes to match")
|
assert.Equal(v.Hash(), state.Validators.Hash(), "expected validator hashes to match")
|
||||||
|
|
||||||
// increment height, save; should be able to load for next height
|
// increment height, save; should be able to load for next height
|
||||||
state.LastBlockHeight += 1
|
state.LastBlockHeight++
|
||||||
state.saveValidatorsInfo()
|
state.saveValidatorsInfo()
|
||||||
v, err = state.LoadValidators(state.LastBlockHeight + 1)
|
v, err = state.LoadValidators(state.LastBlockHeight + 1)
|
||||||
assert.Nil(err, "expected no err")
|
assert.Nil(err, "expected no err")
|
||||||
@ -113,12 +113,9 @@ func TestValidatorSimpleSaveLoad(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestValidatorChangesSaveLoad(t *testing.T) {
|
func TestValidatorChangesSaveLoad(t *testing.T) {
|
||||||
|
tearDown, _, state := setupTestCase(t)
|
||||||
|
defer tearDown(t)
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
config := cfg.ResetTestRoot("state_")
|
|
||||||
// Get State db
|
|
||||||
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
|
|
||||||
state := GetState(stateDB, config.GenesisFile())
|
|
||||||
state.SetLogger(log.TestingLogger())
|
|
||||||
|
|
||||||
// change vals at these heights
|
// change vals at these heights
|
||||||
changeHeights := []int{1, 2, 4, 5, 10, 15, 16, 17, 20}
|
changeHeights := []int{1, 2, 4, 5, 10, 15, 16, 17, 20}
|
||||||
@ -141,7 +138,7 @@ func TestValidatorChangesSaveLoad(t *testing.T) {
|
|||||||
// when we get to a change height,
|
// when we get to a change height,
|
||||||
// use the next pubkey
|
// use the next pubkey
|
||||||
if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
|
if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
|
||||||
changeIndex += 1
|
changeIndex++
|
||||||
pubkey = pubkeys[changeIndex]
|
pubkey = pubkeys[changeIndex]
|
||||||
}
|
}
|
||||||
header, parts, responses := makeHeaderPartsResponses(state, i, pubkey)
|
header, parts, responses := makeHeaderPartsResponses(state, i, pubkey)
|
||||||
@ -157,7 +154,7 @@ func TestValidatorChangesSaveLoad(t *testing.T) {
|
|||||||
// we we get to the height after a change height
|
// we we get to the height after a change height
|
||||||
// use the next pubkey (note our counter starts at 0 this time)
|
// use the next pubkey (note our counter starts at 0 this time)
|
||||||
if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
|
if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
|
||||||
changeIndex += 1
|
changeIndex++
|
||||||
pubkey = pubkeys[changeIndex]
|
pubkey = pubkeys[changeIndex]
|
||||||
}
|
}
|
||||||
testCases[i-1] = valChangeTestCase{i, pubkey}
|
testCases[i-1] = valChangeTestCase{i, pubkey}
|
||||||
|
Reference in New Issue
Block a user