Repository navigation
Expand file tree
/
Copy pathretry_test.go
More file actions
109 lines (103 loc) · 3.73 KB
/
Copy pathretry_test.go
File metadata and controls
109 lines (103 loc) · 3.73 KB
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
package sdk
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
)
func newRetryClient(t *testing.T, retries int, h http.HandlerFunc) *Client {
t.Helper()
srv := httptest.NewServer(h)
t.Cleanup(srv.Close)
c, err := New(srv.URL, APIKeyAuth{Key: "k", Secret: "s"}, WithRetries(retries))
if err != nil {
t.Fatal(err)
}
return c
}
func TestRetriesOnlyIdempotentMethods(t *testing.T) {
var calls atomic.Int32
h := func(w http.ResponseWriter, _ *http.Request) {
if calls.Add(1) < 3 {
w.Header().Set("Retry-After", "0")
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusNoContent)
}
ctx := context.Background()
if _, err := newRetryClient(t, 0, h).Do(ctx, Request{Operation: "x", Method: "DELETE", Path: "/api/x"}); !errors.Is(err, ErrServer) || calls.Load() != 1 {
t.Fatalf("off: err = %v, calls = %d", err, calls.Load())
}
calls.Store(0)
if _, err := newRetryClient(t, 3, h).Do(ctx, Request{Operation: "x", Method: "POST", Path: "/api/x", Body: []byte(`{}`)}); !errors.Is(err, ErrServer) || calls.Load() != 1 {
t.Fatalf("POST: err = %v, calls = %d", err, calls.Load())
}
calls.Store(0)
if _, err := newRetryClient(t, 3, h).Do(ctx, Request{Operation: "x", Method: "DELETE", Path: "/api/x"}); err != nil || calls.Load() != 3 {
t.Fatalf("DELETE: err = %v, calls = %d", err, calls.Load())
}
}
func TestRetryAfterTooLongIsNotWaitedFor(t *testing.T) {
var calls atomic.Int32
c := newRetryClient(t, 5, func(w http.ResponseWriter, _ *http.Request) {
calls.Add(1)
w.Header().Set("Retry-After", "3600")
w.WriteHeader(http.StatusTooManyRequests)
})
_, err := c.Do(context.Background(), Request{Operation: "x", Method: "GET", Path: "/api/x"})
var e *Error
if !errors.As(err, &e) || e.Status != 429 || e.Header.Get("Retry-After") != "3600" || calls.Load() != 1 {
t.Fatalf("err = %v, calls = %d", err, calls.Load())
}
}
func TestARetryAfterARefreshCarriesTheNewToken(t *testing.T) {
var auths []string
src := &countingSource{tokens: []string{"old", "new", "newer"}}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auths = append(auths, r.Header.Get("Authorization"))
switch len(auths) {
case 1:
w.WriteHeader(http.StatusUnauthorized)
case 2:
w.Header().Set("Retry-After", "0")
w.WriteHeader(http.StatusServiceUnavailable)
default:
w.WriteHeader(http.StatusNoContent)
}
}))
t.Cleanup(srv.Close)
c, err := New(srv.URL, BearerAuth{Source: src}, WithRetries(2))
if err != nil {
t.Fatal(err)
}
if _, err := c.Do(context.Background(), Request{Operation: "x", Method: "GET", Path: "/api/x"}); err != nil {
t.Fatal(err)
}
if want := []string{"Bearer old", "Bearer new", "Bearer new"}; strings.Join(auths, ",") != strings.Join(want, ",") || src.invalidated != 1 {
t.Errorf("auths = %q, invalidated %d times", auths, src.invalidated)
}
}
func TestARefreshDoesNotResetTheRetryBudget(t *testing.T) {
var statuses []int
src := &countingSource{tokens: []string{"old", "new", "newer"}}
replies := []int{http.StatusServiceUnavailable, http.StatusUnauthorized, http.StatusServiceUnavailable, http.StatusNoContent}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
code := replies[len(statuses)]
statuses = append(statuses, code)
w.Header().Set("Retry-After", "0")
w.WriteHeader(code)
}))
t.Cleanup(srv.Close)
c, err := New(srv.URL, BearerAuth{Source: src}, WithRetries(1))
if err != nil {
t.Fatal(err)
}
_, err = c.Do(context.Background(), Request{Operation: "x", Method: "GET", Path: "/api/x"})
if !errors.Is(err, ErrServer) || len(statuses) != 3 || src.invalidated != 1 {
t.Fatalf("err = %v, replies sent = %v, refreshed %d times", err, statuses, src.invalidated)
}
}