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

     1  package holochain
     2  
     3  import (
     4  	"fmt"
     5  	. "github.com/holochain/holochain-proto/hash"
     6  	peer "github.com/libp2p/go-libp2p-peer"
     7  	. "github.com/smartystreets/goconvey/convey"
     8  	"testing"
     9  	_ "time"
    10  )
    11  
    12  func TestGetClosestPeers(t *testing.T) {
    13  	nodesCount := 30
    14  	mt := setupMultiNodeTesting(nodesCount)
    15  	defer mt.cleanupMultiNodeTesting()
    16  	nodes := mt.nodes
    17  
    18  	randConnect(t, mt.ctx, nodes, nodesCount, 7, 4)
    19  
    20  	Convey("it should return a list of close peers", t, func() {
    21  		fooHash, _ := NewHash("QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHx")
    22  		//fooHash := HashFromPeerID(nodes[29].node.HashAddr)
    23  		peers, err := nodes[1].node.GetClosestPeers(mt.ctx, fooHash)
    24  		So(err, ShouldBeNil)
    25  
    26  		var out []peer.ID
    27  		for p := range peers {
    28  			out = append(out, p)
    29  		}
    30  
    31  		So(len(out), ShouldEqual, KValue)
    32  	})
    33  
    34  	starConnect(t, mt.ctx, nodes, nodesCount)
    35  
    36  	Convey("nodes should agree on who's the closest to a hash", t, func() {
    37  		hash, _ := NewHash("QmS4bKx7zZt6qoX2om5M5ik3X2k4Fco2nFx82CDJ3iVKj2")
    38  		var closest peer.ID
    39  		for i, h := range nodes {
    40  			peers, err := h.node.GetClosestPeers(mt.ctx, hash)
    41  			if err != nil {
    42  				fmt.Printf("%d--%v:  %v", i, h.nodeID.Pretty(), err)
    43  			} else {
    44  				var out []peer.ID
    45  				for p := range peers {
    46  					out = append(out, p)
    47  				}
    48  				//fmt.Printf("%v thinks %v,%v is closest\n", h.nodeID.Pretty()[2:4], out[0].Pretty()[2:4], out[1].Pretty()[2:4])
    49  
    50  				if i != 0 {
    51  					So(closest.Pretty(), ShouldEqual, out[0].Pretty())
    52  				} else {
    53  					closest = out[0]
    54  				}
    55  			}
    56  		}
    57  	})
    58  
    59  }