Path: blob/main/components/common-go/grpc/ratelimit_test.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package grpc56import (7"strconv"8"strings"9"testing"1011"github.com/google/go-cmp/cmp"12"google.golang.org/protobuf/proto"13"google.golang.org/protobuf/types/known/apipb"14"google.golang.org/protobuf/types/known/sourcecontextpb"15"google.golang.org/protobuf/types/known/typepb"16)1718func TestGetFieldValue(t *testing.T) {19type Expectation struct {20Found bool21Val string22}23tests := []struct {24Name string25Message proto.Message26Path string27Expectation Expectation28}{29{30Name: "direct access",31Message: &apipb.Api{Name: "bar"},32Path: "name",33Expectation: Expectation{Found: true, Val: "bar"},34},35{36Name: "empty string field",37Message: &apipb.Api{},38Path: "name",39Expectation: Expectation{Found: true},40},41{42Name: "enum field",43Message: &apipb.Api{Syntax: typepb.Syntax_SYNTAX_PROTO3},44Path: "syntax",45Expectation: Expectation{Found: true, Val: strconv.Itoa(int(typepb.Syntax_SYNTAX_PROTO3))},46},47{48Name: "empty enum field",49Message: &apipb.Api{},50Path: "syntax",51Expectation: Expectation{Found: true, Val: "0"},52},53{54Name: "bool field",55Message: &apipb.Method{RequestStreaming: true},56Path: "request_streaming",57Expectation: Expectation{Found: true, Val: "t"},58},59{60Name: "empty bool field",61Message: &apipb.Method{},62Path: "request_streaming",63Expectation: Expectation{Found: true, Val: "f"},64},65{66Name: "non-existent field",67Message: &apipb.Api{},68Path: "does-not-exist",69Expectation: Expectation{Found: false},70},71{72Name: "nest struct",73Message: &apipb.Api{74SourceContext: &sourcecontextpb.SourceContext{75FileName: "bar",76},77},78Path: "source_context.file_name",79Expectation: Expectation{Found: true, Val: "bar"},80},81}82for _, test := range tests {83t.Run(test.Name, func(t *testing.T) {84var act Expectation85act.Val, act.Found = getFieldValue(test.Message.ProtoReflect(), strings.Split(test.Path, "."))86if diff := cmp.Diff(test.Expectation, act); diff != "" {87t.Errorf("unexpected getFieldValue (-want +got):\n%s", diff)88}89})90}91}9293func TestFieldAccessKey(t *testing.T) {94type Expectation struct {95Val string96Err error97}98tests := []struct {99Name string100Message proto.Message101Key string102Expectation Expectation103}{104{105Name: "composite key",106Message: &apipb.Api{107SourceContext: &sourcecontextpb.SourceContext{108FileName: "bar",109},110Syntax: typepb.Syntax_SYNTAX_PROTO3,111},112Key: "source_context.file_name,syntax",113Expectation: Expectation{Val: "|bar|1", Err: nil},114},115}116for _, test := range tests {117t.Run(test.Name, func(t *testing.T) {118var act Expectation119keyFn := fieldAccessKey(test.Key)120act.Val, act.Err = keyFn(test.Message)121if diff := cmp.Diff(test.Expectation, act); diff != "" {122t.Errorf("unexpected fieldAccessKey (-want +got):\n%s", diff)123}124})125}126}127128func BenchmarkGetFieldValue(b *testing.B) {129msg := apipb.Api{130SourceContext: &sourcecontextpb.SourceContext{131FileName: "bar",132},133}134msgr := msg.ProtoReflect()135path := []string{"source_context", "file_name"}136// run the Fib function b.N times137for n := 0; n < b.N; n++ {138getFieldValue(msgr, path)139}140}141142func BenchmarkFieldAccessKey_String(b *testing.B) {143msg := &apipb.Api{144Name: "bar",145}146keyFn := fieldAccessKey("name")147for n := 0; n < b.N; n++ {148if _, err := keyFn(msg); err != nil {149b.Logf("failed to access key: %v", err)150b.Fail()151}152}153}154155func BenchmarkFieldAccessKey_Enum(b *testing.B) {156msg := &apipb.Api{157Syntax: typepb.Syntax_SYNTAX_PROTO3,158}159keyFn := fieldAccessKey("syntax")160for n := 0; n < b.N; n++ {161if _, err := keyFn(msg); err != nil {162b.Logf("failed to access key: %v", err)163b.Fail()164}165}166}167168func BenchmarkFieldAccessKey_Bool(b *testing.B) {169msg := &apipb.Method{170RequestStreaming: true,171}172keyFn := fieldAccessKey("request_streaming")173for n := 0; n < b.N; n++ {174if _, err := keyFn(msg); err != nil {175b.Logf("failed to access key: %v", err)176b.Fail()177}178}179}180181func BenchmarkFieldAccessKey_Composite(b *testing.B) {182msg := &apipb.Method{183Name: "bar",184RequestStreaming: true,185}186keyFn := fieldAccessKey("name,request_streaming")187for n := 0; n < b.N; n++ {188if _, err := keyFn(msg); err != nil {189b.Logf("failed to access key: %v", err)190b.Fail()191}192}193}194195196