Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(other): using testify properly #1433

Merged
merged 26 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ You can use these commands in the Makefile:
Error and log messages should not start with a capital letter (unless it's a proper noun or acronym) and
should not end with punctuation.

All changes on core must contain proper and well-defined unit-tests, also previous tests must be passed as well.
This codebase used `testify` for unit tests, make sure you follow these guide for tests:

- For panic cases make sure you use `assert.Panics`
- For checking err using `assert.ErrorIs` make sure you pass expected error as second argument.
- For checking equality using `assert.Equal` make sure you pass expected value as the first argument.


> This code guideline must be followed for both contributors and maintainers to review the PRs.

#### Examples

- Correct ✅: "unable to connect to server"
Expand Down
118 changes: 57 additions & 61 deletions committee/committee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func TestProposer(t *testing.T) {

cmt, _ := ts.GenerateTestCommittee(4)

assert.Equal(t, cmt.Proposer(0).Number(), int32(0))
assert.Equal(t, cmt.Proposer(3).Number(), int32(3))
assert.Equal(t, cmt.Proposer(4).Number(), int32(0))
assert.Equal(t, int32(0), cmt.Proposer(0).Number())
assert.Equal(t, int32(3), cmt.Proposer(3).Number())
assert.Equal(t, int32(0), cmt.Proposer(4).Number())

cmt.Update(0, nil)
assert.Equal(t, cmt.Proposer(0).Number(), int32(1))
assert.Equal(t, int32(1), cmt.Proposer(0).Number())
}

func TestInvalidProposerJoinAndLeave(t *testing.T) {
Expand Down Expand Up @@ -68,29 +68,29 @@ func TestProposerMove(t *testing.T) {
// +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+
//

assert.Equal(t, cmt.Proposer(0).Number(), int32(1))
assert.Equal(t, cmt.Proposer(7).Number(), int32(1))
assert.Equal(t, cmt.Validators(), []*validator.Validator{val1, val2, val3, val4, val5, val6, val7})
assert.Equal(t, int32(1), cmt.Proposer(0).Number())
assert.Equal(t, int32(1), cmt.Proposer(7).Number())
assert.Equal(t, []*validator.Validator{val1, val2, val3, val4, val5, val6, val7}, cmt.Validators())
fmt.Println(cmt.String())

// Height 1001
cmt.Update(0, nil)
assert.Equal(t, cmt.Proposer(0).Number(), int32(2))
assert.Equal(t, cmt.Proposer(1).Number(), int32(3))
assert.Equal(t, cmt.Proposer(7).Number(), int32(2))
assert.Equal(t, cmt.Validators(), []*validator.Validator{val1, val2, val3, val4, val5, val6, val7})
assert.Equal(t, int32(2), cmt.Proposer(0).Number())
assert.Equal(t, int32(3), cmt.Proposer(1).Number())
assert.Equal(t, int32(2), cmt.Proposer(7).Number())
assert.Equal(t, []*validator.Validator{val1, val2, val3, val4, val5, val6, val7}, cmt.Validators())
fmt.Println(cmt.String())

// Height 1002
cmt.Update(3, nil)
assert.Equal(t, cmt.Proposer(0).Number(), int32(6))
assert.Equal(t, cmt.Validators(), []*validator.Validator{val1, val2, val3, val4, val5, val6, val7})
assert.Equal(t, int32(6), cmt.Proposer(0).Number())
assert.Equal(t, []*validator.Validator{val1, val2, val3, val4, val5, val6, val7}, cmt.Validators())
fmt.Println(cmt.String())

// Height 1003
cmt.Update(1, nil)
assert.Equal(t, cmt.Proposer(0).Number(), int32(1))
assert.Equal(t, cmt.Validators(), []*validator.Validator{val1, val2, val3, val4, val5, val6, val7})
assert.Equal(t, int32(1), cmt.Proposer(0).Number())
assert.Equal(t, []*validator.Validator{val1, val2, val3, val4, val5, val6, val7}, cmt.Validators())
fmt.Println(cmt.String())
}

Expand All @@ -105,14 +105,10 @@ func TestValidatorConsistency(t *testing.T) {
cmt, _ := committee.NewCommittee([]*validator.Validator{val1, val2, val3, val4}, 4, val1.Address())

t.Run("Updating validators' stake, Should panic", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()

val1.AddToStake(1)
cmt.Update(0, []*validator.Validator{val1})
assert.Panics(t, func() {
val1.AddToStake(1)
cmt.Update(0, []*validator.Validator{val1})
}, "The code did not panic")
})
}

Expand All @@ -129,7 +125,7 @@ func TestProposerJoin(t *testing.T) {

cmt, err := committee.NewCommittee([]*validator.Validator{val1, val2, val3, val4}, 7, val1.Address())
assert.NoError(t, err)
assert.Equal(t, cmt.Size(), 4)
assert.Equal(t, 4, cmt.Size())

//
// h=1000, r=0 h=1001, r=0 h=1002, r=1 h=1003, r=1 h=1004, r=0
Expand All @@ -142,39 +138,39 @@ func TestProposerJoin(t *testing.T) {
// Val2 is in the committee
val2.UpdateLastSortitionHeight(1000)
cmt.Update(0, []*validator.Validator{val2})
assert.Equal(t, cmt.Proposer(0).Number(), int32(2))
assert.Equal(t, cmt.Committers(), []int32{1, 2, 3, 4})
assert.Equal(t, cmt.Size(), 4)
assert.Equal(t, int32(2), cmt.Proposer(0).Number())
assert.Equal(t, []int32{1, 2, 3, 4}, cmt.Committers())
assert.Equal(t, 4, cmt.Size())
fmt.Println(cmt.String())

// Height 1001
val5.UpdateLastSortitionHeight(1001)
cmt.Update(0, []*validator.Validator{val5})
assert.Equal(t, cmt.Proposer(0).Number(), int32(3))
assert.Equal(t, cmt.Proposer(1).Number(), int32(4))
assert.Equal(t, cmt.Committers(), []int32{1, 5, 2, 3, 4})
assert.Equal(t, cmt.Size(), 5)
assert.Equal(t, int32(3), cmt.Proposer(0).Number())
assert.Equal(t, int32(4), cmt.Proposer(1).Number())
assert.Equal(t, []int32{1, 5, 2, 3, 4}, cmt.Committers())
assert.Equal(t, 5, cmt.Size())
fmt.Println(cmt.String())

// Height 1002
cmt.Update(1, nil)
assert.Equal(t, cmt.Proposer(0).Number(), int32(1))
assert.Equal(t, cmt.Proposer(1).Number(), int32(5))
assert.Equal(t, int32(1), cmt.Proposer(0).Number())
assert.Equal(t, int32(5), cmt.Proposer(1).Number())
fmt.Println(cmt.String())

// Height 1003
val3.UpdateLastSortitionHeight(1003)
val6.UpdateLastSortitionHeight(1003)
val7.UpdateLastSortitionHeight(1003)
cmt.Update(1, []*validator.Validator{val7, val3, val6})
assert.Equal(t, cmt.Proposer(0).Number(), int32(2))
assert.Equal(t, cmt.Committers(), []int32{6, 7, 1, 5, 2, 3, 4})
assert.Equal(t, cmt.Size(), 7)
assert.Equal(t, int32(2), cmt.Proposer(0).Number())
assert.Equal(t, []int32{6, 7, 1, 5, 2, 3, 4}, cmt.Committers())
assert.Equal(t, 7, cmt.Size())
fmt.Println(cmt.String())

// Height 1004
cmt.Update(0, nil)
assert.Equal(t, cmt.Proposer(0).Number(), int32(3))
assert.Equal(t, int32(3), cmt.Proposer(0).Number())
fmt.Println(cmt.String())
}

Expand Down Expand Up @@ -260,49 +256,49 @@ func TestProposerJoinAndLeave(t *testing.T) {
// Height 1001
val8.UpdateLastSortitionHeight(1001)
cmt.Update(0, []*validator.Validator{val8})
assert.Equal(t, cmt.Proposer(0).Number(), int32(2))
assert.Equal(t, cmt.Proposer(1).Number(), int32(3))
assert.Equal(t, cmt.Proposer(2).Number(), int32(4))
assert.Equal(t, cmt.Committers(), []int32{8, 2, 3, 4, 5, 6, 7})
assert.Equal(t, int32(2), cmt.Proposer(0).Number())
assert.Equal(t, int32(3), cmt.Proposer(1).Number())
assert.Equal(t, int32(4), cmt.Proposer(2).Number())
assert.Equal(t, []int32{8, 2, 3, 4, 5, 6, 7}, cmt.Committers())
fmt.Println(cmt.String())

// Height 1002
val3.UpdateLastSortitionHeight(1002)
cmt.Update(3, []*validator.Validator{val3})
assert.Equal(t, cmt.Proposer(0).Number(), int32(6))
assert.Equal(t, int32(6), cmt.Proposer(0).Number())
fmt.Println(cmt.String())

// Height 1003
val2.UpdateLastSortitionHeight(1003)
val9.UpdateLastSortitionHeight(1003)
cmt.Update(0, []*validator.Validator{val9, val2})
assert.Equal(t, cmt.Proposer(0).Number(), int32(7))
assert.Equal(t, cmt.Proposer(1).Number(), int32(8))
assert.Equal(t, cmt.Committers(), []int32{8, 2, 3, 5, 9, 6, 7})
assert.Equal(t, int32(7), cmt.Proposer(0).Number())
assert.Equal(t, int32(8), cmt.Proposer(1).Number())
assert.Equal(t, []int32{8, 2, 3, 5, 9, 6, 7}, cmt.Committers())
fmt.Println(cmt.String())

// Height 1004
valA.UpdateLastSortitionHeight(1004)
cmt.Update(1, []*validator.Validator{valA})
assert.Equal(t, cmt.Proposer(0).Number(), int32(2))
assert.Equal(t, cmt.Committers(), []int32{8, 2, 3, 9, 6, 10, 7})
assert.Equal(t, int32(2), cmt.Proposer(0).Number())
assert.Equal(t, []int32{8, 2, 3, 9, 6, 10, 7}, cmt.Committers())
fmt.Println(cmt.String())

// Height 1005
valB.UpdateLastSortitionHeight(1005)
valC.UpdateLastSortitionHeight(1005)
cmt.Update(0, []*validator.Validator{valC, valB})
assert.Equal(t, cmt.Proposer(0).Number(), int32(3))
assert.Equal(t, cmt.Proposer(1).Number(), int32(9))
assert.Equal(t, cmt.Proposer(2).Number(), int32(10))
assert.Equal(t, cmt.Committers(), []int32{8, 11, 12, 2, 3, 9, 10})
assert.Equal(t, int32(3), cmt.Proposer(0).Number())
assert.Equal(t, int32(9), cmt.Proposer(1).Number())
assert.Equal(t, int32(10), cmt.Proposer(2).Number())
assert.Equal(t, []int32{8, 11, 12, 2, 3, 9, 10}, cmt.Committers())
fmt.Println(cmt.String())

// Height 1006
val1.UpdateLastSortitionHeight(1006)
cmt.Update(2, []*validator.Validator{val1})
assert.Equal(t, cmt.Proposer(0).Number(), int32(11))
assert.Equal(t, cmt.Committers(), []int32{11, 12, 2, 1, 3, 9, 10})
assert.Equal(t, int32(11), cmt.Proposer(0).Number())
assert.Equal(t, []int32{11, 12, 2, 1, 3, 9, 10}, cmt.Committers())
fmt.Println(cmt.String())

// Height 1007
Expand All @@ -311,8 +307,8 @@ func TestProposerJoinAndLeave(t *testing.T) {
val5.UpdateLastSortitionHeight(1007)
val6.UpdateLastSortitionHeight(1007)
cmt.Update(4, []*validator.Validator{val2, val3, val5, val6})
assert.Equal(t, cmt.Proposer(0).Number(), int32(5))
assert.Equal(t, cmt.Committers(), []int32{5, 6, 11, 12, 2, 1, 3})
assert.Equal(t, int32(5), cmt.Proposer(0).Number())
assert.Equal(t, []int32{5, 6, 11, 12, 2, 1, 3}, cmt.Committers())
fmt.Println(cmt.String())
}

Expand All @@ -327,12 +323,12 @@ func TestIsProposer(t *testing.T) {
cmt, err := committee.NewCommittee([]*validator.Validator{val1, val2, val3, val4}, 4, val1.Address())
assert.NoError(t, err)

assert.Equal(t, cmt.Proposer(0).Number(), int32(0))
assert.Equal(t, cmt.Proposer(1).Number(), int32(1))
assert.Equal(t, int32(0), cmt.Proposer(0).Number())
assert.Equal(t, int32(1), cmt.Proposer(1).Number())
assert.True(t, cmt.IsProposer(val3.Address(), 2))
assert.False(t, cmt.IsProposer(val4.Address(), 2))
assert.False(t, cmt.IsProposer(ts.RandAccAddress(), 2))
assert.Equal(t, cmt.Validators(), []*validator.Validator{val1, val2, val3, val4})
assert.Equal(t, []*validator.Validator{val1, val2, val3, val4}, cmt.Validators())
}

func TestCommitters(t *testing.T) {
Expand All @@ -345,7 +341,7 @@ func TestCommitters(t *testing.T) {

cmt, err := committee.NewCommittee([]*validator.Validator{val1, val2, val3, val4}, 4, val1.Address())
assert.NoError(t, err)
assert.Equal(t, cmt.Committers(), []int32{0, 1, 2, 3})
assert.Equal(t, []int32{0, 1, 2, 3}, cmt.Committers())
}

func TestSortJoined(t *testing.T) {
Expand Down Expand Up @@ -383,6 +379,6 @@ func TestTotalPower(t *testing.T) {

totalPower := val0.Power() + val1.Power() + val2.Power() + val3.Power() + val4.Power()
totalStake := val0.Stake() + val1.Stake() + val2.Stake() + val3.Stake() + val4.Stake()
assert.Equal(t, cmt.TotalPower(), totalPower)
assert.Equal(t, cmt.TotalPower(), int64(totalStake+1))
assert.Equal(t, totalPower, cmt.TotalPower())
assert.Equal(t, int64(totalStake+1), cmt.TotalPower())
}
56 changes: 28 additions & 28 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func TestSaveMainnetConfig(t *testing.T) {
assert.NoError(t, err)

assert.NoError(t, conf.BasicCheck())
assert.Equal(t, DefaultConfigMainnet(), conf)
assert.Equal(t, conf, DefaultConfigMainnet())

confData, _ := util.ReadFile(path)
exampleData, _ := util.ReadFile("example_config.toml")
assert.Equal(t, confData, exampleData)
assert.Equal(t, exampleData, confData)
}

func TestSaveTestnetConfig(t *testing.T) {
Expand All @@ -32,7 +32,7 @@ func TestSaveTestnetConfig(t *testing.T) {

conf, err := LoadFromFile(path, true, defConf)
assert.NoError(t, err)
assert.Equal(t, DefaultConfigTestnet(), conf)
assert.Equal(t, conf, DefaultConfigTestnet())

assert.NoError(t, conf.BasicCheck())
}
Expand All @@ -42,75 +42,75 @@ func TestDefaultConfig(t *testing.T) {

assert.NoError(t, conf.BasicCheck())
assert.Empty(t, conf.Network.ListenAddrStrings)
assert.Equal(t, conf.Network.NetworkName, "")
assert.Equal(t, conf.Network.DefaultPort, 0)
assert.Zero(t, conf.Network.NetworkName)
assert.Zero(t, conf.Network.DefaultPort)

assert.False(t, conf.GRPC.Enable)
assert.False(t, conf.GRPC.Gateway.Enable)
assert.False(t, conf.HTTP.Enable)
assert.False(t, conf.Nanomsg.Enable)

assert.Equal(t, conf.GRPC.Listen, "")
assert.Equal(t, conf.GRPC.Gateway.Listen, "")
assert.Equal(t, conf.HTTP.Listen, "")
assert.Equal(t, conf.Nanomsg.Listen, "")
assert.Zero(t, conf.GRPC.Listen)
assert.Zero(t, conf.GRPC.Gateway.Listen)
assert.Zero(t, conf.HTTP.Listen)
assert.Zero(t, conf.Nanomsg.Listen)
}

func TestMainnetConfig(t *testing.T) {
conf := DefaultConfigMainnet()

assert.NoError(t, conf.BasicCheck())
assert.Empty(t, conf.Network.ListenAddrStrings)
assert.Equal(t, conf.Network.NetworkName, "pactus")
assert.Equal(t, conf.Network.DefaultPort, 21888)
assert.Equal(t, "pactus", conf.Network.NetworkName)
assert.Equal(t, 21888, conf.Network.DefaultPort)

assert.True(t, conf.GRPC.Enable)
assert.False(t, conf.GRPC.Gateway.Enable)
assert.False(t, conf.HTTP.Enable)
assert.False(t, conf.Nanomsg.Enable)

assert.Equal(t, conf.GRPC.Listen, "127.0.0.1:50051")
assert.Equal(t, conf.GRPC.Gateway.Listen, "127.0.0.1:8080")
assert.Equal(t, conf.HTTP.Listen, "127.0.0.1:80")
assert.Equal(t, conf.Nanomsg.Listen, "tcp://127.0.0.1:40899")
assert.Equal(t, "127.0.0.1:50051", conf.GRPC.Listen)
assert.Equal(t, "127.0.0.1:8080", conf.GRPC.Gateway.Listen)
assert.Equal(t, "127.0.0.1:80", conf.HTTP.Listen)
assert.Equal(t, "tcp://127.0.0.1:40899", conf.Nanomsg.Listen)
}

func TestTestnetConfig(t *testing.T) {
conf := DefaultConfigTestnet()

assert.NoError(t, conf.BasicCheck())
assert.Empty(t, conf.Network.ListenAddrStrings)
assert.Equal(t, conf.Network.NetworkName, "pactus-testnet")
assert.Equal(t, conf.Network.DefaultPort, 21777)
assert.Equal(t, "pactus-testnet", conf.Network.NetworkName)
assert.Equal(t, 21777, conf.Network.DefaultPort)

assert.True(t, conf.GRPC.Enable)
assert.True(t, conf.GRPC.Gateway.Enable)
assert.False(t, conf.HTTP.Enable)
assert.False(t, conf.Nanomsg.Enable)

assert.Equal(t, conf.GRPC.Listen, "[::]:50052")
assert.Equal(t, conf.GRPC.Gateway.Listen, "[::]:8080")
assert.Equal(t, conf.HTTP.Listen, "[::]:80")
assert.Equal(t, conf.Nanomsg.Listen, "tcp://[::]:40799")
assert.Equal(t, "[::]:50052", conf.GRPC.Listen)
assert.Equal(t, "[::]:8080", conf.GRPC.Gateway.Listen)
assert.Equal(t, "[::]:80", conf.HTTP.Listen)
assert.Equal(t, "tcp://[::]:40799", conf.Nanomsg.Listen)
}

func TestLocalnetConfig(t *testing.T) {
conf := DefaultConfigLocalnet()

assert.NoError(t, conf.BasicCheck())
assert.Empty(t, conf.Network.ListenAddrStrings)
assert.Equal(t, conf.Network.NetworkName, "pactus-localnet")
assert.Equal(t, conf.Network.DefaultPort, 0)
assert.Equal(t, "pactus-localnet", conf.Network.NetworkName)
assert.Equal(t, 0, conf.Network.DefaultPort)

assert.True(t, conf.GRPC.Enable)
assert.True(t, conf.GRPC.Gateway.Enable)
assert.True(t, conf.HTTP.Enable)
assert.True(t, conf.Nanomsg.Enable)

assert.Equal(t, conf.GRPC.Listen, "[::]:50052")
assert.Equal(t, conf.GRPC.Gateway.Listen, "[::]:8080")
assert.Equal(t, conf.HTTP.Listen, "[::]:0")
assert.Equal(t, conf.Nanomsg.Listen, "tcp://[::]:40799")
assert.Equal(t, "[::]:50052", conf.GRPC.Listen)
assert.Equal(t, "[::]:8080", conf.GRPC.Gateway.Listen)
assert.Equal(t, "[::]:0", conf.HTTP.Listen)
assert.Equal(t, "tcp://[::]:40799", conf.Nanomsg.Listen)
}

func TestLoadFromFile(t *testing.T) {
Expand All @@ -126,7 +126,7 @@ func TestLoadFromFile(t *testing.T) {

conf, err := LoadFromFile(path, false, defConf)
assert.NoError(t, err)
assert.Equal(t, conf, defConf)
assert.Equal(t, defConf, conf)
}

func TestExampleConfig(t *testing.T) {
Expand Down
Loading
Loading