-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorclass_test.go
More file actions
291 lines (247 loc) · 7.41 KB
/
Copy patherrorclass_test.go
File metadata and controls
291 lines (247 loc) · 7.41 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
package rhttp_test
import (
"context"
"crypto/x509"
"errors"
"net"
"net/url"
"syscall"
"testing"
"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_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.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.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")
}
if rhttp.IsDNS(context.Canceled) {
t.Error("expected IsDNS to be false for Canceled")
}
}
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: "no such host"},
rhttp.ErrKindTLS: x509.UnknownAuthorityError{},
}
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")
}
}