github.com/aergoio/aergo@v1.3.1/p2p/actorwork_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/types"
    11  	"github.com/aergoio/etcd/raft/raftpb"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/aergoio/aergo-lib/log"
    16  	"github.com/aergoio/aergo/message"
    17  	"github.com/aergoio/aergo/p2p/p2pmock"
    18  	"github.com/aergoio/aergo/pkg/component"
    19  	"github.com/golang/mock/gomock"
    20  )
    21  
    22  func TestP2P_GetAddresses(t *testing.T) {
    23  	ctrl := gomock.NewController(t)
    24  	defer ctrl.Finish()
    25  
    26  	dummyPeerMeta := p2pcommon.PeerMeta{ID:dummyPeerID, IPAddress:"127.0.0.1", Port:7846}
    27  
    28  	type args struct {
    29  		peerID types.PeerID
    30  		size   uint32
    31  	}
    32  	tests := []struct {
    33  		name    string
    34  		args    args
    35  		hasPeer bool
    36  
    37  		wantSend int
    38  		want     bool
    39  	}{
    40  		{"TNormal", args{dummyPeerID, 10}, true, 1,true},
    41  		{"TNoPeer", args{dummyPeerID, 10}, false, 0,false},
    42  	}
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			mockPM := p2pmock.NewMockPeerManager(ctrl)
    46  			mockMF := p2pmock.NewMockMoFactory(ctrl)
    47  			mockPeer := (*p2pmock.MockRemotePeer)(nil)
    48  			if tt.hasPeer {
    49  				mockPeer = p2pmock.NewMockRemotePeer(ctrl)
    50  				mockPeer.EXPECT().SendMessage(gomock.Any()).Times(1)
    51  			}
    52  			p2pmock.NewMockRemotePeer(ctrl)
    53  			mockPM.EXPECT().GetPeer(dummyPeerID).Return(mockPeer, tt.hasPeer).Times(1)
    54  			mockPM.EXPECT().SelfMeta().Return(dummyPeerMeta).Times(tt.wantSend).MaxTimes(tt.wantSend)
    55  			mockMF.EXPECT().NewMsgRequestOrder(true, p2pcommon.AddressesRequest, gomock.AssignableToTypeOf(&types.AddressesRequest{})).Times(tt.wantSend)
    56  			p2ps := &P2P{
    57  				pm:mockPM, mf:mockMF,
    58  			}
    59  			p2ps.BaseComponent = component.NewBaseComponent(message.P2PSvc, p2ps, log.NewLogger("p2p.test"))
    60  
    61  			if got := p2ps.GetAddresses(tt.args.peerID, tt.args.size); got != tt.want {
    62  				t.Errorf("P2P.GetAddresses() = %v, want %v", got, tt.want)
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  func TestP2P_GetBlocksChunk(t *testing.T) {
    69  	ctrl := gomock.NewController(t)
    70  	defer ctrl.Finish()
    71  
    72  	sampleMsg := &message.GetBlockChunks{GetBlockInfos: message.GetBlockInfos{ToWhom: samplePeerID}, TTL: time.Minute}
    73  
    74  	// fail: cancel create receiver and return fail instantly
    75  	mockPM := p2pmock.NewMockPeerManager(ctrl)
    76  	mockPM.EXPECT().GetPeer(gomock.Any()).Return(nil, false)
    77  	mockCtx := p2pmock.NewMockContext(ctrl)
    78  	mockCtx.EXPECT().Respond(gomock.Any()).Times(1)
    79  	ps := &P2P{}
    80  	ps.BaseComponent = component.NewBaseComponent(message.P2PSvc, ps, log.NewLogger("p2p"))
    81  	ps.pm = mockPM
    82  
    83  	ps.GetBlocksChunk(mockCtx, sampleMsg)
    84  
    85  	// success case
    86  	mockPeer := p2pmock.NewMockRemotePeer(ctrl)
    87  	mockMF := p2pmock.NewMockMoFactory(ctrl)
    88  
    89  	mockPM.EXPECT().GetPeer(gomock.Any()).Return(mockPeer, true)
    90  	mockCtx.EXPECT().Respond(gomock.Any()).Times(0)
    91  	mockPeer.EXPECT().MF().Return(mockMF)
    92  	mockPeer.EXPECT().SendMessage(gomock.Any()).Times(1)
    93  
    94  	dummyMo = createDummyMo(ctrl)
    95  	mockMF.EXPECT().NewMsgBlockRequestOrder(gomock.Any(), p2pcommon.GetBlocksRequest, gomock.Any()).
    96  		Return(dummyMo).Times(1)
    97  
    98  	ps = &P2P{}
    99  	ps.BaseComponent = component.NewBaseComponent(message.P2PSvc, ps, log.NewLogger("p2p"))
   100  	ps.pm = mockPM
   101  	ps.GetBlocksChunk(mockCtx, sampleMsg)
   102  
   103  	//mockCtx.AssertNotCalled(t, "Respond", mock.Anything)
   104  	// verify that receiver start working.
   105  	//mockMF.AssertNumberOfCalls(t, "newMsgBlockRequestOrder", 1)
   106  	//mockPeer.AssertNumberOfCalls(t, "sendMessage", 1)
   107  }
   108  
   109  func TestP2P_GetBlockHashByNo(t *testing.T) {
   110  	ctrl := gomock.NewController(t)
   111  	defer ctrl.Finish()
   112  
   113  
   114  	sampleMsg := &message.GetHashByNo{ToWhom: samplePeerID, BlockNo: uint64(111111)}
   115  
   116  	// fail: cancel create receiver and return fail instantly
   117  	mockPM := p2pmock.NewMockPeerManager(ctrl)
   118  	mockCtx := p2pmock.NewMockContext(ctrl)
   119  	mockPM.EXPECT().GetPeer(gomock.Any()).Return(nil, false)
   120  	mockCtx.EXPECT().Respond(gomock.Any()).Times(1)
   121  	ps := &P2P{}
   122  	ps.BaseComponent = component.NewBaseComponent(message.P2PSvc, ps, log.NewLogger("p2p"))
   123  	ps.pm = mockPM
   124  
   125  	ps.GetBlockHashByNo(mockCtx, sampleMsg)
   126  
   127  	// success case
   128  	mockPM = p2pmock.NewMockPeerManager(ctrl)
   129  	mockCtx = p2pmock.NewMockContext(ctrl)
   130  	mockPeer := p2pmock.NewMockRemotePeer(ctrl)
   131  	mockMF := p2pmock.NewMockMoFactory(ctrl)
   132  
   133  	mockCtx.EXPECT().Respond(gomock.Any()).Times(0)
   134  	mockPM.EXPECT().GetPeer(gomock.Any()).Return(mockPeer, true)
   135  	mockPeer.EXPECT().MF().Return(mockMF)
   136  	mockPeer.EXPECT().SendMessage(gomock.Any()).Times(1)
   137  
   138  	dummyMo = createDummyMo(ctrl)
   139  	mockMF.EXPECT().NewMsgBlockRequestOrder(gomock.Any(), p2pcommon.GetHashByNoRequest, gomock.Any()).
   140  		Return(dummyMo).Times(1)
   141  
   142  	ps = &P2P{}
   143  	ps.BaseComponent = component.NewBaseComponent(message.P2PSvc, ps, log.NewLogger("p2p"))
   144  	ps.pm = mockPM
   145  	ps.GetBlockHashByNo(mockCtx, sampleMsg)
   146  }
   147  
   148  func TestP2P_SendRaftMessage(t *testing.T) {
   149  	ctrl := gomock.NewController(t)
   150  	defer ctrl.Finish()
   151  
   152  	type args struct {
   153  		pid 	types.PeerID
   154  		body    interface{}
   155  	}
   156  	tests := []struct {
   157  		name   string
   158  		args   args
   159  
   160  		wantErr bool
   161  		wantReport bool
   162  	}{
   163  		{"TSucc", args{samplePeerID, raftpb.Message{Type:raftpb.MsgVote}}, false, false },
   164  		{"TNoPeer", args{dummyPeerID, raftpb.Message{Type:raftpb.MsgVote}}, true, true},
   165  		{"TWrongBody", args{samplePeerID, types.Status{}}, true, false},
   166  		//{"TNilBody", args{samplePeerID, &types.Status{}}, true },
   167  
   168  	}
   169  	for _, tt := range tests {
   170  		t.Run(tt.name, func(t *testing.T) {
   171  			sentCnt := 1
   172  			if tt.wantErr {
   173  				sentCnt = 0
   174  			}
   175  			dummyMo = createDummyMo(ctrl)
   176  
   177  			mockCtx := p2pmock.NewMockContext(ctrl)
   178  			mockPM := p2pmock.NewMockPeerManager(ctrl)
   179  			mockPeer := p2pmock.NewMockRemotePeer(ctrl)
   180  			mockMF := p2pmock.NewMockMoFactory(ctrl)
   181  			mockConsAcc := p2pmock.NewMockConsensusAccessor(ctrl)
   182  			mockRaftAcc := p2pmock.NewMockAergoRaftAccessor(ctrl)
   183  			if tt.wantReport {
   184  				mockConsAcc.EXPECT().RaftAccessor().Return(mockRaftAcc)
   185  				mockRaftAcc.EXPECT().ReportUnreachable(tt.args.pid)
   186  			}
   187  
   188  			mockMF.EXPECT().NewRaftMsgOrder(raftpb.MsgVote,gomock.Any() ).Return(dummyMo).MaxTimes(1)
   189  			mockPM.EXPECT().GetPeer(gomock.Eq(samplePeerID)).Return(mockPeer, true).MaxTimes(1)
   190  			mockPM.EXPECT().GetPeer(gomock.Not(samplePeerID)).Return(nil, false).MaxTimes(1)
   191  			mockPeer.EXPECT().SendMessage(gomock.Any()).Times(sentCnt)
   192  			ps := &P2P{pm:mockPM, mf:mockMF, consacc:mockConsAcc}
   193  			ps.BaseComponent = component.NewBaseComponent(message.P2PSvc, ps, log.NewLogger("p2p"))
   194  
   195  			ps.SendRaftMessage(mockCtx, &message.SendRaft{ToWhom:tt.args.pid, Body:tt.args.body})
   196  
   197  		})
   198  	}
   199  }