go.uber.org/yarpc@v1.72.1/yarpcconfig/backoff_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 yarpcconfig
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  	"go.uber.org/yarpc/internal/config"
    30  	"go.uber.org/yarpc/internal/whitespace"
    31  	"gopkg.in/yaml.v2"
    32  )
    33  
    34  func TestBackoffConfig(t *testing.T) {
    35  	type testCase struct {
    36  		name string
    37  		give string
    38  		env  map[string]string
    39  		want Backoff
    40  		err  bool
    41  	}
    42  
    43  	tests := []testCase{
    44  		{
    45  			name: "empty",
    46  		},
    47  		{
    48  			name: "specified",
    49  			give: `
    50  				exponential:
    51  					first: 1s
    52  					max: 2s
    53  			`,
    54  			want: Backoff{
    55  				Exponential: ExponentialBackoff{
    56  					First: time.Second,
    57  					Max:   2 * time.Second,
    58  				},
    59  			},
    60  		},
    61  		{
    62  			name: "bogus",
    63  			give: `
    64  				whatevenis: true
    65  			`,
    66  			err: true,
    67  		},
    68  	}
    69  
    70  	for _, tt := range tests {
    71  		t.Run(tt.name, func(t *testing.T) {
    72  			text := whitespace.Expand(tt.give)
    73  			var data map[string]interface{}
    74  			require.NoError(t, yaml.Unmarshal([]byte(text), &data))
    75  
    76  			var cfg Backoff
    77  			err := config.DecodeInto(&cfg, data, config.InterpolateWith(mapVariableResolver(tt.env)))
    78  
    79  			if err == nil {
    80  				_, err = cfg.Strategy()
    81  			}
    82  
    83  			if tt.err {
    84  				require.NotNil(t, err)
    85  			} else {
    86  				require.NoError(t, err)
    87  				assert.Equal(t, cfg, tt.want)
    88  			}
    89  		})
    90  	}
    91  }