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 17package orca 18 19import ( 20 "reflect" 21 "strings" 22 "testing" 23 24 "github.com/golang/protobuf/proto" 25 "google.golang.org/grpc/internal/balancerload/orca/orca_v1" 26 "google.golang.org/grpc/metadata" 27) 28 29var ( 30 testMessage = &orca_v1.LoadReport{ 31 CpuUtilization: 0.1, 32 MemUtilization: 0.2, 33 NicInUtilization: 0, 34 NicOutUtilization: 0, 35 RequestCostOrUtilization: map[string]float64{"ttt": 0.4}, 36 } 37 testBytes, _ = proto.Marshal(testMessage) 38) 39 40func TestToMetadata(t *testing.T) { 41 tests := []struct { 42 name string 43 r *orca_v1.LoadReport 44 want metadata.MD 45 }{{ 46 name: "nil", 47 r: nil, 48 want: nil, 49 }, { 50 name: "valid", 51 r: testMessage, 52 want: metadata.MD{ 53 strings.ToLower(mdKey): []string{string(testBytes)}, 54 }, 55 }} 56 for _, tt := range tests { 57 t.Run(tt.name, func(t *testing.T) { 58 if got := ToMetadata(tt.r); !reflect.DeepEqual(got, tt.want) { 59 t.Errorf("ToMetadata() = %v, want %v", got, tt.want) 60 } 61 }) 62 } 63} 64 65func TestFromMetadata(t *testing.T) { 66 tests := []struct { 67 name string 68 md metadata.MD 69 want *orca_v1.LoadReport 70 }{{ 71 name: "nil", 72 md: nil, 73 want: nil, 74 }, { 75 name: "valid", 76 md: metadata.MD{ 77 strings.ToLower(mdKey): []string{string(testBytes)}, 78 }, 79 want: testMessage, 80 }} 81 for _, tt := range tests { 82 t.Run(tt.name, func(t *testing.T) { 83 if got := FromMetadata(tt.md); !proto.Equal(got, tt.want) { 84 t.Errorf("FromMetadata() = %v, want %v", got, tt.want) 85 } 86 }) 87 } 88} 89