Skip to content

Commit

Permalink
test: ♻️ segregate unit and e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atvanguard committed Jun 4, 2020
1 parent 187fc06 commit 12ec141
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 28 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ ios:
@echo "Import \"$(GOBIN)/bor.framework\" to use the library."

test: bor
go test github.com/maticnetwork/bor/consensus/bor/bor_test
go test github.com/maticnetwork/bor/consensus/bor
go test github.com/maticnetwork/bor/tests/bor

lint: ## Run linters.
$(GORUN) build/ci.go lint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bortest
package bor

import (
"math/rand"
Expand All @@ -9,7 +9,6 @@ import (
"github.com/stretchr/testify/assert"

"github.com/maticnetwork/bor/common"
"github.com/maticnetwork/bor/consensus/bor"
)

const (
Expand All @@ -18,8 +17,8 @@ const (

func TestGetSignerSuccessionNumber_ProposerIsSigner(t *testing.T) {
validators := buildRandomValidatorSet(numVals)
validatorSet := bor.NewValidatorSet(validators)
snap := bor.Snapshot{
validatorSet := NewValidatorSet(validators)
snap := Snapshot{
ValidatorSet: validatorSet,
}

Expand All @@ -35,14 +34,14 @@ func TestGetSignerSuccessionNumber_ProposerIsSigner(t *testing.T) {
func TestGetSignerSuccessionNumber_SignerIndexIsLarger(t *testing.T) {
validators := buildRandomValidatorSet(numVals)

// sort validators by address, which is what bor.NewValidatorSet also does
sort.Sort(bor.ValidatorsByAddress(validators))
// sort validators by address, which is what NewValidatorSet also does
sort.Sort(ValidatorsByAddress(validators))
proposerIndex := 32
signerIndex := 56
// give highest ProposerPriority to a particular val, so that they become the proposer
validators[proposerIndex].VotingPower = 200
snap := bor.Snapshot{
ValidatorSet: bor.NewValidatorSet(validators),
snap := Snapshot{
ValidatorSet: NewValidatorSet(validators),
}

// choose a signer at an index greater than proposer index
Expand All @@ -60,8 +59,8 @@ func TestGetSignerSuccessionNumber_SignerIndexIsSmaller(t *testing.T) {
signerIndex := 11
// give highest ProposerPriority to a particular val, so that they become the proposer
validators[proposerIndex].VotingPower = 200
snap := bor.Snapshot{
ValidatorSet: bor.NewValidatorSet(validators),
snap := Snapshot{
ValidatorSet: NewValidatorSet(validators),
}

// choose a signer at an index greater than proposer index
Expand All @@ -75,46 +74,46 @@ func TestGetSignerSuccessionNumber_SignerIndexIsSmaller(t *testing.T) {

func TestGetSignerSuccessionNumber_ProposerNotFound(t *testing.T) {
validators := buildRandomValidatorSet(numVals)
snap := bor.Snapshot{
ValidatorSet: bor.NewValidatorSet(validators),
snap := Snapshot{
ValidatorSet: NewValidatorSet(validators),
}
dummyProposerAddress := randomAddress()
snap.ValidatorSet.Proposer = &bor.Validator{Address: dummyProposerAddress}
snap.ValidatorSet.Proposer = &Validator{Address: dummyProposerAddress}
// choose any signer
signer := snap.ValidatorSet.Validators[3].Address
_, err := snap.GetSignerSuccessionNumber(signer)
assert.NotNil(t, err)
e, ok := err.(*bor.UnauthorizedProposerError)
e, ok := err.(*UnauthorizedProposerError)
assert.True(t, ok)
assert.Equal(t, dummyProposerAddress.Bytes(), e.Proposer)
}

func TestGetSignerSuccessionNumber_SignerNotFound(t *testing.T) {
validators := buildRandomValidatorSet(numVals)
snap := bor.Snapshot{
ValidatorSet: bor.NewValidatorSet(validators),
snap := Snapshot{
ValidatorSet: NewValidatorSet(validators),
}
dummySignerAddress := randomAddress()
_, err := snap.GetSignerSuccessionNumber(dummySignerAddress)
assert.NotNil(t, err)
e, ok := err.(*bor.UnauthorizedSignerError)
e, ok := err.(*UnauthorizedSignerError)
assert.True(t, ok)
assert.Equal(t, dummySignerAddress.Bytes(), e.Signer)
}

func buildRandomValidatorSet(numVals int) []*bor.Validator {
func buildRandomValidatorSet(numVals int) []*Validator {
rand.Seed(time.Now().Unix())
validators := make([]*bor.Validator, numVals)
validators := make([]*Validator, numVals)
for i := 0; i < numVals; i++ {
validators[i] = &bor.Validator{
validators[i] = &Validator{
Address: randomAddress(),
// cannot process validators with voting power 0, hence +1
VotingPower: int64(rand.Intn(99) + 1),
}
}

// sort validators by address, which is what bor.NewValidatorSet also does
sort.Sort(bor.ValidatorsByAddress(validators))
// sort validators by address, which is what NewValidatorSet also does
sort.Sort(ValidatorsByAddress(validators))
return validators
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bortest
package bor

import (
"encoding/hex"
Expand Down
8 changes: 4 additions & 4 deletions consensus/bor/bor_test/helper.go → tests/bor/helper.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bortest
package bor

import (
"encoding/hex"
Expand Down Expand Up @@ -45,7 +45,7 @@ type initializeData struct {
}

func buildEthereumInstance(t *testing.T, db ethdb.Database) *initializeData {
genesisData, err := ioutil.ReadFile("genesis.json")
genesisData, err := ioutil.ReadFile("./testdata/genesis.json")
if err != nil {
t.Fatalf("%s", err)
}
Expand Down Expand Up @@ -157,7 +157,7 @@ func sign(t *testing.T, header *types.Header, signer []byte) {
}

func stateSyncEventsPayload(t *testing.T) *bor.ResponseWithHeight {
stateData, err := ioutil.ReadFile("states.json")
stateData, err := ioutil.ReadFile("./testdata/states.json")
if err != nil {
t.Fatalf("%s", err)
}
Expand All @@ -169,7 +169,7 @@ func stateSyncEventsPayload(t *testing.T) *bor.ResponseWithHeight {
}

func loadSpanFromFile(t *testing.T) (*bor.ResponseWithHeight, *bor.HeimdallSpan) {
spanData, err := ioutil.ReadFile("span.json")
spanData, err := ioutil.ReadFile("./testdata/span.json")
if err != nil {
t.Fatalf("%s", err)
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 12ec141

Please sign in to comment.