-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathretry_decorator_test.go
103 lines (95 loc) · 2.87 KB
/
retry_decorator_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
package service_decorators
import (
"errors"
"testing"
"time"
)
var ErrorConnection = errors.New("connection exception")
var retriableChecker = func(err error) bool {
if err == ErrorConnection {
return true
}
return false
}
func TestRetryWhenRetriableErrorOccurred(t *testing.T) {
maxRetryTimes := 3
cntExecution := 0
connectionErrFn := func(req Request) (Response, error) {
cntExecution++
return cntExecution, ErrorConnection
}
retryDec, err := CreateRetryDecorator(maxRetryTimes, time.Second*1, time.Second*1, retriableChecker)
checkErr(err, t)
decFn := retryDec.Decorate(connectionErrFn)
res, decErr := decFn(1)
if decErr != ErrorConnection {
t.Error("The connection exception is expected.")
}
if res.(int) != maxRetryTimes+1 {
t.Errorf("The expected execution times is %v, the actual is %v", maxRetryTimes+1, res)
}
}
func TestRetryWhenRetriableErrorOccurredAndRecovered(t *testing.T) {
maxRetryTimes := 3
cntExecution := 0
connectionErrFn := func(req Request) (Response, error) {
cntExecution++
if cntExecution == 2 {
return cntExecution, nil
}
return cntExecution, ErrorConnection
}
retryDec, err := CreateRetryDecorator(maxRetryTimes, time.Second*1, time.Second*1, retriableChecker)
checkErr(err, t)
decFn := retryDec.Decorate(connectionErrFn)
res, decErr := decFn(1)
checkErr(decErr, t)
if res.(int) != 2 {
t.Errorf("The expected execution times is %v, the actual is %v", maxRetryTimes+1, res)
}
}
func TestRetryWithoutError(t *testing.T) {
maxRetryTimes := 3
cntExecution := 0
connectionErrFn := func(req Request) (Response, error) {
cntExecution++
return cntExecution, nil
}
retryDec, err := CreateRetryDecorator(maxRetryTimes, time.Second*1, time.Second*1, retriableChecker)
checkErr(err, t)
decFn := retryDec.Decorate(connectionErrFn)
res, decErr := decFn(1)
checkErr(decErr, t)
if res.(int) != 1 {
t.Errorf("The expected execution times is %v, the actual is %v", 1, res)
}
}
func TestRetryWhenUnretriableErrorOccurred(t *testing.T) {
maxRetryTimes := 3
cntExecution := 0
connectionErrFn := func(req Request) (Response, error) {
cntExecution++
return cntExecution, errors.New("other exception")
}
retryDec, err := CreateRetryDecorator(maxRetryTimes, time.Second*1, time.Second*1, retriableChecker)
checkErr(err, t)
decFn := retryDec.Decorate(connectionErrFn)
res, decErr := decFn(1)
if decErr == nil || decErr == ErrorConnection {
t.Error("The error is not expected.")
}
if res.(int) != 1 {
t.Errorf("The expected execution times is %v, the actual is %v", 1, res)
}
}
func TestCreateDecorateWithInvalidSettings(t *testing.T) {
var err1, err2 error
_, err1 = CreateRetryDecorator(0, time.Second*1, time.Second*1, retriableChecker)
if err1 == nil {
t.Error("Setting error is expected")
}
_, err2 = CreateRetryDecorator(3, 0, time.Second*1, retriableChecker)
if err2 == nil {
t.Error("Setting error is expected")
}
}