/
retry_test.go
169 lines (145 loc) · 4.84 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package aurestretry
import (
"context"
"errors"
aulogging "github.com/StephanHCB/go-autumn-logging"
aurestclientapi "github.com/StephanHCB/go-autumn-restclient/api"
aurestcapture "github.com/StephanHCB/go-autumn-restclient/implementation/capture"
aurestmock "github.com/StephanHCB/go-autumn-restclient/implementation/mock"
"github.com/go-http-utils/headers"
"github.com/stretchr/testify/require"
"testing"
)
func tstMock() aurestclientapi.Client {
mockClient := aurestmock.New(
map[string]aurestclientapi.ParsedResponse{
"GET http://ok <nil>": {
Body: nil,
Status: 200,
Header: map[string][]string{
headers.ContentType: []string{aurestclientapi.ContentTypeApplicationJson},
},
},
"GET http://500 <nil>": {
Body: nil,
Status: 500,
Header: map[string][]string{
headers.ContentType: []string{aurestclientapi.ContentTypeApplicationJson},
},
},
},
map[string]error{
"GET http://err <nil>": errors.New("some transport error"),
},
)
recordingMockClient := aurestcapture.New(mockClient)
return recordingMockClient
}
func TestSuccessNoRetry(t *testing.T) {
aulogging.SetupNoLoggerForTesting()
mock := tstMock()
cut := New(mock, 0,
func(ctx context.Context, response *aurestclientapi.ParsedResponse, err error) bool {
return true
},
nil)
response := &aurestclientapi.ParsedResponse{}
err := cut.Perform(context.Background(), "GET", "http://ok", nil, response)
require.Nil(t, err)
require.Equal(t, []string{"GET http://ok <nil>"}, aurestcapture.GetRecording(mock))
}
func TestSuccessWithRetry(t *testing.T) {
aulogging.SetupNoLoggerForTesting()
mock := tstMock()
cut := New(mock, 2,
func(ctx context.Context, response *aurestclientapi.ParsedResponse, err error) bool {
return true
},
nil)
response := &aurestclientapi.ParsedResponse{}
err := cut.Perform(context.Background(), "GET", "http://ok", nil, response)
require.Nil(t, err)
r := "GET http://ok <nil>"
require.Equal(t, []string{r, r, r}, aurestcapture.GetRecording(mock))
}
func TestSuccessWithNotNeededRetry(t *testing.T) {
aulogging.SetupNoLoggerForTesting()
mock := tstMock()
cut := New(mock, 2,
func(ctx context.Context, response *aurestclientapi.ParsedResponse, err error) bool {
return false
},
nil)
response := &aurestclientapi.ParsedResponse{}
err := cut.Perform(context.Background(), "GET", "http://ok", nil, response)
require.Nil(t, err)
r := "GET http://ok <nil>"
require.Equal(t, []string{r}, aurestcapture.GetRecording(mock))
}
func TestFailNoRetry(t *testing.T) {
aulogging.SetupNoLoggerForTesting()
mock := tstMock()
cut := New(mock, 0,
func(ctx context.Context, response *aurestclientapi.ParsedResponse, err error) bool {
return true
},
nil)
response := &aurestclientapi.ParsedResponse{}
err := cut.Perform(context.Background(), "GET", "http://err", nil, response)
require.Equal(t, "some transport error", err.Error())
require.Equal(t, []string{"GET http://err <nil>"}, aurestcapture.GetRecording(mock))
}
func TestFailWithRetry(t *testing.T) {
aulogging.SetupNoLoggerForTesting()
retryCount := 0
mock := tstMock()
cut := New(mock, 3,
func(ctx context.Context, response *aurestclientapi.ParsedResponse, err error) bool {
return true
},
func(ctx context.Context, originalResponse *aurestclientapi.ParsedResponse, originalError error) error {
retryCount++
return nil
})
response := &aurestclientapi.ParsedResponse{}
err := cut.Perform(context.Background(), "GET", "http://err", nil, response)
require.NotNil(t, err)
require.Equal(t, "some transport error", err.Error())
r := "GET http://err <nil>"
require.Equal(t, []string{r, r, r, r}, aurestcapture.GetRecording(mock))
}
func TestFailWithRetryWithOpts(t *testing.T) {
aulogging.SetupNoLoggerForTesting()
mock := tstMock()
cut := NewWithOptions(mock,
func(ctx context.Context, response *aurestclientapi.ParsedResponse, err error) bool {
return true
},
RetryOptions{
BeforeRetryOrNil: nil,
SilenceGivingUp: true,
},
)
response := &aurestclientapi.ParsedResponse{}
err := cut.Perform(context.Background(), "GET", "http://err", nil, response)
require.NotNil(t, err)
require.Equal(t, "some transport error", err.Error())
r := "GET http://err <nil>"
require.Equal(t, []string{r, r, r}, aurestcapture.GetRecording(mock))
}
func TestAbortRetry(t *testing.T) {
aulogging.SetupNoLoggerForTesting()
mock := tstMock()
cut := New(mock, 2,
func(ctx context.Context, response *aurestclientapi.ParsedResponse, err error) bool {
return true
},
func(ctx context.Context, originalResponse *aurestclientapi.ParsedResponse, originalError error) error {
return originalError
})
response := &aurestclientapi.ParsedResponse{}
err := cut.Perform(context.Background(), "GET", "http://err", nil, response)
require.NotNil(t, err)
r := "GET http://err <nil>"
require.Equal(t, []string{r}, aurestcapture.GetRecording(mock))
}