github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/balancer/orca/orca_test.go (about) 1 /* 2 * Copyright 2019 gRPC authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package orca 18 19 import ( 20 "strings" 21 "testing" 22 23 orcapb "github.com/cncf/xds/go/xds/data/orca/v3" 24 "github.com/golang/protobuf/proto" 25 "github.com/google/go-cmp/cmp" 26 "github.com/hxx258456/ccgo/grpc/internal/grpctest" 27 "github.com/hxx258456/ccgo/grpc/metadata" 28 ) 29 30 var ( 31 testMessage = &orcapb.OrcaLoadReport{ 32 CpuUtilization: 0.1, 33 MemUtilization: 0.2, 34 RequestCost: map[string]float64{"ccc": 3.4}, 35 Utilization: map[string]float64{"ttt": 0.4}, 36 } 37 testBytes, _ = proto.Marshal(testMessage) 38 ) 39 40 type s struct { 41 grpctest.Tester 42 } 43 44 func Test(t *testing.T) { 45 grpctest.RunSubTests(t, s{}) 46 } 47 48 func (s) TestToMetadata(t *testing.T) { 49 tests := []struct { 50 name string 51 r *orcapb.OrcaLoadReport 52 want metadata.MD 53 }{{ 54 name: "nil", 55 r: nil, 56 want: nil, 57 }, { 58 name: "valid", 59 r: testMessage, 60 want: metadata.MD{ 61 strings.ToLower(mdKey): []string{string(testBytes)}, 62 }, 63 }} 64 for _, tt := range tests { 65 t.Run(tt.name, func(t *testing.T) { 66 if got := ToMetadata(tt.r); !cmp.Equal(got, tt.want) { 67 t.Errorf("ToMetadata() = %v, want %v", got, tt.want) 68 } 69 }) 70 } 71 } 72 73 func (s) TestFromMetadata(t *testing.T) { 74 tests := []struct { 75 name string 76 md metadata.MD 77 want *orcapb.OrcaLoadReport 78 }{{ 79 name: "nil", 80 md: nil, 81 want: nil, 82 }, { 83 name: "valid", 84 md: metadata.MD{ 85 strings.ToLower(mdKey): []string{string(testBytes)}, 86 }, 87 want: testMessage, 88 }} 89 for _, tt := range tests { 90 t.Run(tt.name, func(t *testing.T) { 91 if got := FromMetadata(tt.md); !cmp.Equal(got, tt.want, cmp.Comparer(proto.Equal)) { 92 t.Errorf("FromMetadata() = %v, want %v", got, tt.want) 93 } 94 }) 95 } 96 }