github.com/aergoio/aergo@v1.3.1/p2p/hashreceiver_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/chain"
    10  	"github.com/aergoio/aergo/p2p/p2pcommon"
    11  	"github.com/funkygao/golib/rand"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/aergoio/aergo/message"
    16  	"github.com/aergoio/aergo/p2p/p2pmock"
    17  	"github.com/aergoio/aergo/types"
    18  	"github.com/golang/mock/gomock"
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  func TestBlockHashesReceiver_StartGet(t *testing.T) {
    23  	sampleBlk := &types.BlockInfo{Hash:dummyBlockHash, No:10000}
    24  	ctrl := gomock.NewController(t)
    25  	defer ctrl.Finish()
    26  
    27  	inputHashes := make([]message.BlockHash, len(sampleBlks))
    28  	for i, hash := range sampleBlks {
    29  		inputHashes[i] = hash
    30  	}
    31  	tests := []struct {
    32  		name  string
    33  		input  *message.GetHashes
    34  		ttl   time.Duration
    35  	}{
    36  		{"TSimple", &message.GetHashes{100, dummyPeerID, sampleBlk, 100}, time.Millisecond * 10},
    37  		// TODO: test cases
    38  	}
    39  	for _, test := range tests {
    40  		t.Run(test.name, func(t *testing.T) {
    41  			//mockContext := new(mockContext)
    42  			mockActor := p2pmock.NewMockActorService(ctrl)
    43  			//mockActor.On("SendRequest", message.P2PSvc, mock.AnythingOfType("*types.GetBlock"))
    44  			//mockActor.On("TellRequest", message.SyncerSvc, mock.AnythingOfType("*types.GetBlock"))
    45  			mockMF := p2pmock.NewMockMoFactory(ctrl)
    46  			mockMo := createDummyMo(ctrl)
    47  			mockMF.EXPECT().NewMsgBlockRequestOrder(gomock.Any(), gomock.Any(), gomock.Any()).Return(mockMo)
    48  			mockPeer := p2pmock.NewMockRemotePeer(ctrl)
    49  			mockPeer.EXPECT().MF().Return(mockMF)
    50  			mockPeer.EXPECT().SendMessage(mockMo).Times(1)
    51  
    52  			expire := time.Now().Add(test.ttl)
    53  			br := NewBlockHashesReceiver(mockActor, mockPeer, test.input.Seq, test.input, test.ttl)
    54  
    55  			br.StartGet()
    56  
    57  			assert.False(t, expire.After(br.timeout))
    58  		})
    59  	}
    60  }
    61  
    62  func TestBlockHashesReceiver_ReceiveResp(t *testing.T) {
    63  	//t.Skip("make check by status. and make another test to check handleInWaiting method")
    64  	sampleBlk := &types.BlockInfo{Hash:dummyBlockHash, No:10000}
    65  	limit := uint64(10)
    66  	ctrl := gomock.NewController(t)
    67  	defer ctrl.Finish()
    68  	chain.Init(1<<20 , "", false, 1, 1 )
    69  
    70  	totalInCnt := 10
    71  	seqNo := uint64(8723)
    72  	inputHashes := make([][]byte, totalInCnt)
    73  	for i:= 0 ; i < totalInCnt ; i++ {
    74  		inputHashes[i] = rand.RandomByteSlice(hashSize)
    75  	}
    76  	tests := []struct {
    77  		name         string
    78  		input        *message.GetHashes
    79  		ttl          time.Duration
    80  		hashInterval time.Duration
    81  		hashInput    [][][]byte
    82  
    83  		// to verify
    84  		consumed  int
    85  		sentResp  int
    86  		respError bool
    87  	}{
    88  		{"TSingleResp", &message.GetHashes{seqNo,dummyPeerID, sampleBlk, limit}, time.Minute, 0, [][][]byte{inputHashes}, 1, 1, false},
    89  		{"TMultiResp", &message.GetHashes{seqNo,dummyPeerID, sampleBlk, limit}, time.Minute, 0, [][][]byte{inputHashes[:1], inputHashes[1:3], inputHashes[3:]}, 1, 1, false},
    90  		// Fail1 remote err
    91  		{"TRemoteFail", &message.GetHashes{seqNo,dummyPeerID, sampleBlk, limit}, time.Minute, 0, [][][]byte{inputHashes[:0]}, 1, 1, true},
    92  		{"TTooManyBlks", &message.GetHashes{seqNo,dummyPeerID, sampleBlk, limit-2}, time.Minute*4,0,[][][]byte{inputHashes[:1],inputHashes[1:3],inputHashes[3:]},1,1, true},
    93  		// Fail4 response sent after timeout
    94  		{"TTimeout", &message.GetHashes{seqNo,dummyPeerID, sampleBlk, limit}, time.Millisecond * 10, time.Millisecond * 20, [][][]byte{inputHashes[:1], inputHashes[1:3], inputHashes[3:]}, 1, 0, false},
    95  	}
    96  	for _, test := range tests {
    97  		t.Run(test.name, func(t *testing.T) {
    98  			//mockContext := new(mockContext)
    99  			mockActor := p2pmock.NewMockActorService(ctrl)
   100  			if test.sentResp > 0 {
   101  				mockActor.EXPECT().TellRequest(message.SyncerSvc, gomock.AssignableToTypeOf(&message.GetHashesRsp{})).
   102  					DoAndReturn(func(a string, arg *message.GetHashesRsp) {
   103  						if !((arg.Err != nil) == test.respError) {
   104  							t.Fatalf("Wrong error (have %v)\n", arg.Err)
   105  						}
   106  						if arg.Seq != seqNo {
   107  							t.Fatalf("Wrong seqNo %d, want %d)\n", arg.Seq, seqNo)
   108  						}
   109  					}).Times(test.sentResp)
   110  			}
   111  
   112  			mockMF := p2pmock.NewMockMoFactory(ctrl)
   113  			mockMo := createDummyMo(ctrl)
   114  			mockMF.EXPECT().NewMsgBlockRequestOrder(gomock.Any(), gomock.Any(), gomock.Any()).Return(mockMo)
   115  			mockPeer := p2pmock.NewMockRemotePeer(ctrl)
   116  			mockPeer.EXPECT().ID().Return(dummyPeerID).AnyTimes()
   117  			mockPeer.EXPECT().MF().Return(mockMF)
   118  			mockPeer.EXPECT().SendMessage(gomock.Any()).Times(1)
   119  			mockPeer.EXPECT().ConsumeRequest(gomock.Any()).Times(test.consumed) //mock.AnythingOfType("p2pcommon.MsgID"))
   120  
   121  			//expire := time.Now().Add(test.ttl)
   122  			br := NewBlockHashesReceiver(mockActor, mockPeer, seqNo, test.input, test.ttl)
   123  			br.StartGet()
   124  
   125  			msg := p2pcommon.NewSimpleMsgVal(p2pcommon.GetHashesRequest, sampleMsgID)
   126  			for i, hashes := range test.hashInput {
   127  				if test.hashInterval > 0 {
   128  					time.Sleep(test.hashInterval)
   129  				}
   130  				body := &types.GetHashesResponse{Hashes: hashes, HasNext: i < len(test.hashInput)-1}
   131  				br.ReceiveResp(msg, body)
   132  				if br.status == receiverStatusFinished {
   133  					break
   134  				}
   135  			}
   136  
   137  		})
   138  	}
   139  }