Skip to content

Commit

Permalink
Add Additional Pbft State Test Case
Browse files Browse the repository at this point in the history
  • Loading branch information
hoonkii authored and junbeomlee committed Sep 15, 2018
1 parent 4790be5 commit db933f0
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
93 changes: 93 additions & 0 deletions consensus/pbft/state_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package pbft

import (
"testing"

"github.com/stretchr/testify/assert"
)

// When Representative Number : 6, prevoteMsg Num : 4 -> then true
func TestState_CheckPrevoteCondition_Satisfy(t *testing.T) {
// 6 rep
satisfyPrevoteConditionState := setUpState()
prevoteMsgs := make([]PrevoteMsg, 0)
for i := 0; i < 4; i++ {
prevoteMsgs = append(prevoteMsgs, PrevoteMsg{
StateID: StateID{"state1"},
SenderID: "user1",
})
}
satisfyPrevoteConditionState.PrevoteMsgPool = PrevoteMsgPool{messages: prevoteMsgs}

assert.Equal(t, true, satisfyPrevoteConditionState.CheckPrevoteCondition())
}

// When Representative Number : 6, prevoteMsg Number : 3 -> then false
func TestState_CheckPrevoteCondition_UnSatisfy(t *testing.T) {
unSatisfyPrevoteConditionState := setUpState()
prevoteMsgs := make([]PrevoteMsg, 0)
for i := 0; i < 3; i++ {
prevoteMsgs = append(prevoteMsgs, PrevoteMsg{
StateID: StateID{"state1"},
SenderID: "user1",
})
}
unSatisfyPrevoteConditionState.PrevoteMsgPool = PrevoteMsgPool{messages: prevoteMsgs}

//then false
assert.Equal(t, false, unSatisfyPrevoteConditionState.CheckPrevoteCondition())
}

// When Representative Number : 6, prevoteCommitMsg Number : 4 -> then true
func TestState_CheckPreCommitCondition_Satisfy(t *testing.T) {
satisfyPrecommitConditionState := setUpState()
precommitMsgs := make([]PreCommitMsg, 0)
for i := 0; i < 4; i++ {
precommitMsgs = append(precommitMsgs, PreCommitMsg{
StateID: StateID{"state1"},
SenderID: "user1",
})
}
satisfyPrecommitConditionState.PreCommitMsgPool = PreCommitMsgPool{messages: precommitMsgs}

assert.Equal(t, true, satisfyPrecommitConditionState.CheckPreCommitCondition())
}

// When Representative Number : 6, prevoteCommitMsg Number : 3 -> then false
func TestState_CheckPreCommitCondition_UnSatisfy(t *testing.T) {

unSatisfyPrecommitConditionState := setUpState()
precommitMsgs := make([]PreCommitMsg, 0)
for i := 0; i < 3; i++ {
precommitMsgs = append(precommitMsgs, PreCommitMsg{
StateID: StateID{"state1"},
SenderID: "user1",
})
}
unSatisfyPrecommitConditionState.PreCommitMsgPool = PreCommitMsgPool{messages: precommitMsgs}

assert.Equal(t, false, unSatisfyPrecommitConditionState.CheckPreCommitCondition())
}

func setUpState() State {
//when representatives consist 6 member
reps := make([]*Representative, 0)
for i := 0; i < 6; i++ {
reps = append(reps, &Representative{
ID: "user",
})
}

var normalBlock = ProposedBlock{
Seal: []byte{1, 2, 3, 4},
Body: []byte{1, 2, 3, 5},
}

state1 := State{
StateID: StateID{"state1"},
Representatives: reps,
Block: normalBlock,
CurrentStage: IDLE_STAGE,
}
return state1
}
10 changes: 5 additions & 5 deletions consensus/pbft/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestPrevoteMsgPool_Save(t *testing.T) {
assert.Equal(t, 2, len(pPool.Get()))
}

func TestPrepareMsgPool_RemoveAllMsgs(t *testing.T) {
func TestPrevoteMsgPool_RemoveAllMsgs(t *testing.T) {
// given
pPool := pbft.NewPrevoteMsgPool()

Expand All @@ -99,7 +99,7 @@ func TestPrepareMsgPool_RemoveAllMsgs(t *testing.T) {
assert.Equal(t, 0, len(pPool.Get()))
}

func TestCommitMsgPool_Save(t *testing.T) {
func TestPreCommitMsgPool_Save(t *testing.T) {
// given
cPool := pbft.NewPreCommitMsgPool()

Expand Down Expand Up @@ -140,7 +140,7 @@ func TestCommitMsgPool_Save(t *testing.T) {
assert.Equal(t, 2, len(cPool.Get()))
}

func TestCommitMsgPool_RemoveAllMsgs(t *testing.T) {
func TestPreCommitMsgPool_RemoveAllMsgs(t *testing.T) {
// given
cPool := pbft.NewPreCommitMsgPool()

Expand All @@ -158,7 +158,7 @@ func TestCommitMsgPool_RemoveAllMsgs(t *testing.T) {
assert.Equal(t, 0, len(cPool.Get()))
}

func TestConsensus_SavePrevoteMsg(t *testing.T) {
func TestState_SavePrevoteMsg(t *testing.T) {
// given
c := pbft.State{
StateID: pbft.NewStateID("c1"),
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestConsensus_SavePrevoteMsg(t *testing.T) {
assert.Equal(t, 1, len(c.PrevoteMsgPool.Get()))
}

func TestConsensus_SavePreCommitMsg(t *testing.T) {
func TestState_SavePreCommitMsg(t *testing.T) {
// given
c := pbft.State{
StateID: pbft.NewStateID("c1"),
Expand Down

0 comments on commit db933f0

Please sign in to comment.