-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry_test.go
118 lines (83 loc) · 2.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
package curator
import (
"testing"
"time"
"github.com/go-zookeeper/zk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestRetryLoop(t *testing.T) {
d := 3 * time.Second
p := NewRetryNTimes(3, d)
sleeper := &mockRetrySleeper{}
tracer := &mockTracerDriver{}
retryLoop := newRetryLoop(p, tracer)
assert.NotNil(t, retryLoop)
assert.Equal(t, 0, retryLoop.retryCount)
retryLoop.retrySleeper = sleeper
errors := []error{zk.ErrSessionExpired, zk.ErrSessionMoved, nil}
sleeper.On("SleepFor", d).Return(nil).Times(2)
tracer.On("AddCount", "retries-allowed", 1).Return().Twice()
_, err := retryLoop.CallWithRetry(func() (interface{}, error) {
return nil, errors[retryLoop.retryCount]
})
assert.NoError(t, err)
assert.Equal(t, 2, retryLoop.retryCount)
sleeper.AssertExpectations(t)
tracer.AssertExpectations(t)
// retry loop failed
retryLoop = newRetryLoop(p, nil)
_, err = retryLoop.CallWithRetry(func() (interface{}, error) {
return nil, zk.ErrClosing
})
assert.EqualError(t, err, zk.ErrClosing.Error())
}
func TestRetryNTimes(t *testing.T) {
d := 3 * time.Second
p := NewRetryNTimes(3, d)
s := &mockRetrySleeper{}
assert.NotNil(t, p)
s.On("SleepFor", d).Return(nil).Times(3)
assert.True(t, p.AllowRetry(0, 0, s))
assert.True(t, p.AllowRetry(1, 0, s))
assert.True(t, p.AllowRetry(2, 0, s))
assert.False(t, p.AllowRetry(3, 0, s))
s.AssertExpectations(t)
}
func TestRetryOneTime(t *testing.T) {
d := 3 * time.Second
p := NewRetryOneTime(d)
s := &mockRetrySleeper{}
assert.NotNil(t, p)
s.On("SleepFor", d).Return(nil).Once()
assert.True(t, p.AllowRetry(0, 0, s))
assert.False(t, p.AllowRetry(1, 0, s))
s.AssertExpectations(t)
}
func TestExponentialBackoffRetry(t *testing.T) {
d := 3 * time.Second
p := NewExponentialBackoffRetry(d, 3, 9*time.Second)
s := &mockRetrySleeper{}
assert.NotNil(t, p)
s.On("SleepFor", mock.AnythingOfType("Duration")).Return(nil).Times(3)
assert.True(t, p.AllowRetry(0, 0, s))
assert.True(t, p.AllowRetry(1, 0, s))
assert.True(t, p.AllowRetry(2, 0, s))
assert.False(t, p.AllowRetry(3, 0, s))
assert.True(t, s.Calls[0].Arguments.Get(0).(time.Duration) < 1*d)
assert.True(t, s.Calls[1].Arguments.Get(0).(time.Duration) < 2*d)
assert.True(t, s.Calls[2].Arguments.Get(0).(time.Duration) < 4*d)
s.AssertExpectations(t)
}
func TestRetryUntilElapsed(t *testing.T) {
d := 3 * time.Second
p := NewRetryUntilElapsed(3*d, d)
s := &mockRetrySleeper{}
assert.NotNil(t, p)
s.On("SleepFor", d).Return(nil).Times(3)
assert.True(t, p.AllowRetry(0, 0, s))
assert.True(t, p.AllowRetry(0, d*1, s))
assert.True(t, p.AllowRetry(0, d*2, s))
assert.False(t, p.AllowRetry(0, d*3, s))
s.AssertExpectations(t)
}