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