-
Notifications
You must be signed in to change notification settings - Fork 28
/
fixtures.go
218 lines (182 loc) · 5.11 KB
/
fixtures.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package testutil
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"sync/atomic"
"time"
"github.com/failsafe-go/failsafe-go"
)
type CompositeError struct {
Cause error
}
func (ce *CompositeError) Error() string {
return "CompositeError"
}
func (ce *CompositeError) Unwrap() error {
return ce.Cause
}
func (ce *CompositeError) Is(err error) bool {
_, ok := err.(*CompositeError)
return ok
}
func NewCompositeError(cause error) *CompositeError {
return &CompositeError{
Cause: cause,
}
}
var ErrInvalidArgument = errors.New("invalid argument")
var ErrInvalidState = errors.New("invalid state")
var ErrConnecting = errors.New("connection error")
var NoopFn = func() error {
return nil
}
var GetFalseFn = func() (bool, error) {
return false, nil
}
var GetTrueFn = func() (bool, error) {
return true, nil
}
func RunFn(err error) func(failsafe.Execution[any]) error {
return func(exec failsafe.Execution[any]) error {
return err
}
}
func GetFn[R any](result R, err error) func(failsafe.Execution[R]) (R, error) {
return func(exec failsafe.Execution[R]) (R, error) {
return result, err
}
}
// ErrorNTimesThenReturn returns a stub function that returns the err errorTimes and then returns the results.
// Can be used with failsafe.GetWithExecution.
func ErrorNTimesThenReturn[R any](err error, errorTimes int, results ...R) (fn func(failsafe.Execution[R]) (R, error), resetFn func()) {
errorCounter := 0
resultIndex := 0
return func(exec failsafe.Execution[R]) (R, error) {
if errorCounter < errorTimes {
errorCounter++
return *(new(R)), err
} else if resultIndex < len(results) {
result := results[resultIndex]
resultIndex++
return result, nil
}
return *(new(R)), nil
}, func() {
errorCounter = 0
resultIndex = 0
}
}
// ErrorNTimesThenPanic returns a stub function that returns the err errorTimes and then panics with the panicValue.
// Can be used with failsafe.GetWithExecution.
func ErrorNTimesThenPanic[R any](err error, errorTimes int, panicValue any) func(failsafe.Execution[R]) (R, error) {
errorCounter := 0
return func(exec failsafe.Execution[R]) (R, error) {
if errorCounter < errorTimes {
errorCounter++
return *(new(R)), err
}
panic(panicValue)
}
}
// ErrorNTimesThenError returns a stub function that returns the err errorTimes and then returns the finalError.
// Can be used with failsafe.GetWithExecution.
func ErrorNTimesThenError[R any](err error, errorTimes int, finalError error) func(failsafe.Execution[R]) (R, error) {
errorCounter := 0
return func(exec failsafe.Execution[R]) (R, error) {
if errorCounter < errorTimes {
errorCounter++
return *(new(R)), err
}
return *(new(R)), finalError
}
}
func MockResponse(statusCode int, body string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
w.WriteHeader(statusCode)
fmt.Fprintf(w, body)
}))
}
func MockFlakyServer(failTimes int, responseCode int, retryAfterDelay time.Duration, finalResponse string) *httptest.Server {
failures := atomic.Int32{}
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
if failures.Add(1) <= int32(failTimes) {
if retryAfterDelay > 0 {
w.Header().Add("Retry-After", strconv.Itoa(int(retryAfterDelay.Seconds())))
}
fmt.Println("Replying with", responseCode)
w.WriteHeader(responseCode)
} else {
fmt.Fprintf(w, finalResponse)
}
}))
}
func MockDelayedResponse(statusCode int, body string, delay time.Duration) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
timer := time.NewTimer(delay)
select {
case <-timer.C:
w.WriteHeader(statusCode)
fmt.Fprintf(w, body)
case <-request.Context().Done():
timer.Stop()
}
}))
}
type TestExecution[R any] struct {
TheLastResult R
TheAttempts int
TheRetries int
TheHedges int
}
func (e TestExecution[R]) Attempts() int {
return e.TheAttempts
}
func (e TestExecution[R]) Executions() int {
panic("unimplemented stub")
}
func (e TestExecution[R]) StartTime() time.Time {
panic("unimplemented stub")
}
func (e TestExecution[R]) Retries() int {
return e.TheRetries
}
func (e TestExecution[R]) Hedges() int {
return e.TheHedges
}
func (e TestExecution[R]) IsFirstAttempt() bool {
panic("unimplemented stub")
}
func (e TestExecution[R]) IsRetry() bool {
panic("unimplemented stub")
}
func (e TestExecution[R]) ElapsedTime() time.Duration {
panic("unimplemented stub")
}
func (e TestExecution[R]) IsHedge() bool {
panic("unimplemented stub")
}
func (e TestExecution[R]) LastResult() R {
return e.TheLastResult
}
func (e TestExecution[R]) LastError() error {
panic("unimplemented stub")
}
func (e TestExecution[R]) AttemptStartTime() time.Time {
panic("unimplemented stub")
}
func (e TestExecution[R]) ElapsedAttemptTime() time.Duration {
panic("unimplemented stub")
}
func (e TestExecution[R]) Context() context.Context {
return nil
}
func (e TestExecution[R]) IsCanceled() bool {
panic("unimplemented stub")
}
func (e TestExecution[R]) Canceled() <-chan struct{} {
panic("unimplemented stub")
}