beaconchain tests

pull/203/head
ak 6 years ago
parent 9c902e808d
commit 411328709b
  1. 37
      internal/beaconchain/libs/beaconchain_test.go

@ -5,8 +5,20 @@ import (
"strconv" "strconv"
"testing" "testing"
proto "github.com/harmony-one/harmony/api/beaconchain" "github.com/harmony-one/harmony/api/proto/bcconn"
beaconchain "github.com/harmony-one/harmony/internal/beaconchain/rpc" beaconchain "github.com/harmony-one/harmony/internal/beaconchain/rpc"
"github.com/harmony-one/harmony/p2p"
)
var (
leader1 = &bcconn.NodeInfo{Self: p2p.Peer{IP: "127.0.0.1", Port: "1"}}
leader2 = &bcconn.NodeInfo{Self: p2p.Peer{IP: "127.0.0.1", Port: "2"}}
leaders = []*bcconn.NodeInfo{&bcconn.NodeInfo{Self: p2p.Peer{IP: "127.0.0.1", Port: "1"}},
&bcconn.NodeInfo{Self: p2p.Peer{IP: "127.0.0.1", Port: "2"}}}
shardLeaderMap = map[int]*bcconn.NodeInfo{
0: leader1,
1: leader2,
}
) )
func TestNewNode(t *testing.T) { func TestNewNode(t *testing.T) {
@ -29,19 +41,34 @@ func TestNewNode(t *testing.T) {
} }
} }
func TestShardLeaderMap(t *testing.T) {
var ip string
ip = "127.0.0.1"
beaconport := "8080"
numshards := 1
bc := New(numshards, ip, beaconport)
bc.Leaders = leaders
if !reflect.DeepEqual(bc.GetShardLeaderMap(), shardLeaderMap) {
t.Error("The function GetShardLeaderMap doesn't work well")
}
}
func TestFetchLeaders(t *testing.T) { func TestFetchLeaders(t *testing.T) {
var ip string var ip string
ip = "127.0.0.1" ip = "127.0.0.1"
beaconport := "8080" beaconport := "8080"
numshards := 1 numshards := 1
bc := New(numshards, ip, beaconport) bc := New(numshards, ip, beaconport)
bc.SupportRPC() bc.Leaders = leaders
bc.rpcServer = beaconchain.NewServer(bc.GetShardLeaderMap)
bc.StartRPCServer()
port, _ := strconv.Atoi(beaconport) port, _ := strconv.Atoi(beaconport)
bcClient := beaconchain.NewClient("127.0.0.1", strconv.Itoa(port+BeaconchainServicePortDiff)) bcClient := beaconchain.NewClient("127.0.0.1", strconv.Itoa(port+BeaconchainServicePortDiff))
response := bcClient.GetLeaders() response := bcClient.GetLeaders()
expresponse := &proto.FetchLeadersResponse{} retleaders := response.GetLeaders()
if !reflect.DeepEqual(response, expresponse) { if !(retleaders[0].GetIp() == leaders[0].Self.IP || retleaders[0].GetPort() == leaders[0].Self.Port || retleaders[1].GetPort() == leaders[1].Self.Port) {
t.Error("was expexting a empty leaders array") t.Error("Fetch leaders response is not as expected")
} }
} }

Loading…
Cancel
Save