github.com/aergoio/aergo@v1.3.1/p2p/hashbynoreceiver_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  	"testing"
    11  	"time"
    12  
    13  	"github.com/aergoio/aergo/message"
    14  	"github.com/aergoio/aergo/p2p/p2pmock"
    15  	"github.com/aergoio/aergo/types"
    16  	"github.com/golang/mock/gomock"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestBlockHashByNoReceiver_StartGet(t *testing.T) {
    21  	ctrl := gomock.NewController(t)
    22  	defer ctrl.Finish()
    23  
    24  	inputNo := types.BlockNo(2222)
    25  	tests := []struct {
    26  		name  string
    27  		input types.BlockNo
    28  		ttl   time.Duration
    29  	}{
    30  		{"TSimple", inputNo, time.Millisecond * 10},
    31  		// TODO: test cases
    32  	}
    33  	for _, test := range tests {
    34  		t.Run(test.name, func(t *testing.T) {
    35  			//mockContext := new(mockContext)
    36  			mockActor := p2pmock.NewMockActorService(ctrl)
    37  
    38  			mockMo := createDummyMo(ctrl)
    39  			mockMF := p2pmock.NewMockMoFactory(ctrl)
    40  			mockMF.EXPECT().NewMsgBlockRequestOrder(gomock.Any(), gomock.Any(), gomock.Any()).Return(mockMo)
    41  			mockPeer := p2pmock.NewMockRemotePeer(ctrl)
    42  			mockPeer.EXPECT().MF().Return(mockMF)
    43  			mockPeer.EXPECT().SendMessage(mockMo).Times(1)
    44  
    45  			expire := time.Now().Add(test.ttl)
    46  			br := NewBlockHashByNoReceiver(mockActor, mockPeer, 0, test.input, test.ttl)
    47  
    48  			br.StartGet()
    49  
    50  			assert.Equal(t, test.input, br.blockNo)
    51  			assert.False(t, expire.After(br.timeout))
    52  
    53  			// getBlock must be sent
    54  		})
    55  	}
    56  }
    57  
    58  func TestBlockHashByNoReceiver_ReceiveResp(t *testing.T) {
    59  	ctrl := gomock.NewController(t)
    60  	defer ctrl.Finish()
    61  
    62  	seqNo := uint64(33)
    63  	blkNo := types.BlockNo(2222)
    64  	blkHash := dummyBlockHash
    65  	tests := []struct {
    66  		name        string
    67  		input       types.BlockNo
    68  		ttl         time.Duration
    69  		blkInterval time.Duration
    70  		blkRsp      []byte
    71  		rspStatus   types.ResultStatus
    72  
    73  		// to verify
    74  		consumed  int
    75  		sentResp  int
    76  		respError bool
    77  	}{
    78  		{"TSingleResp", blkNo, time.Minute, 0, blkHash, types.ResultStatus_OK, 1, 1, false},
    79  		// Fail1 remote err
    80  		{"TRemoteFail", blkNo, time.Minute, 0, nil, types.ResultStatus_INTERNAL, 1, 1, true},
    81  		// Fail2 can't find block
    82  		{"TMissingBlk", blkNo, time.Minute, 0, nil, types.ResultStatus_NOT_FOUND, 1, 1, true},
    83  		// Fail4 response sent after timeout
    84  		{"TTimeout", blkNo, time.Millisecond * 10, time.Millisecond * 20, blkHash, types.ResultStatus_OK, 1, 0, false},
    85  	}
    86  	for _, test := range tests {
    87  		t.Run(test.name, func(t *testing.T) {
    88  			//mockContext := new(mockContext)
    89  			mockActor := p2pmock.NewMockActorService(ctrl)
    90  			//mockActor.EXPECT().SendRequest(message.P2PSvc, gomock.Any())
    91  			if test.sentResp > 0 {
    92  				mockActor.EXPECT().TellRequest(message.SyncerSvc, gomock.Any()).DoAndReturn(func(a string, arg *message.GetHashByNoRsp) {
    93  					if !((arg.Err != nil) == test.respError) {
    94  						t.Fatalf("Wrong error (have %v)\n", arg.Err)
    95  					}
    96  					if arg.Seq != seqNo {
    97  						t.Fatalf("Wrong seqNo %d, want %d)\n", arg.Seq, seqNo)
    98  					}
    99  				})
   100  			}
   101  			//mockContext.On("Respond",mock.AnythingOfType("*message.GetBlockChunksRsp"))
   102  			mockMF := p2pmock.NewMockMoFactory(ctrl)
   103  			mockPeer := p2pmock.NewMockRemotePeer(ctrl)
   104  			//	mockPeer.On("ID").Return(dummyPeerID)
   105  			mockPeer.EXPECT().MF().Return(mockMF)
   106  			mockMo := createDummyMo(ctrl)
   107  			mockPeer.EXPECT().ConsumeRequest(gomock.Any()).Times(test.consumed)
   108  			mockPeer.EXPECT().SendMessage(gomock.Any())
   109  			mockMF.EXPECT().NewMsgBlockRequestOrder(gomock.Any(), gomock.Any(), gomock.Any()).Return(mockMo)
   110  
   111  			//expire := time.Now().Add(test.ttl)
   112  			br := NewBlockHashByNoReceiver(mockActor, mockPeer, seqNo, test.input, test.ttl)
   113  			br.StartGet()
   114  
   115  			msg := p2pcommon.NewSimpleMsgVal(p2pcommon.GetHashByNoResponse, sampleMsgID)
   116  			body := &types.GetHashByNoResponse{BlockHash: test.blkRsp, Status: test.rspStatus}
   117  			if test.blkInterval > 0 {
   118  				time.Sleep(test.blkInterval)
   119  			}
   120  			br.ReceiveResp(msg, body)
   121  
   122  		})
   123  	}
   124  }
   125