forked from bdellegrazie/git-credential-github-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-credential-github-app_test.go
More file actions
177 lines (156 loc) · 5.1 KB
/
git-credential-github-app_test.go
File metadata and controls
177 lines (156 loc) · 5.1 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
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
package main
import (
"io"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)
// fastClient returns a retryable HTTP client with the production retry policy but
// negligible backoff so tests run quickly.
func fastClient() *http.Client {
c := newRetryableClient()
c.RetryWaitMin = time.Millisecond
c.RetryWaitMax = 5 * time.Millisecond
return c.StandardClient()
}
func TestRetriesTransientServerErrors(t *testing.T) {
var calls int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Fail with 503 for the first two attempts, then succeed.
if atomic.AddInt32(&calls, 1) <= 2 {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
io.WriteString(w, "ok")
}))
defer srv.Close()
resp, err := fastClient().Get(srv.URL)
if err != nil {
t.Fatalf("expected eventual success, got error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected status 200, got %d", resp.StatusCode)
}
if got := atomic.LoadInt32(&calls); got != 3 {
t.Fatalf("expected 3 requests (2 failures + 1 success), got %d", got)
}
}
func TestExhaustsRetriesAndSurfacesError(t *testing.T) {
var calls int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&calls, 1)
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer srv.Close()
resp, err := fastClient().Get(srv.URL)
if resp != nil {
resp.Body.Close()
}
// After retries are exhausted the client surfaces either an error or the
// final non-2xx response; in both cases the failure must reach the caller.
if err == nil && (resp == nil || resp.StatusCode < 500) {
t.Fatalf("expected failure to surface after exhausting retries, got resp=%v err=%v", resp, err)
}
// RetryMax=4 means 1 initial attempt + 4 retries = 5 total requests.
if got := atomic.LoadInt32(&calls); got != 5 {
t.Fatalf("expected 5 requests (1 + 4 retries), got %d", got)
}
}
func TestRetriesForbiddenWithRetryAfter(t *testing.T) {
var calls int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// GitHub secondary rate limits surface as 403 with Retry-After.
if atomic.AddInt32(&calls, 1) == 1 {
w.Header().Set("Retry-After", "1")
w.WriteHeader(http.StatusForbidden)
return
}
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
resp, err := fastClient().Get(srv.URL)
if err != nil {
t.Fatalf("expected eventual success, got error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected status 200, got %d", resp.StatusCode)
}
if got := atomic.LoadInt32(&calls); got != 2 {
t.Fatalf("expected 2 requests (1 rate-limited + 1 success), got %d", got)
}
}
func TestNoRetryOnForbiddenWithoutRetryAfter(t *testing.T) {
var calls int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&calls, 1)
// A plain 403 (e.g. primary rate limit / permissions) must not be retried.
w.WriteHeader(http.StatusForbidden)
}))
defer srv.Close()
resp, err := fastClient().Get(srv.URL)
if err != nil {
t.Fatalf("unexpected transport error: %v", err)
}
defer resp.Body.Close()
if got := atomic.LoadInt32(&calls); got != 1 {
t.Fatalf("expected exactly 1 request for a non-retryable 403, got %d", got)
}
}
func TestCappedBackoffHonorsButBoundsRetryAfter(t *testing.T) {
max := 30 * time.Second
min := time.Second
cases := []struct {
name string
status int
retryAfter string
want time.Duration
}{
{"503 short Retry-After respected", http.StatusServiceUnavailable, "5", 5 * time.Second},
{"503 long Retry-After capped", http.StatusServiceUnavailable, "3600", max},
{"429 Retry-After capped", http.StatusTooManyRequests, "600", max},
{"403 Retry-After respected", http.StatusForbidden, "10", 10 * time.Second},
{"403 long Retry-After capped", http.StatusForbidden, "99999", max},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
resp := &http.Response{StatusCode: tc.status, Header: http.Header{}}
resp.Header.Set("Retry-After", tc.retryAfter)
got := cappedBackoff(min, max, 0, resp)
if got != tc.want {
t.Fatalf("cappedBackoff = %v, want %v", got, tc.want)
}
})
}
}
func TestCappedBackoffFallsBackToExponential(t *testing.T) {
max := 30 * time.Second
min := time.Second
// No response / no Retry-After: exponential backoff, always within bounds.
for attempt := 0; attempt < 10; attempt++ {
got := cappedBackoff(min, max, attempt, nil)
if got > max {
t.Fatalf("attempt %d: backoff %v exceeds max %v", attempt, got, max)
}
}
}
func TestNoRetryOnSuccess(t *testing.T) {
var calls int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&calls, 1)
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
resp, err := fastClient().Get(srv.URL)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer resp.Body.Close()
if got := atomic.LoadInt32(&calls); got != 1 {
t.Fatalf("expected exactly 1 request for a successful response, got %d", got)
}
}