-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretry_test.go
More file actions
88 lines (70 loc) · 2.27 KB
/
retry_test.go
File metadata and controls
88 lines (70 loc) · 2.27 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
package ewrap
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestWithRetry(t *testing.T) {
maxAttempts := 3
delay := time.Second
err := New("test error", WithRetry(maxAttempts, delay))
retryInfo, ok := err.metadata["retry_info"].(*RetryInfo)
assert.True(t, ok)
assert.Equal(t, maxAttempts, retryInfo.MaxAttempts)
assert.Equal(t, delay, retryInfo.Delay)
assert.Equal(t, 0, retryInfo.CurrentAttempt)
assert.NotZero(t, retryInfo.LastAttempt)
assert.NotNil(t, retryInfo.ShouldRetry)
}
func TestCanRetry(t *testing.T) {
t.Run("WithValidRetryInfo", func(t *testing.T) {
err := New("test error", WithRetry(3, time.Second))
assert.True(t, err.CanRetry())
err.IncrementRetry()
assert.True(t, err.CanRetry())
err.IncrementRetry()
assert.True(t, err.CanRetry())
err.IncrementRetry()
assert.False(t, err.CanRetry())
})
t.Run("WithoutRetryInfo", func(t *testing.T) {
err := New("test error")
assert.False(t, err.CanRetry())
})
}
func TestWithRetryCustomShouldRetry(t *testing.T) {
shouldRetry := func(error) bool { return false }
err := New("test error", WithRetry(3, time.Second, WithRetryShould(shouldRetry)))
assert.False(t, err.CanRetry())
}
func TestDefaultShouldRetry(t *testing.T) {
t.Run("ValidationError", func(t *testing.T) {
err := New("validation error").
WithMetadata("error_context", &ErrorContext{Type: ErrorTypeValidation})
assert.False(t, defaultShouldRetry(err))
})
t.Run("OtherError", func(t *testing.T) {
err := New("other error").
WithMetadata("error_context", &ErrorContext{Type: ErrorTypeInternal})
assert.True(t, defaultShouldRetry(err))
})
t.Run("NoContext", func(t *testing.T) {
err := New("no context error")
assert.True(t, defaultShouldRetry(err))
})
}
func TestIncrementRetry(t *testing.T) {
t.Run("WithRetryInfo", func(t *testing.T) {
err := New("test error", WithRetry(3, time.Second))
initialTime := err.metadata["retry_info"].(*RetryInfo).LastAttempt
time.Sleep(time.Millisecond)
err.IncrementRetry()
retryInfo := err.metadata["retry_info"].(*RetryInfo)
assert.Equal(t, 1, retryInfo.CurrentAttempt)
assert.True(t, retryInfo.LastAttempt.After(initialTime))
})
t.Run("WithoutRetryInfo", func(t *testing.T) {
err := New("test error")
err.IncrementRetry() // Should not panic
})
}