go.temporal.io/server@v1.23.0/common/rpc/interceptor/sdk_version_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2022 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package interceptor
    26  
    27  import (
    28  	"context"
    29  	"sort"
    30  	"testing"
    31  
    32  	"github.com/stretchr/testify/assert"
    33  
    34  	"go.temporal.io/version/check"
    35  
    36  	"go.temporal.io/server/common/headers"
    37  )
    38  
    39  func TestSDKVersionRecorder(t *testing.T) {
    40  	interceptor := &SDKVersionInterceptor{
    41  		sdkInfoSet:     make(map[check.SDKInfo]struct{}),
    42  		maxSetSize:     2,
    43  		versionChecker: headers.NewDefaultVersionChecker(),
    44  	}
    45  
    46  	sdkVersion := "1.10.1"
    47  
    48  	// Record first tuple
    49  	ctx := headers.SetVersionsForTests(context.Background(), sdkVersion, headers.ClientNameGoSDK, headers.SupportedServerVersions, headers.AllFeatures)
    50  	_, err := interceptor.Intercept(ctx, nil, nil, func(ctx context.Context, req interface{}) (interface{}, error) {
    51  		return nil, nil
    52  	})
    53  	assert.NoError(t, err)
    54  
    55  	// Record second tuple
    56  	ctx = headers.SetVersionsForTests(context.Background(), sdkVersion, headers.ClientNameTypeScriptSDK, headers.SupportedServerVersions, headers.AllFeatures)
    57  	_, err = interceptor.Intercept(ctx, nil, nil, func(ctx context.Context, req interface{}) (interface{}, error) {
    58  		return nil, nil
    59  	})
    60  	assert.NoError(t, err)
    61  
    62  	// Do not record when over capacity
    63  	ctx = headers.SetVersionsForTests(context.Background(), sdkVersion, headers.ClientNameJavaSDK, headers.SupportedServerVersions, headers.AllFeatures)
    64  	_, err = interceptor.Intercept(ctx, nil, nil, func(ctx context.Context, req interface{}) (interface{}, error) {
    65  		return nil, nil
    66  	})
    67  	assert.NoError(t, err)
    68  
    69  	// Empty SDK version should not be recorded
    70  	ctx = headers.SetVersionsForTests(context.Background(), "", headers.ClientNameGoSDK, headers.SupportedServerVersions, headers.AllFeatures)
    71  	_, err = interceptor.Intercept(ctx, nil, nil, func(ctx context.Context, req interface{}) (interface{}, error) {
    72  		return nil, nil
    73  	})
    74  	assert.NoError(t, err)
    75  
    76  	// Empty SDK name should not be recorded
    77  	ctx = headers.SetVersionsForTests(context.Background(), sdkVersion, "", headers.SupportedServerVersions, headers.AllFeatures)
    78  	_, err = interceptor.Intercept(ctx, nil, nil, func(ctx context.Context, req interface{}) (interface{}, error) {
    79  		return nil, nil
    80  	})
    81  	assert.NoError(t, err)
    82  
    83  	info := interceptor.GetAndResetSDKInfo()
    84  	sort.SliceStable(info, func(i, j int) bool {
    85  		return info[i].Name < info[j].Name
    86  	})
    87  	assert.Equal(t, 2, len(info))
    88  	assert.Equal(t, headers.ClientNameGoSDK, info[0].Name)
    89  	assert.Equal(t, sdkVersion, info[0].Version)
    90  	assert.Equal(t, headers.ClientNameTypeScriptSDK, info[1].Name)
    91  	assert.Equal(t, sdkVersion, info[1].Version)
    92  }