github.com/true-sqn/fabric@v2.1.1+incompatible/internal/pkg/comm/config_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package comm
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"google.golang.org/grpc"
    15  	"google.golang.org/grpc/keepalive"
    16  )
    17  
    18  func TestServerKeepaliveOptions(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	kap := keepalive.ServerParameters{
    22  		Time:    DefaultKeepaliveOptions.ServerInterval,
    23  		Timeout: DefaultKeepaliveOptions.ServerTimeout,
    24  	}
    25  	kep := keepalive.EnforcementPolicy{
    26  		MinTime:             DefaultKeepaliveOptions.ServerMinInterval,
    27  		PermitWithoutStream: true,
    28  	}
    29  	expectedOpts := []grpc.ServerOption{
    30  		grpc.KeepaliveParams(kap),
    31  		grpc.KeepaliveEnforcementPolicy(kep),
    32  	}
    33  	opts := ServerKeepaliveOptions(DefaultKeepaliveOptions)
    34  	assert.ObjectsAreEqual(expectedOpts, opts)
    35  
    36  }
    37  
    38  func TestClientKeepaliveOptions(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	kap := keepalive.ClientParameters{
    42  		Time:                DefaultKeepaliveOptions.ClientInterval,
    43  		Timeout:             DefaultKeepaliveOptions.ClientTimeout,
    44  		PermitWithoutStream: true,
    45  	}
    46  	expectedOpts := []grpc.DialOption{grpc.WithKeepaliveParams(kap)}
    47  	opts := ClientKeepaliveOptions(DefaultKeepaliveOptions)
    48  	assert.ObjectsAreEqual(expectedOpts, opts)
    49  
    50  }
    51  
    52  func TestClientConfigClone(t *testing.T) {
    53  	origin := ClientConfig{
    54  		KaOpts: KeepaliveOptions{
    55  			ClientInterval: time.Second,
    56  		},
    57  		SecOpts: SecureOptions{
    58  			Key: []byte{1, 2, 3},
    59  		},
    60  		Timeout:      time.Second,
    61  		AsyncConnect: true,
    62  	}
    63  
    64  	clone := origin.Clone()
    65  
    66  	// Same content, different inner fields references.
    67  	assert.Equal(t, origin, clone)
    68  
    69  	// We change the contents of the fields and ensure it doesn't
    70  	// propagate across instances.
    71  	origin.AsyncConnect = false
    72  	origin.KaOpts.ServerInterval = time.Second
    73  	origin.KaOpts.ClientInterval = time.Hour
    74  	origin.SecOpts.Certificate = []byte{1, 2, 3}
    75  	origin.SecOpts.Key = []byte{5, 4, 6}
    76  	origin.Timeout = time.Second * 2
    77  
    78  	clone.SecOpts.UseTLS = true
    79  	clone.KaOpts.ServerMinInterval = time.Hour
    80  
    81  	expectedOriginState := ClientConfig{
    82  		KaOpts: KeepaliveOptions{
    83  			ClientInterval: time.Hour,
    84  			ServerInterval: time.Second,
    85  		},
    86  		SecOpts: SecureOptions{
    87  			Key:         []byte{5, 4, 6},
    88  			Certificate: []byte{1, 2, 3},
    89  		},
    90  		Timeout: time.Second * 2,
    91  	}
    92  
    93  	expectedCloneState := ClientConfig{
    94  		KaOpts: KeepaliveOptions{
    95  			ClientInterval:    time.Second,
    96  			ServerMinInterval: time.Hour,
    97  		},
    98  		SecOpts: SecureOptions{
    99  			Key:    []byte{1, 2, 3},
   100  			UseTLS: true,
   101  		},
   102  		Timeout:      time.Second,
   103  		AsyncConnect: true,
   104  	}
   105  
   106  	assert.Equal(t, expectedOriginState, origin)
   107  	assert.Equal(t, expectedCloneState, clone)
   108  }