Skip to content

Commit

Permalink
Added test cases for msg pool.
Browse files Browse the repository at this point in the history
  • Loading branch information
hihiboss authored and junbeomlee committed Nov 4, 2018
1 parent 7b21d17 commit fb67178
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 21 deletions.
5 changes: 1 addition & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 45 additions & 8 deletions consensus/pbft/api/state_api_intenal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ func TestStateApi_checkPrevote(t *testing.T) {
prevoteMsgPool := pbft.NewPrevoteMsgPool()
for i := 1; i < 6; i++ {
senderStr := "user"
senderStr += string(i)
senderStr += strconv.Itoa(i)
prevoteMsgPool.Save(&pbft.PrevoteMsg{
MsgID: "m" + string(i),
MsgID: "m" + strconv.Itoa(i),
StateID: pbft.StateID{"state"},
SenderID: senderStr,
BlockHash: []byte{1, 2, 3, 4},
Expand All @@ -259,9 +259,9 @@ func TestStateApi_checkPreCommit(t *testing.T) {

for i := 1; i < 6; i++ {
senderStr := "user"
senderStr += string(i)
senderStr += strconv.Itoa(i)
precommitMsgPool.Save(&pbft.PreCommitMsg{
MsgID: "m" + string(i),
MsgID: "m" + strconv.Itoa(i),
StateID: pbft.StateID{"state"},
SenderID: senderStr,
})
Expand Down Expand Up @@ -298,9 +298,9 @@ func TestStateApi_updatePrevoteMsgPool(t *testing.T) {
msgNum := 2
for i := 0; i < msgNum; i++ {
msg := pbft.PrevoteMsg{
MsgID: "m" + string(i),
MsgID: "m" + strconv.Itoa(i),
StateID: pbft.StateID{"sid"},
SenderID: string(i),
SenderID: strconv.Itoa(i),
BlockHash: []byte{'h', 'a', 's', 'h'},
}
stateApi.tempPrevoteMsgPool.Save(&msg)
Expand All @@ -315,6 +315,25 @@ func TestStateApi_updatePrevoteMsgPool(t *testing.T) {
// then
assert.Equal(t, 0, len(stateApi.tempPrevoteMsgPool.FindAll()))
assert.Equal(t, msgNum, len(loadedState.PrevoteMsgPool.FindAll()))

// given
for i := 0; i < 3; i++ {
msg := pbft.PrevoteMsg{
MsgID: "msg" + strconv.Itoa(i),
StateID: pbft.StateID{"sid2"},
SenderID: "sender" + strconv.Itoa(i),
BlockHash: []byte{1, 2, 3, 4},
}
stateApi.tempPrevoteMsgPool.Save(&msg)
}

// when
loadedState, _ = stateApi.stateRepository.Load()
loadedState = stateApi.updatePrevoteMsgPool(loadedState)

// then
assert.Equal(t, 3, len(stateApi.tempPrevoteMsgPool.FindAll()))
assert.Equal(t, msgNum, len(loadedState.PrevoteMsgPool.FindAll()))
}

func TestStateApi_updatePreCommitMsgPool(t *testing.T) {
Expand Down Expand Up @@ -342,9 +361,9 @@ func TestStateApi_updatePreCommitMsgPool(t *testing.T) {
msgNum := 3
for i := 0; i < msgNum; i++ {
msg := pbft.PreCommitMsg{
MsgID: "m" + string(i),
MsgID: "m" + strconv.Itoa(i),
StateID: pbft.StateID{"sid"},
SenderID: string(i),
SenderID: strconv.Itoa(i),
}
stateApi.tempPreCommitMsgPool.Save(&msg)
}
Expand All @@ -358,6 +377,24 @@ func TestStateApi_updatePreCommitMsgPool(t *testing.T) {
// then
assert.Equal(t, 0, len(stateApi.tempPreCommitMsgPool.FindAll()))
assert.Equal(t, msgNum, len(loadedState.PreCommitMsgPool.FindAll()))

// given
for i := 0; i < 3; i++ {
msg := pbft.PreCommitMsg{
MsgID: "msg" + strconv.Itoa(i),
StateID: pbft.StateID{"sid2"},
SenderID: "sender" + strconv.Itoa(i),
}
stateApi.tempPreCommitMsgPool.Save(&msg)
}

// when
loadedState, _ = stateApi.stateRepository.Load()
loadedState = stateApi.updatePreCommitMsgPool(loadedState)

// then
assert.Equal(t, 3, len(stateApi.tempPreCommitMsgPool.FindAll()))
assert.Equal(t, msgNum, len(loadedState.PreCommitMsgPool.FindAll()))
}

func setUpApiCondition(peerNum int, isRepoFull, isNormalBlock bool, stage pbft.Stage) *StateApi {
Expand Down
70 changes: 61 additions & 9 deletions consensus/pbft/state_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,73 @@
package pbft

import (
"strconv"
"testing"

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

func TestPrevoteMsgPool_checkSenderExisting(t *testing.T) {
// given
msgPool := NewPrevoteMsgPool()

for i := 1; i < 4; i++ {
msgPool.Save(&PrevoteMsg{
MsgID: "msg" + strconv.Itoa(i),
StateID: StateID{"state1"},
SenderID: "sender1",
BlockHash: []byte{1, 2, 3, 4},
})
}

// when : existing
result := msgPool.checkSenderExisting("sender1")

// then
assert.True(t, result)

// when : not existing
result = msgPool.checkSenderExisting("sender2")

// then
assert.False(t, result)
}

func TestPreCommitMsgPool_checkSenderExisting(t *testing.T) {
// given
msgPool := NewPreCommitMsgPool()

for i := 1; i < 4; i++ {
msgPool.Save(&PreCommitMsg{
MsgID: "msg" + strconv.Itoa(i),
StateID: StateID{"state1"},
SenderID: "sender1",
})
}

// when : existing
result := msgPool.checkSenderExisting("sender1")

// then
assert.True(t, result)

// when : not existing
result = msgPool.checkSenderExisting("sender2")

// then
assert.False(t, result)
}

// When Representative Number : 6, prevoteMsg Num : 4 -> then true
func TestState_CheckPrevoteCondition_Satisfy(t *testing.T) {
// 6 rep
satisfyPrevoteConditionState := setUpState()
satisfyPrevoteConditionState.PrevoteMsgPool = NewPrevoteMsgPool()
for i := 0; i < 4; i++ {
msg := PrevoteMsg{
MsgID: "msg" + string(i),
MsgID: "msg" + strconv.Itoa(i),
StateID: StateID{"state1"},
SenderID: "user" + string(i),
SenderID: "user" + strconv.Itoa(i),
BlockHash: []byte{1, 2, 3, 4},
}
satisfyPrevoteConditionState.PrevoteMsgPool.Save(&msg)
Expand All @@ -30,9 +82,9 @@ func TestState_CheckPrevoteCondition_UnSatisfy(t *testing.T) {
unSatisfyPrevoteConditionState.PrevoteMsgPool = NewPrevoteMsgPool()
for i := 0; i < 2; i++ {
msg := PrevoteMsg{
MsgID: "msg" + string(i),
MsgID: "msg" + strconv.Itoa(i),
StateID: StateID{"state1"},
SenderID: "user" + string(i),
SenderID: "user" + strconv.Itoa(i),
BlockHash: []byte{1, 2, 3, 4},
}
unSatisfyPrevoteConditionState.PrevoteMsgPool.Save(&msg)
Expand All @@ -49,9 +101,9 @@ func TestState_CheckPreCommitCondition_Satisfy(t *testing.T) {
satisfyPrecommitConditionState.PreCommitMsgPool = NewPreCommitMsgPool()
for i := 0; i < 4; i++ {
msg := PreCommitMsg{
MsgID: "msg" + string(i),
MsgID: "msg" + strconv.Itoa(i),
StateID: StateID{"state1"},
SenderID: "user" + string(i),
SenderID: "user" + strconv.Itoa(i),
}
satisfyPrecommitConditionState.PreCommitMsgPool.Save(&msg)
}
Expand All @@ -65,9 +117,9 @@ func TestState_CheckPreCommitCondition_UnSatisfy(t *testing.T) {
unSatisfyPrecommitConditionState.PreCommitMsgPool = NewPreCommitMsgPool()
for i := 0; i < 2; i++ {
msg := PreCommitMsg{
MsgID: "msg" + string(i),
MsgID: "msg" + strconv.Itoa(i),
StateID: StateID{"state1"},
SenderID: "user" + string(i),
SenderID: "user" + strconv.Itoa(i),
}
unSatisfyPrecommitConditionState.PreCommitMsgPool.Save(&msg)
}
Expand All @@ -80,7 +132,7 @@ func setUpState() State {
reps := make([]Representative, 0)
for i := 0; i < 6; i++ {
reps = append(reps, Representative{
ID: "user" + string(i),
ID: "user" + strconv.Itoa(i),
})
}

Expand Down

0 comments on commit fb67178

Please sign in to comment.