github.com/turingchain2020/turingchain@v1.1.21/system/p2p/dht/manage/peerinfo_test.go (about)

     1  package manage
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/turingchain2020/turingchain/queue"
     8  	"github.com/turingchain2020/turingchain/types"
     9  	"github.com/libp2p/go-libp2p"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestPeerInfoManager(t *testing.T) {
    14  	h1, err := libp2p.New(context.Background())
    15  	require.Nil(t, err)
    16  	q := queue.New("test")
    17  	go sub(q.Client())
    18  	mgr := NewPeerInfoManager(context.Background(), h1, q.Client())
    19  	require.Nil(t, mgr.Fetch(h1.ID()))
    20  	require.Nil(t, mgr.FetchAll())
    21  	mgr.Refresh(&types.Peer{
    22  		Name: h1.ID().Pretty(),
    23  		Header: &types.Header{
    24  			Height: 888,
    25  		},
    26  	})
    27  	require.NotNil(t, mgr.Fetch(h1.ID()))
    28  	require.Equal(t, 1, len(mgr.FetchAll()))
    29  	require.Equal(t, int64(888), mgr.PeerHeight(h1.ID()))
    30  	mgr.prune()
    31  	require.Equal(t, int64(888), mgr.PeerHeight(h1.ID()))
    32  }
    33  
    34  func sub(cli queue.Client) {
    35  	cli.Sub("blockchain")
    36  	for range cli.Recv() {
    37  		cli.Reply(cli.NewMessage("", 0, &types.Header{Height: 10000}))
    38  	}
    39  }