-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
retry_test.go
164 lines (145 loc) · 3.67 KB
/
retry_test.go
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
package retry_test
import (
"context"
"fmt"
"reflect"
"testing"
"time"
. "github.com/kamilsk/retry/v5"
"github.com/kamilsk/retry/v5/strategy"
)
func TestDo(t *testing.T) {
tests := testCases
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var attempts uint
action := func(ctx context.Context) error {
attempts++
return test.action(ctx)
}
err := Do(test.breaker, action, test.strategies...)
if test.expected.attempts != attempts {
t.Errorf("expected: %d, obtained: %d", test.expected.attempts, attempts)
}
if !reflect.DeepEqual(test.expected.error, err) {
t.Errorf("expected: %#v, obtained: %#v", test.expected.error, err)
}
})
}
t.Run("preserve context values", func(t *testing.T) {
val := "value"
ctx := context.WithValue(context.TODO(), key{}, val)
action := func(ctx context.Context) error {
if !reflect.DeepEqual(val, ctx.Value(key{})) {
t.Error("value is not preserved")
}
return nil
}
if err := Do(ctx, action); err != nil {
t.Error("unexpected error")
}
})
}
func TestGo(t *testing.T) {
tests := append(
testCases,
testCase{
"action call with error panic",
breaker(),
How{strategy.Wait(time.Hour)},
func(context.Context) error { panic(Error("failure")) },
expected{1, Error("failure")},
},
testCase{
"action call with non-error panic",
breaker(),
How{strategy.Wait(time.Hour)},
func(context.Context) error { panic("non-error") },
expected{1, fmt.Errorf("retry: unexpected panic: %#v", "non-error")},
},
)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var attempts uint
action := func(ctx context.Context) error {
attempts++
return test.action(ctx)
}
err := Go(test.breaker, action, test.strategies...)
if test.expected.attempts != attempts {
t.Errorf("expected: %d, obtained: %d", test.expected.attempts, attempts)
}
if !reflect.DeepEqual(test.expected.error, err) {
t.Errorf("expected: %#v, obtained: %#v", test.expected.error, err)
}
})
}
t.Run("preserve context values", func(t *testing.T) {
val := "value"
ctx := context.WithValue(context.TODO(), key{}, val)
action := func(ctx context.Context) error {
if !reflect.DeepEqual(val, ctx.Value(key{})) {
t.Error("value is not preserved")
}
return nil
}
if err := Go(ctx, action); err != nil {
t.Error("unexpected error")
}
})
}
// helpers
func breaker() Breaker {
return context.TODO()
}
func interrupted() Breaker {
ctx, cancel := context.WithCancel(context.TODO())
cancel()
return ctx
}
type causer struct{ error }
func (causer causer) Cause() error { return causer.error }
type expected struct {
attempts uint
error error
}
type key struct{}
type layer struct{ error }
func (layer layer) Unwrap() error { return layer.error }
type testCase struct {
name string
breaker Breaker
strategies How
action func(context.Context) error
expected expected
}
var testCases = []testCase{
{
"successful action call",
breaker(),
How{strategy.Wait(time.Hour)},
func(context.Context) error { return nil },
expected{1, nil},
},
{
"failed action call",
breaker(),
How{strategy.Limit(10)},
func(context.Context) error { return layer{causer{Error("failure")}} },
expected{10, layer{causer{Error("failure")}}},
},
{
"action call with interrupted breaker",
interrupted(),
How{strategy.Delay(time.Hour)},
func(context.Context) error { return Error("zero iterations") },
expected{0, context.Canceled},
},
{
"have no action call",
breaker(),
How{strategy.Limit(0)},
func(context.Context) error { return layer{causer{Error("failure")}} },
expected{0, Error("have no any try")},
},
}