github.com/aergoio/aergo@v1.3.1/p2p/versionmanager_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/p2pmock"
    10  	"github.com/golang/mock/gomock"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/aergoio/aergo/p2p/p2pcommon"
    15  	"github.com/aergoio/aergo/types"
    16  )
    17  
    18  func Test_defaultVersionManager_FindBestP2PVersion(t *testing.T) {
    19  	ctrl := gomock.NewController(t)
    20  	defer ctrl.Finish()
    21  
    22  	dummyChainID := &types.ChainID{}
    23  
    24  
    25  	type args struct {
    26  		versions []p2pcommon.P2PVersion
    27  	}
    28  	tests := []struct {
    29  		name   string
    30  		args   args
    31  
    32  		want   p2pcommon.P2PVersion
    33  	}{
    34  		{"TSingle", args{[]p2pcommon.P2PVersion{p2pcommon.P2PVersion030}}, p2pcommon.P2PVersion030},
    35  		{"TMulti", args{[]p2pcommon.P2PVersion{p2pcommon.P2PVersion031, p2pcommon.P2PVersion030}}, p2pcommon.P2PVersion031},
    36  		{"TUnknown", args{[]p2pcommon.P2PVersion{9999999, 9999998}}, p2pcommon.P2PVersionUnknown},
    37  	}
    38  	for _, tt := range tests {
    39  		t.Run(tt.name, func(t *testing.T) {
    40  			pm := p2pmock.NewMockPeerManager(ctrl)
    41  			actor := p2pmock.NewMockActorService(ctrl)
    42  			ca := p2pmock.NewMockChainAccessor(ctrl)
    43  			vm := newDefaultVersionManager(pm, actor, ca, logger, dummyChainID)
    44  
    45  			if got := vm.FindBestP2PVersion(tt.args.versions); !reflect.DeepEqual(got, tt.want) {
    46  				t.Errorf("defaultVersionManager.FindBestP2PVersion() = %v, want %v", got, tt.want)
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func Test_defaultVersionManager_GetVersionedHandshaker(t *testing.T) {
    53  	ctrl := gomock.NewController(t)
    54  	defer ctrl.Finish()
    55  
    56  	dummyChainID := &types.ChainID{}
    57  
    58  
    59  	type args struct {
    60  		version p2pcommon.P2PVersion
    61  	}
    62  	tests := []struct {
    63  		name   string
    64  		args   args
    65  
    66  		wantErr bool
    67  	}{
    68  		//
    69  		{"TRecent", args{p2pcommon.P2PVersion031}, false},
    70  		{"TLegacy", args{p2pcommon.P2PVersion030}, false},
    71  		{"TUnknown", args{9999999}, true},
    72  	}
    73  	for _, tt := range tests {
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			pm := p2pmock.NewMockPeerManager(ctrl)
    76  			actor := p2pmock.NewMockActorService(ctrl)
    77  			ca := p2pmock.NewMockChainAccessor(ctrl)
    78  
    79  			r := p2pmock.NewMockReadWriteCloser(ctrl)
    80  
    81  			h := newDefaultVersionManager(pm, actor, ca, logger, dummyChainID)
    82  
    83  			got, err := h.GetVersionedHandshaker(tt.args.version, sampleID, r)
    84  			if (err != nil) != tt.wantErr {
    85  				t.Errorf("defaultVersionManager.GetVersionedHandshaker() error = %v, wantErr %v", err, tt.wantErr)
    86  				return
    87  			}
    88  			if (got != nil) == tt.wantErr {
    89  				t.Errorf("defaultVersionManager.GetVersionedHandshaker() returns nil == %v , want %v", (got != nil), tt.wantErr )
    90  				return
    91  			}
    92  		})
    93  	}
    94  }