google.golang.org/grpc@v1.62.1/orca/orca_test.go (about) 1 /* 2 * 3 * Copyright 2022 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 orca_test 20 21 import ( 22 "testing" 23 "time" 24 25 "github.com/google/go-cmp/cmp" 26 "google.golang.org/grpc/internal/grpctest" 27 "google.golang.org/grpc/internal/pretty" 28 "google.golang.org/grpc/metadata" 29 "google.golang.org/grpc/orca/internal" 30 "google.golang.org/protobuf/proto" 31 32 v3orcapb "github.com/cncf/xds/go/xds/data/orca/v3" 33 ) 34 35 type s struct { 36 grpctest.Tester 37 } 38 39 func Test(t *testing.T) { 40 grpctest.RunSubTests(t, s{}) 41 } 42 43 const defaultTestTimeout = 5 * time.Second 44 45 func (s) TestToLoadReport(t *testing.T) { 46 goodReport := &v3orcapb.OrcaLoadReport{ 47 CpuUtilization: 1.0, 48 MemUtilization: 50.0, 49 RequestCost: map[string]float64{"queryCost": 25.0}, 50 Utilization: map[string]float64{"queueSize": 75.0}, 51 } 52 tests := []struct { 53 name string 54 md metadata.MD 55 want *v3orcapb.OrcaLoadReport 56 wantErr bool 57 }{ 58 { 59 name: "no load report in metadata", 60 md: metadata.MD{}, 61 wantErr: false, 62 }, 63 { 64 name: "badly marshaled load report", 65 md: func() metadata.MD { 66 return metadata.Pairs("endpoint-load-metrics-bin", string("foo-bar")) 67 }(), 68 wantErr: true, 69 }, 70 { 71 name: "multiple load reports", 72 md: func() metadata.MD { 73 b, _ := proto.Marshal(goodReport) 74 return metadata.Pairs("endpoint-load-metrics-bin", string(b), "endpoint-load-metrics-bin", string(b)) 75 }(), 76 wantErr: true, 77 }, 78 { 79 name: "good load report", 80 md: func() metadata.MD { 81 b, _ := proto.Marshal(goodReport) 82 return metadata.Pairs("endpoint-load-metrics-bin", string(b)) 83 }(), 84 want: goodReport, 85 }, 86 } 87 88 for _, test := range tests { 89 t.Run(test.name, func(t *testing.T) { 90 got, err := internal.ToLoadReport(test.md) 91 if (err != nil) != test.wantErr { 92 t.Fatalf("orca.ToLoadReport(%v) = %v, wantErr: %v", test.md, err, test.wantErr) 93 } 94 if test.wantErr { 95 return 96 } 97 if !cmp.Equal(got, test.want, cmp.Comparer(proto.Equal)) { 98 t.Fatalf("Extracted load report from metadata: %s, want: %s", pretty.ToJSON(got), pretty.ToJSON(test.want)) 99 } 100 }) 101 } 102 }