Merge pull request #203 from harmony-one/bcrpc-tests

beaconchain rpc tests
pull/207/head
alajko 6 years ago committed by GitHub
commit fd77c17c83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 52
      internal/beaconchain/libs/beaconchain_test.go

@ -1,6 +1,24 @@
package beaconchain
import "testing"
import (
"reflect"
"strconv"
"testing"
"github.com/harmony-one/harmony/api/proto/bcconn"
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{leader1, leader2}
shardLeaderMap = map[int]*bcconn.NodeInfo{
0: leader1,
1: leader2,
}
)
func TestNewNode(t *testing.T) {
var ip, port string
@ -21,3 +39,35 @@ func TestNewNode(t *testing.T) {
t.Error("beacon chain number of shards not initialized to given number of desired shards")
}
}
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) {
var ip string
ip = "127.0.0.1"
beaconport := "8080"
numshards := 1
bc := New(numshards, ip, beaconport)
bc.Leaders = leaders
bc.rpcServer = beaconchain.NewServer(bc.GetShardLeaderMap)
bc.StartRPCServer()
port, _ := strconv.Atoi(beaconport)
bcClient := beaconchain.NewClient("127.0.0.1", strconv.Itoa(port+BeaconchainServicePortDiff))
response := bcClient.GetLeaders()
retleaders := response.GetLeaders()
if !(retleaders[0].GetIp() == leaders[0].Self.IP || retleaders[0].GetPort() == leaders[0].Self.Port || retleaders[1].GetPort() == leaders[1].Self.Port) {
t.Error("Fetch leaders response is not as expected")
}
}

Loading…
Cancel
Save