-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorclass_test.go
More file actions
477 lines (410 loc) · 12.7 KB
/
Copy patherrorclass_test.go
File metadata and controls
477 lines (410 loc) · 12.7 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package rhttp_test
import (
"context"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"syscall"
"testing"
"time"
"github.com/oswaldom-code/rhttp"
)
func TestClassify_DeadlineExceeded(t *testing.T) {
classified := rhttp.Classify(context.DeadlineExceeded)
if classified.Kind != rhttp.ErrKindTimeout {
t.Errorf("expected ErrKindTimeout, got %v", classified.Kind)
}
if !errors.Is(classified, context.DeadlineExceeded) {
t.Error("expected Unwrap to return original error")
}
}
func TestClassify_Canceled(t *testing.T) {
classified := rhttp.Classify(context.Canceled)
if classified.Kind != rhttp.ErrKindCanceled {
t.Errorf("expected ErrKindCanceled, got %v", classified.Kind)
}
}
func TestClassify_DNSError(t *testing.T) {
dnsErr := &net.DNSError{
Err: "no such host",
Name: "invalid.example.com",
}
classified := rhttp.Classify(dnsErr)
if classified.Kind != rhttp.ErrKindDNS {
t.Errorf("expected ErrKindDNS, got %v", classified.Kind)
}
}
func TestClassify_DNSNotFound(t *testing.T) {
dnsErr := &net.DNSError{
Err: "no such host",
Name: "this-host-does-not-exist.invalid",
IsNotFound: true,
}
classified := rhttp.Classify(dnsErr)
if classified.Kind != rhttp.ErrKindDNSNotFound {
t.Errorf("expected ErrKindDNSNotFound, got %v", classified.Kind)
}
if classified.Kind.IsRetryable() {
t.Error("NXDOMAIN is permanent: expected IsRetryable to be false")
}
if rhttp.IsRetryable(dnsErr) {
t.Error("expected package-level IsRetryable to be false for NXDOMAIN")
}
if rhttp.DefaultIsRetryable(nil, dnsErr) {
t.Error("expected DefaultIsRetryable to be false for NXDOMAIN")
}
}
func TestClassify_DNSNotFound_WrappedInURLError(t *testing.T) {
err := &url.Error{
Op: "Get",
URL: "http://this-host-does-not-exist.invalid/",
Err: &net.OpError{
Op: "dial",
Net: "tcp",
Err: &net.DNSError{Err: "no such host", IsNotFound: true},
},
}
if got := rhttp.Classify(err).Kind; got != rhttp.ErrKindDNSNotFound {
t.Errorf("expected ErrKindDNSNotFound through the real error chain, got %v", got)
}
}
func TestClassify_DNSTemporaryStaysRetryable(t *testing.T) {
dnsErr := &net.DNSError{
Err: "server misbehaving",
Name: "example.com",
IsTemporary: true,
}
classified := rhttp.Classify(dnsErr)
if classified.Kind != rhttp.ErrKindDNS {
t.Errorf("expected ErrKindDNS, got %v", classified.Kind)
}
if !classified.Kind.IsRetryable() {
t.Error("expected a temporary DNS failure to stay retryable")
}
}
func TestClassify_ConnectionRefused(t *testing.T) {
err := &net.OpError{Op: "dial", Net: "tcp", Err: syscall.ECONNREFUSED}
classified := rhttp.Classify(err)
if classified.Kind != rhttp.ErrKindConnection {
t.Errorf("expected ErrKindConnection, got %v", classified.Kind)
}
}
func TestClassify_ConnectionReset(t *testing.T) {
err := &net.OpError{Op: "read", Net: "tcp", Err: syscall.ECONNRESET}
classified := rhttp.Classify(err)
if classified.Kind != rhttp.ErrKindConnection {
t.Errorf("expected ErrKindConnection, got %v", classified.Kind)
}
}
func TestClassify_TLSError(t *testing.T) {
err := x509.UnknownAuthorityError{}
classified := rhttp.Classify(err)
if classified.Kind != rhttp.ErrKindTLS {
t.Errorf("expected ErrKindTLS, got %v", classified.Kind)
}
}
func TestClassify_X509Error(t *testing.T) {
err := x509.CertificateInvalidError{Reason: x509.Expired}
classified := rhttp.Classify(err)
if classified.Kind != rhttp.ErrKindTLS {
t.Errorf("expected ErrKindTLS, got %v", classified.Kind)
}
}
func TestClassify_WrappedURLError(t *testing.T) {
urlErr := &url.Error{
Op: "Get",
URL: "http://example.com",
Err: context.DeadlineExceeded,
}
classified := rhttp.Classify(urlErr)
if classified.Kind != rhttp.ErrKindTimeout {
t.Errorf("expected ErrKindTimeout for wrapped deadline, got %v", classified.Kind)
}
}
func TestClassify_URLErrorTimeout(t *testing.T) {
urlErr := &url.Error{
Op: "Get",
URL: "http://example.com",
Err: &timeoutError{},
}
classified := rhttp.Classify(urlErr)
if classified.Kind != rhttp.ErrKindTimeout {
t.Errorf("expected ErrKindTimeout, got %v", classified.Kind)
}
}
// timeoutError implements net.Error with Timeout() = true
type timeoutError struct{}
func (e *timeoutError) Error() string { return "timeout" }
func (e *timeoutError) Timeout() bool { return true }
func (e *timeoutError) Temporary() bool { return true }
func TestClassify_NilError(t *testing.T) {
classified := rhttp.Classify(nil)
if classified != nil {
t.Error("expected nil for nil error")
}
}
func TestClassify_UnknownError(t *testing.T) {
err := errors.New("something completely unexpected")
classified := rhttp.Classify(err)
if classified.Kind != rhttp.ErrKindUnknown {
t.Errorf("expected ErrKindUnknown, got %v", classified.Kind)
}
}
func TestClassifiedError_Error(t *testing.T) {
classified := rhttp.Classify(syscall.ECONNREFUSED)
expected := "connection: " + syscall.ECONNREFUSED.Error()
if classified.Error() != expected {
t.Errorf("expected %q, got %q", expected, classified.Error())
}
}
func TestClassifiedError_Unwrap(t *testing.T) {
originalErr := errors.New("original error")
classified := rhttp.Classify(originalErr)
if !errors.Is(classified, originalErr) {
t.Error("errors.Is should match original error")
}
}
func TestErrorKind_String(t *testing.T) {
tests := []struct {
kind rhttp.ErrorKind
expected string
}{
{rhttp.ErrKindTimeout, "timeout"},
{rhttp.ErrKindCanceled, "canceled"},
{rhttp.ErrKindConnection, "connection"},
{rhttp.ErrKindDNS, "dns"},
{rhttp.ErrKindDNSNotFound, "dns_not_found"},
{rhttp.ErrKindTLS, "tls"},
{rhttp.ErrKindUnknown, "unknown"},
}
for _, tt := range tests {
if tt.kind.String() != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, tt.kind.String())
}
}
}
func TestErrorKind_IsRetryable(t *testing.T) {
retryable := []rhttp.ErrorKind{
rhttp.ErrKindTimeout,
rhttp.ErrKindConnection,
rhttp.ErrKindDNS,
}
for _, k := range retryable {
if !k.IsRetryable() {
t.Errorf("expected %v to be retryable", k)
}
}
notRetryable := []rhttp.ErrorKind{
rhttp.ErrKindCanceled,
rhttp.ErrKindTLS,
rhttp.ErrKindDNSNotFound,
rhttp.ErrKindUnknown,
}
for _, k := range notRetryable {
if k.IsRetryable() {
t.Errorf("expected %v to not be retryable", k)
}
}
}
func TestIsTimeout(t *testing.T) {
if !rhttp.IsTimeout(context.DeadlineExceeded) {
t.Error("expected IsTimeout to be true for DeadlineExceeded")
}
if rhttp.IsTimeout(context.Canceled) {
t.Error("expected IsTimeout to be false for Canceled")
}
if rhttp.IsTimeout(nil) {
t.Error("expected IsTimeout to be false for nil")
}
}
func TestIsCanceled(t *testing.T) {
if !rhttp.IsCanceled(context.Canceled) {
t.Error("expected IsCanceled to be true for Canceled")
}
if rhttp.IsCanceled(context.DeadlineExceeded) {
t.Error("expected IsCanceled to be false for DeadlineExceeded")
}
if rhttp.IsCanceled(nil) {
t.Error("expected IsCanceled to be false for nil")
}
}
func TestIsConnection(t *testing.T) {
if !rhttp.IsConnection(syscall.ECONNREFUSED) {
t.Error("expected IsConnection to be true for ECONNREFUSED")
}
if rhttp.IsConnection(context.Canceled) {
t.Error("expected IsConnection to be false for Canceled")
}
}
func TestIsDNS(t *testing.T) {
dnsErr := &net.DNSError{Err: "no such host", Name: "invalid.example.com"}
if !rhttp.IsDNS(dnsErr) {
t.Error("expected IsDNS to be true for DNSError")
}
notFound := &net.DNSError{Err: "no such host", Name: "invalid.example.com", IsNotFound: true}
if !rhttp.IsDNS(notFound) {
t.Error("expected IsDNS to stay true for NXDOMAIN: it is still a DNS failure")
}
if rhttp.IsDNS(context.Canceled) {
t.Error("expected IsDNS to be false for Canceled")
}
}
func TestIsDNSNotFound(t *testing.T) {
notFound := &net.DNSError{Err: "no such host", Name: "invalid.example.com", IsNotFound: true}
if !rhttp.IsDNSNotFound(notFound) {
t.Error("expected IsDNSNotFound to be true for NXDOMAIN")
}
transient := &net.DNSError{Err: "server misbehaving", IsTemporary: true}
if rhttp.IsDNSNotFound(transient) {
t.Error("expected IsDNSNotFound to be false for a transient DNS failure")
}
if rhttp.IsDNSNotFound(nil) {
t.Error("expected IsDNSNotFound to be false for nil")
}
}
func TestIsTLS(t *testing.T) {
if !rhttp.IsTLS(x509.UnknownAuthorityError{}) {
t.Error("expected IsTLS to be true for TLS error")
}
if rhttp.IsTLS(context.Canceled) {
t.Error("expected IsTLS to be false for Canceled")
}
}
func TestClassify_AllKindsAreReachable(t *testing.T) {
producers := map[rhttp.ErrorKind]error{
rhttp.ErrKindUnknown: errors.New("something completely unexpected"),
rhttp.ErrKindTimeout: context.DeadlineExceeded,
rhttp.ErrKindCanceled: context.Canceled,
rhttp.ErrKindConnection: syscall.ECONNREFUSED,
rhttp.ErrKindDNS: &net.DNSError{Err: "server misbehaving"},
rhttp.ErrKindTLS: x509.UnknownAuthorityError{},
rhttp.ErrKindDNSNotFound: &net.DNSError{Err: "no such host", IsNotFound: true},
rhttp.ErrKindCircuitOpen: rhttp.ErrCircuitOpen,
rhttp.ErrKindRateLimited: rhttp.ErrRateLimited,
}
for kind, err := range producers {
if got := rhttp.Classify(err).Kind; got != kind {
t.Errorf("expected %v to classify as %v, got %v", err, kind, got)
}
}
for k := rhttp.ErrKindUnknown; ; k++ {
if k != rhttp.ErrKindUnknown && k.String() == "unknown" {
break
}
if _, ok := producers[k]; !ok {
t.Errorf("ErrorKind %d (%s) has no producing error: orphaned kind", k, k)
}
}
}
func TestIsRetryable(t *testing.T) {
// Retryable
if !rhttp.IsRetryable(context.DeadlineExceeded) {
t.Error("expected timeout to be retryable")
}
if !rhttp.IsRetryable(syscall.ECONNREFUSED) {
t.Error("expected connection error to be retryable")
}
// Not retryable
if rhttp.IsRetryable(context.Canceled) {
t.Error("expected canceled to not be retryable")
}
if rhttp.IsRetryable(x509.UnknownAuthorityError{}) {
t.Error("expected TLS error to not be retryable")
}
if rhttp.IsRetryable(nil) {
t.Error("expected nil to not be retryable")
}
}
func TestClassify_Sentinels(t *testing.T) {
tests := []struct {
name string
err error
kind rhttp.ErrorKind
want string
}{
{"circuit open", rhttp.ErrCircuitOpen, rhttp.ErrKindCircuitOpen, "circuit_open"},
{"rate limited", rhttp.ErrRateLimited, rhttp.ErrKindRateLimited, "rate_limited"},
{
"circuit open wrapped",
fmt.Errorf("calling users service: %w", rhttp.ErrCircuitOpen),
rhttp.ErrKindCircuitOpen,
"circuit_open",
},
{
"rate limited wrapped",
fmt.Errorf("calling users service: %w", rhttp.ErrRateLimited),
rhttp.ErrKindRateLimited,
"rate_limited",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
classified := rhttp.Classify(tt.err)
if classified.Kind != tt.kind {
t.Errorf("Kind = %v, want %v", classified.Kind, tt.kind)
}
if got := classified.Kind.String(); got != tt.want {
t.Errorf("Kind.String() = %q, want %q", got, tt.want)
}
})
}
}
func TestClassify_SentinelsStayNonRetryable(t *testing.T) {
for _, err := range []error{rhttp.ErrCircuitOpen, rhttp.ErrRateLimited} {
if rhttp.IsRetryable(err) {
t.Errorf("IsRetryable(%v) = true, want false", err)
}
if rhttp.DefaultIsRetryable(nil, err) {
t.Errorf("DefaultIsRetryable(nil, %v) = true, want false", err)
}
}
}
func TestErrorKind_SentinelKindsAreAppended(t *testing.T) {
if rhttp.ErrKindUnknown != 0 {
t.Errorf("ErrKindUnknown = %d, want 0", rhttp.ErrKindUnknown)
}
if rhttp.ErrKindCircuitOpen <= rhttp.ErrKindDNSNotFound {
t.Error("ErrKindCircuitOpen must come after ErrKindDNSNotFound")
}
if rhttp.ErrKindRateLimited <= rhttp.ErrKindCircuitOpen {
t.Error("ErrKindRateLimited must come after ErrKindCircuitOpen")
}
}
func TestClassify_MetricsAboveBreakerNameTheOutcome(t *testing.T) {
rt := rhttp.RoundTripperFunc(func(*http.Request) (*http.Response, error) {
return nil, &net.OpError{Op: "dial", Net: "tcp", Err: syscall.ECONNREFUSED}
})
var kinds []string
c := rhttp.New(
rhttp.WithTransport(rt),
rhttp.WithMiddleware(
rhttp.Metrics(rhttp.MetricsConfig{
Recorder: rhttp.MetricsRecorderFunc(func(e rhttp.MetricEvent) {
if e.Error != nil {
kinds = append(kinds, rhttp.Classify(e.Error).Kind.String())
}
}),
}),
rhttp.CircuitBreaker(rhttp.CircuitBreakerConfig{
FailureThreshold: 2,
ResetTimeout: time.Hour,
}),
),
)
for i := 0; i < 6; i++ {
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
_, _ = c.Do(context.Background(), req)
}
want := []string{"connection", "connection", "circuit_open", "circuit_open", "circuit_open", "circuit_open"}
if len(kinds) != len(want) {
t.Fatalf("kinds = %v, want %v", kinds, want)
}
for i := range want {
if kinds[i] != want[i] {
t.Errorf("kind %d = %q, want %q (full: %v)", i, kinds[i], want[i], kinds)
}
}
}