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

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package subproto
     7  
     8  import (
     9  	"github.com/aergoio/aergo-lib/log"
    10  	"github.com/aergoio/aergo/internal/enc"
    11  	"github.com/aergoio/aergo/p2p/p2pcommon"
    12  	"github.com/aergoio/aergo/p2p/p2pmock"
    13  	"github.com/aergoio/aergo/types"
    14  	"github.com/golang/mock/gomock"
    15  	"github.com/golang/protobuf/proto"
    16  	"testing"
    17  )
    18  
    19  func Test_blockProducedNoticeHandler_handle(t *testing.T) {
    20  	ctrl := gomock.NewController(t)
    21  	defer ctrl.Finish()
    22  
    23  	logger := log.NewLogger("test.subproto")
    24  	dummyBlockHash, _ := enc.ToBytes("v6zbuQ4aVSdbTwQhaiZGp5pcL5uL55X3kt2wfxor5W6")
    25  	var dummyPeerID, _ = types.IDB58Decode("16Uiu2HAmN5YU8V2LnTy9neuuJCLNsxLnd5xVSRZqkjvZUHS3mLoD")
    26  
    27  	dummyBlock := &types.Block{Hash: dummyBlockHash,
    28  		Header: &types.BlockHeader{}, Body: &types.BlockBody{}}
    29  	wrongBlock := &types.Block{Hash: nil,
    30  		Header: &types.BlockHeader{}, Body: &types.BlockBody{}}
    31  	type args struct {
    32  		msg     p2pcommon.Message
    33  		msgBody proto.Message
    34  	}
    35  	tests := []struct {
    36  		name       string
    37  		cached     bool
    38  		payloadBlk *types.Block
    39  
    40  		syncmanagerCallCnt int
    41  	}{
    42  		// 1. normal case.
    43  		{"TSucc", false, dummyBlock, 1},
    44  		// 2. wrong notice (block data is missing)
    45  		{"TW1", false, nil, 0},
    46  		// 2. wrong notice1 (invalid block data)
    47  		{"TW2", false, wrongBlock, 0},
    48  	}
    49  	for _, tt := range tests {
    50  		t.Run(tt.name, func(t *testing.T) {
    51  			mockPM := p2pmock.NewMockPeerManager(ctrl)
    52  			mockPeer := p2pmock.NewMockRemotePeer(ctrl)
    53  			mockActor := p2pmock.NewMockActorService(ctrl)
    54  			mockPeer.EXPECT().ID().Return(dummyPeerID).AnyTimes()
    55  			mockPeer.EXPECT().Name().Return("16..aadecf@1").AnyTimes()
    56  			mockPeer.EXPECT().UpdateLastNotice(dummyBlockHash, gomock.Any()).Times(tt.syncmanagerCallCnt)
    57  			mockCA := p2pmock.NewMockChainAccessor(ctrl)
    58  			mockActor.EXPECT().GetChainAccessor().Return(mockCA).MaxTimes(1)
    59  
    60  			mockSM := p2pmock.NewMockSyncManager(ctrl)
    61  			mockSM.EXPECT().HandleBlockProducedNotice(gomock.Any(), gomock.AssignableToTypeOf(&types.Block{})).Times(tt.syncmanagerCallCnt)
    62  
    63  			dummyMsg :=&testMessage{id:p2pcommon.NewMsgID(), subProtocol: p2pcommon.BlockProducedNotice}
    64  			body := &types.BlockProducedNotice{Block: tt.payloadBlk}
    65  			h := NewBlockProducedNoticeHandler(mockPM, mockPeer, logger, mockActor, mockSM)
    66  			h.Handle(dummyMsg, body)
    67  		})
    68  	}
    69  }