Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
67 lines (61 loc) · 1.72 KB
/
Copy patherrors_test.go
File metadata and controls
67 lines (61 loc) · 1.72 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
package portless_test
import (
"context"
"errors"
"fmt"
"net"
"syscall"
"testing"
portless "github.com/sanketsudake/go-portless"
)
func TestRetryable(t *testing.T) {
base := errors.New("starting")
err := portless.Retryable(base)
if !portless.IsRetryable(err) {
t.Fatal("Retryable-wrapped error should be retryable")
}
if !errors.Is(err, base) {
t.Fatal("Retryable must preserve the error chain")
}
if portless.Retryable(nil) != nil {
t.Fatal("Retryable(nil) must be nil")
}
// wrapped deeper in a chain still detected
if !portless.IsRetryable(fmt.Errorf("outer: %w", err)) {
t.Fatal("retryable inside a wrap chain should be detected")
}
}
func TestIsRetryableClassification(t *testing.T) {
// real connection-refused
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
addr := l.Addr().String()
l.Close()
_, dialErr := (&net.Dialer{}).DialContext(context.Background(), "tcp", addr)
if dialErr == nil {
t.Skip("port unexpectedly accepting")
}
if !portless.IsRetryable(dialErr) {
t.Fatalf("connection refused should be retryable: %v", dialErr)
}
if !errors.Is(dialErr, syscall.ECONNREFUSED) {
t.Logf("note: dial error is not ECONNREFUSED on this platform: %v", dialErr)
}
// network timeout
toErr := &net.DNSError{Err: "timeout", IsTimeout: true}
if !portless.IsRetryable(toErr) {
t.Fatal("net timeout should be retryable")
}
// arbitrary errors are not retryable
if portless.IsRetryable(errors.New("bad config")) {
t.Fatal("plain error must not be retryable")
}
if portless.IsRetryable(nil) {
t.Fatal("nil must not be retryable")
}
if portless.IsRetryable(context.DeadlineExceeded) {
t.Fatal("context.DeadlineExceeded from the caller's ctx must not be retryable")
}
}