-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpolicy_test.go
More file actions
100 lines (86 loc) · 2.37 KB
/
Copy pathpolicy_test.go
File metadata and controls
100 lines (86 loc) · 2.37 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
// Copyright (c) 2026 Onur Cinar.
// The source code is provided under MIT License.
// https://github.com/cinar/resile
package resile
import (
"errors"
"testing"
)
var errTest = errors.New("test error")
var errOther = errors.New("other error")
func TestFatalError(t *testing.T) {
t.Parallel()
t.Run("WrapsError", func(t *testing.T) {
err := FatalError(errTest)
if err.Error() != errTest.Error() {
t.Errorf("expected %q, got %q", errTest.Error(), err.Error())
}
})
t.Run("UnwrapsError", func(t *testing.T) {
err := FatalError(errTest)
if !errors.Is(err, errTest) {
t.Errorf("expected errors.Is(err, errTest) to be true")
}
})
t.Run("IdentifiesFatal", func(t *testing.T) {
err := FatalError(errTest)
if !isFatal(err) {
t.Error("expected isFatal(err) to be true")
}
})
t.Run("NestedFatal", func(t *testing.T) {
err := FatalError(errTest)
wrapped := errors.Join(err, errOther)
if !isFatal(wrapped) {
t.Error("expected isFatal(wrapped) to be true for nested fatal error")
}
})
t.Run("NilFatal", func(t *testing.T) {
if FatalError(nil) != nil {
t.Error("FatalError(nil) should be nil")
}
})
}
func TestRetryPolicy_ShouldRetry(t *testing.T) {
t.Parallel()
t.Run("DefaultPolicy", func(t *testing.T) {
policy := &retryPolicy{}
if policy.shouldRetry(nil) {
t.Error("shouldRetry(nil) should be false")
}
if !policy.shouldRetry(errTest) {
t.Error("expected default policy to retry errors")
}
if policy.shouldRetry(FatalError(errTest)) {
t.Error("expected default policy to NOT retry fatal errors")
}
})
t.Run("RetryIfTarget", func(t *testing.T) {
policy := &retryPolicy{retryIf: errTest}
if !policy.shouldRetry(errTest) {
t.Error("expected policy to retry errTest")
}
if policy.shouldRetry(errOther) {
t.Error("expected policy to NOT retry errOther")
}
})
t.Run("RetryIfFunc", func(t *testing.T) {
policy := &retryPolicy{
retryIfFunc: func(err error) bool {
return errors.Is(err, errTest)
},
}
if !policy.shouldRetry(errTest) {
t.Error("expected policy to retry errTest via func")
}
if policy.shouldRetry(errOther) {
t.Error("expected policy to NOT retry errOther via func")
}
})
t.Run("FatalOverridesPolicy", func(t *testing.T) {
policy := &retryPolicy{retryIf: errTest}
if policy.shouldRetry(FatalError(errTest)) {
t.Error("expected fatal error to override retryIf policy")
}
})
}