google.golang.org/grpc@v1.62.1/xds/internal/balancer/ringhash/config_test.go (about) 1 /* 2 * 3 * Copyright 2021 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package ringhash 20 21 import ( 22 "testing" 23 24 "github.com/google/go-cmp/cmp" 25 "google.golang.org/grpc/internal/envconfig" 26 ) 27 28 func (s) TestParseConfig(t *testing.T) { 29 tests := []struct { 30 name string 31 js string 32 envConfigCap uint64 33 want *LBConfig 34 wantErr bool 35 }{ 36 { 37 name: "OK", 38 js: `{"minRingSize": 1, "maxRingSize": 2}`, 39 want: &LBConfig{MinRingSize: 1, MaxRingSize: 2}, 40 }, 41 { 42 name: "OK with default min", 43 js: `{"maxRingSize": 2000}`, 44 want: &LBConfig{MinRingSize: defaultMinSize, MaxRingSize: 2000}, 45 }, 46 { 47 name: "OK with default max", 48 js: `{"minRingSize": 2000}`, 49 want: &LBConfig{MinRingSize: 2000, MaxRingSize: defaultMaxSize}, 50 }, 51 { 52 name: "min greater than max", 53 js: `{"minRingSize": 10, "maxRingSize": 2}`, 54 want: nil, 55 wantErr: true, 56 }, 57 { 58 name: "min greater than max greater than global limit", 59 js: `{"minRingSize": 6000, "maxRingSize": 5000}`, 60 want: nil, 61 wantErr: true, 62 }, 63 { 64 name: "max greater than global limit", 65 js: `{"minRingSize": 1, "maxRingSize": 6000}`, 66 want: &LBConfig{MinRingSize: 1, MaxRingSize: 4096}, 67 }, 68 { 69 name: "min and max greater than global limit", 70 js: `{"minRingSize": 5000, "maxRingSize": 6000}`, 71 want: &LBConfig{MinRingSize: 4096, MaxRingSize: 4096}, 72 }, 73 { 74 name: "min and max less than raised global limit", 75 js: `{"minRingSize": 5000, "maxRingSize": 6000}`, 76 envConfigCap: 8000, 77 want: &LBConfig{MinRingSize: 5000, MaxRingSize: 6000}, 78 }, 79 { 80 name: "min and max greater than raised global limit", 81 js: `{"minRingSize": 10000, "maxRingSize": 10000}`, 82 envConfigCap: 8000, 83 want: &LBConfig{MinRingSize: 8000, MaxRingSize: 8000}, 84 }, 85 { 86 name: "min greater than upper bound", 87 js: `{"minRingSize": 8388610, "maxRingSize": 10}`, 88 want: nil, 89 wantErr: true, 90 }, 91 { 92 name: "max greater than upper bound", 93 js: `{"minRingSize": 10, "maxRingSize": 8388610}`, 94 want: nil, 95 wantErr: true, 96 }, 97 } 98 for _, tt := range tests { 99 t.Run(tt.name, func(t *testing.T) { 100 if tt.envConfigCap != 0 { 101 old := envconfig.RingHashCap 102 defer func() { envconfig.RingHashCap = old }() 103 envconfig.RingHashCap = tt.envConfigCap 104 } 105 got, err := parseConfig([]byte(tt.js)) 106 if (err != nil) != tt.wantErr { 107 t.Errorf("parseConfig() error = %v, wantErr %v", err, tt.wantErr) 108 return 109 } 110 if diff := cmp.Diff(got, tt.want); diff != "" { 111 t.Errorf("parseConfig() got unexpected output, diff (-got +want): %v", diff) 112 } 113 }) 114 } 115 }