-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathretry_test.go
More file actions
268 lines (251 loc) · 7.46 KB
/
Copy pathretry_test.go
File metadata and controls
268 lines (251 loc) · 7.46 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package clone
import (
"context"
"errors"
"os"
"path/filepath"
"slices"
"testing"
"time"
)
var errGitExit = errors.New("exit status 128")
func TestRetryRetriesTransientFailureAndNotifies(t *testing.T) {
calls := 0
var notices []Notice
retry := Retry{
Attempts: 2,
Run: func(context.Context, string, []string, ...string) (string, error) {
calls++
if calls == 1 {
return "fatal: the remote end hung up unexpectedly", errGitExit
}
return "ok", nil
},
Sleep: func(context.Context, time.Duration) error { return nil },
Notify: func(notice Notice) { notices = append(notices, notice) },
}
out, err := retry.Do(context.Background(), Command{Label: "fetch", Args: []string{"fetch"}})
if err != nil {
t.Fatalf("Do: %v", err)
}
if out != "ok" || calls != 2 {
t.Fatalf("Do returned %q after %d calls, want ok after 2", out, calls)
}
if len(notices) != 1 {
t.Fatalf("notices = %v, want one", notices)
}
notice := notices[0]
if notice.Label != "fetch" || notice.Attempt != 1 || notice.Attempts != 2 {
t.Errorf("notice = %+v", notice)
}
if notice.Delay < DefaultBaseDelay || notice.Delay >= DefaultBaseDelay+DefaultBaseDelay/jitterDivisor {
t.Errorf("notice delay = %v, want first backoff near %v", notice.Delay, DefaultBaseDelay)
}
}
func TestRetryConfirmRecognizesAmbiguousSuccess(t *testing.T) {
calls := 0
confirmations := 0
retry := Retry{
Attempts: 2,
Run: func(context.Context, string, []string, ...string) (string, error) {
calls++
return "fatal: the remote end hung up unexpectedly", errGitExit
},
Sleep: func(context.Context, time.Duration) error { return nil },
}
_, err := retry.Do(context.Background(), Command{
Label: "push",
Args: []string{"push"},
Confirm: func(context.Context) (bool, error) {
confirmations++
return true, nil
},
})
if err != nil {
t.Fatalf("Do: %v", err)
}
if calls != 1 || confirmations != 1 {
t.Fatalf("calls = %d, confirmations = %d, want 1 each", calls, confirmations)
}
}
func TestRetryConfirmationErrorFallsBackToRetry(t *testing.T) {
calls := 0
retry := Retry{
Attempts: 2,
Run: func(context.Context, string, []string, ...string) (string, error) {
calls++
if calls == 1 {
return "fatal: the remote end hung up unexpectedly", errGitExit
}
return "", nil
},
Sleep: func(context.Context, time.Duration) error { return nil },
}
_, err := retry.Do(context.Background(), Command{
Label: "push",
Confirm: func(context.Context) (bool, error) {
return false, errors.New("confirmation unavailable")
},
})
if err != nil {
t.Fatalf("Do: %v", err)
}
if calls != 2 {
t.Fatalf("calls = %d, want 2", calls)
}
}
func TestRetryResetsAfterEveryFailure(t *testing.T) {
resets := 0
retry := Retry{
Attempts: 2,
Run: func(context.Context, string, []string, ...string) (string, error) {
return "fatal: the remote end hung up unexpectedly", errGitExit
},
Sleep: func(context.Context, time.Duration) error { return nil },
}
_, err := retry.Do(context.Background(), Command{
Label: "clone",
Reset: func() error {
resets++
return nil
},
})
if !errors.Is(err, errGitExit) {
t.Fatalf("error = %v, want Git error", err)
}
if resets != 2 {
t.Fatalf("resets = %d, want 2", resets)
}
}
func TestRetryDoesNotRetryPermanentFailure(t *testing.T) {
calls := 0
retry := Retry{
Run: func(context.Context, string, []string, ...string) (string, error) {
calls++
return "remote: Repository not found.", errGitExit
},
Sleep: func(context.Context, time.Duration) error {
t.Fatal("permanent failure must not sleep")
return nil
},
}
_, err := retry.Do(context.Background(), Command{Label: "clone"})
if !errors.Is(err, errGitExit) {
t.Fatalf("error = %v, want Git error", err)
}
if calls != 1 {
t.Fatalf("calls = %d, want 1", calls)
}
}
func TestRetryReturnsCancellationAndJoinsResetError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
resetErr := errors.New("cleanup failed")
retry := Retry{
Run: func(context.Context, string, []string, ...string) (string, error) {
cancel()
return "fatal: the remote end hung up unexpectedly", errGitExit
},
}
_, err := retry.Do(ctx, Command{Reset: func() error { return resetErr }})
if !errors.Is(err, context.Canceled) || !errors.Is(err, resetErr) {
t.Fatalf("error = %v, want cancellation and reset error", err)
}
}
func TestRetryResolvedUsesDefaults(t *testing.T) {
resolved := (Retry{}).Resolved()
if resolved.Attempts != DefaultAttempts ||
resolved.BaseDelay != DefaultBaseDelay ||
resolved.MaxDelay != DefaultMaxDelay ||
resolved.Run == nil ||
resolved.Sleep == nil {
t.Errorf("resolved defaults = %+v", resolved)
}
}
func TestDestResetOnlyRemovesEmptyOrAbsentDestination(t *testing.T) {
occupied := t.TempDir()
keep := filepath.Join(occupied, "keep")
if err := os.WriteFile(keep, []byte("caller content"), 0o644); err != nil {
t.Fatal(err)
}
if reset := DestReset(occupied); reset != nil {
t.Fatal("DestReset returned cleanup for occupied destination")
}
empty := t.TempDir()
reset := DestReset(empty)
if reset == nil {
t.Fatal("DestReset returned nil for empty destination")
}
if err := reset(); err != nil {
t.Fatalf("reset: %v", err)
}
if _, err := os.Stat(empty); !os.IsNotExist(err) {
t.Errorf("empty destination still exists: %v", err)
}
absent := filepath.Join(t.TempDir(), "absent")
if reset := DestReset(absent); reset == nil {
t.Error("DestReset returned nil for absent destination")
}
}
func TestTransientFailure(t *testing.T) {
transient := []string{
"fatal: unable to access remote: Could not resolve host: example.invalid",
"fatal: unable to access remote: Connection refused",
"fatal: the remote end hung up unexpectedly",
"error: RPC failed; HTTP 503 curl 22 Service Temporarily Unavailable",
"fatal: unable to access remote: The requested URL returned error: 429",
"fatal: unable to access remote: The requested URL returned error: 524",
}
for _, out := range transient {
if !TransientFailure(out) {
t.Errorf("TransientFailure(%q) = false", out)
}
}
permanent := []string{
"",
"remote: Repository not found.",
"fatal: Authentication failed",
"fatal: couldn't find remote ref missing",
"fatal: write error: No space left on device",
"fatal: an unclassified failure",
}
for _, out := range permanent {
if TransientFailure(out) {
t.Errorf("TransientFailure(%q) = true", out)
}
}
}
func TestTransientFailurePermanentMarkerWins(t *testing.T) {
cases := []string{
"Connection reset by peer\nremote: Repository not found.",
"No space left on device\nfatal: the remote end hung up unexpectedly",
"HTTP code = 401\nfatal: the remote end hung up unexpectedly",
}
for _, out := range cases {
if TransientFailure(out) {
t.Errorf("mixed output classified as transient: %q", out)
}
}
}
func TestRetryPassesCommandFieldsToRunner(t *testing.T) {
var gotDir string
var gotEnv, gotArgs []string
retry := Retry{
Run: func(_ context.Context, dir string, env []string, args ...string) (string, error) {
gotDir = dir
gotEnv = append([]string(nil), env...)
gotArgs = append([]string(nil), args...)
return "", nil
},
}
command := Command{
Dir: "/tmp/repo",
Env: []string{"GIT_TERMINAL_PROMPT=0"},
Args: []string{"fetch", "--", "origin"},
}
if _, err := retry.Do(context.Background(), command); err != nil {
t.Fatal(err)
}
if gotDir != command.Dir || !slices.Equal(gotEnv, command.Env) || !slices.Equal(gotArgs, command.Args) {
t.Errorf("runner received dir=%q env=%v args=%v", gotDir, gotEnv, gotArgs)
}
}