From ff9f0c68b3eff7f91896da23170418572b956546 Mon Sep 17 00:00:00 2001 From: ak Date: Wed, 2 Jan 2019 15:07:17 -0800 Subject: [PATCH] newnode test --- internal/newnode/newnode.go | 9 ++++++--- internal/newnode/newnode_test.go | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/internal/newnode/newnode.go b/internal/newnode/newnode.go index 29a209b73..33ede62b4 100644 --- a/internal/newnode/newnode.go +++ b/internal/newnode/newnode.go @@ -1,6 +1,7 @@ package newnode import ( + "errors" "fmt" "os" "strconv" @@ -55,9 +56,9 @@ type registerResponseRandomNumber struct { } // ContactBeaconChain starts a newservice in the candidate node -func (node *NewNode) ContactBeaconChain(BCPeer p2p.Peer) { +func (node *NewNode) ContactBeaconChain(BCPeer p2p.Peer) error { go node.host.BindHandlerAndServe(node.NodeHandler) - node.requestBeaconChain(BCPeer) + return node.requestBeaconChain(BCPeer) } func (node NewNode) String() string { @@ -65,7 +66,7 @@ func (node NewNode) String() string { } // RequestBeaconChain requests beacon chain for identity data -func (node *NewNode) requestBeaconChain(BCPeer p2p.Peer) { +func (node *NewNode) requestBeaconChain(BCPeer p2p.Peer) (err error) { node.log.Info("connecting to beacon chain now ...") pubk, err := node.PubK.MarshalBinary() if err != nil { @@ -94,9 +95,11 @@ checkLoop: } } if !gotShardInfo { + err = errors.New("could not create connection") node.log.Crit("Could not get sharding info after 5 minutes") os.Exit(1) } + return } // ProcessShardInfo diff --git a/internal/newnode/newnode_test.go b/internal/newnode/newnode_test.go index 6d1ca6faa..f9c62b66e 100644 --- a/internal/newnode/newnode_test.go +++ b/internal/newnode/newnode_test.go @@ -1,6 +1,11 @@ package newnode -import "testing" +import ( + "testing" + + beaconchain "github.com/harmony-one/harmony/internal/beaconchain/libs" + "github.com/harmony-one/harmony/p2p" +) func TestNewNode(t *testing.T) { var ip, port string @@ -16,3 +21,19 @@ func TestNewNode(t *testing.T) { t.Error("new node setinfo initialized to true! (should be false)") } } + +func TestBeaconChainConnect(t *testing.T) { + var ip, beaconport, nodeport string + ip = "127.0.0.1" + beaconport = "8080" + nodeport = "8081" + nnode := New(ip, nodeport) + bc := beaconchain.New(1, ip, beaconport) + go bc.StartServer() + BCPeer := p2p.Peer{IP: ip, Port: beaconport} + err := nnode.ContactBeaconChain(BCPeer) + if err != nil { + t.Error("could not read from connection") + } + +}