Add test for consensus leader/validator

pull/6/head
Rongjian Lan 6 years ago
parent 8151820190
commit 4419aa15df
  1. 34
      consensus/consensus_leader_test.go
  2. 30
      consensus/consensus_validator_test.go

@ -0,0 +1,34 @@
package consensus
import (
"testing"
"harmony-benchmark/p2p"
)
func TestConstructAnnounceMessage(test *testing.T) {
header := getBlockHeader()
leader := p2p.Peer{Ip: "1", Port:"2"}
validator := p2p.Peer{Ip: "3", Port:"5"}
consensus := NewConsensus("1", "2", []p2p.Peer{leader, validator}, leader)
consensus.blockHash = getBlockHash(make([]byte, 10))
msg, err := consensus.constructAnnounceMessage()
if err != nil {
test.Error("Annouce message is not constructed successfully")
}
if len(msg) != 1 + 1 + 1 + 4 + 32 + 2 + 4 + 64 + len(header) {
test.Errorf("Annouce message is not constructed in the correct size: %d", len(msg))
}
}
func TestConstructChallengeMessage(test *testing.T) {
leader := p2p.Peer{Ip: "1", Port:"2"}
validator := p2p.Peer{Ip: "3", Port:"5"}
consensus := NewConsensus("1", "2", []p2p.Peer{leader, validator}, leader)
consensus.blockHash = getBlockHash(make([]byte, 10))
msg := consensus.constructChallengeMessage()
if len(msg) != 1 + 1 + 1 + 4 + 32 + 2 + 33 + 33 + 32 + 64 {
test.Errorf("Annouce message is not constructed in the correct size: %d", len(msg))
}
}

@ -0,0 +1,30 @@
package consensus
import (
"testing"
"harmony-benchmark/p2p"
)
func TestConstructCommitMessage(test *testing.T) {
leader := p2p.Peer{Ip: "1", Port:"2"}
validator := p2p.Peer{Ip: "3", Port:"5"}
consensus := NewConsensus("1", "2", []p2p.Peer{leader, validator}, leader)
consensus.blockHash = getBlockHash(make([]byte, 10))
msg := consensus.constructCommitMessage()
if len(msg) != 1 + 1 + 1 + 4 + 32 + 2 + 33 + 64 {
test.Errorf("Commit message is not constructed in the correct size: %d", len(msg))
}
}
func TestConstructResponseMessage(test *testing.T) {
leader := p2p.Peer{Ip: "1", Port:"2"}
validator := p2p.Peer{Ip: "3", Port:"5"}
consensus := NewConsensus("1", "2", []p2p.Peer{leader, validator}, leader)
consensus.blockHash = getBlockHash(make([]byte, 10))
msg := consensus.constructResponseMessage()
if len(msg) != 1 + 1 + 1 + 4 + 32 + 2 + 32 + 64 {
test.Errorf("Response message is not constructed in the correct size: %d", len(msg))
}
}
Loading…
Cancel
Save