gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/balancer/weightedroundrobin/weightedroundrobin_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 weightedroundrobin 20 21 import ( 22 "testing" 23 24 "gitee.com/ks-custle/core-gm/grpc/attributes" 25 "gitee.com/ks-custle/core-gm/grpc/resolver" 26 "github.com/google/go-cmp/cmp" 27 ) 28 29 func TestAddrInfoToAndFromAttributes(t *testing.T) { 30 tests := []struct { 31 desc string 32 inputAddrInfo AddrInfo 33 inputAttributes *attributes.Attributes 34 wantAddrInfo AddrInfo 35 }{ 36 { 37 desc: "empty attributes", 38 inputAddrInfo: AddrInfo{Weight: 100}, 39 inputAttributes: nil, 40 wantAddrInfo: AddrInfo{Weight: 100}, 41 }, 42 { 43 desc: "non-empty attributes", 44 inputAddrInfo: AddrInfo{Weight: 100}, 45 inputAttributes: attributes.New("foo", "bar"), 46 wantAddrInfo: AddrInfo{Weight: 100}, 47 }, 48 { 49 desc: "addrInfo not present in empty attributes", 50 inputAddrInfo: AddrInfo{}, 51 inputAttributes: nil, 52 wantAddrInfo: AddrInfo{}, 53 }, 54 { 55 desc: "addrInfo not present in non-empty attributes", 56 inputAddrInfo: AddrInfo{}, 57 inputAttributes: attributes.New("foo", "bar"), 58 wantAddrInfo: AddrInfo{}, 59 }, 60 } 61 62 for _, test := range tests { 63 t.Run(test.desc, func(t *testing.T) { 64 addr := resolver.Address{Attributes: test.inputAttributes} 65 addr = SetAddrInfo(addr, test.inputAddrInfo) 66 gotAddrInfo := GetAddrInfo(addr) 67 if !cmp.Equal(gotAddrInfo, test.wantAddrInfo) { 68 t.Errorf("gotAddrInfo: %v, wantAddrInfo: %v", gotAddrInfo, test.wantAddrInfo) 69 } 70 71 }) 72 } 73 } 74 75 func TestGetAddInfoEmpty(t *testing.T) { 76 addr := resolver.Address{} 77 gotAddrInfo := GetAddrInfo(addr) 78 wantAddrInfo := AddrInfo{} 79 if !cmp.Equal(gotAddrInfo, wantAddrInfo) { 80 t.Errorf("gotAddrInfo: %v, wantAddrInfo: %v", gotAddrInfo, wantAddrInfo) 81 } 82 }