go.uber.org/yarpc@v1.72.1/encoding/thrift/inject_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package thrift
    22  
    23  import (
    24  	"reflect"
    25  	"testing"
    26  
    27  	"github.com/golang/mock/gomock"
    28  	"github.com/stretchr/testify/assert"
    29  	"go.uber.org/yarpc/api/transport/transporttest"
    30  )
    31  
    32  type someInterface interface{}
    33  
    34  var _typeOfSomeInterface = reflect.TypeOf((*someInterface)(nil)).Elem()
    35  
    36  func TestClientBuilderOptions(t *testing.T) {
    37  
    38  	tests := []struct {
    39  		desc string
    40  		give reflect.StructField
    41  		want clientConfig
    42  	}{
    43  		{
    44  			desc: "no options",
    45  			give: reflect.StructField{
    46  				Name: "Client",
    47  				Type: _typeOfSomeInterface,
    48  				Tag:  `service:"keyvalue"`,
    49  			},
    50  		},
    51  		{
    52  			desc: "enveloped",
    53  			give: reflect.StructField{
    54  				Name: "Client",
    55  				Type: _typeOfSomeInterface,
    56  				Tag:  `service:"keyvalue" thrift:"enveloped"`,
    57  			},
    58  			want: clientConfig{Enveloping: true},
    59  		},
    60  		{
    61  			desc: "multiplexed",
    62  			give: reflect.StructField{
    63  				Name: "Client",
    64  				Type: _typeOfSomeInterface,
    65  				Tag:  `service:"keyvalue" thrift:"multiplexed"`,
    66  			},
    67  			want: clientConfig{Multiplexed: true},
    68  		},
    69  		{
    70  			desc: "enveloped and multiplexed",
    71  			give: reflect.StructField{
    72  				Name: "Client",
    73  				Type: _typeOfSomeInterface,
    74  				Tag:  `service:"keyvalue" thrift:"enveloped,multiplexed"`,
    75  			},
    76  			want: clientConfig{Enveloping: true, Multiplexed: true},
    77  		},
    78  		{
    79  			desc: "ignore unknown",
    80  			give: reflect.StructField{
    81  				Name: "Client",
    82  				Type: _typeOfSomeInterface,
    83  				Tag:  `service:"keyvalue" thrift:"enveloped,foo=bar,Multiplexed"`,
    84  			},
    85  			want: clientConfig{Enveloping: true, Multiplexed: true},
    86  		},
    87  	}
    88  
    89  	for _, tt := range tests {
    90  		t.Run(tt.desc, func(t *testing.T) {
    91  			mockCtrl := gomock.NewController(t)
    92  			defer mockCtrl.Finish()
    93  
    94  			var cfg clientConfig
    95  			opts := ClientBuilderOptions(transporttest.NewMockClientConfig(mockCtrl), tt.give)
    96  			for _, o := range opts {
    97  				o.applyClientOption(&cfg)
    98  			}
    99  
   100  			assert.Equal(t, tt.want, cfg)
   101  		})
   102  	}
   103  }