-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretryable_action_test.go
More file actions
98 lines (77 loc) · 2.3 KB
/
retryable_action_test.go
File metadata and controls
98 lines (77 loc) · 2.3 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
package chain
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"testing"
)
func TestRetryableAction(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
checkZero := NewSimpleAction(
"checkZero",
func(ctx context.Context, input int) (int, error) {
if input == 0 {
return input, nil
}
return input, fmt.Errorf("%d is not zero", input)
})
decrease := NewSimpleAction(
"decrase",
func(ctx context.Context, input int) (int, error) {
return input - 1, nil
})
expectZero := AsRetryableAction("expectZero", checkZero, decrease, 3)
t.Run("direct success", func(t *testing.T) {
output, err := expectZero.Run(context.Background(), 0)
assert.NoError(t, err)
assert.Equal(t, 0, output)
})
t.Run("first retry success", func(t *testing.T) {
output, err := expectZero.Run(context.Background(), 1)
assert.NoError(t, err)
assert.Equal(t, 0, output)
})
t.Run("max retry success", func(t *testing.T) {
output, err := expectZero.Run(context.Background(), 2)
assert.NoError(t, err)
assert.Equal(t, 0, output)
})
t.Run("max retry fail", func(t *testing.T) {
output, err := expectZero.Run(context.Background(), 10)
assert.Error(t, err)
assert.NotEqual(t, 0, output)
})
}
func TestRetryableAction_withoutRollback(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
checkZeroAndDecrease := NewSimpleAction(
"checkZeroAndDecrease",
func(ctx context.Context, input int) (int, error) {
if input == 0 {
return input, nil
}
return input - 1, fmt.Errorf("%d was not zero", input)
})
expectZero := AsRetryableAction("expectZero", checkZeroAndDecrease, SkipRollback[int](), 3)
t.Run("direct success", func(t *testing.T) {
output, err := expectZero.Run(context.Background(), 0)
assert.NoError(t, err)
assert.Equal(t, 0, output)
})
t.Run("first retry success", func(t *testing.T) {
output, err := expectZero.Run(context.Background(), 1)
assert.NoError(t, err)
assert.Equal(t, 0, output)
})
t.Run("max retry success", func(t *testing.T) {
output, err := expectZero.Run(context.Background(), 2)
assert.NoError(t, err)
assert.Equal(t, 0, output)
})
t.Run("max retry fail", func(t *testing.T) {
output, err := expectZero.Run(context.Background(), 10)
assert.Error(t, err)
assert.NotEqual(t, 0, output)
})
}