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