-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial_test.go
125 lines (119 loc) · 2.48 KB
/
polynomial_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
package goretry_test
import (
"bytes"
"errors"
"testing"
"time"
"github.com/vnteamopen/goretry"
)
func TestPolynomial(t *testing.T) {
testCases := []struct {
name string
instance goretry.Instance
action func(counter *int64) error
expectedCounter int64
expectedLog string
}{
{
name: "default",
instance: goretry.Instance{},
action: func(counter *int64) error {
(*counter)++
if (*counter) >= 4 {
return nil
}
return errors.New("fake error")
},
expectedCounter: 4,
expectedLog: `do action()
sleep 10ms
do action()
sleep 160ms
do action()
sleep 810ms
do action()
`,
},
{
name: "MaxStopRetries",
instance: goretry.Instance{
MaxStopRetries: 3,
},
action: func(counter *int64) error {
(*counter)++
if (*counter) >= 4 {
return nil
}
return errors.New("fake error")
},
expectedCounter: 3,
expectedLog: `do action()
sleep 10ms
do action()
sleep 160ms
do action()
`,
},
{
name: "MaxStopTotalWaiting",
instance: goretry.Instance{
MaxStopTotalWaiting: time.Duration(50 * time.Millisecond),
},
action: func(counter *int64) error {
(*counter)++
if (*counter) >= 4 {
return nil
}
return errors.New("fake error")
},
expectedCounter: 2,
expectedLog: `do action()
sleep 10ms
do action()
`,
},
{
name: "MaxWaiting",
instance: goretry.Instance{
CeilingSleep: time.Duration(165 * time.Millisecond),
},
action: func(counter *int64) error {
(*counter)++
if (*counter) >= 4 {
return nil
}
return errors.New("fake error")
},
expectedCounter: 4,
expectedLog: `do action()
sleep 10ms
do action()
sleep 160ms
do action()
sleep 165ms
do action()
`,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
var counter int64
var buffer bytes.Buffer
instance := goretry.Instance{
MaxStopRetries: testCase.instance.MaxStopRetries,
MaxStopTotalWaiting: testCase.instance.MaxStopTotalWaiting,
CeilingSleep: testCase.instance.CeilingSleep,
Logger: &buffer,
}
instance.Polynomial(10*time.Millisecond, 4, func() error {
return testCase.action(&counter)
})
if counter != testCase.expectedCounter {
t.Errorf("Do() expected counting: %d, actual: %d", testCase.expectedCounter, counter)
}
if buffer.String() != testCase.expectedLog {
t.Errorf("Expected: %v, got: %v", testCase.expectedLog, buffer.String())
}
})
}
}