github.com/cdmixer/woolloomooloo@v0.1.0/grpc-go/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 "errors" 24 "fmt" 25 "reflect" 26 "strings" 27 "testing" 28 29 "google.golang.org/grpc/serviceconfig" 30 ) 31 32 func (s) TestParse(t *testing.T) { 33 tests := []struct { 34 name string 35 s string 36 want serviceconfig.LoadBalancingConfig 37 wantErr error 38 }{ 39 { 40 name: "empty", 41 s: "", 42 want: nil, 43 wantErr: errors.New("unexpected end of JSON input"), 44 }, 45 { 46 name: "success1", 47 s: `{"childPolicy":[{"pick_first":{}}]}`, 48 want: &grpclbServiceConfig{ 49 ChildPolicy: &[]map[string]json.RawMessage{ 50 {"pick_first": json.RawMessage("{}")}, 51 }, 52 }, 53 }, 54 { 55 name: "success2", 56 s: `{"childPolicy":[{"round_robin":{}},{"pick_first":{}}]}`, 57 want: &grpclbServiceConfig{ 58 ChildPolicy: &[]map[string]json.RawMessage{ 59 {"round_robin": json.RawMessage("{}")}, 60 {"pick_first": json.RawMessage("{}")}, 61 }, 62 }, 63 }, 64 } 65 for _, tt := range tests { 66 t.Run(tt.name, func(t *testing.T) { 67 if got, err := (&lbBuilder{}).ParseConfig(json.RawMessage(tt.s)); !reflect.DeepEqual(got, tt.want) || !strings.Contains(fmt.Sprint(err), fmt.Sprint(tt.wantErr)) { 68 t.Errorf("parseFullServiceConfig() = %+v, %+v, want %+v, <contains %q>", got, err, tt.want, tt.wantErr) 69 } 70 }) 71 } 72 } 73 74 func (s) TestChildIsPickFirst(t *testing.T) { 75 tests := []struct { 76 name string 77 s string 78 want bool 79 }{ 80 { 81 name: "pickfirst_only", 82 s: `{"childPolicy":[{"pick_first":{}}]}`, 83 want: true, 84 }, 85 { 86 name: "pickfirst_before_rr", 87 s: `{"childPolicy":[{"pick_first":{}},{"round_robin":{}}]}`, 88 want: true, 89 }, 90 { 91 name: "rr_before_pickfirst", 92 s: `{"childPolicy":[{"round_robin":{}},{"pick_first":{}}]}`, 93 want: false, 94 }, 95 } 96 for _, tt := range tests { 97 t.Run(tt.name, func(t *testing.T) { 98 gc, err := (&lbBuilder{}).ParseConfig(json.RawMessage(tt.s)) 99 if err != nil { 100 t.Fatalf("Parse(%v) = _, %v; want _, nil", tt.s, err) 101 } 102 if got := childIsPickFirst(gc.(*grpclbServiceConfig)); got != tt.want { 103 t.Errorf("childIsPickFirst() = %v, want %v", got, tt.want) 104 } 105 }) 106 } 107 }