-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexponential_test.go
More file actions
110 lines (84 loc) · 2.57 KB
/
exponential_test.go
File metadata and controls
110 lines (84 loc) · 2.57 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
package backoff_test
import (
"errors"
"testing"
"time"
"github.com/defaltd/backoff"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)
type ExponentialTestSuite struct {
suite.Suite
service backoff.ServiceAPI
mockFunc *MockFunction
}
func (st *ExponentialTestSuite) SetupTest() {
st.service, _ = backoff.New(&backoff.Policy{
Algorithm: backoff.AlgorithmExponential,
StartInterval: time.Duration(time.Nanosecond * 100),
RandomizationFactor: 0.5,
IntervalMultiplier: 0.5,
MaxInterval: time.Duration(time.Nanosecond * 500),
MaxElapsedTime: time.Duration(time.Second * 1),
})
st.mockFunc = &MockFunction{}
}
type MockFunction struct {
mock.Mock
}
func (fun *MockFunction) BadAction() error {
return errors.New("FAILED")
}
func (fun *MockFunction) GoodAction() error {
return nil
}
func (fun *MockFunction) BadFunction() (interface{}, error) {
return nil, errors.New("FAILED")
}
func (fun *MockFunction) GoodFunction() (interface{}, error) {
return "Hello World", nil
}
func (st *ExponentialTestSuite) TestBadAction() {
startTime := time.Now()
err := st.service.ExecuteAction(st.mockFunc.BadAction)
assert.Error(st.T(), err)
assert.True(st.T(), time.Since(startTime).Nanoseconds() >= 1000)
}
func (st *ExponentialTestSuite) TestGoodAction() {
startTime := time.Now()
err := st.service.ExecuteAction(st.mockFunc.GoodAction)
assert.Nil(st.T(), err)
assert.True(st.T(), time.Since(startTime).Seconds() < 1)
}
func (st *ExponentialTestSuite) TestBadFunction() {
startTime := time.Now()
_, err := st.service.ExecuteFunction(st.mockFunc.BadFunction)
assert.Error(st.T(), err)
assert.True(st.T(), time.Since(startTime).Nanoseconds() >= 1000)
}
func (st *ExponentialTestSuite) TestGoodFunction() {
startTime := time.Now()
output, err := st.service.ExecuteFunction(st.mockFunc.GoodFunction)
assert.Nil(st.T(), err)
assert.NotNil(st.T(), output)
assert.True(st.T(), time.Since(startTime).Seconds() < 1)
}
func (st *ExponentialTestSuite) TestMaxIntervial() {
st.service, _ = backoff.New(&backoff.Policy{
Algorithm: backoff.AlgorithmExponential,
MaxInterval: time.Duration(time.Second * 1),
MaxElapsedTime: time.Duration(time.Second * 10),
})
err := st.service.ExecuteAction(st.mockFunc.BadAction)
assert.Error(st.T(), err)
}
func (st *ExponentialTestSuite) TestInvalidAlgorithm() {
_, err := backoff.New(&backoff.Policy{
Algorithm: -1,
})
assert.Error(st.T(), err)
}
func TestExponentialTestSuite(t *testing.T) {
suite.Run(t, new(ExponentialTestSuite))
}