github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/balancer/priority/config_test.go (about)

     1  /*
     2   *
     3   * Copyright 2020 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 priority
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	"github.com/hxx258456/ccgo/grpc/balancer/roundrobin"
    26  	internalserviceconfig "github.com/hxx258456/ccgo/grpc/internal/serviceconfig"
    27  )
    28  
    29  func TestParseConfig(t *testing.T) {
    30  	tests := []struct {
    31  		name    string
    32  		js      string
    33  		want    *LBConfig
    34  		wantErr bool
    35  	}{
    36  		{
    37  			name: "child not found",
    38  			js: `{
    39    "priorities": ["child-1", "child-2", "child-3"],
    40    "children": {
    41      "child-1": {"config": [{"round_robin":{}}]},
    42      "child-3": {"config": [{"round_robin":{}}]}
    43    }
    44  }
    45  			`,
    46  			wantErr: true,
    47  		},
    48  		{
    49  			name: "child not used",
    50  			js: `{
    51    "priorities": ["child-1", "child-2"],
    52    "children": {
    53      "child-1": {"config": [{"round_robin":{}}]},
    54      "child-2": {"config": [{"round_robin":{}}]},
    55      "child-3": {"config": [{"round_robin":{}}]}
    56    }
    57  }
    58  			`,
    59  			wantErr: true,
    60  		},
    61  		{
    62  			name: "good",
    63  			js: `{
    64    "priorities": ["child-1", "child-2", "child-3"],
    65    "children": {
    66      "child-1": {"config": [{"round_robin":{}}], "ignoreReresolutionRequests": true},
    67      "child-2": {"config": [{"round_robin":{}}]},
    68      "child-3": {"config": [{"round_robin":{}}]}
    69    }
    70  }
    71  			`,
    72  			want: &LBConfig{
    73  				Children: map[string]*Child{
    74  					"child-1": {
    75  						Config: &internalserviceconfig.BalancerConfig{
    76  							Name: roundrobin.Name,
    77  						},
    78  						IgnoreReresolutionRequests: true,
    79  					},
    80  					"child-2": {
    81  						Config: &internalserviceconfig.BalancerConfig{
    82  							Name: roundrobin.Name,
    83  						},
    84  					},
    85  					"child-3": {
    86  						Config: &internalserviceconfig.BalancerConfig{
    87  							Name: roundrobin.Name,
    88  						},
    89  					},
    90  				},
    91  				Priorities: []string{"child-1", "child-2", "child-3"},
    92  			},
    93  			wantErr: false,
    94  		},
    95  	}
    96  	for _, tt := range tests {
    97  		t.Run(tt.name, func(t *testing.T) {
    98  			got, err := parseConfig([]byte(tt.js))
    99  			if (err != nil) != tt.wantErr {
   100  				t.Errorf("parseConfig() error = %v, wantErr %v", err, tt.wantErr)
   101  				return
   102  			}
   103  			if diff := cmp.Diff(got, tt.want); diff != "" {
   104  				t.Errorf("parseConfig() got = %v, want %v, diff: %s", got, tt.want, diff)
   105  			}
   106  		})
   107  	}
   108  }