-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathoptions_test.go
More file actions
225 lines (208 loc) · 6.1 KB
/
Copy pathoptions_test.go
File metadata and controls
225 lines (208 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// SPDX-License-Identifier: Apache-2.0
package grpc_sentry
import (
"testing"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestReportAlways(t *testing.T) {
// Test that ReportAlways always returns true regardless of the error
tests := []struct {
name string
error error
}{
{
name: "nil error",
error: nil,
},
{
name: "status error",
error: status.Error(codes.Internal, "internal error"),
},
{
name: "different status error",
error: status.Error(codes.InvalidArgument, "invalid argument"),
},
{
name: "non-status error",
error: &testError{message: "test error"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ReportAlways(tt.error)
if result != true {
t.Errorf("Expected ReportAlways to return true for %v, got %v", tt.error, result)
}
})
}
}
func TestReportOnCodes(t *testing.T) {
tests := []struct {
name string
error error
codes []codes.Code
shouldReport bool
}{
{
name: "report on matching code",
error: status.Error(codes.Internal, "internal error"),
codes: []codes.Code{codes.Internal},
shouldReport: true,
},
{
name: "don't report on non-matching code",
error: status.Error(codes.InvalidArgument, "invalid argument"),
codes: []codes.Code{codes.Internal},
shouldReport: false,
},
{
name: "report on one of multiple codes",
error: status.Error(codes.NotFound, "not found"),
codes: []codes.Code{codes.Internal, codes.NotFound, codes.Unavailable},
shouldReport: true,
},
{
name: "don't report on none of multiple codes",
error: status.Error(codes.InvalidArgument, "invalid argument"),
codes: []codes.Code{codes.Internal, codes.NotFound, codes.Unavailable},
shouldReport: false,
},
{
name: "nil error with any codes",
error: nil,
codes: []codes.Code{codes.Internal, codes.NotFound},
shouldReport: false,
},
{
name: "non-status error with any codes",
error: &testError{message: "test error"},
codes: []codes.Code{codes.Internal, codes.NotFound},
shouldReport: false,
},
{
name: "empty codes list",
error: status.Error(codes.Internal, "internal error"),
codes: []codes.Code{},
shouldReport: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reporter := ReportOnCodes(tt.codes...)
result := reporter(tt.error)
if result != tt.shouldReport {
t.Errorf("Expected ReportOnCodes to return %v for error %v with codes %v, got %v",
tt.shouldReport, tt.error, tt.codes, result)
}
})
}
}
func TestReportOnCodes_AllCodes(t *testing.T) {
// Test all gRPC status codes to ensure they work correctly
allCodes := []codes.Code{
codes.OK,
codes.Canceled,
codes.Unknown,
codes.InvalidArgument,
codes.DeadlineExceeded,
codes.NotFound,
codes.AlreadyExists,
codes.PermissionDenied,
codes.ResourceExhausted,
codes.FailedPrecondition,
codes.Aborted,
codes.OutOfRange,
codes.Unimplemented,
codes.Internal,
codes.Unavailable,
codes.DataLoss,
codes.Unauthenticated,
}
for _, code := range allCodes {
t.Run(code.String(), func(t *testing.T) {
reporter := ReportOnCodes(code)
error := status.Error(code, "test error")
result := reporter(error)
if result != true {
t.Errorf("Expected ReportOnCodes to return true for code %s, got %v", code, result)
}
})
}
}
func TestReportOnCodes_Combination(t *testing.T) {
// Test combinations of codes
tests := []struct {
name string
error error
codes []codes.Code
shouldReport bool
}{
{
name: "multiple matching codes",
error: status.Error(codes.Internal, "internal error"),
codes: []codes.Code{codes.Internal, codes.Internal, codes.Internal},
shouldReport: true,
},
{
name: "mixed matching and non-matching codes",
error: status.Error(codes.NotFound, "not found"),
codes: []codes.Code{codes.Internal, codes.NotFound, codes.Unavailable},
shouldReport: true,
},
{
name: "all non-matching codes",
error: status.Error(codes.InvalidArgument, "invalid argument"),
codes: []codes.Code{codes.Internal, codes.NotFound, codes.Unavailable},
shouldReport: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reporter := ReportOnCodes(tt.codes...)
result := reporter(tt.error)
if result != tt.shouldReport {
t.Errorf("Expected ReportOnCodes to return %v for error %v with codes %v, got %v",
tt.shouldReport, tt.error, tt.codes, result)
}
})
}
}
// testError is a simple error type for testing
type testError struct {
message string
}
func (e *testError) Error() string {
return e.message
}
func TestDefaultOptions(t *testing.T) {
// Test that default options are set correctly
if defaultOptions.Repanic != false {
t.Errorf("Expected default Repanic to be false, got %v", defaultOptions.Repanic)
}
if defaultOptions.WaitForDelivery != false {
t.Errorf("Expected default WaitForDelivery to be false, got %v", defaultOptions.WaitForDelivery)
}
if defaultOptions.Timeout != 1*time.Second {
t.Errorf("Expected default Timeout to be 1s, got %v", defaultOptions.Timeout)
}
if defaultOptions.OperationNameOverride != "" {
t.Errorf("Expected default OperationNameOverride to be empty, got %v", defaultOptions.OperationNameOverride)
}
if defaultOptions.CaptureRequestBody != true {
t.Errorf("Expected default CaptureRequestBody to be true, got %v", defaultOptions.CaptureRequestBody)
}
if defaultOptions.ReportOn == nil {
t.Error("Expected default ReportOn to be set, got nil")
}
}
func TestDefaultOperationNames(t *testing.T) {
// Test that default operation names are set correctly
if defaultServerOperationName != "grpc.server" {
t.Errorf("Expected defaultServerOperationName to be 'grpc.server', got %s", defaultServerOperationName)
}
if defaultClientOperationName != "grpc.client" {
t.Errorf("Expected defaultClientOperationName to be 'grpc.client', got %s", defaultClientOperationName)
}
}