github.com/turingchain2020/turingchain@v1.1.21/system/p2p/dht/manage/conns_test.go (about) 1 package manage 2 3 import ( 4 "context" 5 "fmt" 6 "io" 7 "sort" 8 "testing" 9 "time" 10 11 p2pty "github.com/turingchain2020/turingchain/system/p2p/dht/types" 12 "github.com/turingchain2020/turingchain/types" 13 "github.com/libp2p/go-libp2p" 14 bhost "github.com/libp2p/go-libp2p-blankhost" 15 "github.com/libp2p/go-libp2p-core/metrics" 16 "github.com/libp2p/go-libp2p-core/peer" 17 "github.com/libp2p/go-libp2p-core/protocol" 18 dht "github.com/libp2p/go-libp2p-kad-dht" 19 swarmt "github.com/libp2p/go-libp2p-swarm/testing" 20 "github.com/multiformats/go-multiaddr" 21 22 "github.com/libp2p/go-libp2p-core/network" 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 ) 26 27 type testConn struct { 28 io.Closer 29 network.ConnSecurity 30 network.ConnMultiaddrs 31 stat network.Stat 32 } 33 34 func (t testConn) ID() string { 35 return "" 36 } 37 38 // NewStream constructs a new Stream over this conn. 39 func (t testConn) NewStream() (network.Stream, error) { return nil, nil } 40 41 // GetStreams returns all open streams over this conn. 42 func (t testConn) GetStreams() []network.Stream { return nil } 43 44 // Stat stores metadata pertaining to this conn. 45 func (t testConn) Stat() network.Stat { 46 return t.stat 47 } 48 49 func newtestConn(stat network.Stat) network.Conn { 50 return testConn{stat: stat} 51 52 } 53 func Test_SortConn(t *testing.T) { 54 var testconn conns 55 56 var s1, s2, s3 network.Stat 57 s1.Opened = time.Now().Add(time.Second * 10) 58 s2.Opened = time.Now().Add(time.Second * 15) 59 s3.Opened = time.Now().Add(time.Minute) 60 c1 := newtestConn(s1) 61 c2 := newtestConn(s2) 62 c3 := newtestConn(s3) 63 64 testconn = append(testconn, c1, c2, c3) 65 sort.Sort(testconn) 66 require.Equal(t, testconn[0], c3) 67 require.Equal(t, testconn[2], c1) 68 } 69 70 func TestConnManager(t *testing.T) { 71 m1, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 13666)) 72 require.Nil(t, err) 73 h1, err := libp2p.New(context.Background(), libp2p.ListenAddrs(m1)) 74 require.Nil(t, err) 75 m2, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 13777)) 76 require.Nil(t, err) 77 h2, err := libp2p.New(context.Background(), libp2p.ListenAddrs(m2)) 78 require.Nil(t, err) 79 80 addr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/13666/p2p/%s", h1.ID().Pretty())) 81 peerInfo, _ := peer.AddrInfoFromP2pAddr(addr) 82 err = h2.Connect(context.Background(), *peerInfo) 83 require.Nil(t, err) 84 85 kademliaDHT, err := dht.New(context.Background(), h1) 86 require.Nil(t, err) 87 bandwidthTracker := metrics.NewBandwidthCounter() 88 protocolID := protocol.ID("test") 89 bandwidthTracker.LogSentMessageStream(1024, protocolID, h2.ID()) 90 bandwidthTracker.LogRecvMessageStream(2048, protocolID, h2.ID()) 91 subCfg := &p2pty.P2PSubConfig{} 92 mgr := NewConnManager(context.Background(), h1, kademliaDHT.RoutingTable(), bandwidthTracker, subCfg) 93 info := mgr.BandTrackerByProtocol() 94 require.NotNil(t, info) 95 h1.Peerstore().RecordLatency(h2.ID(), time.Second/100) 96 mgr.printMonitorInfo() 97 mgr.procConnections() 98 kademliaDHT.RoutingTable().Update(h2.ID()) 99 peers := mgr.FetchNearestPeers(1) 100 require.NotNil(t, peers) 101 require.Equal(t, h2.ID(), peers[0]) 102 103 require.Equal(t, 1, int(mgr.CheckDirection(h2.ID()))) 104 require.Equal(t, 1, len(mgr.InBounds())) 105 require.Equal(t, 0, len(mgr.OutBounds())) 106 107 require.False(t, mgr.IsNeighbors(h2.ID())) 108 mgr.AddNeighbors(&peer.AddrInfo{ID: h2.ID()}) 109 require.True(t, mgr.IsNeighbors(h2.ID())) 110 } 111 112 func Test_ConnManager(t *testing.T) { 113 ctx, cancel := context.WithCancel(context.Background()) 114 defer cancel() 115 netw := swarmt.GenSwarm(t, ctx) 116 h := bhost.NewBlankHost(netw) 117 cfg := types.NewTuringchainConfig(types.ReadFile("../../../../cmd/turingchain/turingchain.test.toml")) 118 mcfg := &p2pty.P2PSubConfig{} 119 types.MustDecode(cfg.GetSubConfig().P2P["dht"], mcfg) 120 bandwidthTracker := metrics.NewBandwidthCounter() 121 cmm := NewConnManager(ctx, h, nil, bandwidthTracker, mcfg) 122 assert.NotNil(t, cmm) 123 124 ratestr := cmm.RateCalculate(1024) 125 t.Log("rate", ratestr) 126 netw2 := swarmt.GenSwarm(t, ctx) 127 h2 := bhost.NewBlankHost(netw2) 128 h2info := peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()} 129 cmm.AddNeighbors(&h2info) 130 assert.False(t, cmm.IsNeighbors(h.ID())) 131 assert.True(t, cmm.IsNeighbors(h2.ID())) 132 in, out := cmm.BoundSize() 133 assert.Equal(t, 0, in+out) 134 h.Connect(ctx, h2info) 135 _, outSize := cmm.BoundSize() 136 assert.Equal(t, 1, outSize) 137 assert.Equal(t, 1, len(cmm.FetchConnPeers())) 138 139 direction := cmm.CheckDirection(h2info.ID) 140 assert.Equal(t, network.DirOutbound, direction) 141 inSize, outSize := cmm.BoundSize() 142 assert.Equal(t, 0, inSize) 143 assert.Equal(t, len(cmm.InBounds()), inSize) 144 assert.Equal(t, len(cmm.OutBounds()), outSize) 145 cmm.GetNetRate() 146 cmm.BandTrackerByProtocol() 147 }