github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/balancer/grpclb/grpclb_config_test.go (about) 1 /* 2 * 3 * Copyright 2019 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 grpclb 20 21 import ( 22 "encoding/json" 23 "testing" 24 25 "github.com/google/go-cmp/cmp" 26 "github.com/hxx258456/ccgo/grpc/serviceconfig" 27 ) 28 29 func (s) TestParse(t *testing.T) { 30 tests := []struct { 31 name string 32 sc string 33 want serviceconfig.LoadBalancingConfig 34 wantErr bool 35 }{ 36 { 37 name: "empty", 38 sc: "", 39 want: nil, 40 wantErr: true, 41 }, 42 { 43 name: "success1", 44 sc: ` 45 { 46 "childPolicy": [ 47 {"pick_first":{}} 48 ], 49 "targetName": "foo-service" 50 }`, 51 want: &grpclbServiceConfig{ 52 ChildPolicy: &[]map[string]json.RawMessage{ 53 {"pick_first": json.RawMessage("{}")}, 54 }, 55 TargetName: "foo-service", 56 }, 57 }, 58 { 59 name: "success2", 60 sc: ` 61 { 62 "childPolicy": [ 63 {"round_robin":{}}, 64 {"pick_first":{}} 65 ], 66 "targetName": "foo-service" 67 }`, 68 want: &grpclbServiceConfig{ 69 ChildPolicy: &[]map[string]json.RawMessage{ 70 {"round_robin": json.RawMessage("{}")}, 71 {"pick_first": json.RawMessage("{}")}, 72 }, 73 TargetName: "foo-service", 74 }, 75 }, 76 } 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 got, err := (&lbBuilder{}).ParseConfig(json.RawMessage(tt.sc)) 80 if (err != nil) != (tt.wantErr) { 81 t.Fatalf("ParseConfig(%q) returned error: %v, wantErr: %v", tt.sc, err, tt.wantErr) 82 } 83 if diff := cmp.Diff(tt.want, got); diff != "" { 84 t.Fatalf("ParseConfig(%q) returned unexpected difference (-want +got):\n%s", tt.sc, diff) 85 } 86 }) 87 } 88 } 89 90 func (s) TestChildIsPickFirst(t *testing.T) { 91 tests := []struct { 92 name string 93 s string 94 want bool 95 }{ 96 { 97 name: "pickfirst_only", 98 s: `{"childPolicy":[{"pick_first":{}}]}`, 99 want: true, 100 }, 101 { 102 name: "pickfirst_before_rr", 103 s: `{"childPolicy":[{"pick_first":{}},{"round_robin":{}}]}`, 104 want: true, 105 }, 106 { 107 name: "rr_before_pickfirst", 108 s: `{"childPolicy":[{"round_robin":{}},{"pick_first":{}}]}`, 109 want: false, 110 }, 111 } 112 for _, tt := range tests { 113 t.Run(tt.name, func(t *testing.T) { 114 gc, err := (&lbBuilder{}).ParseConfig(json.RawMessage(tt.s)) 115 if err != nil { 116 t.Fatalf("Parse(%v) = _, %v; want _, nil", tt.s, err) 117 } 118 if got := childIsPickFirst(gc.(*grpclbServiceConfig)); got != tt.want { 119 t.Errorf("childIsPickFirst() = %v, want %v", got, tt.want) 120 } 121 }) 122 } 123 }