-
Notifications
You must be signed in to change notification settings - Fork 1
/
retry_test.go
92 lines (81 loc) · 1.8 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
package goneric
import (
"errors"
"github.com/stretchr/testify/assert"
"sync"
"testing"
"time"
)
type fa struct {
i int
sync.Mutex
}
func (f *fa) OkAfter(i int) (int, error) {
f.Lock()
defer f.Unlock()
f.i++
if f.i >= i {
return f.i, nil
} else {
return f.i, errors.New("fail")
}
}
func TestTools(t *testing.T) {
a := fa{}
r1, e1 := a.OkAfter(3)
assert.Equal(t, 1, r1)
assert.Error(t, e1)
r2, e2 := a.OkAfter(3)
assert.Equal(t, 2, r2)
assert.Error(t, e2)
r3, e3 := a.OkAfter(3)
assert.Equal(t, 3, r3)
assert.NoError(t, e3)
}
func TestRetry(t *testing.T) {
a1 := fa{}
out1, err1 := Retry(3, func() (int, error) { return a1.OkAfter(3) })
assert.NoError(t, err1)
assert.Equal(t, 3, out1)
a2 := fa{}
out, err2 := Retry(3, func() (int, error) { return a2.OkAfter(2) })
assert.NoError(t, err2)
assert.Equal(t, 2, out)
a3 := fa{}
out, err3 := Retry(3, func() (int, error) { return a3.OkAfter(4) })
assert.Error(t, err3)
assert.Equal(t, 3, out)
}
func TestRetryExp(t *testing.T) {
a1 := fa{}
out1, err1 := RetryAfter(
time.Millisecond*5,
time.Millisecond*50,
time.Millisecond*500,
func() (int, error) {
return a1.OkAfter(3)
})
assert.NoError(t, err1)
assert.Equal(t, 3, out1)
a2 := fa{}
_, err2 := RetryAfter(
time.Millisecond*30,
time.Millisecond*40,
time.Millisecond*50,
func() (int, error) { return a2.OkAfter(9) })
assert.Error(t, err2)
}
func TestTry(t *testing.T) {
funcList := []func() (int, error){
func() (int, error) { return 1, errors.New("1") },
func() (int, error) { return 2, errors.New("2") },
func() (int, error) { return 3, errors.New("3") },
func() (int, error) { return 4, nil },
}
out, err := Try(funcList...)
assert.Equal(t, 4, out)
assert.NoError(t, err)
out, err = Try(funcList[:2]...)
assert.Equal(t, 2, out)
assert.Error(t, err)
}