github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/kad_rpc_test.go (about)

     1  package holochain
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	. "github.com/holochain/holochain-proto/hash"
     7  	peer "github.com/libp2p/go-libp2p-peer"
     8  	pstore "github.com/libp2p/go-libp2p-peerstore"
     9  	. "github.com/smartystreets/goconvey/convey"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestNodeFindLocal(t *testing.T) {
    15  	nodesCount := 3
    16  	mt := setupMultiNodeTesting(nodesCount)
    17  	defer mt.cleanupMultiNodeTesting()
    18  	nodes := mt.nodes
    19  	node0 := nodes[0].node
    20  	node1 := nodes[1].node
    21  	node2 := nodes[2].node
    22  	Convey("it should return empty record if not in routing table", t, func() {
    23  		pi := node0.FindLocal(node1.HashAddr)
    24  		So(fmt.Sprintf("%v", pi), ShouldEqual, fmt.Sprintf("%v", pstore.PeerInfo{}))
    25  	})
    26  
    27  	Convey("it should return peerinfo if in peerstore", t, func() {
    28  		connect(t, mt.ctx, nodes[0], nodes[1])
    29  		pi := node0.FindLocal(node1.HashAddr)
    30  		So(pi.ID.Pretty(), ShouldEqual, node1.HashAddr.Pretty())
    31  	})
    32  
    33  	Convey("it should return peerinfo if in Routing Table", t, func() {
    34  		node0.routingTable.Update(node2.HashAddr)
    35  		pi := node0.FindLocal(node2.HashAddr)
    36  		So(pi.ID.Pretty(), ShouldEqual, node2.HashAddr.Pretty())
    37  	})
    38  
    39  }
    40  
    41  func TestKademliaReceiver(t *testing.T) {
    42  	d, _, h := PrepareTestChain("test")
    43  	defer CleanupTestChain(h, d)
    44  
    45  	Convey("FIND_NODE_REQUEST should X", t, func() {
    46  	})
    47  
    48  }
    49  
    50  func TestNodeFindPeer(t *testing.T) {
    51  	// t.Skip("skipping test to debug another")
    52  	if testing.Short() {
    53  		t.SkipNow()
    54  	}
    55  
    56  	nodesCount := 6
    57  	mt := setupMultiNodeTesting(nodesCount)
    58  	defer mt.cleanupMultiNodeTesting()
    59  	nodes := mt.nodes
    60  
    61  	ctxT, cancel := context.WithTimeout(mt.ctx, time.Second*5)
    62  	defer cancel()
    63  
    64  	Convey("searching before connected should fail with empty routing table", t, func() {
    65  		_, err := nodes[0].node.FindPeer(ctxT, nodes[2].node.HashAddr)
    66  		So(err, ShouldEqual, ErrEmptyRoutingTable)
    67  	})
    68  
    69  	for i := 0; i < nodesCount-1; i++ {
    70  		connect(t, mt.ctx, nodes[i], nodes[i+1])
    71  	}
    72  
    73  	Convey("searching for unreachable node should fail with node not found", t, func() {
    74  		unknownPeer, _ := makePeer("unknown peer")
    75  		_, err := nodes[0].node.FindPeer(ctxT, unknownPeer)
    76  		So(err, ShouldEqual, ErrHashNotFound)
    77  	})
    78  
    79  	lastNode := nodes[nodesCount-1].node.HashAddr
    80  	Convey("searching for a node connected through others should succeed", t, func() {
    81  
    82  		pi, err := nodes[0].node.FindPeer(ctxT, lastNode)
    83  		So(err, ShouldBeNil)
    84  		So(pi.ID, ShouldEqual, lastNode)
    85  	})
    86  
    87  	Convey("findPeerSingle should return closer peers", t, func() {
    88  		c, err := nodes[0].node.findPeerSingle(mt.ctx, nodes[1].node.HashAddr, HashFromPeerID(lastNode))
    89  		So(err, ShouldBeNil)
    90  		So(len(c), ShouldEqual, 1)
    91  		So(c[0].ID, ShouldEqual, nodes[2].node.HashAddr)
    92  	})
    93  }
    94  
    95  func TestNodeBetterPeers(t *testing.T) {
    96  	nodesCount := 6
    97  	mt := setupMultiNodeTesting(nodesCount)
    98  	defer mt.cleanupMultiNodeTesting()
    99  	nodes := mt.nodes
   100  
   101  	ctx, cancel := context.WithTimeout(mt.ctx, time.Second*5)
   102  	defer cancel()
   103  
   104  	hash, _ := NewHash("QmS4bKx7zZt6qoX2om5M5ik3X2k4Fco2nFx82CDJ3iVKj2")
   105  
   106  	fullConnect(t, ctx, nodes, nodesCount)
   107  	Convey("nodes should agree on who's the closest to a hash", t, func() {
   108  		var closest peer.ID
   109  		for i, h := range nodes {
   110  			out := h.node.betterPeersForHash(&hash, h.nodeID, true, 3)
   111  			if out == nil {
   112  				out = []peer.ID{h.nodeID}
   113  			}
   114  			//fmt.Printf("%v thinks %v is closest\n", h.nodeID.Pretty()[2:4], out[0].Pretty()[2:4])
   115  
   116  			if i != 0 {
   117  				So(closest.Pretty(), ShouldEqual, out[0].Pretty())
   118  			} else {
   119  				closest = out[0]
   120  			}
   121  
   122  		}
   123  		//		fmt.Printf("%v", closest)
   124  	})
   125  }