/
client_test.go
133 lines (110 loc) · 3.42 KB
/
client_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
// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package stripe_test
import (
"bytes"
"context"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stripe/stripe-go/v75"
"github.com/stripe/stripe-go/v75/form"
"go.uber.org/zap/zaptest"
"storj.io/common/testcontext"
"storj.io/common/time2"
stripe1 "storj.io/storj/satellite/payments/stripe"
)
var backendError = &stripe.Error{
APIResource: stripe.APIResource{
LastResponse: &stripe.APIResponse{
Header: http.Header{
"Stripe-Should-Retry": []string{"true"},
},
StatusCode: http.StatusTooManyRequests,
},
},
}
type mockBackend struct {
calls int64
}
func (b *mockBackend) Call(method, path, key string, params stripe.ParamsContainer, v stripe.LastResponseSetter) error {
b.calls++
return backendError
}
func (b *mockBackend) CallStreaming(method, path, key string, params stripe.ParamsContainer, v stripe.StreamingLastResponseSetter) error {
return b.Call("", "", "", nil, nil)
}
func (b *mockBackend) CallRaw(method, path, key string, body *form.Values, params *stripe.Params, v stripe.LastResponseSetter) error {
return b.Call("", "", "", nil, nil)
}
func (b *mockBackend) CallMultipart(method, path, key, boundary string, body *bytes.Buffer, params *stripe.Params, v stripe.LastResponseSetter) error {
return b.Call("", "", "", nil, nil)
}
func (b *mockBackend) SetMaxNetworkRetries(max int64) {}
func TestBackendWrapper(t *testing.T) {
tm := time2.NewMachine()
retryCfg := stripe1.RetryConfig{
InitialBackoff: time.Millisecond,
MaxBackoff: 3 * time.Millisecond,
Multiplier: 2,
MaxRetries: 5,
}
backend := stripe1.NewBackendWrapper(zaptest.NewLogger(t), stripe.APIBackend, retryCfg)
mock := &mockBackend{}
backend.TestSwapBackend(mock)
backend.TestSwapClock(tm.Clock())
newCall := func(t *testing.T, ctx context.Context) (wait func(context.Context) error) {
mock.calls = 0
done := make(chan error)
go func() {
done <- backend.Call("", "", "", &stripe.Params{Context: ctx}, nil)
}()
wait = func(ctx context.Context) error {
select {
case err := <-done:
return err
case <-ctx.Done():
return ctx.Err()
}
}
return wait
}
t.Run("backoff intervals", func(t *testing.T) {
ctx := testcontext.New(t)
wait := newCall(t, ctx)
expectedBackoff := retryCfg.InitialBackoff
for i := 0; i < int(retryCfg.MaxRetries); i++ {
if !tm.BlockThenAdvance(ctx, 1, expectedBackoff) {
t.Fatal("failed waiting for the client to attempt retry", i+1)
}
expectedBackoff *= 2
if expectedBackoff > retryCfg.MaxBackoff {
expectedBackoff = retryCfg.MaxBackoff
}
}
require.Error(t, wait(ctx), backendError)
require.Equal(t, retryCfg.MaxRetries+1, mock.calls)
})
t.Run("context cancellation", func(t *testing.T) {
t.Run("before retries", func(t *testing.T) {
ctx := testcontext.New(t)
subCtx, cancel := context.WithCancel(ctx)
cancel()
wait := newCall(t, subCtx)
require.ErrorIs(t, wait(ctx), context.Canceled)
require.Zero(t, mock.calls)
})
t.Run("during retries", func(t *testing.T) {
ctx := testcontext.New(t)
subCtx, cancel := context.WithCancel(ctx)
wait := newCall(t, subCtx)
if !tm.BlockThenAdvance(ctx, 1, retryCfg.InitialBackoff) {
t.Fatal("failed waiting for the client to attempt first retry")
}
cancel()
require.ErrorIs(t, wait(ctx), context.Canceled)
require.Equal(t, int64(2), mock.calls)
})
})
}