dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/balancer/priority/config_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  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   *
    20   * Copyright 2021 gRPC authors.
    21   *
    22   */
    23  
    24  package priority
    25  
    26  import (
    27  	"testing"
    28  )
    29  
    30  import (
    31  	"github.com/google/go-cmp/cmp"
    32  
    33  	"google.golang.org/grpc/balancer/roundrobin"
    34  )
    35  
    36  import (
    37  	internalserviceconfig "dubbo.apache.org/dubbo-go/v3/xds/utils/serviceconfig"
    38  )
    39  
    40  func TestParseConfig(t *testing.T) {
    41  	tests := []struct {
    42  		name    string
    43  		js      string
    44  		want    *LBConfig
    45  		wantErr bool
    46  	}{
    47  		{
    48  			name: "child not found",
    49  			js: `{
    50    "priorities": ["child-1", "child-2", "child-3"],
    51    "children": {
    52      "child-1": {"config": [{"round_robin":{}}]},
    53      "child-3": {"config": [{"round_robin":{}}]}
    54    }
    55  }
    56  			`,
    57  			wantErr: true,
    58  		},
    59  		{
    60  			name: "child not used",
    61  			js: `{
    62    "priorities": ["child-1", "child-2"],
    63    "children": {
    64      "child-1": {"config": [{"round_robin":{}}]},
    65      "child-2": {"config": [{"round_robin":{}}]},
    66      "child-3": {"config": [{"round_robin":{}}]}
    67    }
    68  }
    69  			`,
    70  			wantErr: true,
    71  		},
    72  		{
    73  			name: "good",
    74  			js: `{
    75    "priorities": ["child-1", "child-2", "child-3"],
    76    "children": {
    77      "child-1": {"config": [{"round_robin":{}}], "ignoreReresolutionRequests": true},
    78      "child-2": {"config": [{"round_robin":{}}]},
    79      "child-3": {"config": [{"round_robin":{}}]}
    80    }
    81  }
    82  			`,
    83  			want: &LBConfig{
    84  				Children: map[string]*Child{
    85  					"child-1": {
    86  						Config: &internalserviceconfig.BalancerConfig{
    87  							Name: roundrobin.Name,
    88  						},
    89  						IgnoreReresolutionRequests: true,
    90  					},
    91  					"child-2": {
    92  						Config: &internalserviceconfig.BalancerConfig{
    93  							Name: roundrobin.Name,
    94  						},
    95  					},
    96  					"child-3": {
    97  						Config: &internalserviceconfig.BalancerConfig{
    98  							Name: roundrobin.Name,
    99  						},
   100  					},
   101  				},
   102  				Priorities: []string{"child-1", "child-2", "child-3"},
   103  			},
   104  			wantErr: false,
   105  		},
   106  	}
   107  	for _, tt := range tests {
   108  		t.Run(tt.name, func(t *testing.T) {
   109  			got, err := parseConfig([]byte(tt.js))
   110  			if (err != nil) != tt.wantErr {
   111  				t.Errorf("parseConfig() error = %v, wantErr %v", err, tt.wantErr)
   112  				return
   113  			}
   114  			if diff := cmp.Diff(got, tt.want); diff != "" {
   115  				t.Errorf("parseConfig() got = %v, want %v, diff: %s", got, tt.want, diff)
   116  			}
   117  		})
   118  	}
   119  }