func (s *XLSuite) TestMockLocalHostTcpCluster(c *C) { if VERBOSITY > 0 { fmt.Println("TEST_MOCK_LOCAL_HOST_TCP_CLUSTER") } var err error const K = 5 nodes, accs := MockLocalHostCluster(K) defer func() { for i := 0; i < K; i++ { if accs[i] != nil { accs[i].Close() } } }() for i := 0; i < K; i++ { c.Assert(nodes, Not(IsNil)) c.Assert(accs, Not(IsNil)) } nameSet := make(map[string]bool) names := make([]string, K) nodeIDs := make([]*xi.NodeID, K) for i := 0; i < K; i++ { names[i] = nodes[i].GetName() _, ok := nameSet[names[i]] c.Assert(ok, Equals, false) nameSet[names[i]] = true // XXX should also verify nodeIDs are unique nodeIDs[i] = nodes[i].GetNodeID() } ar, err := xo.NewCIDRAddrRange("127.0.0.0/8") c.Assert(err, Equals, nil) overlay, err := xo.NewIPOverlay("XO", ar, "tcp", 1.0) c.Assert(err, Equals, nil) _ = overlay accEndPoints := make([]*xt.TcpEndPoint, K) for i := 0; i < K; i++ { accEndPoints[i] = accs[i].GetEndPoint().(*xt.TcpEndPoint) c.Assert(accEndPoints[i], Not(IsNil)) c.Assert(nodes[i].SizeEndPoints(), Equals, 1) c.Assert(nodes[i].SizeAcceptors(), Equals, 1) c.Assert(nodes[i].SizeOverlays(), Equals, 1) c.Assert(overlay.Equal(nodes[i].GetOverlay(0)), Equals, true) } // XXX NEEDS CHECKING FROM HERE commsKeys := make([]*rsa.PublicKey, K) sigKeys := make([]*rsa.PublicKey, K) ctors := make([]*xt.TcpConnector, K) for i := 0; i < K; i++ { commsKeys[i] = nodes[i].GetCommsPublicKey() sigKeys[i] = nodes[i].GetSigPublicKey() ctors[i], err = xt.NewTcpConnector(accEndPoints[i]) c.Assert(err, Equals, nil) } //overlaySlice := []xo.OverlayI{overlay} // peers := make([]*Peer, K) for i := 0; i < K; i++ { //ctorSlice := []xt.ConnectorI{ctors[i]} //_ = ctorSlice //peers[i], err = NewPeer(names[i], nodeIDs[i], commsKeys[i], sigKeys[i], // overlaySlice, ctorSlice) //c.Assert(err, Equals, nil) } // Use the information collected to configure each node. for i := 0; i < K; i++ { //for j := 0; j < K; j++ { // if i != j { // ndx, err := nodes[i].AddPeer(peers[j]) // c.Assert(err, Equals, nil) // var expectedNdx int // if j < i { // expectedNdx = j // } else { // expectedNdx = j - 1 // } // c.Assert(ndx, Equals, expectedNdx) // } //} // GEEP c.Assert(nodes[i].SizeAcceptors(), Equals, 1) // XXX WRONG APPROACH - SizeConnectors() is a Peer // function, and in this case should return 1 for each peer. // c.Assert(nodes[i].SizeConnectors(),Equals, K-1) c.Assert(nodes[i].SizeEndPoints(), Equals, 1) c.Assert(nodes[i].SizeOverlays(), Equals, 1) c.Assert(nodes[i].SizePeers(), Equals, K-1) } // GEEP }
func MockLocalHostCluster(K int) (nodes []*Node, accs []*xt.TcpAcceptor) { rng := xr.MakeSimpleRNG() // Create K nodes, each with a NodeID, two RSA private keys (sig and // comms), and two RSA public keys. Each node creates a TcpAcceptor // running on 127.0.0.1 and a random (= system-supplied) port. names := make([]string, K) nodeIDs := make([]*xi.NodeID, K) for i := 0; i < K; i++ { // TODO: MAKE NAMES UNIQUE names[i] = rng.NextFileName(4) val := make([]byte, xu.SHA1_BIN_LEN) rng.NextBytes(val) nodeIDs[i], _ = xi.NewNodeID(val) } nodes = make([]*Node, K) accs = make([]*xt.TcpAcceptor, K) accEndPoints := make([]*xt.TcpEndPoint, K) for i := 0; i < K; i++ { lfs := "tmp/" + hex.EncodeToString(nodeIDs[i].Value()) nodes[i], _ = NewNew(names[i], nodeIDs[i], lfs) } // XXX We need this functionality in using code // defer func() { // for i := 0; i < K; i++ { // if accs[i] != nil { // accs[i].CloseAcc() // } // } // }() // Collect the nodeID, public keys, and listening address from each // node. // all nodes on the same overlay ar, _ := xo.NewCIDRAddrRange("127.0.0.0/8") overlay, _ := xo.NewIPOverlay("XO", ar, "tcp", 1.0) // add an endpoint to each node for i := 0; i < K; i++ { ep, _ := xt.NewTcpEndPoint("127.0.0.1:0") nodes[i].AddEndPoint(ep) nodes[i].OpenAcc() // XXX POSSIBLE ERRORS IGNORED accs[i] = nodes[i].GetAcceptor(0).(*xt.TcpAcceptor) accEndPoints[i] = accs[i].GetEndPoint().(*xt.TcpEndPoint) } ckPrivs := make([]*rsa.PublicKey, K) skPrivs := make([]*rsa.PublicKey, K) ctors := make([]*xt.TcpConnector, K) for i := 0; i < K; i++ { // we already have nodeIDs ckPrivs[i] = nodes[i].GetCommsPublicKey() skPrivs[i] = nodes[i].GetSigPublicKey() ctors[i], _ = xt.NewTcpConnector(accEndPoints[i]) } overlaySlice := []xo.OverlayI{overlay} peers := make([]*Peer, K) for i := 0; i < K; i++ { ctorSlice := []xt.ConnectorI{ctors[i]} _ = ctorSlice peers[i], _ = NewPeer(names[i], nodeIDs[i], ckPrivs[i], skPrivs[i], overlaySlice, ctorSlice) } // Use the information collected to configure each node. for i := 0; i < K; i++ { for j := 0; j < K; j++ { if i != j { nodes[i].AddPeer(peers[j]) } } } return }