Repository navigation
Expand file tree
/
Copy pathretry_test.go
More file actions
187 lines (175 loc) · 4.85 KB
/
Copy pathretry_test.go
File metadata and controls
187 lines (175 loc) · 4.85 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
package cron_test
import (
"context"
"errors"
"sync/atomic"
"testing"
"testing/synctest"
"time"
"github.com/libtnb/cron"
)
func TestRetryPolicy_IsZero(t *testing.T) {
if !(cron.RetryPolicy{}).IsZero() {
t.Fatal("zero policy must be IsZero")
}
if (cron.RetryPolicy{MaxRetries: 1}).IsZero() {
t.Fatal("MaxRetries=1 is non-zero")
}
if !(cron.RetryPolicy{Initial: time.Second, Multiplier: 2}).IsZero() {
t.Fatal("MaxRetries=0 must mean IsZero regardless of other fields")
}
}
func TestRetry_ConstructorEquivalentToStructLiteral(t *testing.T) {
got := cron.Retry(3,
cron.RetryInitial(10*time.Millisecond),
cron.RetryMaxDelay(100*time.Millisecond),
cron.RetryMultiplier(2),
cron.RetryJitterFrac(0.1),
)
want := cron.RetryPolicy{
MaxRetries: 3,
Initial: 10 * time.Millisecond,
MaxDelay: 100 * time.Millisecond,
Multiplier: 2,
JitterFrac: 0.1,
}
if got != want {
t.Fatalf("got %+v, want %+v", got, want)
}
}
func TestRetryPolicy_WrapperRetriesUntilSuccess(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
var attempts atomic.Int32
boom := errors.New("boom")
p := cron.RetryPolicy{
MaxRetries: 2,
Initial: 10 * time.Millisecond,
MaxDelay: 100 * time.Millisecond,
Multiplier: 2.0,
JitterFrac: 0.1,
}
j := p.Wrapper()(cron.JobFunc(func(ctx context.Context) error {
if attempts.Add(1) < 3 {
return boom
}
return nil
}))
if err := j.Run(context.Background()); err != nil {
t.Fatalf("err=%v", err)
}
if got := attempts.Load(); got != 3 {
t.Fatalf("attempts=%d", got)
}
})
}
func TestRetryPolicy_WrapperNegativeMaxIsForeverUntilCtx(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
p := cron.RetryPolicy{MaxRetries: -1, Initial: time.Millisecond}
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
err := p.Wrapper()(cron.JobFunc(func(ctx context.Context) error {
return errors.New("always")
})).Run(ctx)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("err=%v", err)
}
})
}
func TestRetryPolicy_WrapperZeroMaxNoRetry(t *testing.T) {
var attempts atomic.Int32
boom := errors.New("boom")
err := cron.RetryPolicy{}.Wrapper()(cron.JobFunc(func(ctx context.Context) error {
attempts.Add(1)
return boom
})).Run(context.Background())
if !errors.Is(err, boom) {
t.Fatalf("err=%v", err)
}
if attempts.Load() != 1 {
t.Fatalf("attempts=%d, want 1 (no retries on zero policy via Wrapper)", attempts.Load())
}
}
func TestRetryPolicy_WrapperDefaultInitialOneSecond(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
var attempts atomic.Int32
policy := cron.RetryPolicy{MaxRetries: 1}
err := policy.Wrapper()(cron.JobFunc(func(ctx context.Context) error {
attempts.Add(1)
return errors.New("boom")
})).Run(context.Background())
if err == nil {
t.Fatal("expected error")
}
if attempts.Load() != 2 {
t.Fatalf("attempts=%d, want 2", attempts.Load())
}
})
}
func TestRetryPolicy_WrapperMaxDelayCaps(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
var attempts atomic.Int32
policy := cron.RetryPolicy{
MaxRetries: 3,
Initial: 10 * time.Millisecond,
MaxDelay: 20 * time.Millisecond,
Multiplier: 10.0,
}
_ = policy.Wrapper()(cron.JobFunc(func(ctx context.Context) error {
attempts.Add(1)
return errors.New("boom")
})).Run(context.Background())
if attempts.Load() != 4 {
t.Fatalf("attempts=%d, want 4", attempts.Load())
}
})
}
func TestRetryPolicy_WrapperCtxCancelDuringBackoff(t *testing.T) {
policy := cron.RetryPolicy{
MaxRetries: 5,
Initial: 50 * time.Millisecond,
}
ctx, cancel := context.WithCancel(context.Background())
var attempts atomic.Int32
job := cron.JobFunc(func(ctx context.Context) error {
if attempts.Add(1) == 1 {
go cancel()
}
return errors.New("boom")
})
err := policy.Wrapper()(job).Run(ctx)
if !errors.Is(err, context.Canceled) {
t.Fatalf("err=%v, want context.Canceled", err)
}
}
func TestRetryPolicy_WrapperBackoffZeroSkipsTimer(t *testing.T) {
var attempts atomic.Int32
policy := cron.RetryPolicy{
MaxRetries: 3,
Initial: time.Millisecond,
JitterFrac: 5.0, // jitter range exceeds d, can land on 0 or negative -> clamped to 0
}
_ = policy.Wrapper()(cron.JobFunc(func(ctx context.Context) error {
attempts.Add(1)
return errors.New("boom")
})).Run(context.Background())
if attempts.Load() != 4 {
t.Fatalf("attempts=%d, want 4", attempts.Load())
}
}
func TestRetryPolicy_WrapperJitterPositive(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
var attempts atomic.Int32
policy := cron.RetryPolicy{
MaxRetries: 2,
Initial: 100 * time.Millisecond,
JitterFrac: 0.5,
}
_ = policy.Wrapper()(cron.JobFunc(func(ctx context.Context) error {
attempts.Add(1)
return errors.New("boom")
})).Run(context.Background())
if attempts.Load() != 3 {
t.Fatalf("attempts=%d, want 3", attempts.Load())
}
})
}