github.com/aergoio/aergo@v1.3.1/p2p/peerfinder_test.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2p
     7  
     8  import (
     9  	"github.com/aergoio/aergo/p2p/p2pcommon"
    10  	"github.com/aergoio/aergo/p2p/p2pmock"
    11  	"github.com/aergoio/aergo/p2p/p2putil"
    12  	"github.com/aergoio/aergo/types"
    13  	"github.com/golang/mock/gomock"
    14  	"github.com/libp2p/go-libp2p-core/crypto"
    15  	"reflect"
    16  	"testing"
    17  )
    18  
    19  const desigCnt = 10
    20  
    21  var (
    22  	desigIDs     []types.PeerID
    23  	desigPeers   []p2pcommon.PeerMeta
    24  	desigPeerMap = make(map[types.PeerID]p2pcommon.PeerMeta)
    25  
    26  	unknownIDs   []types.PeerID
    27  	unknownPeers []p2pcommon.PeerMeta
    28  )
    29  
    30  func init() {
    31  	desigIDs = make([]types.PeerID, desigCnt)
    32  	desigPeers = make([]p2pcommon.PeerMeta, desigCnt)
    33  	for i := 0; i < desigCnt; i++ {
    34  		priv, _, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
    35  		pid, _ := types.IDFromPrivateKey(priv)
    36  		desigIDs[i] = pid
    37  		desigPeers[i] = p2pcommon.PeerMeta{ID: pid, Designated: true}
    38  		desigPeerMap[desigIDs[i]] = desigPeers[i]
    39  	}
    40  	unknownIDs = make([]types.PeerID, desigCnt)
    41  	unknownPeers = make([]p2pcommon.PeerMeta, desigCnt)
    42  	for i := 0; i < desigCnt; i++ {
    43  		priv, _, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
    44  		pid, _ := types.IDFromPrivateKey(priv)
    45  		unknownIDs[i] = pid
    46  		unknownPeers[i] = p2pcommon.PeerMeta{ID: pid, Designated: false}
    47  	}
    48  }
    49  func createDummyPM() *peerManager {
    50  	dummyPM := &peerManager{designatedPeers: desigPeerMap,
    51  		remotePeers:  make(map[types.PeerID]p2pcommon.RemotePeer),
    52  		waitingPeers: make(map[types.PeerID]*p2pcommon.WaitingPeer, 10),
    53  	}
    54  	return dummyPM
    55  }
    56  
    57  func TestNewPeerFinder(t *testing.T) {
    58  	ctrl := gomock.NewController(t)
    59  	defer ctrl.Finish()
    60  
    61  	type args struct {
    62  		useDiscover bool
    63  		usePolaris  bool
    64  	}
    65  	tests := []struct {
    66  		name string
    67  		args args
    68  		want p2pcommon.PeerFinder
    69  	}{
    70  		{"Tstatic", args{false, false}, &staticPeerFinder{}},
    71  		{"TstaticWPolaris", args{false, true}, &staticPeerFinder{}},
    72  		{"Tdyn", args{true, false}, &dynamicPeerFinder{}},
    73  		{"TdynWPolaris", args{true, true}, &dynamicPeerFinder{}},
    74  	}
    75  	for _, tt := range tests {
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			dummyPM := createDummyPM()
    78  			mockActor := p2pmock.NewMockActorService(ctrl)
    79  			got := NewPeerFinder(logger, dummyPM, mockActor, 10, tt.args.useDiscover, tt.args.usePolaris)
    80  			if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
    81  				t.Errorf("NewPeerFinder() = %v, want %v", reflect.TypeOf(got), reflect.TypeOf(tt.want))
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  func Test_dynamicPeerFinder_OnPeerDisconnect(t *testing.T) {
    88  	ctrl := gomock.NewController(t)
    89  	defer ctrl.Finish()
    90  
    91  	type args struct {
    92  		preConnected []types.PeerID
    93  		inMeta       p2pcommon.PeerMeta
    94  	}
    95  	tests := []struct {
    96  		name      string
    97  		args      args
    98  		wantCount int
    99  	}{
   100  		{"TDesignatedPeer", args{desigIDs, desigPeers[0]}, 1},
   101  		{"TNonPeer", args{unknownIDs, unknownPeers[0]}, 0},
   102  	}
   103  	for _, tt := range tests {
   104  		t.Run(tt.name, func(t *testing.T) {
   105  			dummyPM := createDummyPM()
   106  			mockActor := p2pmock.NewMockActorService(ctrl)
   107  			mockPeer := p2pmock.NewMockRemotePeer(ctrl)
   108  			mockPeer.EXPECT().ID().Return(tt.args.inMeta.ID).AnyTimes()
   109  			mockPeer.EXPECT().Meta().Return(tt.args.inMeta).AnyTimes()
   110  			mockPeer.EXPECT().Name().Return(p2putil.ShortMetaForm(tt.args.inMeta)).AnyTimes()
   111  
   112  			dp := NewPeerFinder(logger, dummyPM, mockActor, 10, true, false).(*dynamicPeerFinder)
   113  			for _, id := range tt.args.preConnected {
   114  				dummyPM.remotePeers[id] = &remotePeerImpl{}
   115  				dp.OnPeerConnect(id)
   116  			}
   117  			statCnt := len(dp.qStats)
   118  			dp.OnPeerDisconnect(mockPeer)
   119  
   120  			if statCnt-1 != len(dp.qStats) {
   121  				t.Errorf("count of query peers was not decreaded %v, want %v", len(dp.qStats), statCnt)
   122  			}
   123  		})
   124  	}
   125  }
   126  
   127  func Test_dynamicPeerFinder_OnPeerConnect(t *testing.T) {
   128  	ctrl := gomock.NewController(t)
   129  	defer ctrl.Finish()
   130  
   131  	type args struct {
   132  		preConnected []types.PeerID
   133  		inMeta       p2pcommon.PeerMeta
   134  	}
   135  	tests := []struct {
   136  		name          string
   137  		args          args
   138  		wantStatCount int
   139  	}{
   140  		{"TDesigPeer", args{desigIDs, desigPeers[0]}, 1},
   141  		{"TNonPeer", args{unknownIDs, unknownPeers[0]}, 1},
   142  	}
   143  	for _, tt := range tests {
   144  		t.Run(tt.name, func(t *testing.T) {
   145  			dummyPM := createDummyPM()
   146  			mockActor := p2pmock.NewMockActorService(ctrl)
   147  			mockPeer := p2pmock.NewMockRemotePeer(ctrl)
   148  			mockPeer.EXPECT().ID().Return(tt.args.inMeta.ID).AnyTimes()
   149  			mockPeer.EXPECT().Meta().Return(tt.args.inMeta).AnyTimes()
   150  			mockPeer.EXPECT().Name().Return(p2putil.ShortMetaForm(tt.args.inMeta)).AnyTimes()
   151  
   152  			dp := NewPeerFinder(logger, dummyPM, mockActor, 10, true, false).(*dynamicPeerFinder)
   153  
   154  			dp.OnPeerConnect(tt.args.inMeta.ID)
   155  
   156  			if len(dp.qStats) != tt.wantStatCount {
   157  				t.Errorf("count of query peers was not decreaded %v, want %v", len(dp.qStats),  tt.wantStatCount)
   158  			} else {
   159  				if _, exist := dp.qStats[tt.args.inMeta.ID] ; !exist {
   160  					t.Errorf("peer query for pid %v missing, want exists", p2putil.ShortForm(tt.args.inMeta.ID))
   161  				}
   162  			}
   163  		})
   164  	}
   165  }