-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci_test.go
139 lines (133 loc) · 2.63 KB
/
fibonacci_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
package goretry_test
import (
"bytes"
"errors"
"testing"
"time"
"github.com/vnteamopen/goretry"
)
func TestFibonacci(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) >= 6 {
return nil
}
return errors.New("fake error")
},
expectedCounter: 6,
expectedLog: `do action()
sleep 10ms
do action()
sleep 10ms
do action()
sleep 20ms
do action()
sleep 30ms
do action()
sleep 50ms
do action()
`,
},
{
name: "MaxStopRetries",
instance: goretry.Instance{
MaxStopRetries: 4,
},
action: func(counter *int64) error {
(*counter)++
if (*counter) >= 6 {
return nil
}
return errors.New("fake error")
},
expectedCounter: 4,
expectedLog: `do action()
sleep 10ms
do action()
sleep 10ms
do action()
sleep 20ms
do action()
`,
},
{
name: "MaxStopTotalWaiting",
instance: goretry.Instance{
MaxStopTotalWaiting: time.Duration(30 * time.Millisecond),
},
action: func(counter *int64) error {
(*counter)++
if (*counter) >= 6 {
return nil
}
return errors.New("fake error")
},
expectedCounter: 4,
expectedLog: `do action()
sleep 10ms
do action()
sleep 10ms
do action()
sleep 20ms
do action()
`,
},
{
name: "MaxWaiting",
instance: goretry.Instance{
CeilingSleep: time.Duration(12 * time.Millisecond),
},
action: func(counter *int64) error {
(*counter)++
if (*counter) >= 6 {
return nil
}
return errors.New("fake error")
},
expectedCounter: 6,
expectedLog: `do action()
sleep 10ms
do action()
sleep 10ms
do action()
sleep 12ms
do action()
sleep 12ms
do action()
sleep 12ms
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.Fibonacci(10*time.Millisecond, 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())
}
})
}
}