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