dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/balancer/clusterimpl/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 2020 gRPC authors.
    21   *
    22   */
    23  
    24  package clusterimpl
    25  
    26  import (
    27  	"testing"
    28  )
    29  
    30  import (
    31  	"github.com/google/go-cmp/cmp"
    32  
    33  	"google.golang.org/grpc/balancer"
    34  	_ "google.golang.org/grpc/balancer/roundrobin"
    35  	_ "google.golang.org/grpc/balancer/weightedtarget"
    36  )
    37  
    38  import (
    39  	internalserviceconfig "dubbo.apache.org/dubbo-go/v3/xds/utils/serviceconfig"
    40  )
    41  
    42  const (
    43  	testJSONConfig = `{
    44    "cluster": "test_cluster",
    45    "edsServiceName": "test-eds",
    46    "lrsLoadReportingServerName": "lrs_server",
    47    "maxConcurrentRequests": 123,
    48    "dropCategories": [
    49      {
    50        "category": "drop-1",
    51        "requestsPerMillion": 314
    52      },
    53      {
    54        "category": "drop-2",
    55        "requestsPerMillion": 159
    56      }
    57    ],
    58    "childPolicy": [
    59      {
    60        "weighted_target_experimental": {
    61          "targets": {
    62            "wt-child-1": {
    63              "weight": 75,
    64              "childPolicy":[{"round_robin":{}}]
    65            },
    66            "wt-child-2": {
    67              "weight": 25,
    68              "childPolicy":[{"round_robin":{}}]
    69            }
    70          }
    71        }
    72      }
    73    ]
    74  }`
    75  
    76  	wtName = "weighted_target_experimental"
    77  )
    78  
    79  var (
    80  	wtConfigParser = balancer.Get(wtName).(balancer.ConfigParser)
    81  	wtConfigJSON   = `{
    82    "targets": {
    83      "wt-child-1": {
    84        "weight": 75,
    85        "childPolicy":[{"round_robin":{}}]
    86      },
    87      "wt-child-2": {
    88        "weight": 25,
    89        "childPolicy":[{"round_robin":{}}]
    90      }
    91    }
    92  }`
    93  
    94  	wtConfig, _ = wtConfigParser.ParseConfig([]byte(wtConfigJSON))
    95  )
    96  
    97  func TestParseConfig(t *testing.T) {
    98  	tests := []struct {
    99  		name    string
   100  		js      string
   101  		want    *LBConfig
   102  		wantErr bool
   103  	}{
   104  		{
   105  			name:    "empty json",
   106  			js:      "",
   107  			want:    nil,
   108  			wantErr: true,
   109  		},
   110  		{
   111  			name:    "bad json",
   112  			js:      "{",
   113  			want:    nil,
   114  			wantErr: true,
   115  		},
   116  		{
   117  			name: "OK",
   118  			js:   testJSONConfig,
   119  			want: &LBConfig{
   120  				Cluster:                 "test_cluster",
   121  				EDSServiceName:          "test-eds",
   122  				LoadReportingServerName: newString("lrs_server"),
   123  				MaxConcurrentRequests:   newUint32(123),
   124  				DropCategories: []DropConfig{
   125  					{Category: "drop-1", RequestsPerMillion: 314},
   126  					{Category: "drop-2", RequestsPerMillion: 159},
   127  				},
   128  				ChildPolicy: &internalserviceconfig.BalancerConfig{
   129  					Name:   wtName,
   130  					Config: wtConfig,
   131  				},
   132  			},
   133  			wantErr: false,
   134  		},
   135  	}
   136  	for _, tt := range tests {
   137  		t.Run(tt.name, func(t *testing.T) {
   138  			got, err := parseConfig([]byte(tt.js))
   139  			if (err != nil) != tt.wantErr {
   140  				t.Fatalf("parseConfig() error = %v, wantErr %v", err, tt.wantErr)
   141  			}
   142  			if !cmp.Equal(got, tt.want) {
   143  				t.Errorf("parseConfig() got unexpected result, diff: %v", cmp.Diff(got, tt.want))
   144  			}
   145  		})
   146  	}
   147  }
   148  
   149  func newString(s string) *string {
   150  	return &s
   151  }
   152  
   153  func newUint32(i uint32) *uint32 {
   154  	return &i
   155  }