gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/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  )
    26  
    27  func TestParseConfig(t *testing.T) {
    28  	tests := []struct {
    29  		name    string
    30  		js      string
    31  		want    *LBConfig
    32  		wantErr bool
    33  	}{
    34  		{
    35  			name: "OK",
    36  			js:   `{"minRingSize": 1, "maxRingSize": 2}`,
    37  			want: &LBConfig{MinRingSize: 1, MaxRingSize: 2},
    38  		},
    39  		{
    40  			name: "OK with default min",
    41  			js:   `{"maxRingSize": 2000}`,
    42  			want: &LBConfig{MinRingSize: defaultMinSize, MaxRingSize: 2000},
    43  		},
    44  		{
    45  			name: "OK with default max",
    46  			js:   `{"minRingSize": 2000}`,
    47  			want: &LBConfig{MinRingSize: 2000, MaxRingSize: defaultMaxSize},
    48  		},
    49  		{
    50  			name:    "min greater than max",
    51  			js:      `{"minRingSize": 10, "maxRingSize": 2}`,
    52  			want:    nil,
    53  			wantErr: true,
    54  		},
    55  	}
    56  	for _, tt := range tests {
    57  		t.Run(tt.name, func(t *testing.T) {
    58  			got, err := parseConfig([]byte(tt.js))
    59  			if (err != nil) != tt.wantErr {
    60  				t.Errorf("parseConfig() error = %v, wantErr %v", err, tt.wantErr)
    61  				return
    62  			}
    63  			if diff := cmp.Diff(got, tt.want); diff != "" {
    64  				t.Errorf("parseConfig() got unexpected output, diff (-got +want): %v", diff)
    65  			}
    66  		})
    67  	}
    68  }