orca_test.go 2.6 KB

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